From 0a2fdd7cfc761fc61aac44816331b3bbb6f86884 Mon Sep 17 00:00:00 2001 From: lltcggie <7032792+lltcggie@users.noreply.github.com> Date: Wed, 25 Mar 2026 22:21:49 +0900 Subject: [PATCH] =?UTF-8?q?OpenCV=20DNN=E3=81=B8=E3=81=AE=E7=A7=BB?= =?UTF-8?q?=E8=A1=8C=E3=83=86=E3=82=B9=E3=83=88=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 + .gitmodules | 4 - .../noise0_scale2.0x_model.prototxt | 10 +- caffe | 1 - caffe_build.bat | 7 +- common/cNet.cpp | 30 +- common/cNet.h | 18 +- common/stImage.cpp | 157 ++- common/stImage.h | 16 +- common/waifu2x.cpp | 82 +- common/waifu2x.h | 44 +- opencv | 2 +- opencv_build.bat | 95 +- waifu2x-caffe-dll/Source.cpp | 4 +- waifu2x-caffe-dll/waifu2x-caffe-dll.vcxproj | 8 +- waifu2x-caffe-gui/MainDialog.cpp | 144 +-- waifu2x-caffe-gui/MainDialog.h | 20 +- waifu2x-caffe-gui/tstring.h | 4 +- waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj | 8 +- waifu2x-caffe/Source.cpp | 154 +-- waifu2x-caffe/Test.cpp | 471 +++++++++ waifu2x-caffe/axpy.hpp | 106 ++ waifu2x-caffe/axpy_fast_layer.cpp | 371 +++++++ waifu2x-caffe/slice_layer.cpp | 992 ++++++++++++++++++ waifu2x-caffe/waifu2x-caffe.vcxproj | 44 +- waifu2x-caffe/waifu2x-caffe.vcxproj.filters | 12 + 26 files changed, 2415 insertions(+), 392 deletions(-) delete mode 160000 caffe create mode 100644 waifu2x-caffe/Test.cpp create mode 100644 waifu2x-caffe/axpy.hpp create mode 100644 waifu2x-caffe/axpy_fast_layer.cpp create mode 100644 waifu2x-caffe/slice_layer.cpp diff --git a/.gitignore b/.gitignore index 902f4de..cccb6ca 100644 --- a/.gitignore +++ b/.gitignore @@ -243,5 +243,8 @@ ModelManifest.xml # FAKE - F# Make .fake/ + *.caffemodel *.protobin +/lib/ +/opencv_contrib-*/ diff --git a/.gitmodules b/.gitmodules index 3828a4f..8aeafec 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,10 +7,6 @@ [submodule "msgpack-c"] path = msgpack-c url = https://github.com/msgpack/msgpack-c -[submodule "caffe"] - path = caffe - url = https://github.com/lltcggie/caffe.git - branch = lltcggie/custom [submodule "opencv"] path = opencv url = https://github.com/opencv/opencv.git diff --git a/bin/models/upresnet10/noise0_scale2.0x_model.prototxt b/bin/models/upresnet10/noise0_scale2.0x_model.prototxt index c083cc7..9def1bf 100644 --- a/bin/models/upresnet10/noise0_scale2.0x_model.prototxt +++ b/bin/models/upresnet10/noise0_scale2.0x_model.prototxt @@ -196,7 +196,7 @@ layer { } layer { name: "/res1/axpy" - type: "Axpy" + type: "AxpyFast" bottom: "/res1/fc2_sigmoid" bottom: "/res1/conv2_relu" bottom: "/res1/crop" @@ -353,7 +353,7 @@ layer { } layer { name: "/res2/axpy" - type: "Axpy" + type: "AxpyFast" bottom: "/res2/fc2_sigmoid" bottom: "/res2/conv2_relu" bottom: "/res2/crop" @@ -510,7 +510,7 @@ layer { } layer { name: "/res3/axpy" - type: "Axpy" + type: "AxpyFast" bottom: "/res3/fc2_sigmoid" bottom: "/res3/conv2_relu" bottom: "/res3/crop" @@ -667,7 +667,7 @@ layer { } layer { name: "/res4/axpy" - type: "Axpy" + type: "AxpyFast" bottom: "/res4/fc2_sigmoid" bottom: "/res4/conv2_relu" bottom: "/res4/crop" @@ -824,7 +824,7 @@ layer { } layer { name: "/res5/axpy" - type: "Axpy" + type: "AxpyFast" bottom: "/res5/fc2_sigmoid" bottom: "/res5/conv2_relu" bottom: "/res5/crop" diff --git a/caffe b/caffe deleted file mode 160000 index 9d5aa36..0000000 --- a/caffe +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 9d5aa36c65a0082768f10c1d474c56d8a7cc9934 diff --git a/caffe_build.bat b/caffe_build.bat index d0d207a..bea6d88 100644 --- a/caffe_build.bat +++ b/caffe_build.bat @@ -4,7 +4,7 @@ cd /d "%~dp0" cd caffe -set MSVC_VERSION=14 +set MSVC_VERSION=17 set USE_CUDNN=1 set USE_NCCL=0 set USE_OPENCV=0 @@ -21,7 +21,8 @@ set BUILD_PYTHON_LAYER=0 set RUN_TESTS=0 set CMAKE_INSTALL_PREFIX=../../lib set RUN_INSTALL=1 -set CUDA_ARCH_BIN=35 50 52 53 60 61 62 70 75 80 -set CUDA_ARCH_PTX=80 +rem set CUDA_ARCH_BIN=75 80 86 89 90 100 120 +set CUDA_ARCH_BIN=89 +set CUDA_ARCH_PTX=120 scripts\build_win.cmd \ No newline at end of file diff --git a/common/cNet.cpp b/common/cNet.cpp index 71686de..3cfedef 100644 --- a/common/cNet.cpp +++ b/common/cNet.cpp @@ -29,7 +29,7 @@ static bool readFile(boost::iostreams::stream -static bool readFile(const boost::filesystem::path &path, std::vector &buf) +static bool readFile(const std::filesystem::path &path, std::vector &buf) { boost::iostreams::stream is; @@ -45,7 +45,7 @@ static bool readFile(const boost::filesystem::path &path, std::vector & return readFile(is, buf); } -static Waifu2x::eWaifu2xError readProtoText(const boost::filesystem::path &path, ::google::protobuf::Message* proto) +static Waifu2x::eWaifu2xError readProtoText(const std::filesystem::path &path, ::google::protobuf::Message* proto) { boost::iostreams::stream is; @@ -74,7 +74,7 @@ static Waifu2x::eWaifu2xError readProtoText(const boost::filesystem::path &path, return Waifu2x::eWaifu2xError_OK; } -static Waifu2x::eWaifu2xError writeProtoBinary(const ::google::protobuf::Message& proto, const boost::filesystem::path &path) +static Waifu2x::eWaifu2xError writeProtoBinary(const ::google::protobuf::Message& proto, const std::filesystem::path &path) { boost::iostreams::stream os; @@ -96,7 +96,7 @@ static Waifu2x::eWaifu2xError writeProtoBinary(const ::google::protobuf::Message return Waifu2x::eWaifu2xError_OK; } -static Waifu2x::eWaifu2xError readProtoBinary(const boost::filesystem::path &path, ::google::protobuf::Message* proto) +static Waifu2x::eWaifu2xError readProtoBinary(const std::filesystem::path &path, ::google::protobuf::Message* proto) { boost::iostreams::stream is; @@ -130,7 +130,7 @@ static Waifu2x::eWaifu2xError readProtoBinary(const boost::filesystem::path &pat namespace { - Waifu2x::eWaifu2xError ReadJson(const boost::filesystem::path &info_path, rapidjson::Document &d, std::vector &jsonBuf) + Waifu2x::eWaifu2xError ReadJson(const std::filesystem::path &info_path, rapidjson::Document &d, std::vector &jsonBuf) { try { @@ -174,7 +174,7 @@ cNet::cNet() : mModelScale(0), mInnerScale(0), mNetOffset(0), mInputPlane(0), mH cNet::~cNet() {} -Waifu2x::eWaifu2xError cNet::GetInfo(const boost::filesystem::path & info_path, Waifu2x::stInfo &info) +Waifu2x::eWaifu2xError cNet::GetInfo(const std::filesystem::path & info_path, Waifu2x::stInfo &info) { rapidjson::Document d; std::vector jsonBuf; @@ -291,7 +291,7 @@ Waifu2x::eWaifu2xError cNet::GetInfo(const boost::filesystem::path & info_path, // モデルファイルからネットワークを構築 // processでcudnnが指定されなかった場合はcuDNNが呼び出されないように変更する -Waifu2x::eWaifu2xError cNet::ConstractNet(const Waifu2x::eWaifu2xModelType mode, const boost::filesystem::path &model_path, const boost::filesystem::path ¶m_path, const Waifu2x::stInfo &info, const std::string &process) +Waifu2x::eWaifu2xError cNet::ConstractNet(const Waifu2x::eWaifu2xModelType mode, const std::filesystem::path &model_path, const std::filesystem::path ¶m_path, const Waifu2x::stInfo &info, const std::string &process) { Waifu2x::eWaifu2xError ret; @@ -299,9 +299,9 @@ Waifu2x::eWaifu2xError cNet::ConstractNet(const Waifu2x::eWaifu2xModelType mode, LoadParamFromInfo(mode, info); - boost::filesystem::path modelbin_path = model_path; + std::filesystem::path modelbin_path = model_path; modelbin_path += ".protobin"; - boost::filesystem::path caffemodel_path = param_path; + std::filesystem::path caffemodel_path = param_path; caffemodel_path += ".caffemodel"; caffe::NetParameter param_model; @@ -331,7 +331,7 @@ Waifu2x::eWaifu2xError cNet::ConstractNet(const Waifu2x::eWaifu2xModelType mode, if (!caffe::UpgradeNetAsNeeded(caffemodel_path.string(), ¶m_caffemodel)) return Waifu2x::eWaifu2xError_FailedParseModelFile; - mNet = boost::shared_ptr>(new caffe::Net(param_model)); + mNet = std::shared_ptr>(new caffe::Net(param_model)); mNet->CopyTrainedLayersFrom(param_caffemodel); } else @@ -437,8 +437,8 @@ Waifu2x::eWaifu2xError cNet::SetParameter(caffe::NetParameter ¶m, const std: return Waifu2x::eWaifu2xError_OK; } -Waifu2x::eWaifu2xError cNet::LoadParameterFromJson(const boost::filesystem::path &model_path, const boost::filesystem::path ¶m_path - , const boost::filesystem::path &modelbin_path, const boost::filesystem::path &caffemodel_path, const std::string &process) +Waifu2x::eWaifu2xError cNet::LoadParameterFromJson(const std::filesystem::path &model_path, const std::filesystem::path ¶m_path + , const std::filesystem::path &modelbin_path, const std::filesystem::path &caffemodel_path, const std::string &process) { Waifu2x::eWaifu2xError ret; @@ -455,7 +455,7 @@ Waifu2x::eWaifu2xError cNet::LoadParameterFromJson(const boost::filesystem::path if (ret != Waifu2x::eWaifu2xError_OK) return ret; - mNet = boost::shared_ptr>(new caffe::Net(param)); + mNet = std::shared_ptr>(new caffe::Net(param)); rapidjson::Document d; std::vector jsonBuf; @@ -509,7 +509,7 @@ Waifu2x::eWaifu2xError cNet::LoadParameterFromJson(const boost::filesystem::path if (inputPlane != outputPlane) return Waifu2x::eWaifu2xError_FailedParseModelFile; - std::vector>> list; + std::vector>> list; auto &v = mNet->layers(); for (auto &l : v) { @@ -858,7 +858,7 @@ Waifu2x::eWaifu2xError cNet::ReconstructImage(const bool UseTTA, const int crop_ return Waifu2x::eWaifu2xError_OK; } -std::string cNet::GetModelName(const boost::filesystem::path &info_path) +std::string cNet::GetModelName(const std::filesystem::path &info_path) { Waifu2x::eWaifu2xError ret; diff --git a/common/cNet.h b/common/cNet.h index fe7cf2e..0c69cbe 100644 --- a/common/cNet.h +++ b/common/cNet.h @@ -9,7 +9,7 @@ class cNet private: Waifu2x::eWaifu2xModelType mMode; - boost::shared_ptr> mNet; + std::shared_ptr> mNet; int mModelScale; // モデルが対象とする拡大率 int mInnerScale; // ネット内部で拡大される倍率 @@ -18,18 +18,18 @@ private: bool mHasNoiseScaleModel; private: - void LoadParamFromInfo(const Waifu2x::eWaifu2xModelType mode, const Waifu2x::stInfo &info); - Waifu2x::eWaifu2xError LoadParameterFromJson(const boost::filesystem::path &model_path, const boost::filesystem::path ¶m_path - , const boost::filesystem::path &modelbin_path, const boost::filesystem::path &caffemodel_path, const std::string &process); - Waifu2x::eWaifu2xError SetParameter(caffe::NetParameter ¶m, const std::string &process) const; + void LoadParamFromInfo(const Waifu2x::eWaifu2xModelType mode, const Waifu2x::stInfo& info); + Waifu2x::eWaifu2xError LoadParameterFromJson(const std::filesystem::path& model_path, const std::filesystem::path& param_path + , const std::filesystem::path& modelbin_path, const std::filesystem::path& caffemodel_path, const std::string& process); + Waifu2x::eWaifu2xError SetParameter(caffe::NetParameter& param, const std::string& process) const; public: cNet(); ~cNet(); - static Waifu2x::eWaifu2xError GetInfo(const boost::filesystem::path &info_path, Waifu2x::stInfo &info); + static Waifu2x::eWaifu2xError GetInfo(const std::filesystem::path& info_path, Waifu2x::stInfo& info); - Waifu2x::eWaifu2xError ConstractNet(const Waifu2x::eWaifu2xModelType mode, const boost::filesystem::path &model_path, const boost::filesystem::path ¶m_path, const Waifu2x::stInfo &info, const std::string &process); + Waifu2x::eWaifu2xError ConstractNet(const Waifu2x::eWaifu2xModelType mode, const std::filesystem::path& model_path, const std::filesystem::path& param_path, const Waifu2x::stInfo& info, const std::string& process); int GetInputPlane() const; int GetInnerScale() const; @@ -39,7 +39,7 @@ public: int GetInputMemorySize(const int crop_w, const int crop_h, const int outer_padding, const int batch_size) const; int GetOutputMemorySize(const int crop_w, const int crop_h, const int outer_padding, const int batch_size) const; - Waifu2x::eWaifu2xError ReconstructImage(const bool UseTTA, const int crop_w, const int crop_h, const int outer_padding, const int batch_size, float *outputBlockBuf, const cv::Mat &inMat, cv::Mat &outMat); + Waifu2x::eWaifu2xError ReconstructImage(const bool UseTTA, const int crop_w, const int crop_h, const int outer_padding, const int batch_size, float* outputBlockBuf, const cv::Mat& inMat, cv::Mat& outMat); - static std::string GetModelName(const boost::filesystem::path &info_path); + static std::string GetModelName(const std::filesystem::path& info_path); }; diff --git a/common/stImage.cpp b/common/stImage.cpp index 8f9b601..f9ebde8 100644 --- a/common/stImage.cpp +++ b/common/stImage.cpp @@ -1,7 +1,7 @@ #include "stImage.h" -#include -#include -#include +//#include +//#include +//#include #include #include #include @@ -11,10 +11,10 @@ #define STB_IMAGE_WRITE_IMPLEMENTATION #include -const int YToRGBConvertMode = CV_GRAY2RGB; -const int YToRGBConverInversetMode = CV_RGB2GRAY; -const int BGRToYConvertMode = CV_BGR2YUV; -const int BGRToConvertInverseMode = CV_YUV2BGR; +const int YToRGBConvertMode = cv::COLOR_GRAY2RGB; +const int YToRGBConverInversetMode = cv::COLOR_RGB2GRAY; +const int BGRToYConvertMode = cv::COLOR_BGR2YUV; +const int BGRToConvertInverseMode = cv::COLOR_YUV2BGR; // floatな画像をuint8_tな画像に変換する際の四捨五入に使う値 // https://github.com/nagadomi/waifu2x/commit/797b45ae23665a1c5e3c481c018e48e6f0d0e383 @@ -24,39 +24,22 @@ const double clip_eps32 = 1.0 * 0.5 - (1.0e-7 * 0.5); const std::vector stImage::OutputExtentionList = { - {L".png",{8, 16}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, - {L".bmp",{8}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, + {L".png",{8, 16}, std::optional(), std::optional(), std::optional(), std::optional()}, + {L".bmp",{8}, std::optional(), std::optional(), std::optional(), std::optional()}, {L".jpg",{8}, 0, 100, 95, cv::IMWRITE_JPEG_QUALITY}, - {L".jp2",{8, 16}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, - {L".sr",{8}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, - {L".tif",{8, 16, 32}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, - {L".hdr",{8, 16, 32}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, - {L".exr",{8, 16, 32}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, - {L".ppm",{8, 16}, boost::optional(), boost::optional(), boost::optional(), boost::optional()}, + {L".jp2",{8, 16}, std::optional(), std::optional(), std::optional(), std::optional()}, + {L".sr",{8}, std::optional(), std::optional(), std::optional(), std::optional()}, + {L".tif",{8, 16, 32}, std::optional(), std::optional(), std::optional(), std::optional()}, + {L".hdr",{8, 16, 32}, std::optional(), std::optional(), std::optional(), std::optional()}, + {L".exr",{8, 16, 32}, std::optional(), std::optional(), std::optional(), std::optional()}, + {L".ppm",{8, 16}, std::optional(), std::optional(), std::optional(), std::optional()}, {L".webp",{8}, 1, 100, 100, cv::IMWRITE_WEBP_QUALITY}, {L".tga",{8}, 0, 1, 0, 0}, }; template -static bool readFile(boost::iostreams::stream &is, std::vector &buf) -{ - if (!is) - return false; - - const auto size = is.seekg(0, std::ios::end).tellg(); - is.seekg(0, std::ios::beg); - - buf.resize((size / sizeof(BufType)) + (size % sizeof(BufType))); - is.read(buf.data(), size); - if (is.gcount() != size) - return false; - - return true; -} - -template -static bool readFile(const boost::filesystem::path &path, std::vector &buf) +static bool readFile(const std::filesystem::path& path, std::vector& buf) { boost::iostreams::stream is; @@ -73,21 +56,7 @@ static bool readFile(const boost::filesystem::path &path, std::vector & } template -static bool writeFile(boost::iostreams::stream &os, const std::vector &buf) -{ - if (!os) - return false; - - const auto WriteSize = sizeof(BufType) * buf.size(); - os.write((const char *)buf.data(), WriteSize); - if (os.fail()) - return false; - - return true; -} - -template -static bool writeFile(const boost::filesystem::path &path, std::vector &buf) +static bool writeFile(const std::filesystem::path& path, std::vector& buf) { boost::iostreams::stream os; @@ -103,10 +72,10 @@ static bool writeFile(const boost::filesystem::path &path, std::vector return writeFile(os, buf); } -static void Waifu2x_stbi_write_func(void *context, void *data, int size) +static void Waifu2x_stbi_write_func(void* context, void* data, int size) { - boost::iostreams::stream *osp = (boost::iostreams::stream *)context; - osp->write((const char *)data, size); + boost::iostreams::stream* osp = (boost::iostreams::stream *)context; + osp->write((const char*)data, size); } int stImage::DepthBitToCVDepth(const int depth_bit) @@ -164,7 +133,7 @@ double stImage::GetEPS(const int cv_depth) } -Waifu2x::eWaifu2xError stImage::AlphaMakeBorder(std::vector &planes, const cv::Mat &alpha, const int offset) +Waifu2x::eWaifu2xError stImage::AlphaMakeBorder(std::vector& planes, const cv::Mat& alpha, const int offset) { // このカーネルと画像の畳込みを行うと、(x, y)を中心とした3×3領域の合計値が求まる const static cv::Mat sum2d_kernel = (cv::Mat_(3, 3) << @@ -178,7 +147,7 @@ Waifu2x::eWaifu2xError stImage::AlphaMakeBorder(std::vector &planes, co cv::Mat mask_nega; cv::threshold(mask, mask_nega, 0.0, 1.0, cv::THRESH_BINARY_INV); // 反転したマスク(値が1の箇所は完全透明でない有効な画素となる) - for (auto &p : planes) // 完全に透明なピクセルにあるゴミを取る + for (auto& p : planes) // 完全に透明なピクセルにあるゴミを取る { p = p.mul(mask); } @@ -191,7 +160,7 @@ Waifu2x::eWaifu2xError stImage::AlphaMakeBorder(std::vector &planes, co cv::Mat mask_nega_u8; mask_nega.convertTo(mask_nega_u8, CV_8U, 255.0, clip_eps8); // mask_negaのCV_U8版(OpenCVのAPI上必要になる) - for (auto &p : planes) // 1チャンネルずつ処理 + for (auto& p : planes) // 1チャンネルずつ処理 { // チャンネルの3×3領域内の有効画素の平均値を求める cv::Mat border; @@ -209,7 +178,7 @@ Waifu2x::eWaifu2xError stImage::AlphaMakeBorder(std::vector &planes, co } // 画素を0から1にクリッピング - for (auto &p : planes) + for (auto& p : planes) { cv::threshold(p, p, 1.0, 1.0, cv::THRESH_TRUNC); cv::threshold(p, p, 0.0, 0.0, cv::THRESH_TOZERO); @@ -219,7 +188,7 @@ Waifu2x::eWaifu2xError stImage::AlphaMakeBorder(std::vector &planes, co } // 画像を読み込んで値を0.0f〜1.0fの範囲に変換 -Waifu2x::eWaifu2xError stImage::LoadMat(cv::Mat &im, const boost::filesystem::path &input_file) +Waifu2x::eWaifu2xError stImage::LoadMat(cv::Mat& im, const std::filesystem::path& input_file) { cv::Mat original_image; @@ -228,7 +197,7 @@ Waifu2x::eWaifu2xError stImage::LoadMat(cv::Mat &im, const boost::filesystem::pa if (!readFile(input_file, img_data)) return Waifu2x::eWaifu2xError_FailedOpenInputFile; - const boost::filesystem::path ipext(input_file.extension()); + const std::filesystem::path ipext(input_file.extension()); if (!boost::iequals(ipext.string(), ".bmp")) // 特定のファイル形式の場合OpenCVで読むとバグることがあるのでSTBIを優先させる { cv::Mat im(img_data.size(), 1, CV_8U, img_data.data()); @@ -259,10 +228,10 @@ Waifu2x::eWaifu2xError stImage::LoadMat(cv::Mat &im, const boost::filesystem::pa return Waifu2x::eWaifu2xError_OK; } -Waifu2x::eWaifu2xError stImage::LoadMatBySTBI(cv::Mat &im, const std::vector &img_data) +Waifu2x::eWaifu2xError stImage::LoadMatBySTBI(cv::Mat& im, const std::vector& img_data) { int x, y, comp; - stbi_uc *data = stbi_load_from_memory((const stbi_uc *)img_data.data(), img_data.size(), &x, &y, &comp, 0); + stbi_uc* data = stbi_load_from_memory((const stbi_uc*)img_data.data(), img_data.size(), &x, &y, &comp, 0); if (!data) return Waifu2x::eWaifu2xError_FailedOpenInputFile; @@ -315,7 +284,7 @@ Waifu2x::eWaifu2xError stImage::LoadMatBySTBI(cv::Mat &im, const std::vector= 3) // RGBなのでBGRにする { @@ -426,7 +395,7 @@ void stImage::Preprocess(const int input_plane, const int net_offset) ConvertToNetFormat(input_plane, net_offset); } -bool stImage::IsOneColor(const cv::Mat & im) +bool stImage::IsOneColor(const cv::Mat& im) { assert(im.channels() == 1); @@ -437,7 +406,7 @@ bool stImage::IsOneColor(const cv::Mat & im) if (Width == 0 && Height == 0) return true; - const float *ptr = (const float *)im.data; + const float* ptr = (const float*)im.data; const float color = ptr[0]; for (size_t i = 0; i < Height; i++) @@ -524,7 +493,7 @@ void stImage::ConvertToNetFormat(const int input_plane, const int alpha_offset) } // 画像から輝度の画像を取り出す -Waifu2x::eWaifu2xError stImage::CreateBrightnessImage(const cv::Mat &float_image, cv::Mat &im) +Waifu2x::eWaifu2xError stImage::CreateBrightnessImage(const cv::Mat& float_image, cv::Mat& im) { if (float_image.channels() > 1) { @@ -548,29 +517,29 @@ bool stImage::HasAlpha() const return !mTmpImageA.empty(); } -void stImage::GetScalePaddingedRGB(cv::Mat &im, cv::Size_ &size, const int net_offset, const int outer_padding, +void stImage::GetScalePaddingedRGB(cv::Mat& im, cv::Size_& size, const int net_offset, const int outer_padding, const int crop_w, const int crop_h, const int scale) { GetScalePaddingedImage(mTmpImageRGB, im, size, net_offset, outer_padding, crop_w, crop_h, scale); } -void stImage::SetReconstructedRGB(cv::Mat &im, const cv::Size_ &size, const int inner_scale) +void stImage::SetReconstructedRGB(cv::Mat& im, const cv::Size_& size, const int inner_scale) { SetReconstructedImage(mTmpImageRGB, im, size, inner_scale); } -void stImage::GetScalePaddingedA(cv::Mat &im, cv::Size_ &size, const int net_offset, const int outer_padding, +void stImage::GetScalePaddingedA(cv::Mat& im, cv::Size_& size, const int net_offset, const int outer_padding, const int crop_w, const int crop_h, const int scale) { GetScalePaddingedImage(mTmpImageA, im, size, net_offset, outer_padding, crop_w, crop_h, scale); } -void stImage::SetReconstructedA(cv::Mat &im, const cv::Size_ &size, const int inner_scale) +void stImage::SetReconstructedA(cv::Mat& im, const cv::Size_& size, const int inner_scale) { SetReconstructedImage(mTmpImageA, im, size, inner_scale); } -void stImage::GetScalePaddingedImage(cv::Mat &in, cv::Mat &out, cv::Size_ &size, const int net_offset, const int outer_padding, +void stImage::GetScalePaddingedImage(cv::Mat& in, cv::Mat& out, cv::Size_& size, const int net_offset, const int outer_padding, const int crop_w, const int crop_h, const int scale) { cv::Mat ret; @@ -597,8 +566,8 @@ void stImage::GetScalePaddingedImage(cv::Mat &in, cv::Mat &out, cv::Size_ & // 入力画像の(Photoshopでいう)キャンバスサイズをoutput_sizeの倍数に変更 // 画像は左上配置、余白はcv::BORDER_REPLICATEで埋める -void stImage::PaddingImage(const cv::Mat &input, const int net_offset, const int outer_padding, - const int crop_w, const int crop_h, cv::Mat &output) +void stImage::PaddingImage(const cv::Mat& input, const int net_offset, const int outer_padding, + const int crop_w, const int crop_h, cv::Mat& output) { const auto pad_w1 = net_offset + outer_padding; const auto pad_h1 = net_offset + outer_padding; @@ -609,7 +578,7 @@ void stImage::PaddingImage(const cv::Mat &input, const int net_offset, const int } // 拡大、パディングされた画像を設定 -void stImage::SetReconstructedImage(cv::Mat &dst, cv::Mat &src, const cv::Size_ &size, const int inner_scale) +void stImage::SetReconstructedImage(cv::Mat& dst, cv::Mat& src, const cv::Size_& size, const int inner_scale) { const cv::Size_ s(size * inner_scale); @@ -718,7 +687,7 @@ void stImage::DeconvertFromNetFormat(const int input_plane) if (!mTmpImageA.empty()) // Aもあるので合体 { // RGBから1chに戻す - cv::cvtColor(mTmpImageA, mTmpImageA, CV_RGB2GRAY); + cv::cvtColor(mTmpImageA, mTmpImageA, cv::COLOR_RGB2GRAY); planes.push_back(mTmpImageA); mTmpImageA.release(); @@ -775,7 +744,7 @@ void stImage::ShrinkImage(const int width, const int height) } } -cv::Mat stImage::DeconvertFromFloat(const cv::Mat &im, const int depth) +cv::Mat stImage::DeconvertFromFloat(const cv::Mat& im, const int depth) { const int cv_depth = DepthBitToCVDepth(depth); const double max_val = GetValumeMaxFromCVDepth(cv_depth); @@ -793,15 +762,15 @@ cv::Mat stImage::DeconvertFromFloat(const cv::Mat &im, const int depth) namespace { template - void AlphaZeroToZero(std::vector &planes) + void AlphaZeroToZero(std::vector& planes) { cv::Mat alpha(planes[3]); - const T *aptr = (const T *)alpha.data; + const T* aptr = (const T*)alpha.data; - T *ptr0 = (T *)planes[0].data; - T *ptr1 = (T *)planes[1].data; - T *ptr2 = (T *)planes[2].data; + T* ptr0 = (T*)planes[0].data; + T* ptr1 = (T*)planes[1].data; + T* ptr2 = (T*)planes[2].data; const size_t Line = alpha.step1(); const size_t Width = alpha.size().width; @@ -820,7 +789,7 @@ namespace } } -void stImage::AlphaCleanImage(cv::Mat &im) +void stImage::AlphaCleanImage(cv::Mat& im) { // 完全透明のピクセルの色を消す(処理の都合上、完全透明のピクセルにも色を付けたから) // モデルによっては画像全域の完全透明の場所にごく小さい値のアルファが広がることがある。それを消すためにcv_depthへ変換してからこの処理を行うことにした @@ -858,7 +827,7 @@ void stImage::AlphaCleanImage(cv::Mat &im) // 入力画像をzoom_sizeの大きさにcv::INTER_CUBICで拡大し、色情報のみを残す -Waifu2x::eWaifu2xError stImage::CreateZoomColorImage(const cv::Mat &float_image, const cv::Size_ &zoom_size, std::vector &cubic_planes) +Waifu2x::eWaifu2xError stImage::CreateZoomColorImage(const cv::Mat& float_image, const cv::Size_& zoom_size, std::vector& cubic_planes) { cv::Mat zoom_cubic_image; cv::resize(float_image, zoom_cubic_image, zoom_size, 0.0, 0.0, cv::INTER_CUBIC); @@ -881,19 +850,19 @@ cv::Mat stImage::GetEndImage() const return mEndImage; } -Waifu2x::eWaifu2xError stImage::Save(const boost::filesystem::path &output_file, const boost::optional &output_quality) +Waifu2x::eWaifu2xError stImage::Save(const std::filesystem::path& output_file, const std::optional& output_quality) { return WriteMat(mEndImage, output_file, output_quality); } -Waifu2x::eWaifu2xError stImage::WriteMat(const cv::Mat &im, const boost::filesystem::path &output_file, const boost::optional &output_quality) +Waifu2x::eWaifu2xError stImage::WriteMat(const cv::Mat& im, const std::filesystem::path& output_file, const std::optional& output_quality) { - const boost::filesystem::path ip(output_file); + const std::filesystem::path ip(output_file); const std::string ext = ip.extension().string(); if (boost::iequals(ext, ".tga")) { - unsigned char *data = im.data; + unsigned char* data = im.data; std::vector rgbimg; if (im.channels() >= 3 || im.step1() != im.size().width * im.channels()) // RGB用バッファにコピー(あるいはパディングをとる) @@ -943,8 +912,8 @@ Waifu2x::eWaifu2xError stImage::WriteMat(const cv::Mat &im, const boost::filesys // RLE圧縮の設定 bool isSet = false; - const auto &OutputExtentionList = stImage::OutputExtentionList; - for (const auto &elm : OutputExtentionList) + const auto& OutputExtentionList = stImage::OutputExtentionList; + for (const auto& elm : OutputExtentionList) { if (elm.ext == L".tga") { @@ -970,13 +939,13 @@ Waifu2x::eWaifu2xError stImage::WriteMat(const cv::Mat &im, const boost::filesys try { - const boost::filesystem::path op(output_file); - const boost::filesystem::path opext(op.extension()); + const std::filesystem::path op(output_file); + const std::filesystem::path opext(op.extension()); std::vector params; - const auto &OutputExtentionList = stImage::OutputExtentionList; - for (const auto &elm : OutputExtentionList) + const auto& OutputExtentionList = stImage::OutputExtentionList; + for (const auto& elm : OutputExtentionList) { if (elm.ext == opext) { diff --git a/common/stImage.h b/common/stImage.h index 0272224..de35a23 100644 --- a/common/stImage.h +++ b/common/stImage.h @@ -28,10 +28,10 @@ public: { std::wstring ext; std::vector depthList; - boost::optional imageQualityStart; - boost::optional imageQualityEnd; - boost::optional imageQualityDefault; - boost::optional imageQualitySettingVolume; + std::optional imageQualityStart; + std::optional imageQualityEnd; + std::optional imageQualityDefault; + std::optional imageQualitySettingVolume; }; const static std::vector OutputExtentionList; @@ -46,7 +46,7 @@ private: static cv::Mat DeconvertFromFloat(const cv::Mat &im, const int depth); static void AlphaCleanImage(cv::Mat &im); - static Waifu2x::eWaifu2xError WriteMat(const cv::Mat &im, const boost::filesystem::path &output_file, const boost::optional &output_quality); + static Waifu2x::eWaifu2xError WriteMat(const cv::Mat &im, const std::filesystem::path &output_file, const std::optional &output_quality); // im(1ch)が単色で構成されているか判定 static bool IsOneColor(const cv::Mat &im); @@ -81,9 +81,9 @@ public: void Clear(); - static Waifu2x::eWaifu2xError LoadMat(cv::Mat &im, const boost::filesystem::path &input_file); + static Waifu2x::eWaifu2xError LoadMat(cv::Mat &im, const std::filesystem::path &input_file); - Waifu2x::eWaifu2xError Load(const boost::filesystem::path &input_file); + Waifu2x::eWaifu2xError Load(const std::filesystem::path &input_file); // source: (4チャンネルの場合は)RGBAな画素配列 // dest: (4チャンネルの場合は)処理したRGBAな画素配列 @@ -130,5 +130,5 @@ public: cv::Mat GetEndImage() const; - Waifu2x::eWaifu2xError Save(const boost::filesystem::path &output_file, const boost::optional &output_quality); + Waifu2x::eWaifu2xError Save(const std::filesystem::path &output_file, const std::optional &output_quality); }; diff --git a/common/waifu2x.cpp b/common/waifu2x.cpp index ac7faff..3f31c87 100644 --- a/common/waifu2x.cpp +++ b/common/waifu2x.cpp @@ -327,7 +327,7 @@ private: } catch (...) { - boost::filesystem::remove(SavePath); + std::filesystem::remove(SavePath); } return true; @@ -550,7 +550,7 @@ Waifu2x::~Waifu2x() } Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noise_level, - const boost::filesystem::path &model_dir, const std::string &process, const int GPUNo) + const std::filesystem::path &model_dir, const std::string &process, const int GPUNo) { Waifu2x::eWaifu2xError ret; @@ -581,38 +581,38 @@ Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noi if (Process == "cudnn") { // exeのディレクトリにcuDNNのアルゴリズムデータ保存 - boost::filesystem::path cudnn_data_base_dir_path(ExeDir); + std::filesystem::path cudnn_data_base_dir_path(ExeDir); if (cudnn_data_base_dir_path.is_relative()) - cudnn_data_base_dir_path = boost::filesystem::system_complete(cudnn_data_base_dir_path); + cudnn_data_base_dir_path = std::filesystem::system_complete(cudnn_data_base_dir_path); - if (!boost::filesystem::is_directory(cudnn_data_base_dir_path)) + if (!std::filesystem::is_directory(cudnn_data_base_dir_path)) cudnn_data_base_dir_path = cudnn_data_base_dir_path.branch_path(); - if (!boost::filesystem::exists(cudnn_data_base_dir_path)) + if (!std::filesystem::exists(cudnn_data_base_dir_path)) { // exeのディレクトリが取得できなければカレントディレクトリに保存 - cudnn_data_base_dir_path = boost::filesystem::current_path(); + cudnn_data_base_dir_path = std::filesystem::current_path(); if (cudnn_data_base_dir_path.is_relative()) - cudnn_data_base_dir_path = boost::filesystem::system_complete(cudnn_data_base_dir_path); + cudnn_data_base_dir_path = std::filesystem::system_complete(cudnn_data_base_dir_path); - if (!boost::filesystem::exists(cudnn_data_base_dir_path)) + if (!std::filesystem::exists(cudnn_data_base_dir_path)) cudnn_data_base_dir_path = "./"; } - if (boost::filesystem::exists(cudnn_data_base_dir_path)) + if (std::filesystem::exists(cudnn_data_base_dir_path)) { - const boost::filesystem::path cudnn_data_dir_path(cudnn_data_base_dir_path / "cudnn_data"); + const std::filesystem::path cudnn_data_dir_path(cudnn_data_base_dir_path / "cudnn_data"); bool isOK = false; - if (boost::filesystem::exists(cudnn_data_dir_path)) + if (std::filesystem::exists(cudnn_data_dir_path)) isOK = true; if (!isOK) { boost::system::error_code error; - const bool result = boost::filesystem::create_directory(cudnn_data_dir_path, error); + const bool result = std::filesystem::create_directory(cudnn_data_dir_path, error); if (result && !error) isOK = true; } @@ -628,8 +628,8 @@ Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noi std::string deconv_filename(prop.name); deconv_filename += " deconv "; - const boost::filesystem::path conv_data_path = cudnn_data_dir_path / conv_filename; - const boost::filesystem::path deconv_data_path = cudnn_data_dir_path / deconv_filename; + const std::filesystem::path conv_data_path = cudnn_data_dir_path / conv_filename; + const std::filesystem::path deconv_data_path = cudnn_data_dir_path / deconv_filename; g_ConvCcuDNNAlgorithm.SetDataPath(conv_data_path.string()); g_DeconvCcuDNNAlgorithm.SetDataPath(deconv_data_path.string()); @@ -638,8 +638,8 @@ Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noi } } - const boost::filesystem::path mode_dir_path(GetModeDirPath(model_dir)); - if (!boost::filesystem::exists(mode_dir_path)) + const std::filesystem::path mode_dir_path(GetModeDirPath(model_dir)); + if (!std::filesystem::exists(mode_dir_path)) return Waifu2x::eWaifu2xError_FailedOpenModelFile; CudaDeviceSet devset(process, mGPUNo); @@ -661,7 +661,7 @@ Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noi mInputPlane = 0; mMaxNetOffset = 0; - const boost::filesystem::path info_path = GetInfoPath(mode_dir_path); + const std::filesystem::path info_path = GetInfoPath(mode_dir_path); stInfo info; ret = cNet::GetInfo(info_path, info); @@ -693,8 +693,8 @@ Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noi base_name = "noise" + std::to_string(noise_level) + "_model"; } - const boost::filesystem::path model_path = mode_dir_path / (base_name + ".prototxt"); - const boost::filesystem::path param_path = mode_dir_path / (base_name + ".json"); + const std::filesystem::path model_path = mode_dir_path / (base_name + ".prototxt"); + const std::filesystem::path param_path = mode_dir_path / (base_name + ".json"); ret = mNoiseNet->ConstractNet(Mode, model_path, param_path, info, mProcess); if (ret != Waifu2x::eWaifu2xError_OK) @@ -708,8 +708,8 @@ Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noi { const std::string base_name = "scale2.0x_model"; - const boost::filesystem::path model_path = mode_dir_path / (base_name + ".prototxt"); - const boost::filesystem::path param_path = mode_dir_path / (base_name + ".json"); + const std::filesystem::path model_path = mode_dir_path / (base_name + ".prototxt"); + const std::filesystem::path param_path = mode_dir_path / (base_name + ".json"); mScaleNet.reset(new cNet); @@ -736,16 +736,16 @@ Waifu2x::eWaifu2xError Waifu2x::Init(const eWaifu2xModelType mode, const int noi return Waifu2x::eWaifu2xError_OK; } -boost::filesystem::path Waifu2x::GetModeDirPath(const boost::filesystem::path &model_dir) +std::filesystem::path Waifu2x::GetModeDirPath(const std::filesystem::path &model_dir) { - boost::filesystem::path mode_dir_path(model_dir); + std::filesystem::path mode_dir_path(model_dir); if (!mode_dir_path.is_absolute()) // model_dirが相対パスなら絶対パスに直す { // まずはカレントディレクトリ下にあるか探す - mode_dir_path = boost::filesystem::absolute(model_dir); - if (!boost::filesystem::exists(mode_dir_path) && !ExeDir.empty()) // 無かったらargv[0]から実行ファイルのあるフォルダを推定し、そのフォルダ下にあるか探す + mode_dir_path = std::filesystem::absolute(model_dir); + if (!std::filesystem::exists(mode_dir_path) && !ExeDir.empty()) // 無かったらargv[0]から実行ファイルのあるフォルダを推定し、そのフォルダ下にあるか探す { - boost::filesystem::path a0(ExeDir); + std::filesystem::path a0(ExeDir); if (a0.is_absolute()) mode_dir_path = a0.branch_path() / model_dir; } @@ -754,17 +754,17 @@ boost::filesystem::path Waifu2x::GetModeDirPath(const boost::filesystem::path &m return mode_dir_path; } -boost::filesystem::path Waifu2x::GetInfoPath(const boost::filesystem::path &mode_dir_path) +std::filesystem::path Waifu2x::GetInfoPath(const std::filesystem::path &mode_dir_path) { - const boost::filesystem::path info_path = mode_dir_path / "info.json"; + const std::filesystem::path info_path = mode_dir_path / "info.json"; return info_path; } -Waifu2x::eWaifu2xError Waifu2x::waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file, - const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, +Waifu2x::eWaifu2xError Waifu2x::waifu2x(const std::filesystem::path &input_file, const std::filesystem::path &output_file, + const std::optional scale_ratio, const std::optional scale_width, const std::optional scale_height, const waifu2xCancelFunc cancel_func, const int crop_w, const int crop_h, - const boost::optional output_quality, const int output_depth, const bool use_tta, + const std::optional output_quality, const int output_depth, const bool use_tta, const int batch_size) { Waifu2x::eWaifu2xError ret; @@ -867,7 +867,7 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(const double factor, const void* source, return Waifu2x::eWaifu2xError_OK; } -Factor Waifu2x::CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, +Factor Waifu2x::CalcScaleRatio(const std::optional scale_ratio, const std::optional scale_width, const std::optional scale_height, const stImage &image) { if (scale_ratio) @@ -1155,24 +1155,24 @@ const std::string& Waifu2x::used_process() const return mProcess; } -std::string Waifu2x::GetModelName(const boost::filesystem::path & model_dir) +std::string Waifu2x::GetModelName(const std::filesystem::path & model_dir) { - const boost::filesystem::path mode_dir_path(GetModeDirPath(model_dir)); - if (!boost::filesystem::exists(mode_dir_path)) + const std::filesystem::path mode_dir_path(GetModeDirPath(model_dir)); + if (!std::filesystem::exists(mode_dir_path)) return std::string(); - const boost::filesystem::path info_path = mode_dir_path / "info.json"; + const std::filesystem::path info_path = mode_dir_path / "info.json"; return cNet::GetModelName(info_path); } -bool Waifu2x::GetInfo(const boost::filesystem::path &model_dir, stInfo &info) +bool Waifu2x::GetInfo(const std::filesystem::path &model_dir, stInfo &info) { - const boost::filesystem::path mode_dir_path(GetModeDirPath(model_dir)); - if (!boost::filesystem::exists(mode_dir_path)) + const std::filesystem::path mode_dir_path(GetModeDirPath(model_dir)); + if (!std::filesystem::exists(mode_dir_path)) return false; - const boost::filesystem::path info_path = mode_dir_path / "info.json"; + const std::filesystem::path info_path = mode_dir_path / "info.json"; return cNet::GetInfo(info_path, info) == Waifu2x::eWaifu2xError_OK; } diff --git a/common/waifu2x.h b/common/waifu2x.h index c70a554..29d4e08 100644 --- a/common/waifu2x.h +++ b/common/waifu2x.h @@ -5,9 +5,9 @@ #include #include #include -#include -#include -#include +#include +#include +#include #include #define CUDNN_DLL_NAME "cudnn64_8.dll" @@ -146,31 +146,31 @@ private: int mMaxNetOffset; // ネットに入力するとどれくらい削れるか bool mHasNoiseScaleOnly; - float *mOutputBlock; + float* mOutputBlock; size_t mOutputBlockSize; private: - static boost::filesystem::path GetModeDirPath(const boost::filesystem::path &model_dir); - static boost::filesystem::path GetInfoPath(const boost::filesystem::path &model_dir); + static std::filesystem::path GetModeDirPath(const std::filesystem::path& model_dir); + static std::filesystem::path GetInfoPath(const std::filesystem::path& model_dir); - static Factor CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, - const stImage &image); + static Factor CalcScaleRatio(const std::optional scale_ratio, const std::optional scale_width, const std::optional scale_height, + const stImage& image); - static int GetcuDNNAlgorithm(const char *layer_name, int num_input, int num_output, int batch_size, + static int GetcuDNNAlgorithm(const char* layer_name, int num_input, int num_output, int batch_size, int width, int height, int kernel_w, int kernel_h, int pad_w, int pad_h, int stride_w, int stride_h); - static void SetcuDNNAlgorithm(int algo, const char *layer_name, int num_input, int num_output, int batch_size, + static void SetcuDNNAlgorithm(int algo, const char* layer_name, int num_input, int num_output, int batch_size, int width, int height, int kernel_w, int kernel_h, int pad_w, int pad_h, int stride_w, int stride_h); Waifu2x::eWaifu2xError ReconstructImage(const Factor factor, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, - const bool isReconstructNoise, const bool isReconstructScale, const Waifu2x::waifu2xCancelFunc cancel_func, stImage &image); + const bool isReconstructNoise, const bool isReconstructScale, const Waifu2x::waifu2xCancelFunc cancel_func, stImage& image); Waifu2x::eWaifu2xError ReconstructScale(const int crop_w, const int crop_h, const bool use_tta, const int batch_size, - const Waifu2x::waifu2xCancelFunc cancel_func, stImage &image); + const Waifu2x::waifu2xCancelFunc cancel_func, stImage& image); Waifu2x::eWaifu2xError ReconstructNoiseScale(const int crop_w, const int crop_h, const bool use_tta, const int batch_size, - const Waifu2x::waifu2xCancelFunc cancel_func, stImage &image); + const Waifu2x::waifu2xCancelFunc cancel_func, stImage& image); Waifu2x::eWaifu2xError ReconstructByNet(std::shared_ptr net, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, - const Waifu2x::waifu2xCancelFunc cancel_func, cv::Mat &im); - Waifu2x::eWaifu2xError ProcessNet(std::shared_ptr net, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, cv::Mat &im); + const Waifu2x::waifu2xCancelFunc cancel_func, cv::Mat& im); + Waifu2x::eWaifu2xError ProcessNet(std::shared_ptr net, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, cv::Mat& im); public: Waifu2x(); @@ -186,12 +186,12 @@ public: // mode: noise or scale or noise_scale or auto_scale // process: cpu or gpu or cudnn eWaifu2xError Init(const eWaifu2xModelType mode, const int noise_level, - const boost::filesystem::path &model_dir, const std::string &process, const int gpu_no = 0); + const std::filesystem::path& model_dir, const std::string& process, const int gpu_no = 0); - eWaifu2xError waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file, - const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, + eWaifu2xError waifu2x(const std::filesystem::path& input_file, const std::filesystem::path& output_file, + const std::optional scale_ratio, const std::optional scale_width, const std::optional scale_height, const waifu2xCancelFunc cancel_func = nullptr, const int crop_w = 128, const int crop_h = 128, - const boost::optional output_quality = boost::optional(), const int output_depth = 8, const bool use_tta = false, + const std::optional output_quality = std::optional(), const int output_depth = 8, const bool use_tta = false, const int batch_size = 1); // factor: 倍率 @@ -201,12 +201,12 @@ public: // out_stride: destのストライド(バイト単位) eWaifu2xError waifu2x(const double factor, const void* source, void* dest, const int width, const int height, const int in_channel, const int in_stride, const int out_channel, const int out_stride, - const int crop_w = 128, const int crop_h = 128, const bool use_tta = false, const int batch_size = 1); + const int crop_w = 128, const int crop_h = 128, const bool use_tta = false, const int batch_size = 1); void Destroy(); const std::string& used_process() const; - static std::string GetModelName(const boost::filesystem::path &model_dir); - static bool GetInfo(const boost::filesystem::path &model_dir, stInfo &info); + static std::string GetModelName(const std::filesystem::path& model_dir); + static bool GetInfo(const std::filesystem::path& model_dir, stInfo& info); }; diff --git a/opencv b/opencv index b38c50b..6d889ee 160000 --- a/opencv +++ b/opencv @@ -1 +1 @@ -Subproject commit b38c50b3d0c31e82294315ec44b54b7ef559ef12 +Subproject commit 6d889ee74c94124f6492eb8f0d50946d9c31d8e9 diff --git a/opencv_build.bat b/opencv_build.bat index 73c6b4a..2a2df94 100644 --- a/opencv_build.bat +++ b/opencv_build.bat @@ -1,10 +1,21 @@ @echo off +call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvarsall.bat" amd64 + +cd /d "%~dp0" + +rem wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.12.0.zip +rem opencv_contrib.zip 繧定ァ」蜃阪@縺セ縺吶 +rem cmake -D OPENCV_EXTRA_MODULES_PATH=%~dp0\opencv_contrib-4.12.0\modules + cd opencv mkdir build cd build -cmake .. -G "Visual Studio 15 Win64" ^ +set CFLAGS=/Zc:preprocessor +set CXXFLAGS=/Zc:preprocessor + +cmake .. -G Ninja ^ -DCMAKE_INSTALL_PREFIX=..\..\lib ^ -DBUILD_WITH_STATIC_CRT=OFF ^ -DBUILD_IPP_IW=OFF ^ @@ -12,8 +23,8 @@ cmake .. -G "Visual Studio 15 Win64" ^ -DBUILD_JAVA=OFF ^ -DBUILD_SHARED_LIBS=OFF ^ -DBUILD_TESTS=OFF ^ +-DBUILD_PERF_TESTS=OFF ^ -DBUILD_opencv_calib3d=OFF ^ --DBUILD_opencv_dnn=OFF ^ -DBUILD_opencv_features2d=OFF ^ -DBUILD_opencv_flann=OFF ^ -DBUILD_opencv_highgui=OFF ^ @@ -25,9 +36,60 @@ cmake .. -G "Visual Studio 15 Win64" ^ -DBUILD_opencv_videostab=OFF ^ -DBUILD_opencv_java_bindings_generator=OFF ^ -DBUILD_opencv_python_bindings_generator=OFF ^ +-DBUILD_opencv_apps=OFF ^ +-DBUILD_opencv_aruco=OFF ^ +-DBUILD_opencv_bgsegm=OFF ^ +-DBUILD_opencv_bioinspired=OFF ^ +-DBUILD_opencv_ccalib=OFF ^ +-DBUILD_opencv_cudaarithm=OFF ^ +-DBUILD_opencv_cudabgsegm=OFF ^ +-DBUILD_opencv_cudacodec=OFF ^ +-DBUILD_opencv_cudafeatures2d=OFF ^ +-DBUILD_opencv_cudafilters=OFF ^ +-DBUILD_opencv_cudaimgproc=OFF ^ +-DBUILD_opencv_cudalegacy=OFF ^ +-DBUILD_opencv_cudaobjdetect=OFF ^ +-DBUILD_opencv_cudaoptflow=OFF ^ +-DBUILD_opencv_cudastereo=OFF ^ +-DBUILD_opencv_cudawarping=OFF ^ +-DBUILD_opencv_cudev=OFF ^ +-DBUILD_opencv_datasets=OFF ^ +-DBUILD_opencv_face=OFF ^ +-DBUILD_opencv_freetype=OFF ^ +-DBUILD_opencv_fuzzy=OFF ^ +-DBUILD_opencv_hfs=OFF ^ +-DBUILD_opencv_img_hash=OFF ^ +-DBUILD_opencv_line_descriptor=OFF ^ +-DBUILD_opencv_mcc=OFF ^ +-DBUILD_opencv_objc_bindings_generator=OFF ^ +-DBUILD_opencv_optflow=OFF ^ +-DBUILD_opencv_phase_unwrapping=OFF ^ +-DBUILD_opencv_plot=OFF ^ +-DBUILD_opencv_reg=OFF ^ +-DBUILD_opencv_rgbd=OFF ^ +-DBUILD_opencv_saliency=OFF ^ +-DBUILD_opencv_shape=OFF ^ +-DBUILD_opencv_stereo=OFF ^ +-DBUILD_opencv_structured_light=OFF ^ +-DBUILD_opencv_surface_matching=OFF ^ +-DBUILD_opencv_text=OFF ^ +-DBUILD_opencv_tracking=OFF ^ +-DBUILD_opencv_xfeatures2d=OFF ^ +-DBUILD_opencv_ximgproc=OFF ^ +-DBUILD_opencv_xobjdetect=OFF ^ +-DBUILD_opencv_xphoto=OFF ^ +-DBUILD_opencv_python3=OFF ^ +-DBUILD_opencv_python_tests=OFF ^ +-DBUILD_opencv_quality=OFF ^ +-DBUILD_opencv_rapid=OFF ^ +-DBUILD_opencv_signal=OFF ^ +-DBUILD_opencv_stitching=OFF ^ +-DBUILD_opencv_wechat_qrcode=OFF ^ +-DBUILD_opencv_js_bindings_generator=OFF ^ -DWITH_1394=OFF ^ --DWITH_CUDA=OFF ^ --DWITH_CUFFT=OFF ^ +-DWITH_CUDA=ON ^ +-DWITH_CUDNN=ON ^ +-DWITH_CUFFT=ON ^ -DWITH_DIRECTX=OFF ^ -DWITH_DSHOW=OFF ^ -DWITH_EIGEN=OFF ^ @@ -37,10 +99,27 @@ cmake .. -G "Visual Studio 15 Win64" ^ -DWITH_OPENCAMDBALSL=OFF ^ -DWITH_OPENCLAMDFFT=OFF ^ -DWITH_OPENCL_SVM=OFF ^ --DWITH_PROTOBUF=OFF ^ +-DWITH_ADE=OFF ^ +-DWITH_ARITH_DEC=OFF ^ +-DWITH_ARITH_ENC=OFF ^ +-DWITH_IPP=OFF ^ +-DWITH_ITT=OFF ^ -DWITH_VFW=OFF ^ -DWITH_VTK=OFF ^ --DWITH_WIN32UI=OFF +-DWITH_TESSERACT=OFF ^ +-DWITH_WIN32UI=OFF ^ +-DBUILD_opencv_dnn=ON ^ +-DBUILD_opencv_cudev=ON ^ +-DWITH_PROTOBUF=ON ^ +-DOPENCV_DNN_CUDA=ON ^ +-DCUDA_ARCH_BIN=8.9 ^ +-DCUDA_ARCH_PTX=12.0 ^ +-DCUDA_USE_STATIC_CUDA_RUNTIME=OFF ^ +-DCMAKE_C_FLAGS="/DWIN32 /D_WINDOWS /W3 /Zc:preprocessor" ^ +-DCMAKE_CXX_FLAGS="/DWIN32 /D_WINDOWS /W3 /GR /EHsc /Zc:preprocessor" ^ +-DOPENCV_EXTRA_MODULES_PATH=%~dp0\opencv_contrib-4.12.0\modules ^ +-DCMAKE_BUILD_TYPE=Debug -rem cmake --build . --config Debug --target install -cmake --build . --config Release --target install + +cmake --build . --config Debug --target install +rem cmake --build . --config Release --target install diff --git a/waifu2x-caffe-dll/Source.cpp b/waifu2x-caffe-dll/Source.cpp index bb4a2ff..0f7892a 100644 --- a/waifu2x-caffe-dll/Source.cpp +++ b/waifu2x-caffe-dll/Source.cpp @@ -18,7 +18,7 @@ void* Waifu2xInit(const char *mode, const int noise_level, const char *model_dir else if (strcmp("auto_scale", mode) == 0) mt = Waifu2x::eWaifu2xModelTypeAutoScale; - // if (obj->Init(1, argv, mode, noise_level, 2.0, boost::optional(), boost::optional(), model_dir, process, boost::optional(), output_depth, use_tta, crop_size, batch_size) != Waifu2x::eWaifu2xError_OK) + // if (obj->Init(1, argv, mode, noise_level, 2.0, std::optional(), std::optional(), model_dir, process, std::optional(), output_depth, use_tta, crop_size, batch_size) != Waifu2x::eWaifu2xError_OK) if (obj->Init(mt, noise_level, model_dir, process) != Waifu2x::eWaifu2xError_OK) { delete obj; @@ -43,7 +43,7 @@ void* Waifu2xInitNew(const char *mode, const int noise_level, const char *model_ else if (strcmp("auto_scale", mode) == 0) mt = Waifu2x::eWaifu2xModelTypeAutoScale; - // if (obj->Init(1, argv, mode, noise_level, 2.0, boost::optional(), boost::optional(), model_dir, process, boost::optional(), output_depth, use_tta, crop_size, batch_size) != Waifu2x::eWaifu2xError_OK) + // if (obj->Init(1, argv, mode, noise_level, 2.0, std::optional(), std::optional(), model_dir, process, std::optional(), output_depth, use_tta, crop_size, batch_size) != Waifu2x::eWaifu2xError_OK) if (obj->Init(mt, noise_level, model_dir, process) != Waifu2x::eWaifu2xError_OK) { delete obj; diff --git a/waifu2x-caffe-dll/waifu2x-caffe-dll.vcxproj b/waifu2x-caffe-dll/waifu2x-caffe-dll.vcxproj index c53563e..c720501 100644 --- a/waifu2x-caffe-dll/waifu2x-caffe-dll.vcxproj +++ b/waifu2x-caffe-dll/waifu2x-caffe-dll.vcxproj @@ -14,19 +14,19 @@ {DFF94FEB-78AB-41B1-9B92-4D8B7D799E04} Win32Proj waifu2x-caffe-dll - 8.1 + 10.0 DynamicLibrary true - v140 + v143 Unicode DynamicLibrary false - v140 + v143 true Unicode @@ -61,6 +61,7 @@ Disabled BOOST_ALL_NO_LIB;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) MultiThreadedDebugDLL + stdcpp17 Console @@ -79,6 +80,7 @@ true BOOST_ALL_NO_LIB;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) MultiThreadedDLL + stdcpp17 Console diff --git a/waifu2x-caffe-gui/MainDialog.cpp b/waifu2x-caffe-gui/MainDialog.cpp index d22194e..1ffad3d 100644 --- a/waifu2x-caffe-gui/MainDialog.cpp +++ b/waifu2x-caffe-gui/MainDialog.cpp @@ -58,22 +58,22 @@ namespace } // http://stackoverflow.com/questions/10167382/boostfilesystem-get-relative-path - boost::filesystem::path relativePath(const boost::filesystem::path &path, const boost::filesystem::path &relative_to) + std::filesystem::path relativePath(const std::filesystem::path &path, const std::filesystem::path &relative_to) { // create absolute paths - boost::filesystem::path p = boost::filesystem::absolute(path); - boost::filesystem::path r = boost::filesystem::absolute(relative_to); + std::filesystem::path p = std::filesystem::absolute(path); + std::filesystem::path r = std::filesystem::absolute(relative_to); // if root paths are different, return absolute path if (p.root_path() != r.root_path()) return p; // initialize relative path - boost::filesystem::path result; + std::filesystem::path result; // find out where the two paths diverge - boost::filesystem::path::const_iterator itr_path = p.begin(); - boost::filesystem::path::const_iterator itr_relative_to = r.begin(); + std::filesystem::path::const_iterator itr_path = p.begin(); + std::filesystem::path::const_iterator itr_relative_to = r.begin(); while (*itr_path == *itr_relative_to && itr_path != p.end() && itr_relative_to != r.end()) { ++itr_path; @@ -497,7 +497,7 @@ bool DialogEvent::SyncMember(const bool NotSyncCropSize, const bool silent) return ret; } -void DialogEvent::SetCropSizeList(const boost::filesystem::path & input_path) +void DialogEvent::SetCropSizeList(const std::filesystem::path & input_path) { if (isSetInitCrop) return; @@ -505,7 +505,7 @@ void DialogEvent::SetCropSizeList(const boost::filesystem::path & input_path) HWND hcrop = GetDlgItem(dh, IDC_COMBO_CROP_SIZE); int gcd = 1; - if (boost::filesystem::exists(input_path) && !boost::filesystem::is_directory(input_path)) + if (std::filesystem::exists(input_path) && !std::filesystem::is_directory(input_path)) { cv::Mat mat; const auto ret = stImage::LoadMat(mat, input_path.string()); @@ -620,17 +620,17 @@ void DialogEvent::ProcessWaifu2x() const auto inputFunc = [this, &file_paths](const tstring &input) { - const boost::filesystem::path input_path(boost::filesystem::absolute(input)); + const std::filesystem::path input_path(std::filesystem::absolute(input)); - if (boost::filesystem::is_directory(input_path)) // input_pathがフォルダならそのディレクトリ以下の画像ファイルを一括変換 + if (std::filesystem::is_directory(input_path)) // input_pathがフォルダならそのディレクトリ以下の画像ファイルを一括変換 { - boost::filesystem::path output_path(output_str); + std::filesystem::path output_path(output_str); - output_path = boost::filesystem::absolute(output_path); + output_path = std::filesystem::absolute(output_path); - if (!boost::filesystem::exists(output_path)) + if (!std::filesystem::exists(output_path)) { - if (!boost::filesystem::create_directory(output_path)) + if (!std::filesystem::create_directory(output_path)) { SendMessage(dh, WM_FAILD_CREATE_DIR, (WPARAM)&output_path, 0); PostMessage(dh, WM_END_THREAD, 0, 0); @@ -640,12 +640,12 @@ void DialogEvent::ProcessWaifu2x() } // 変換する画像の入力、出力パスを取得 - const auto func = [this, &input_path, &output_path, &file_paths](const boost::filesystem::path &path) + const auto func = [this, &input_path, &output_path, &file_paths](const std::filesystem::path &path) { - BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::recursive_directory_iterator(path), - boost::filesystem::recursive_directory_iterator())) + BOOST_FOREACH(const std::filesystem::path& p, std::make_pair(std::filesystem::recursive_directory_iterator(path), + std::filesystem::recursive_directory_iterator())) { - if (!boost::filesystem::is_directory(p)) + if (!std::filesystem::is_directory(p)) { tstring ext(getTString(p.extension())); #ifdef UNICODE @@ -674,12 +674,12 @@ void DialogEvent::ProcessWaifu2x() for (const auto &p : file_paths) { - const boost::filesystem::path out_path(p.second); - const boost::filesystem::path out_dir(out_path.parent_path()); + const std::filesystem::path out_path(p.second); + const std::filesystem::path out_dir(out_path.parent_path()); - if (!boost::filesystem::exists(out_dir)) + if (!std::filesystem::exists(out_dir)) { - if (!boost::filesystem::create_directories(out_dir)) + if (!std::filesystem::create_directories(out_dir)) { SendMessage(dh, WM_FAILD_CREATE_DIR, (WPARAM)&out_dir, 0); PostMessage(dh, WM_END_THREAD, 0, 0); @@ -691,12 +691,12 @@ void DialogEvent::ProcessWaifu2x() } else { - const boost::filesystem::path output_path(output_str); + const std::filesystem::path output_path(output_str); const auto outDir = output_path.branch_path(); - if (!outDir.empty() && !boost::filesystem::exists(outDir)) + if (!outDir.empty() && !std::filesystem::exists(outDir)) { - if (!boost::filesystem::create_directories(outDir)) + if (!std::filesystem::create_directories(outDir)) { SendMessage(dh, WM_FAILD_CREATE_DIR, (WPARAM)&outDir, 0); PostMessage(dh, WM_END_THREAD, 0, 0); @@ -711,8 +711,8 @@ void DialogEvent::ProcessWaifu2x() const auto inputFuncMulti = [this, &file_paths](const tstring &input) { - const boost::filesystem::path input_path(boost::filesystem::absolute(input)); - const boost::filesystem::path output_path(boost::filesystem::absolute(output_str)); + const std::filesystem::path input_path(std::filesystem::absolute(input)); + const std::filesystem::path output_path(std::filesystem::absolute(output_str)); const auto outilenameFunc = [&output_path](const tstring &path) -> std::wstring { @@ -720,11 +720,11 @@ void DialogEvent::ProcessWaifu2x() return out.wstring(); }; - if (boost::filesystem::is_directory(input_path)) // input_pathがフォルダならそのディレクトリ以下の画像ファイルを一括変換 + if (std::filesystem::is_directory(input_path)) // input_pathがフォルダならそのディレクトリ以下の画像ファイルを一括変換 { - if (!boost::filesystem::exists(output_path)) + if (!std::filesystem::exists(output_path)) { - if (!boost::filesystem::create_directory(output_path)) + if (!std::filesystem::create_directory(output_path)) { SendMessage(dh, WM_FAILD_CREATE_DIR, (WPARAM)&output_path, 0); PostMessage(dh, WM_END_THREAD, 0, 0); @@ -736,12 +736,12 @@ void DialogEvent::ProcessWaifu2x() const auto inputDirName = input_path.filename(); // 変換する画像の入力、出力パスを取得 - const auto func = [this, &input_path, &output_path, &file_paths, &inputDirName](const boost::filesystem::path &path) + const auto func = [this, &input_path, &output_path, &file_paths, &inputDirName](const std::filesystem::path &path) { - BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::recursive_directory_iterator(path), - boost::filesystem::recursive_directory_iterator())) + BOOST_FOREACH(const std::filesystem::path& p, std::make_pair(std::filesystem::recursive_directory_iterator(path), + std::filesystem::recursive_directory_iterator())) { - if (!boost::filesystem::is_directory(p)) + if (!std::filesystem::is_directory(p)) { tstring ext(getTString(p.extension())); #ifdef UNICODE @@ -770,12 +770,12 @@ void DialogEvent::ProcessWaifu2x() for (const auto &p : file_paths) { - const boost::filesystem::path out_path(p.second); - const boost::filesystem::path out_dir(out_path.parent_path()); + const std::filesystem::path out_path(p.second); + const std::filesystem::path out_dir(out_path.parent_path()); - if (!boost::filesystem::exists(out_dir)) + if (!std::filesystem::exists(out_dir)) { - if (!boost::filesystem::create_directories(out_dir)) + if (!std::filesystem::create_directories(out_dir)) { SendMessage(dh, WM_FAILD_CREATE_DIR, (WPARAM)&out_dir, 0); PostMessage(dh, WM_END_THREAD, 0, 0); @@ -789,9 +789,9 @@ void DialogEvent::ProcessWaifu2x() { const auto &outDir = output_path; - if (!boost::filesystem::exists(outDir)) + if (!std::filesystem::exists(outDir)) { - if (!boost::filesystem::create_directories(outDir)) + if (!std::filesystem::create_directories(outDir)) { SendMessage(dh, WM_FAILD_CREATE_DIR, (WPARAM)&outDir, 0); PostMessage(dh, WM_END_THREAD, 0, 0); @@ -849,8 +849,8 @@ void DialogEvent::ProcessWaifu2x() ProgessFunc(maxFile, 0); - boost::optional ScaleRatio; - boost::optional ScaleWidth, ScaleHeight; + std::optional ScaleRatio; + std::optional ScaleWidth, ScaleHeight; switch (scaleType) { case eScaleTypeRatio: @@ -875,7 +875,7 @@ void DialogEvent::ProcessWaifu2x() const auto fileNum = file_paths.size(); for (const auto &p : file_paths) { - if (isOutputNoOverwrite && boost::filesystem::exists(p.second)) // 上書き禁止ならメッセージ表示して無視 + if (isOutputNoOverwrite && std::filesystem::exists(p.second)) // 上書き禁止ならメッセージ表示して無視 { SendMessage(dh, WM_ON_WAIFU2X_NO_OVERWRITE, (WPARAM)p.first.c_str(), (LPARAM)p.second.c_str()); @@ -943,10 +943,10 @@ void DialogEvent::ReplaceAddString() // { SyncMember(true, true); - const boost::filesystem::path output_path(output_str); + const std::filesystem::path output_path(output_str); tstring stem; - if (input_str_multi.size() == 0 && !boost::filesystem::is_directory(input_str)) + if (input_str_multi.size() == 0 && !std::filesystem::is_directory(input_str)) stem = getTString(output_path.stem()); else stem = getTString(output_path.filename()); @@ -963,8 +963,8 @@ void DialogEvent::ReplaceAddString() // autoSetAddName = addstr; - boost::filesystem::path new_out_path; - if (input_str_multi.size() == 0 && !boost::filesystem::is_directory(input_str)) + std::filesystem::path new_out_path; + if (input_str_multi.size() == 0 && !std::filesystem::is_directory(input_str)) new_out_path = output_path.branch_path() / (new_name + outputExt); else new_out_path = output_path.branch_path() / (new_name); @@ -1040,7 +1040,7 @@ void DialogEvent::SaveIni(const bool isSyncMember) if (isNotSaveParam) return; - const boost::filesystem::path SettingFilePath(exeDir / SettingFileName); + const std::filesystem::path SettingFilePath(exeDir / SettingFileName); tstring tScaleRatio; tstring tScaleWidth; @@ -1338,8 +1338,8 @@ UINT_PTR DialogEvent::OFNHookProcOut(HWND hdlg, UINT uiMsg, WPARAM wParam, LPARA { szPath[_countof(szPath) - 1] = TEXT('\0'); - boost::filesystem::path p(szPath); - if (boost::filesystem::exists(p) && (boost::filesystem::is_empty(p) || boost::filesystem::is_directory(p))) + std::filesystem::path p(szPath); + if (std::filesystem::exists(p) && (std::filesystem::is_empty(p) || std::filesystem::is_directory(p))) { const auto filename = getTString(p.filename()); @@ -1505,7 +1505,7 @@ void DialogEvent::OnDialogEnd(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lp void DialogEvent::OnFaildCreateDir(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData) { - const boost::filesystem::path *p = (const boost::filesystem::path *)wParam; + const std::filesystem::path *p = (const std::filesystem::path *)wParam; TCHAR msg[1024 * 2]; _stprintf(msg, langStringList.GetString(L"MessageCreateOutDirError").c_str(), getTString(*p).c_str()); @@ -1730,15 +1730,15 @@ void DialogEvent::Create(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData) GetModuleFileName(NULL, texepath, _countof(texepath)); texepath[_countof(texepath) - 1] = TEXT('\0'); - const boost::filesystem::path exePath(texepath); + const std::filesystem::path exePath(texepath); exeDir = exePath.branch_path(); } - const boost::filesystem::path SettingFilePath(exeDir / SettingFileName); + const std::filesystem::path SettingFilePath(exeDir / SettingFileName); { - const boost::filesystem::path LangDirPath(exeDir / LangDir); - const boost::filesystem::path LangListPath(exeDir / LangListFileName); + const std::filesystem::path LangDirPath(exeDir / LangDir); + const std::filesystem::path LangListPath(exeDir / LangListFileName); langStringList.SetLangBaseDir(getTString(LangDirPath)); langStringList.ReadLangList(getTString(LangListPath)); } @@ -1811,7 +1811,7 @@ void DialogEvent::Create(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData) SendMessage(houtext, CB_SETCURSEL, 0, 0); } - const boost::filesystem::path CropSizeListPath(exeDir / CropSizeListName); + const std::filesystem::path CropSizeListPath(exeDir / CropSizeListName); std::ifstream ifs(CropSizeListPath.wstring()); if (ifs) { @@ -2132,7 +2132,7 @@ void DialogEvent::Create(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData) SetWindowText(GetDlgItem(hWnd, IDC_EDIT_INPUT_EXT_LIST), inputFileExt.c_str()); - if (tOutputDirFix.length() > 0 && boost::filesystem::exists(tOutputDirFix)) + if (tOutputDirFix.length() > 0 && std::filesystem::exists(tOutputDirFix)) { output_dir = tOutputDirFix; SetWindowText(GetDlgItem(hWnd, IDC_EDIT_OUTPUT), output_dir.c_str()); @@ -2758,9 +2758,9 @@ LRESULT DialogEvent::OnSetInputFilePath(const TCHAR * tPath) { HWND hWnd = GetDlgItem(dh, IDC_EDIT_INPUT); - boost::filesystem::path path(tPath); + std::filesystem::path path(tPath); - if (!boost::filesystem::exists(path)) + if (!std::filesystem::exists(path)) { MessageBox(dh, langStringList.GetString(L"MessageInputCheckError").c_str(), langStringList.GetString(L"MessageTitleError").c_str(), MB_OK | MB_ICONERROR); return 0L; @@ -2770,9 +2770,9 @@ LRESULT DialogEvent::OnSetInputFilePath(const TCHAR * tPath) SyncMember(true, true); - boost::filesystem::path outpath(output_dir); + std::filesystem::path outpath(output_dir); - if (boost::filesystem::is_directory(path)) + if (std::filesystem::is_directory(path)) { HWND ho = GetDlgItem(dh, IDC_EDIT_OUTPUT); @@ -2832,7 +2832,7 @@ LRESULT DialogEvent::OnSetInputFilePath() const tstring addstr(AddName()); autoSetAddName = AddName(); - boost::filesystem::path outpath(output_dir); + std::filesystem::path outpath(output_dir); if (output_dir.length() == 0) // 出力パス未設定なら入力ファイルと同じフォルダ { @@ -2840,10 +2840,10 @@ LRESULT DialogEvent::OnSetInputFilePath() outpath = outpath.branch_path(); } - boost::filesystem::path baseDir(input_str_multi[0]); + std::filesystem::path baseDir(input_str_multi[0]); tstring filename; - if (boost::filesystem::is_directory(baseDir)) + if (std::filesystem::is_directory(baseDir)) filename = baseDir.filename().wstring(); else filename = baseDir.stem().wstring(); @@ -2865,15 +2865,15 @@ LRESULT DialogEvent::OnSetOutputFilePath(const TCHAR * tPath) if (input_str.length() > 0 || input_str_multi.size() > 0) { - boost::filesystem::path path(input_str); - boost::filesystem::path outpath(tPath); + std::filesystem::path path(input_str); + std::filesystem::path outpath(tPath); if (input_str_multi.size() > 0) { path = input_str_multi[0]; } - if (boost::filesystem::is_directory(path)) + if (std::filesystem::is_directory(path)) { HWND ho = GetDlgItem(dh, IDC_EDIT_OUTPUT); @@ -2952,7 +2952,7 @@ LRESULT DialogEvent::DropInput(HWND hWnd, WPARAM wParam, LPARAM lParam, WNDPROC } if (tAutoMode == TEXT("one") || - (tAutoMode == TEXT("multi") && (input_str_multi.size() > 0 || boost::filesystem::is_directory(szTmp)))) + (tAutoMode == TEXT("multi") && (input_str_multi.size() > 0 || std::filesystem::is_directory(szTmp)))) { ::PostMessage(GetDlgItem(dh, IDC_BUTTON_EXEC), BM_CLICK, 0, 0); } @@ -3037,7 +3037,7 @@ void DialogEvent::InputRef(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpDat *tfp = TEXT('\0'); tfp++; - if (tInputDirFix.length() > 0 && boost::filesystem::exists(tInputDirFix)) + if (tInputDirFix.length() > 0 && std::filesystem::exists(tInputDirFix)) ofn.lpstrInitialDir = tInputDirFix.c_str(); else ofn.lpstrInitialDir = szPath; @@ -3104,7 +3104,7 @@ void DialogEvent::InputRef(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpDat } if (tAutoMode == TEXT("one") || - (tAutoMode == TEXT("multi") && (input_str_multi.size() > 0 || boost::filesystem::is_directory(szFile.data())))) + (tAutoMode == TEXT("multi") && (input_str_multi.size() > 0 || std::filesystem::is_directory(szFile.data())))) { ::PostMessage(GetDlgItem(dh, IDC_BUTTON_EXEC), BM_CLICK, 0, 0); } @@ -3139,7 +3139,7 @@ void DialogEvent::OutputRef(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpDa memcpy(tfp, allFilesExt.c_str(), allFilesExt.length() * sizeof(TCHAR)); tfp += allFilesExt.length(); - if (tOutputDirFix.length() > 0 && boost::filesystem::exists(tOutputDirFix)) + if (tOutputDirFix.length() > 0 && std::filesystem::exists(tOutputDirFix)) ofn.lpstrInitialDir = tOutputDirFix.c_str(); else ofn.lpstrInitialDir = szPath; @@ -3417,7 +3417,7 @@ void DialogEvent::AppSetting(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpD tOutputDirFix = cAppSettingDialogEvent.tOutputDirFix; gpu_no = cAppSettingDialogEvent.gpu_no; - if (tOutputDirFix.length() > 0 && boost::filesystem::exists(tOutputDirFix)) + if (tOutputDirFix.length() > 0 && std::filesystem::exists(tOutputDirFix)) { output_dir = tOutputDirFix; } diff --git a/waifu2x-caffe-gui/MainDialog.h b/waifu2x-caffe-gui/MainDialog.h index 102d7dd..df943f4 100644 --- a/waifu2x-caffe-gui/MainDialog.h +++ b/waifu2x-caffe-gui/MainDialog.h @@ -7,8 +7,8 @@ #include #include #include -#include -#include +#include +#include #include "../common/waifu2x.h" #include "resource.h" #include "tstring.h" @@ -77,7 +77,7 @@ private: static LangStringList langStringList; - boost::filesystem::path exeDir; + std::filesystem::path exeDir; std::vector CropSizeList; tstring input_str; @@ -96,7 +96,7 @@ private: bool use_tta; - boost::optional output_quality; + std::optional output_quality; int output_depth; int crop_size; @@ -158,11 +158,11 @@ private: bool SyncMember(const bool NotSyncCropSize, const bool silent = false); - void SetCropSizeList(const boost::filesystem::path &input_path); + void SetCropSizeList(const std::filesystem::path& input_path); - static boost::filesystem::path GetFileName(const boost::filesystem::path &input_path) + static std::filesystem::path GetFileName(const std::filesystem::path& input_path) { - if (boost::filesystem::is_directory(input_path)) + if (std::filesystem::is_directory(input_path)) return input_path.stem(); else return input_path.filename(); @@ -172,7 +172,7 @@ private: void ReplaceAddString(); - void AddLogMessage(const TCHAR *msg); + void AddLogMessage(const TCHAR* msg); void Waifu2xTime(); @@ -231,11 +231,11 @@ public: void OnModelChange(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData); - LRESULT OnSetInputFilePath(const TCHAR *tPath); + LRESULT OnSetInputFilePath(const TCHAR* tPath); LRESULT OnSetInputFilePath(); - LRESULT OnSetOutputFilePath(const TCHAR *tPath); + LRESULT OnSetOutputFilePath(const TCHAR* tPath); // ここで渡されるhWndはIDC_EDITのHWND(コントロールのイベントだから) LRESULT DropInput(HWND hWnd, WPARAM wParam, LPARAM lParam, WNDPROC OrgSubWnd, LPVOID lpData); diff --git a/waifu2x-caffe-gui/tstring.h b/waifu2x-caffe-gui/tstring.h index a44758f..c9ba19c 100644 --- a/waifu2x-caffe-gui/tstring.h +++ b/waifu2x-caffe-gui/tstring.h @@ -6,13 +6,13 @@ #ifdef UNICODE typedef std::wstring tstring; -inline tstring getTString(const boost::filesystem::path& p) +inline tstring getTString(const std::filesystem::path& p) { return p.wstring(); } #else typedef std::string tstring; -inline tstring getTString(const boost::filesystem::path& p) +inline tstring getTString(const std::filesystem::path& p) { return p.string(); } diff --git a/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj b/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj index d685474..6099d07 100644 --- a/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj +++ b/waifu2x-caffe-gui/waifu2x-caffe-gui.vcxproj @@ -14,19 +14,19 @@ {63FB3EFC-63B0-401C-BB54-F3A984DC233F} Win32Proj waifu2xcaffegui - 8.1 + 10.0 Application true - v140 + v143 Unicode Application false - v140 + v143 true Unicode @@ -61,6 +61,7 @@ Disabled BOOST_ALL_NO_LIB;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) MultiThreadedDebugDLL + stdcpp17 Windows @@ -82,6 +83,7 @@ true BOOST_ALL_NO_LIB;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) MultiThreadedDLL + stdcpp17 Windows diff --git a/waifu2x-caffe/Source.cpp b/waifu2x-caffe/Source.cpp index 6fb3f58..6d3cd99 100644 --- a/waifu2x-caffe/Source.cpp +++ b/waifu2x-caffe/Source.cpp @@ -1,11 +1,8 @@ #include +#include #include -#include -#include +#include #include -#include -#include -#include #include #include "../common/waifu2x.h" @@ -17,6 +14,7 @@ #include using namespace TCLAPW; typedef std::wstring tstring; +typedef std::wstringstream tstringstream; typedef wchar_t TCHAR; #ifndef TEXT #define TEXT(x) L##x @@ -26,7 +24,7 @@ typedef wchar_t TCHAR; #define tprintf wprintf #define CHAR_STR_FORMAT L"%S" -const tstring& path_to_tstring(const boost::filesystem::path &p) +tstring path_to_tstring(const std::filesystem::path& p) { return p.wstring(); } @@ -34,6 +32,7 @@ const tstring& path_to_tstring(const boost::filesystem::path &p) #include using namespace TCLAP; typedef std::string tstring; +typedef std::stringstream tstringstream; typedef char TCHAR; #ifndef TEXT #define TEXT(x) x @@ -43,7 +42,7 @@ typedef char TCHAR; #define tprintf printf #define CHAR_STR_FORMAT "%s" -const tstring& path_to_tstring(const boost::filesystem::path &p) +tstring path_to_tstring(const std::filesystem::path& p) { return p.string(); } @@ -51,22 +50,22 @@ const tstring& path_to_tstring(const boost::filesystem::path &p) // http://stackoverflow.com/questions/10167382/boostfilesystem-get-relative-path -boost::filesystem::path relativePath(const boost::filesystem::path &path, const boost::filesystem::path &relative_to) +std::filesystem::path relativePath(const std::filesystem::path& path, const std::filesystem::path& relative_to) { // create absolute paths - boost::filesystem::path p = boost::filesystem::absolute(path); - boost::filesystem::path r = boost::filesystem::absolute(relative_to); + std::filesystem::path p = std::filesystem::absolute(path); + std::filesystem::path r = std::filesystem::absolute(relative_to); // if root paths are different, return absolute path if (p.root_path() != r.root_path()) return p; // initialize relative path - boost::filesystem::path result; + std::filesystem::path result; // find out where the two paths diverge - boost::filesystem::path::const_iterator itr_path = p.begin(); - boost::filesystem::path::const_iterator itr_relative_to = r.begin(); + std::filesystem::path::const_iterator itr_path = p.begin(); + std::filesystem::path::const_iterator itr_relative_to = r.begin(); while (*itr_path == *itr_relative_to && itr_path != p.end() && itr_relative_to != r.end()) { ++itr_path; ++itr_relative_to; @@ -99,14 +98,6 @@ int main(int argc, char** argv) Waifu2x::init_liblary(argc, argv); - // Caffeのエラーでないログを保存しないようにする - google::SetLogDestination(google::GLOG_INFO, ""); - google::SetLogDestination(google::GLOG_WARNING, ""); - - // Caffeのエラーログを「error_log_〜」に出力 - google::SetLogDestination(google::GLOG_ERROR, "error_log_"); - google::SetLogDestination(google::GLOG_FATAL, "error_log_"); - // definition of command line arguments CmdLine cmd(TEXT("waifu2x reimplementation using Caffe"), ' ', TEXT("1.0.0")); @@ -209,22 +200,22 @@ int main(int argc, char** argv) { #ifdef WIN_UNICODE int nArgs = 0; - LPTSTR *lplpszArgs = CommandLineToArgvW(GetCommandLine(), &nArgs); + LPTSTR* lplpszArgs = CommandLineToArgvW(GetCommandLine(), &nArgs); cmd.parse(nArgs, lplpszArgs); LocalFree(lplpszArgs); #else cmd.parse(argc, argv); #endif } - catch (std::exception &e) + catch (std::exception& e) { tprintf(TEXT("エラー: ") CHAR_STR_FORMAT TEXT("\n"), e.what()); return 1; } - boost::optional ScaleRatio; - boost::optional ScaleWidth; - boost::optional ScaleHeight; + std::optional ScaleRatio; + std::optional ScaleWidth; + std::optional ScaleHeight; int crop_w = cmdCropSizeFile.getValue(); int crop_h = cmdCropSizeFile.getValue(); @@ -236,14 +227,14 @@ int main(int argc, char** argv) crop_h = cmdCropHeight.getValue(); if (cmdScaleWidth.getValue() > 0) - ScaleWidth = cmdScaleWidth.getValue(); + ScaleWidth = (int)cmdScaleWidth.getValue(); if (cmdScaleHeight.getValue() > 0) - ScaleHeight = cmdScaleHeight.getValue(); + ScaleHeight = (int)cmdScaleHeight.getValue(); if (cmdScaleWidth.getValue() == 0 && cmdScaleHeight.getValue() == 0) ScaleRatio = cmdScaleRatio.getValue(); - const boost::filesystem::path input_path(boost::filesystem::absolute((cmdInputFile.getValue()))); + const std::filesystem::path input_path(std::filesystem::absolute((cmdInputFile.getValue()))); tstring outputExt = cmdOutputFileExt.getValue(); if (outputExt.length() > 0 && outputExt[0] != TEXT('.')) @@ -265,9 +256,9 @@ int main(int argc, char** argv) const bool use_tta = cmdTTALevel.getValue() == 1; std::vector> file_paths; - if (boost::filesystem::is_directory(input_path)) // input_pathがフォルダならそのディレクトリ以下の画像ファイルを一括変換 + if (std::filesystem::is_directory(input_path)) // input_pathがフォルダならそのディレクトリ以下の画像ファイルを一括変換 { - boost::filesystem::path output_path; + std::filesystem::path output_path; if (cmdOutputFile.getValue() == TEXT("(auto)")) { @@ -277,7 +268,7 @@ int main(int argc, char** argv) addstr += tModelName; addstr += TEXT(")"); - const tstring &mode = cmdMode.getValue(); + const tstring& mode = cmdMode.getValue(); addstr += TEXT("(") + mode + TEXT(")"); @@ -288,7 +279,7 @@ int main(int argc, char** argv) addstr += TEXT("(tta)"); if (mode.find(TEXT("scale")) != mode.npos) { - if(ScaleRatio) + if (ScaleRatio) addstr += TEXT("(x") + to_tstring(*ScaleRatio) + TEXT(")"); else if (ScaleWidth && ScaleHeight) addstr += TEXT("(") + to_tstring(*ScaleWidth) + TEXT("x") + to_tstring(*ScaleHeight) + TEXT(")"); @@ -301,16 +292,16 @@ int main(int argc, char** argv) if (cmdOutputDepth.getValue() != 8) addstr += TEXT("(") + to_tstring(cmdOutputDepth.getValue()) + TEXT("bit)"); - output_path = input_path.branch_path() / (path_to_tstring(input_path.stem()) + addstr); + output_path = input_path.parent_path() / (path_to_tstring(input_path.stem()) + addstr); } else output_path = cmdOutputFile.getValue(); - output_path = boost::filesystem::absolute(output_path); + output_path = std::filesystem::absolute(output_path); - if (!boost::filesystem::exists(output_path)) + if (!std::filesystem::exists(output_path)) { - if (!boost::filesystem::create_directory(output_path)) + if (!std::filesystem::create_directory(output_path)) { tprintf(TEXT("エラー: 出力フォルダ「%s」の作成に失敗しました\n"), path_to_tstring(output_path).c_str()); return 1; @@ -321,58 +312,69 @@ int main(int argc, char** argv) { // input_extention_listを文字列の配列にする - typedef boost::char_separator char_separator; - typedef boost::tokenizer tokenizer; - - char_separator sep(TEXT(":"), TEXT(""), boost::drop_empty_tokens); - tokenizer tokens(cmdInputFileExt.getValue(), sep); - - for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) + tstringstream check1(cmdInputFileExt.getValue()); + tstring ext; + while (std::getline(check1, ext, TEXT(':'))) { - tstring ext(*tok_iter); std::transform(ext.begin(), ext.end(), ext.begin(), totlower); extList.push_back(TEXT(".") + ext); } + + //typedef boost::char_separator char_separator; + //typedef boost::tokenizer tokenizer; + + //char_separator sep(TEXT(":"), TEXT(""), boost::drop_empty_tokens); + //tokenizer tokens(cmdInputFileExt.getValue(), sep); + + //for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) + //{ + // tstring ext(*tok_iter); + // std::transform(ext.begin(), ext.end(), ext.begin(), totlower); + // extList.push_back(TEXT(".") + ext); + //} } - // 変換する画像の入力、出力パスを取得 - const auto func = [&extList, &input_path, &output_path, &outputExt, &file_paths](const boost::filesystem::path &path) - { - BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::recursive_directory_iterator(path), - boost::filesystem::recursive_directory_iterator())) - { - if (boost::filesystem::is_directory(p)) - { - const auto out_relative = relativePath(p, input_path); - const auto out_absolute = output_path / out_relative; - if (!boost::filesystem::exists(out_absolute)) - { - if (!boost::filesystem::create_directory(out_absolute)) - { - tprintf(TEXT("エラー: 出力フォルダ「%s」の作成に失敗しました\n"), path_to_tstring(out_absolute).c_str()); - return false; - } - } - } - else + + // 変換する画像の入力、出力パスを取得 + const auto func = [&extList, &input_path, &output_path, &outputExt, &file_paths](const std::filesystem::path& path) + { + for (const auto& i : std::filesystem::recursive_directory_iterator(path)) { - tstring ext(path_to_tstring(p.extension())); - std::transform(ext.begin(), ext.end(), ext.begin(), totlower); - if (std::find(extList.begin(), extList.end(), ext) != extList.end()) + const std::filesystem::path& p = i.path(); + + if (std::filesystem::is_directory(p)) { const auto out_relative = relativePath(p, input_path); const auto out_absolute = output_path / out_relative; - const auto out = path_to_tstring(out_absolute.branch_path() / out_absolute.stem()) + outputExt; + if (!std::filesystem::exists(out_absolute)) + { + if (!std::filesystem::create_directory(out_absolute)) + { + tprintf(TEXT("エラー: 出力フォルダ「%s」の作成に失敗しました\n"), path_to_tstring(out_absolute).c_str()); + return false; + } + } + } + else + { + tstring ext(path_to_tstring(p.extension())); + std::transform(ext.begin(), ext.end(), ext.begin(), totlower); + if (std::find(extList.begin(), extList.end(), ext) != extList.end()) + { + const auto out_relative = relativePath(p, input_path); + const auto out_absolute = output_path / out_relative; - file_paths.emplace_back(path_to_tstring(p), out); + const auto out = path_to_tstring(out_absolute.parent_path() / out_absolute.stem()) + outputExt; + + file_paths.emplace_back(path_to_tstring(p), out); + } } } - } - return true; - }; + return true; + }; if (!func(input_path)) return 1; @@ -393,7 +395,7 @@ int main(int argc, char** argv) addstr += tModelName; addstr += TEXT(")"); - const tstring &mode = cmdMode.getValue(); + const tstring& mode = cmdMode.getValue(); addstr += TEXT("(") + mode + TEXT(")"); @@ -461,11 +463,11 @@ int main(int argc, char** argv) } bool isError = false; - for (const auto &p : file_paths) + for (const auto& p : file_paths) { const Waifu2x::eWaifu2xError ret = w.waifu2x(p.first, p.second, ScaleRatio, ScaleWidth, ScaleHeight, nullptr, crop_w, crop_h, - cmdOutputQuality.getValue() == -1 ? boost::optional() : cmdOutputQuality.getValue(), cmdOutputDepth.getValue(), use_tta, cmdBatchSizeFile.getValue()); + cmdOutputQuality.getValue() == -1 ? std::optional() : cmdOutputQuality.getValue(), cmdOutputDepth.getValue(), use_tta, cmdBatchSizeFile.getValue()); if (ret != Waifu2x::eWaifu2xError_OK) { switch (ret) diff --git a/waifu2x-caffe/Test.cpp b/waifu2x-caffe/Test.cpp new file mode 100644 index 0000000..1d07495 --- /dev/null +++ b/waifu2x-caffe/Test.cpp @@ -0,0 +1,471 @@ +# include +# include +# include +# include +# include +# include + +# include +# include + +#define CV_VERSION_STR CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION) + +// ビルドモード +#ifdef _DEBUG +#define CV_EXT_STR "d.lib" +#else +#define CV_EXT_STR ".lib" +#endif + +#ifdef _MSC_VER + +#pragma comment(lib, "opencv_core" CV_VERSION_STR CV_EXT_STR) +#pragma comment(lib, "opencv_imgcodecs" CV_VERSION_STR CV_EXT_STR) +#pragma comment(lib, "opencv_imgproc" CV_VERSION_STR CV_EXT_STR) +#pragma comment(lib, "opencv_dnn" CV_VERSION_STR CV_EXT_STR) +#pragma comment(lib, "libprotobuf" CV_EXT_STR) +#pragma comment(lib, "IlmImf" CV_EXT_STR) +#pragma comment(lib, "libjpeg-turbo" CV_EXT_STR) +#pragma comment(lib, "libopenjp2" CV_EXT_STR) +#pragma comment(lib, "libpng" CV_EXT_STR) +#pragma comment(lib, "libtiff" CV_EXT_STR) +#pragma comment(lib, "libwebp" CV_EXT_STR) +#pragma comment(lib, "zlib" CV_EXT_STR) + +#pragma comment(lib, "cudart.lib") +//#pragma comment(lib, "curand.lib") +#pragma comment(lib, "cublas.lib") +#pragma comment(lib, "cudnn.lib") + +#endif + +using namespace std; + +class CropCenterLayer : public cv::dnn::Layer +{ +private: + std::vector cropSize; + cv::Ptr cropLayer; + +public: + CropCenterLayer(const cv::dnn::LayerParams& params) : Layer(params) + { + setParamsFrom(params); + + if (params.has("crop_size")) + { + const auto& paramCropSize = params.get("crop_size"); + const auto& str = paramCropSize.getStringValue(); + + const int s = atoi(str.c_str()); + cropSize.resize(4); + cropSize[0] = 0; + cropSize[1] = 0; + cropSize[2] = s; + cropSize[3] = s; + } + + cv::dnn::LayerParams parasm; + parasm.set("axis", 0); + parasm.set("offset", cv::dnn::DictValue::arrayInt(cropSize.data(), cropSize.size())); + + cropLayer = cv::dnn::CropLayer::create(parasm); + } + + // Destructor + virtual ~CropCenterLayer() = default; + + static cv::Ptr create(cv::dnn::LayerParams& params) + { + return cv::Ptr(new CropCenterLayer(params)); + } + + // Override virtual functions from cv::dnn::Layer and delegate to cropLayer + + CV_DEPRECATED_EXTERNAL virtual void finalize(const std::vector& input, std::vector& output) override + { + cropLayer->finalize(input, output); + } + + virtual void finalize(cv::InputArrayOfArrays inputs_arr, cv::OutputArrayOfArrays outputs_arr) override + { + std::vector inputs; + inputs_arr.getMatVector(inputs); + + CV_Assert(inputs.size() == 1); + const auto& input = inputs[0]; + + //cv::Mat sizeShape(input.dims, input.size.p, input.type()); + cv::Mat sizeShape(input.size.dims(), input.size.p, input.type()); + + auto& sz = sizeShape.size; + for (int i = 0; i < sz.dims(); i++) + { + sz[i] = sz[i] - cropSize[i] * 2; + CV_Assert(sz[i] >= 0); + } + + inputs.push_back(sizeShape); // dummy second input for CropLayer + + cropLayer->finalize(inputs, outputs_arr); + } + + CV_DEPRECATED_EXTERNAL virtual void forward(std::vector& input, std::vector& output, std::vector& internals) + { + cropLayer->forward(input, output, internals); + } + + virtual void forward(cv::InputArrayOfArrays inputs, cv::OutputArrayOfArrays outputs, cv::OutputArrayOfArrays internals) override + { + cropLayer->forward(inputs, outputs, internals); + } + + virtual bool tryQuantize(const std::vector>& scales, + const std::vector>& zeropoints, cv::dnn::LayerParams& params) override + { + return cropLayer->tryQuantize(scales, zeropoints, params); + } + + CV_DEPRECATED_EXTERNAL void finalize(const std::vector& inputs, CV_OUT std::vector& outputs) + { + cropLayer->finalize(inputs, outputs); + } + + CV_DEPRECATED std::vector finalize(const std::vector& inputs) + { + cropLayer->finalize(inputs); + } + + CV_DEPRECATED CV_WRAP void run(const std::vector& inputs, CV_OUT std::vector& outputs, + CV_IN_OUT std::vector& internals) + { + cropLayer->run(inputs, outputs, internals); + } + + virtual int inputNameToIndex(cv::String inputName) override + { + return cropLayer->inputNameToIndex(inputName); + } + + virtual int outputNameToIndex(const cv::String& outputName) override + { + return cropLayer->outputNameToIndex(outputName); + } + + virtual bool supportBackend(int backendId) override + { + return cropLayer->supportBackend(backendId); + } + + virtual cv::Ptr initHalide(const std::vector>& inputs) override + { + // preferableTargetを反映させるタイミングがここしかないっぽいので反映させる + cropLayer->preferableTarget = preferableTarget; + return cropLayer->initHalide(inputs); + } + + virtual cv::Ptr initNgraph(const std::vector>& inputs, + const std::vector>& nodes) override + { + // preferableTargetを反映させるタイミングがここしかないっぽいので反映させる + cropLayer->preferableTarget = preferableTarget; + return cropLayer->initNgraph(inputs, nodes); + } + + virtual cv::Ptr initVkCom(const std::vector>& inputs, + std::vector>& outputs) override + { + // preferableTargetを反映させるタイミングがここしかないっぽいので反映させる + cropLayer->preferableTarget = preferableTarget; + return cropLayer->initVkCom(inputs, outputs); + } + + virtual cv::Ptr initWebnn(const std::vector>& inputs, + const std::vector>& nodes) override + { + // preferableTargetを反映させるタイミングがここしかないっぽいので反映させる + cropLayer->preferableTarget = preferableTarget; + return cropLayer->initWebnn(inputs, nodes); + } + + virtual cv::Ptr initCUDA(void* context, + const std::vector>& inputs, + const std::vector>& outputs) override + { + // preferableTargetを反映させるタイミングがここしかないっぽいので反映させる + cropLayer->preferableTarget = preferableTarget; + return cropLayer->initCUDA(context, inputs, outputs); + } + + virtual cv::Ptr initTimVX(void* timVxInfo, + const std::vector>& inputsWrapper, + const std::vector>& outputsWrapper, + bool isLast) override + { + // preferableTargetを反映させるタイミングがここしかないっぽいので反映させる + cropLayer->preferableTarget = preferableTarget; + return cropLayer->initTimVX(timVxInfo, inputsWrapper, outputsWrapper, isLast); + } + + virtual cv::Ptr initCann(const std::vector>& inputs, + const std::vector>& outputs, + const std::vector>& nodes) override + { + // preferableTargetを反映させるタイミングがここしかないっぽいので反映させる + cropLayer->preferableTarget = preferableTarget; + return cropLayer->initCann(inputs, outputs, nodes); + } + + virtual void applyHalideScheduler(cv::Ptr& node, + const std::vector& inputs, + const std::vector& outputs, + int targetId) const override + { + cropLayer->applyHalideScheduler(node, inputs, outputs, targetId); + } + + virtual cv::Ptr tryAttach(const cv::Ptr& node) override + { + return cropLayer->tryAttach(node); + } + + virtual bool setActivation(const cv::Ptr& layer) override + { + return cropLayer->setActivation(layer); + } + + virtual bool tryFuse(cv::Ptr& top) override + { + return cropLayer->tryFuse(top); + } + + virtual void getScaleShift(cv::Mat& scale, cv::Mat& shift) const override + { + cropLayer->getScaleShift(scale, shift); + } + + virtual void getScaleZeropoint(float& scale, int& zeropoint) const override + { + cropLayer->getScaleZeropoint(scale, zeropoint); + } + + virtual void unsetAttached() override + { + cropLayer->unsetAttached(); + } + + virtual bool getMemoryShapes(const std::vector& inputs, + const int requiredOutputs, + std::vector& outputs, + std::vector& internals) const override + { + CV_Assert(inputs.size() == 1); + + const auto& srcShape = inputs[0]; + + std::vector outShape(srcShape.size()); + for (int i = 0; i < srcShape.size(); i++) + { + outShape[i] = inputs[0][i] - cropSize[i] * 2; + } + outputs.assign(1, outShape); + + return false; + + //return cropLayer->getMemoryShapes(inputs, requiredOutputs, outputs, internals); + } + + virtual bool updateMemoryShapes(const std::vector& inputs) override + { + return cropLayer->updateMemoryShapes(inputs); + } +}; + +static double sumAllElements(const cv::Mat& mat) +{ + CV_Assert(!mat.empty()); + + const cv::Scalar s = cv::sum(mat); // チャンネルごとの合計 + double total = 0.0; + for (int c = 0; c < mat.channels(); ++c) { + total += s[c]; + } + return total; +} + + +// ---- 内部実装 ---- +template +static void printRec(const cv::Mat& m, std::vector& idx, int d) { + if (d == m.dims - 1) { + // 最終軸:一次元の並びを出力 + const int cn = m.channels(); + std::cout << "["; + for (int i = 0; i < m.size[d]; ++i) { + idx[d] = i; + const T* p = m.ptr(idx.data()); // idx の位置の要素先頭(ch=0)へのポインタ + if (cn == 1) { + std::cout << p[0]; + } + else { + std::cout << "("; + for (int c = 0; c < cn; ++c) { + std::cout << p[c]; + if (c + 1 < cn) std::cout << ", "; + } + std::cout << ")"; + } + if (i + 1 < m.size[d]) std::cout << ", "; + } + std::cout << "]"; + } + else { + // 途中軸:再帰で内側へ + std::cout << "["; + for (int i = 0; i < m.size[d]; ++i) { + idx[d] = i; + printRec(m, idx, d + 1); + if (i + 1 < m.size[d]) std::cout << ",\n"; + } + std::cout << "]"; + } +} + +template +static void printMatND_T(const cv::Mat& m) { + // 浮動小数は小数桁を控えめに + if (std::is_floating_point::value) { + std::cout << std::fixed << std::setprecision(6); + } + std::vector idx(m.dims, 0); + printRec(m, idx, 0); + std::cout << std::endl; +} + +// エントリポイント(cv::Mat の depth に応じてディスパッチ) +static void printMatND(const cv::Mat& m) { + switch (m.depth()) { + case CV_8U: printMatND_T(m); break; + case CV_8S: printMatND_T(m); break; + case CV_16U: printMatND_T(m); break; + case CV_16S: printMatND_T(m); break; + case CV_32S: printMatND_T(m); break; + case CV_32F: printMatND_T(m); break; + case CV_64F: printMatND_T(m); break; + default: + throw std::runtime_error("Unsupported Mat depth."); + } +} + + +void reg(); +void reg2(); + +int main(int argc, char** argv) { + //CV_DNN_REGISTER_LAYER_CLASS(CropCenter, CropCenterLayer); + reg(); + reg2(); + + // ImageNet Caffeリファレンスモデル + string protoFile = "models/upresnet10/noise0_scale2.0x_model.prototxt"; + string modelFile = "models/upresnet10/noise0_scale2.0x_model.json.caffemodel"; + + // 画像ファイル + //string imageFile = (argc > 1) ? argv[1] : "images/cat.jpg"; + string imageFile = "red.png"; + + // Caffeモデルの読み込み + cv::dnn::Net net; + try { + net = cv::dnn::readNetFromCaffe(protoFile, modelFile); + + //net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA); + //net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA); + + net.setPreferableBackend(cv::dnn::DNN_BACKEND_OPENCV); + net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU); + } + catch (const cv::Exception& e) { + cerr << e.msg << endl; + exit(-1); + } + + // テスト用の入力画像ファイルの読み込み + cv::Mat img = cv::imread(imageFile); + if (img.empty()) { + cerr << "can't read image: " << imageFile << endl; + exit(-1); + } + try { + // 入力画像をリサイズ + int cropSize = 90; + cv::resize(img, img, cv::Size(cropSize, cropSize)); + // Caffeで扱うBlob形式に変換 (実体はcv::Matのラッパークラス) + const auto inputBlob = cv::dnn::blobFromImage(img, 1.0 / 255.0, cv::Size(), cv::Scalar(), true, false, CV_32F); + + //printMatND(inputBlob); + + std::vector indim(inputBlob.size.p, inputBlob.size.p + inputBlob.size.dims()); + // 入力層に画像を入力 + net.setInput(inputBlob, "input"); + // フォワードパス(順伝播)の計算&出力層(Softmax)の出力を取得, ここに予測結果が格納されている + // ImageNet 1000クラス毎の確率(32bits浮動小数点値)が格納された1x1000の行列(ベクトル) + //const auto probMat = net.forward("/conv_post"); + const auto probMat = net.forward("/res1/axpy"); + + std::vector probMatDim(probMat.size.p, probMat.size.p + probMat.size.dims()); + auto sss = sumAllElements(probMat); + //printMatND(probMat); + + std::vector outImgs; + cv::dnn::imagesFromBlob(probMat, outImgs); + //cv::dnn::imagesFromBlob(inputBlob, outImgs); + auto outImg = outImgs[0]; + + std::vector outdim(outImg.size.p, outImg.size.p + outImg.size.dims()); + printMatND(outImg); + + //std::cout << cv::format(outImg, cv::Formatter::FMT_DEFAULT) << std::endl; + + // 値を0〜1にクリッピング + cv::threshold(outImg, outImg, 1.0, 1.0, cv::THRESH_TRUNC); + cv::threshold(outImg, outImg, 0.0, 0.0, cv::THRESH_TOZERO); + + const double clip_eps8 = (1.0 / 255.0) * 0.5 - (1.0e-7 * (1.0 / 255.0) * 0.5); + outImg.convertTo(outImg, CV_8U, 255.0, clip_eps8); + + cv::cvtColor(outImg, outImg, cv::COLOR_RGB2BGR); + + cv::imwrite("test.png", outImg); + + //// 確率(信頼度)の高い順にソートして、上位5つのインデックスを取得 + //cv::Mat sorted(probMat.rows, probMat.cols, CV_32F); + //cv::sortIdx(probMat, sorted, cv::SORT_EVERY_ROW | cv::SORT_DESCENDING); + //cv::Mat topk = sorted(cv::Rect(0, 0, 5, 1)); + //// カテゴリ名のリストファイル(synset_words.txt)を読み込み + //// データ例: categoryList[951] = "lemon"; + //vector categoryList; + //string category; + //ifstream fs("synset_words.txt"); + //if (!fs.is_open()) { + // cerr << "can't read file" << endl; + // exit(-1); + //} + //while (getline(fs, category)) { + // if (category.length()) { + // categoryList.push_back(category.substr(category.find(' ') + 1)); + // } + //} + //fs.close(); + //// 予測したカテゴリと確率(信頼度)を出力 + //cv::Mat_::const_iterator it = topk.begin(); + //while (it != topk.end()) { + // cout << categoryList[*it] << " : " << probMat.at(*it) * 100 << " %" << endl; + // ++it; + //} + } + catch (const cv::Exception& e) { + cerr << e.msg << endl; + } + return 0; +} diff --git a/waifu2x-caffe/axpy.hpp b/waifu2x-caffe/axpy.hpp new file mode 100644 index 0000000..ec86d83 --- /dev/null +++ b/waifu2x-caffe/axpy.hpp @@ -0,0 +1,106 @@ +// This file is part of OpenCV project. +// It is subject to the license terms in the LICENSE file found in the top-level directory +// of this distribution and at http://opencv.org/license.html. + +#ifndef OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SCALE_SHIFT_HPP +#define OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SCALE_SHIFT_HPP + +//#include "../../op_cuda.hpp" +// +//#include "../csl/stream.hpp" +//#include "../csl/tensor.hpp" +// +//#include "../kernels/scale_shift.hpp" + +#include + +#include + +#include + +#include +#include + +namespace cv { + namespace dnn { + namespace cuda4dnn { + + template + class AxpyOp final : public CUDABackendNode { + public: + using wrapper_type = GetCUDABackendWrapperType; + + AxpyOp(csl::Stream stream_) + : stream(std::move(stream_)), axis(0) + { + } + + void forward( + const std::vector>& inputs, + const std::vector>& outputs, + csl::Workspace& workspace) override + { + CV_Assert(inputs.size() == 3); + CV_Assert(outputs.size() == 1); + + auto input_wrapper = inputs[1].dynamicCast(); + auto input = input_wrapper->getView(); + + auto output_wrapper = outputs[0].dynamicCast(); + auto output = output_wrapper->getSpan(); + + /* number of batches in the weights/bias + * trainable mode: same for all batches + * untrainable mode: could be different for different batch samples + */ + std::size_t parameter_batch_size = 1; + + csl::TensorView weights = inputs[0].dynamicCast()->getView(); + parameter_batch_size = weights.get_axis_size(0); + CV_Assert(parameter_batch_size == input.get_axis_size(0)); + + csl::TensorView bias = inputs[2].dynamicCast()->getView(); + parameter_batch_size = bias.get_axis_size(0); + CV_Assert(parameter_batch_size == input.get_axis_size(0)); + + CV_Assert(!weights.empty() || !bias.empty()); + if (!weights.empty() && !bias.empty()) + { + CV_CheckEQ(weights.size(), bias.size(), "different broadcasting options for weights and bias is not supported"); + } + + const auto num_parameters = !weights.empty() ? weights.size() : bias.size(); + const auto mid_size = num_parameters / parameter_batch_size; + + /* the scale shift operation might require broadcasting */ + const int end_axis = [&] { + if (num_parameters == 1) { + return static_cast(axis + 1); + } + for (int endAxis = axis + 1; endAxis <= input.rank(); endAxis++) { + if (input.size_range(axis, endAxis) == mid_size) + return endAxis; + } + CV_Assert(0 /* failed to find a broadcast config */); + }(); + + std::size_t inner_size = input.size_range(end_axis, input.rank()); + + if (!weights.empty() && !bias.empty()) + kernels::scaleN_with_biasN(stream, output, input, inner_size, weights, bias); + else if (!weights.empty()) + kernels::scaleN(stream, output, input, inner_size, weights); + else + kernels::biasN(stream, output, input, inner_size, bias); + } + + private: + csl::Stream stream; + std::size_t axis; + }; + + } + } +} /* namespace cv::dnn::cuda4dnn */ + +#endif /* OPENCV_DNN_SRC_CUDA4DNN_PRIMITIVES_SCALE_SHIFT_HPP */ diff --git a/waifu2x-caffe/axpy_fast_layer.cpp b/waifu2x-caffe/axpy_fast_layer.cpp new file mode 100644 index 0000000..f2546e3 --- /dev/null +++ b/waifu2x-caffe/axpy_fast_layer.cpp @@ -0,0 +1,371 @@ +#include +//#include +#include + +//#include +#include +#include +#include +#include +#include + +#include +#include + +#include +#include + +#ifdef HAVE_CUDA +//#include +#include "axpy.hpp" +using namespace cv::dnn::cuda4dnn; +#endif + +namespace cv +{ + namespace dnn + { + + class AxpyFastLayerImpl CV_FINAL : public Layer + { + public: +#ifdef HAVE_WEBNN + mutable int dims; + mutable int numChannels; +#endif + AxpyFastLayerImpl(const LayerParams& params) + { + setParamsFrom(params); + } + + bool getMemoryShapes(const std::vector& inputs, + const int requiredOutputs, + std::vector& outputs, + std::vector& internals) const CV_OVERRIDE + { + outputs.assign(1, inputs[1]); +#ifdef HAVE_WEBNN + dims = inputs[0].size(); + numChannels = 1; + if (inputs.size() > 1) + { + for (const size_t& dim : inputs[1]) + numChannels *= dim; + } +#endif + return true; + } + + virtual void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays) CV_OVERRIDE + { + std::vector inputs; + inputs_arr.getMatVector(inputs); + CV_Assert(inputs.size() == 3); + } + + virtual bool supportBackend(int backendId) CV_OVERRIDE + { +#ifdef HAVE_INF_ENGINE + if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) + return true; +#endif + return backendId == DNN_BACKEND_OPENCV || + backendId == DNN_BACKEND_CUDA || + backendId == DNN_BACKEND_HALIDE || + backendId == DNN_BACKEND_WEBNN; + } + + void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE + { + CV_TRACE_FUNCTION(); + CV_TRACE_ARG_VALUE(name, "name", name.c_str()); + + if (inputs_arr.depth() == CV_16F) + { + forward_fallback(inputs_arr, outputs_arr, internals_arr); + return; + } + + std::vector inputs, outputs; + inputs_arr.getMatVector(inputs); + outputs_arr.getMatVector(outputs); + + CV_Assert_N(outputs.size() == 1, inputs.size() == 3); + + Mat& inpBlob = inputs[1]; + Mat& outBlob = outputs[0]; + // There is a mode when we multiply a first blob by a second one + // instead of trainable weights. + Mat weights = inputs[0].reshape(1, 1); + Mat bias = inputs[2].reshape(1, 1); + + MatShape inpShape0 = shape(inputs[0]); + MatShape inpShape1 = shape(inputs[1]); + MatShape inpShape2 = shape(inputs[2]); + + // TODO: 向こうが想定しているbiasがこちらが想定しているshapeと違う想定っぽいので計算処理を書き直す + // こちらが想定しているの: weights.shape == bias.shape + // 向こうが想定しているの: inpBlob.shape == bias.shape + + MatShape inpShape = shape(inpBlob); + const int numWeights = weights.total(); + CV_Assert(numWeights != 0); + CV_CheckEQ(weights.total(), bias.total(), "Incompatible weights/bias blobs"); + + if (weights.total() == 1) + { + // The total() of bias should be same as weights. + inpBlob.convertTo(outBlob, CV_32F, weights.at(0), bias.at(0)); + return; + } + + int endAxis; + for (endAxis = 1; endAxis <= inpBlob.dims; ++endAxis) + { + if (total(inpShape, 0, endAxis) == numWeights) + break; + } + CV_Assert(total(inpShape, 0, endAxis) == numWeights); + CV_Assert(numWeights == bias.total()); + CV_CheckTypeEQ(inpBlob.type(), CV_32FC1, ""); CV_CheckTypeEQ(outBlob.type(), CV_32FC1, ""); + + int numSlices = total(inpShape, 0, 0); + float* inpData = (float*)inpBlob.data; + float* outData = (float*)outBlob.data; + + if (endAxis != inpBlob.dims) + { + float* weightsData = (float*)weights.data; + float* biasesData = (float*)bias.data; + int spatialSize = total(inpShape, endAxis); // spatialSize != 1 + for (int i = 0; i < numSlices; ++i) + { + for (int j = 0; j < numWeights; ++j) + { + float w = weightsData ? weightsData[j] : 1; + float b = biasesData ? biasesData[j] : 0; + Mat inpSlice(1, spatialSize, CV_32F, inpData); + Mat outSlice(1, spatialSize, CV_32F, outData); + + inpSlice.convertTo(outSlice, CV_32F, w, b); + + inpData += spatialSize; + outData += spatialSize; + } + } + } + else + { + for (int i = 0; i < numSlices; ++i) + { + Mat inpSlice(1, numWeights, CV_32F, inpData); + Mat outSlice(1, numWeights, CV_32F, outData); + + multiply(inpSlice, weights, outSlice); + add(outSlice, bias, outSlice); + + inpData += numWeights; + outData += numWeights; + } + } + } + +#ifdef HAVE_CUDA + Ptr initCUDA( + void* context_, + const std::vector>& inputs, + const std::vector>& outputs + ) override + { + auto context = reinterpret_cast(context_); + + CV_Assert(inputs.size() == 3); + + return make_cuda_node(preferableTarget, std::move(context->stream)); + } +#endif + + virtual Ptr tryAttach(const Ptr& node) CV_OVERRIDE + { + switch (node->backendId) + { + case DNN_BACKEND_HALIDE: + { +#ifdef HAVE_HALIDE + auto base = node.dynamicCast(); + Halide::Func& input = base->funcs.back(); + Halide::Var x("x"), y("y"), c("c"), n("n"); + Halide::Func top = attachHalide(input(x, y, c, n)); + return Ptr(new HalideBackendNode(base, top)); +#endif // HAVE_HALIDE + break; + } + } + return Ptr(); + } + + virtual Ptr initHalide(const std::vector >& inputs) CV_OVERRIDE + { +#ifdef HAVE_HALIDE + Halide::Buffer input = halideBuffer(inputs[0]); + Halide::Var x("x"), y("y"), c("c"), n("n"); + Halide::Func top = attachHalide(input(x, y, c, n)); + return Ptr(new HalideBackendNode(top)); +#endif // HAVE_HALIDE + return Ptr(); + } + +#ifdef HAVE_HALIDE + // attachHalide can work both with Halide::Buffer and Halide::Func. In the + // second case it will be a fusion. + Halide::Func attachHalide(const Halide::Expr& input) + { + Halide::Func top = (name.empty() ? Halide::Func() : Halide::Func(name)); + Halide::Var x("x"), y("y"), c("c"), n("n"); + + const int numChannels = blobs[0].total(); + + Halide::Expr topExpr = input; + if (hasWeights) + { + auto weights = wrapToHalideBuffer(blobs[0], { numChannels }); + topExpr *= weights(c); + } + if (hasBias) + { + auto bias = wrapToHalideBuffer(blobs.back(), { numChannels }); + topExpr += bias(c); + } + top(x, y, c, n) = topExpr; + return top; + } +#endif // HAVE_HALIDE + + +#ifdef HAVE_DNN_NGRAPH + virtual Ptr initNgraph(const std::vector >& inputs, const std::vector >& nodes) CV_OVERRIDE + { + auto ieInpNode0 = nodes[0].dynamicCast()->node; + ov::Output ieInpNode1; + if (nodes.size() > 1) + ieInpNode1 = nodes[1].dynamicCast()->node; + + size_t numChannels = 1; + if (blobs.empty()) + for (const size_t& dim : ieInpNode1.get_shape()) + numChannels *= dim; + else + numChannels = blobs[0].total(); + + std::vector shape(ieInpNode0.get_shape().size(), 1); + int cAxis = normalize_axis(axis, shape.size()); + shape[cAxis] = numChannels; + + std::shared_ptr node; + if (hasWeights) + { + ov::Output weight = blobs.empty() ? ieInpNode1 : + std::make_shared(ov::element::f32, ov::Shape(shape), blobs[0].data); + node = std::make_shared(ieInpNode0, weight, ov::op::AutoBroadcastType::NUMPY); + } + if (hasBias || !hasWeights) + { + ov::Output bias; + if (hasBias) + { + bias = blobs.empty() ? ieInpNode1 : + std::make_shared(ov::element::f32, + ov::Shape(shape), blobs.back().data); + } + else + bias = std::make_shared(ov::element::f32, + ov::Shape(shape), std::vector(numChannels, 0).data()); + node = std::make_shared(node, bias, ov::op::AutoBroadcastType::NUMPY); + } + return Ptr(new InfEngineNgraphNode(node)); + } +#endif // HAVE_DNN_NGRAPH + +#ifdef HAVE_WEBNN + virtual Ptr initWebnn(const std::vector >& inputs, const std::vector >& nodes) CV_OVERRIDE + { + Ptr node = nodes[0].dynamicCast(); + auto& webnnInpOperand0 = node->operand; + auto& webnnGraphBuilder = node->net->builder; + auto webnnInpOperand1 = nodes.size() > 1 ? nodes[1].dynamicCast()->operand : nullptr; + auto webnnInpOperand2 = nodes.size() > 2 ? nodes[1].dynamicCast()->operand : nullptr; + std::vector shape(dims, 1); + + size_t channels = 1; + if (blobs.empty()) + channels = numChannels; + else + channels = blobs[0].total(); + + int cAxis = normalize_axis(axis, shape.size()); + shape[cAxis] = channels; + + ml::Operand operand = webnnInpOperand0; + if (hasWeights) + { + ml::Operand webnnWeights = blobs.empty() ? webnnInpOperand1 : webnn::BuildConstant(webnnGraphBuilder, webnn::getShape(blobs[0]), blobs[0].data, blobs[0].total() * blobs[0].elemSize(), ml::OperandType::Float32); + webnnWeights = webnnGraphBuilder.Reshape(webnnWeights, shape.data(), shape.size()); + operand = webnnGraphBuilder.Mul(operand, webnnWeights); + } + if (hasBias) + { + ml::Operand webnnBias; + if (!hasWeights) + webnnBias = blobs.empty() ? webnnInpOperand1 : webnn::BuildConstant(webnnGraphBuilder, webnn::getShape(blobs.back()), blobs.back().data, blobs.back().total() * blobs.back().elemSize(), ml::OperandType::Float32); + else + webnnBias = blobs.empty() ? webnnInpOperand2 : webnn::BuildConstant(webnnGraphBuilder, webnn::getShape(blobs.back()), blobs.back().data, blobs.back().total() * blobs.back().elemSize(), ml::OperandType::Float32); + webnnBias = webnnGraphBuilder.Reshape(webnnBias, shape.data(), shape.size()); + operand = webnnGraphBuilder.Add(operand, webnnBias); + } + + return Ptr(new WebnnBackendNode(operand)); + } +#endif + + + void getScaleShift(Mat& scale, Mat& shift) const CV_OVERRIDE + { + scale = Mat(); + shift = Mat(); + } + + //bool tryQuantize(const std::vector >& scales, + // const std::vector >& zeropoints, LayerParams& params) CV_OVERRIDE + //{ + // params.set("input_scales", DictValue::arrayReal(scales[0].data(), scales[0].size())); + // params.set("input_zeropoints", DictValue::arrayInt(zeropoints[0].data(), zeropoints[0].size())); + // return true; + //} + + virtual int64 getFLOPS(const std::vector& inputs, + const std::vector& outputs) const CV_OVERRIDE + { + CV_UNUSED(outputs); // suppress unused variable warning + long flops = 0; + for (int i = 0; i < inputs.size(); i++) + { + flops += 3 * total(inputs[i]); + } + return flops; + } + + static Ptr create(const LayerParams& params) + { + return Ptr(new AxpyFastLayerImpl(params)); + } + }; + + } // namespace dnn +} // namespace cv + +# include + +void reg2() +{ + CV_DNN_REGISTER_LAYER_CLASS(AxpyFast, cv::dnn::AxpyFastLayerImpl); +} diff --git a/waifu2x-caffe/slice_layer.cpp b/waifu2x-caffe/slice_layer.cpp new file mode 100644 index 0000000..596b09a --- /dev/null +++ b/waifu2x-caffe/slice_layer.cpp @@ -0,0 +1,992 @@ +//#include "../precomp.hpp" +//#include "../op_cuda.hpp" +//#include "../op_inf_engine.hpp" +//#include "../ie_ngraph.hpp" +//#include "../op_cann.hpp" + +//#include "layers_common.hpp" +#include +#include +#include + +#include +#include + +#include + +#ifdef HAVE_OPENCL +#include "opencl_kernels_dnn.hpp" +#endif + +#ifdef HAVE_CUDA +#include +using namespace cv::dnn::cuda4dnn; +#endif + +namespace +{ + cv::Range normalizeRange(const cv::Range& input_range, int n) + { + cv::Range range = input_range; + + if (range.start != n) { + range.start = std::min(std::max(range.start, -n), n - 1); + if (range.start < 0) + { + range.start += n; + } + } + + range.end = std::min(std::max(range.end, -n), n); + if (range.end < 0) + { + range.end += n; + } + + return range; + } + + // TODO: support cv::Range with steps and negative steps to get rid of this transformation + void tranformForNegSteps(const cv::dnn::MatShape& inpShape, std::vector >& sliceRanges, std::vector >& sliceSteps) + { + // in case of negative steps, + // x of shape [5, 10], x[5:0:-1, 10:1:-3] <=> np.flip(x[1:5:1, 2:10:3], aixs=(0, 1)) + // new_end_i = start_i + 1 > dim_i ? dim_i : start_i + 1 + // new_start_i = end + 1 + // new_start_i = new_end_i - 1 - ((new_end_i - 1 - new_start_i) / abs(step_i)) * abs(step_i) + int start, end, new_start, new_end, step; + for (int i = 0; i < sliceSteps[0].size(); ++i) + { + step = sliceSteps[0][i]; + if (step > 0) + continue; + + step = -step; + start = sliceRanges[0][i].start; + end = sliceRanges[0][i].end; + new_end = start >= inpShape[i] ? inpShape[i] : start + 1; + new_start = end + 1; + new_start = new_end - 1 - ((new_end - 1 - new_start) / step) * step; + + sliceSteps[0][i] = step; + sliceRanges[0][i].start = new_start; + sliceRanges[0][i].end = new_end; + } + } + + std::vector > finalizeSliceRange(const cv::dnn::MatShape& inpShape, int& axis, + const std::vector >& inputSliceRanges) + { + std::vector > sliceRanges = inputSliceRanges; + CV_Assert(inpShape.size() > 0); + bool axisNeg = (axis < 0); + axis = (axis + static_cast(inpShape.size())) % inpShape.size(); + + for (size_t i = 0; i < sliceRanges.size(); ++i) { + std::vector& ranges = sliceRanges[i]; + if (axisNeg) + { + ranges.insert(ranges.begin(), axis, cv::Range::all()); + } + + for (size_t j = 0; j < ranges.size(); ++j) + { + int n = inpShape[j]; + if (n <= 0) + { + continue; + } + + ranges[j] = normalizeRange(ranges[j], n); + } + } + + return sliceRanges; + } +} + +namespace cv +{ + namespace dnn + { + + + + class SliceLayerImpl : public SliceLayer + { + public: + SliceLayerImpl(const LayerParams& params) + { + setParamsFrom(params); + hasSteps = false; + axis = params.get("axis", 1); + num_split = params.get("num_split", 0); + hasDynamicShapes = params.get("has_dynamic_shapes", false); + shapesInitialized = !hasDynamicShapes; + + if (params.has("slice_point")) + { + CV_Assert(!params.has("begin") && !params.has("size") && !params.has("end")); + const DictValue& indicesValue = params.get("slice_point"); + int size = axis > 0 ? axis + 1 : 1; + sliceRanges.resize(indicesValue.size() + 1, + std::vector(size, Range::all())); + int prevSlice = 0; + for (int i = 0; i < indicesValue.size(); ++i) + { + sliceRanges[i][size - 1].start = prevSlice; + sliceRanges[i][size - 1].end = indicesValue.get(i); + prevSlice = sliceRanges[i][size - 1].end; + } + sliceRanges.back()[size - 1].start = prevSlice; + } + else if (params.has("begin")) + { + CV_Assert(params.has("size") ^ params.has("end")); + const DictValue& begins = params.get("begin"); + const DictValue& sizesOrEnds = params.has("size") ? params.get("size") : params.get("end"); + CV_Assert(begins.size() == sizesOrEnds.size()); + + if (params.has("steps")) + { + const DictValue& steps = params.get("steps"); + sliceSteps.resize(1); + sliceSteps[0].resize(steps.size()); + + for (int i = 0; i < steps.size(); ++i) + { + int step = steps.get(i); + CV_Assert(step != 0); + if (step < 0) + neg_step_dims.push_back(i); + if (std::abs(step) > 1) + hasSteps = true; + sliceSteps[0][i] = step; + } + } + + sliceRanges.resize(1); + sliceRanges[0].resize(begins.size(), Range::all()); + for (int i = 0; i < begins.size(); ++i) + { + int start = begins.get(i); + int sizeOrEnd = sizesOrEnds.get(i); // It may be negative to reverse indexation. + + sliceRanges[0][i].start = start; + if (params.has("size")) + { + int size = sizeOrEnd; + CV_Assert(size == -1 || size > 0); // -1 value means range [start, axis_size). + sliceRanges[0][i].end = size > 0 ? (start + size) : INT_MAX; // We'll finalize a negative value later. + } + else + { + int end = sizeOrEnd; + if (hasSteps && !neg_step_dims.empty() && sliceSteps[0][i] < 0) + CV_Assert(end < 0 || end != start); // if current step is negative, end < start is allowed. + else + CV_Assert(end < 0 || end > start); // End index is excluded. + sliceRanges[0][i].end = end; // We'll finalize a negative value later. + } + } + } + } + + virtual bool supportBackend(int backendId) CV_OVERRIDE + { +#ifdef HAVE_INF_ENGINE + if (backendId == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) + return sliceRanges.size() == 1 && neg_step_dims.empty(); +#endif +#ifdef HAVE_CUDA + if (backendId == DNN_BACKEND_CUDA) + return !hasSteps && neg_step_dims.empty(); +#endif + return backendId == DNN_BACKEND_OPENCV || backendId == DNN_BACKEND_CANN; + } + + bool getMemoryShapes(const std::vector& inputs, + const int requiredOutputs, + std::vector& outputs, + std::vector& internals) const CV_OVERRIDE + { + CV_Assert(inputs.size() == 1); + MatShape inpShape = inputs[0]; + + std::vector > sliceSteps_ = sliceSteps; + std::vector > sliceRanges_ = sliceRanges; + if (hasSteps && !neg_step_dims.empty()) + tranformForNegSteps(inpShape, sliceRanges_, sliceSteps_); + + int axis_rw = axis; + std::vector > sliceRanges_rw = finalizeSliceRange(inpShape, axis_rw, sliceRanges_); + + if (!sliceRanges_rw.empty()) + { + outputs.resize(sliceRanges_rw.size(), inpShape); + for (int i = 0; i < outputs.size(); ++i) + { + CV_Assert(sliceRanges_rw[i].size() <= inpShape.size()); + for (int j = 0; j < sliceRanges_rw[i].size(); ++j) + { + if (shapesInitialized || inpShape[j] > 0) + outputs[i][j] = normalizeRange(sliceRanges_rw[i][j], inpShape[j]).size(); + + if (!sliceSteps_.empty() && (i < sliceSteps_.size()) && (j < sliceSteps_[i].size()) && (sliceSteps_[i][j] > 1)) + outputs[i][j] = (outputs[i][j] + sliceSteps_[i][j] - 1) / sliceSteps_[i][j]; + } + } + } + else // Divide input blob on equal parts by axis. + { + CV_Assert(0 <= axis_rw && axis_rw < inpShape.size()); + int splits = num_split ? num_split : requiredOutputs; + CV_Assert(splits > 0 && inpShape[axis_rw] % splits == 0); + inpShape[axis_rw] /= splits; + outputs.resize(splits, inpShape); + } + return false; + } + + bool updateMemoryShapes(const std::vector& inputs) CV_OVERRIDE + { + shapesInitialized = true; + return true; + } + + void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr) CV_OVERRIDE + { +#ifdef HAVE_OPENCL + ocl_exec_cache.clear(); +#endif + + std::vector inputs, outputs; + inputs_arr.getMatVector(inputs); + outputs_arr.getMatVector(outputs); + + CV_Assert(inputs.size() == 1); + MatShape inpShape = shape(inputs[0]); + + if (hasSteps && !neg_step_dims.empty()) + tranformForNegSteps(inpShape, sliceRanges, sliceSteps); + + finalSliceRanges = finalizeSliceRange(shape(inputs[0]), axis, sliceRanges); + + if (sliceRanges.empty()) + { + // Divide input blob on equal parts by axis. + int outAxisSize = inpShape[axis] / outputs.size(); + finalSliceRanges.resize(outputs.size(), + std::vector(axis + 1, Range::all())); + int prevSlice = 0; + for (int i = 0; i < outputs.size(); ++i) + { + finalSliceRanges[i][axis].start = prevSlice; + finalSliceRanges[i][axis].end = finalSliceRanges[i][axis].start + outAxisSize; + prevSlice = finalSliceRanges[i][axis].end; + } + } + else + CV_Assert(outputs.size() == sliceRanges.size()); + + for (int i = 0; i < outputs.size(); ++i) + { + CV_Assert(finalSliceRanges[i].size() <= inpShape.size()); + // Fill the rest of ranges. + for (int j = finalSliceRanges[i].size(); j < inpShape.size(); ++j) + { + finalSliceRanges[i].push_back(Range::all()); + } + // Clamp. + for (int j = 0; j < finalSliceRanges[i].size(); ++j) + { + finalSliceRanges[i][j] = normalizeRange(finalSliceRanges[i][j], inpShape[j]); + } + } + + if (!sliceSteps.empty() && sliceSteps[0].size() != inputs[0].dims) + sliceSteps[0].resize(inputs[0].dims, 1); + +#if 0 + std::cout << "DEBUG: DNN/Slice: " << outputs.size() << " inpShape=" << inpShape << std::endl; + for (int i = 0; i < outputs.size(); ++i) + { + for (int j = 0; j < finalSliceRanges[i].size(); ++j) + { + std::cout << finalSliceRanges[i][j]; + } + std::cout << std::endl; + } +#endif + } + +#ifdef HAVE_OPENCL + struct OpenCLExecInfo + { + std::string kernel_name; + std::string build_opts; + size_t local_size[2]; + size_t global_size[2]; + + OpenCLExecInfo() + { + local_size[0] = local_size[1] = 0; + global_size[0] = global_size[1] = 0; + } + }; + std::vector ocl_exec_cache; + + void ocl_prepare(const std::vector& inputs, const std::vector& outputs) + { + CV_TRACE_FUNCTION(); + + CV_Assert(outputs.size() == finalSliceRanges.size()); + ocl_exec_cache.resize(outputs.size()); + + const UMat& input = inputs[0]; + const int dims = input.dims; + + size_t WSZ = 128; + + const int elemSize = (int)input.elemSize(); + String opts0 = cv::format( + "-DDIMS=%d -DELEMSIZE=%d", + dims, elemSize + ); + for (int d = 0; d < dims; d++) + { + opts0 += cv::format(" -DSRC_STEP_%d=%d", d, (int)input.step[dims - 1 - d]); + } + for (size_t i = 0; i < outputs.size(); i++) + { + OpenCLExecInfo& ocl = ocl_exec_cache[i]; + + const UMat& output = outputs[i]; + const std::vector& range = finalSliceRanges[i]; + + String opts = opts0; + + CV_CheckEQ(output.dims, dims, ""); + for (int d = 0; d < dims; d++) + { + opts += cv::format(" -DDST_STEP_%d=%d -DDST_SZ_%d=%d -DSRC_START_%d=%d", + d, (int)output.step[dims - 1 - d], + d, (int)output.size[dims - 1 - d], + d, (int)range[dims - 1 - d].start + ); + CV_CheckEQ(range[d].size(), (int)output.size[d], ""); + } + + const size_t param_LIMIT_BLOCK_SIZE_PER_WG = WSZ * 64; + + int block_dims = 0; + size_t block_size = elemSize; + for (int i = dims - 1; i >= 0; --i) + { + if (input.step[i] != output.step[i]) + break; + block_size *= output.size[i]; + block_dims++; + if (block_size >= param_LIMIT_BLOCK_SIZE_PER_WG) + break; + } + + const size_t total = output.total() * elemSize; + size_t num_blocks = total / block_size; + + if ((num_blocks <= 8 && block_size >= WSZ * 4) || (block_size >= param_LIMIT_BLOCK_SIZE_PER_WG)) + { + // use 1D copy mode + opts += cv::format(" -DUSE_COPY_1D=1"); + + opts += cv::format(" -DBLOCK_DIMS=%d", block_dims); + opts += cv::format(" -DBLOCK_DIMS_CONTIGUOUS=%d", block_dims); + opts += cv::format(" -DBLOCK_SIZE=%d", (int)block_size); + + opts += cv::format(" -DBLOCK_COLS=%d", (int)block_size); + } + else + { + // use 2D copy mode + int block_cols = block_size; + int block_dims_contiguous = block_dims; + size_t input_base_step = input.step[dims - 1 - block_dims_contiguous]; + size_t output_base_step = output.step[dims - 1 - block_dims_contiguous]; + + size_t block_rows = 1; + for (int i = dims - 1 - block_dims_contiguous; i >= 0; --i) + { + if (input.step[i] * output_base_step != output.step[i] * input_base_step) + break; + block_rows *= output.size[i]; + block_dims++; + } + + block_size *= block_rows; + + num_blocks = total / block_size; + + if (block_rows > 1) + { + opts += cv::format(" -DBLOCK_DIMS=%d", block_dims); + opts += cv::format(" -DBLOCK_DIMS_CONTIGUOUS=%d", block_dims_contiguous); + opts += cv::format(" -DBLOCK_SIZE=%d", (int)block_size); + + opts += cv::format(" -DBLOCK_COLS=%d", (int)block_cols); + + opts += cv::format(" -DBLOCK_ROWS=%d", (int)block_rows); + opts += cv::format(" -DBLOCK_SRC_STRIDE=%d", (int)input_base_step); + } + else + { + // use 1D copy mode + opts += cv::format(" -DUSE_COPY_1D=1"); + + opts += cv::format(" -DBLOCK_DIMS=%d", block_dims_contiguous); + opts += cv::format(" -DBLOCK_DIMS_CONTIGUOUS=%d", block_dims_contiguous); + opts += cv::format(" -DBLOCK_SIZE=%d", (int)block_size); + + opts += cv::format(" -DBLOCK_COLS=%d", (int)block_size); + } + } + + const size_t MIN_WORK_ITEMS = 16; + if (block_size <= 4 * MIN_WORK_ITEMS) + WSZ = 4; + else if (block_size <= 8 * MIN_WORK_ITEMS) + WSZ = 8; + else if (block_size <= 16 * MIN_WORK_ITEMS) + WSZ = 16; + else if (block_size <= 32 * MIN_WORK_ITEMS) + WSZ = 32; + else if (block_size <= 64 * MIN_WORK_ITEMS) + WSZ = 64; + + opts += cv::format(" -DWSZ=%d", (int)WSZ); + + std::ostringstream kernel_suffix; + kernel_suffix << dims << 'x' << elemSize << "_bsz" << block_size; + kernel_suffix << "__src_"; + for (int d = 0; d < dims; d++) + { + kernel_suffix << input.size[dims - 1 - d] << '_'; + } + kernel_suffix << '_'; + /*for (int d = 0; d < dims; d++) + { + kernel_suffix << input.step[dims - 1 - d] << '_'; + } + kernel_suffix << '_';*/ + + kernel_suffix << "dst_"; + for (int d = 0; d < dims; d++) + { + kernel_suffix << output.size[dims - 1 - d] << '_'; + } + /*kernel_suffix << '_'; + for (int d = 0; d < dims; d++) + { + kernel_suffix << output.step[dims - 1 - d] << '_'; + }*/ + kernel_suffix << "_slice_"; + for (int d = 0; d < dims; d++) + { + kernel_suffix << range[dims - 1 - d].start << '_'; + } + for (int d = 0; d < dims; d++) + { + kernel_suffix << '_' << range[dims - 1 - d].end; + } + + std::string kernel_suffix_str = kernel_suffix.str(); + opts += cv::format(" -DSLICE_KERNEL_SUFFIX=%s", kernel_suffix_str.c_str()); + + ocl.kernel_name = cv::format("slice_%s", kernel_suffix_str.c_str()); + ocl.build_opts = opts; + ocl.local_size[0] = WSZ; + ocl.local_size[1] = 1; + ocl.global_size[0] = WSZ; + ocl.global_size[1] = num_blocks; + } // for outputs.size() + } // ocl_prepare + + bool forward_ocl(InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, OutputArrayOfArrays internals_) + { + CV_TRACE_FUNCTION(); + + if (hasSteps) + return false; // TODO not implemented yet: https://github.com/opencv/opencv/pull/19546 + + std::vector inputs; + std::vector outputs; + + inputs_.getUMatVector(inputs); + outputs_.getUMatVector(outputs); + + CV_Assert(outputs.size() == finalSliceRanges.size()); + + const UMat& input = inputs[0]; + const int dims = input.dims; + if (dims > 5) + { + CV_LOG_INFO(NULL, "DNN/OpenCL/Slice: implementation doesn't support dims=" << dims << ". Fallback to CPU"); + return false; + } + + if (ocl_exec_cache.empty()) + { + ocl_prepare(inputs, outputs); + } + CV_CheckEQ(ocl_exec_cache.size(), outputs.size(), ""); + + for (size_t i = 0; i < outputs.size(); i++) + { + const OpenCLExecInfo& ocl = ocl_exec_cache[i]; + + UMat& output = outputs[i]; + + ocl::Kernel kernel(ocl.kernel_name.c_str(), ocl::dnn::slice_oclsrc, ocl.build_opts); + if (kernel.empty()) + return false; + bool ret = kernel.args( + ocl::KernelArg::PtrReadOnly(input), + ocl::KernelArg::PtrWriteOnly(output) + ) + .run_(2, (size_t*)ocl.global_size, (size_t*)ocl.local_size, false); + if (!ret) + return false; + } // for outputs.size() + + return true; + } // forward_ocl +#endif + + void forward(InputArrayOfArrays inputs_arr, OutputArrayOfArrays outputs_arr, OutputArrayOfArrays internals_arr) CV_OVERRIDE + { + CV_TRACE_FUNCTION(); + CV_TRACE_ARG_VALUE(name, "name", name.c_str()); + + CV_OCL_RUN(IS_DNN_OPENCL_TARGET(preferableTarget), + forward_ocl(inputs_arr, outputs_arr, internals_arr)) + + std::vector inputs, outputs; + inputs_arr.getMatVector(inputs); + outputs_arr.getMatVector(outputs); + + const Mat& inpMat = inputs[0]; + CV_Assert(outputs.size() == finalSliceRanges.size()); + + if (!hasSteps) + { + for (size_t i = 0; i < outputs.size(); i++) + { + if (finalSliceRanges[i][0].start != finalSliceRanges[i][0].end) { + inpMat(finalSliceRanges[i]).copyTo(outputs[i]); + } + } + } + else + { + int dimsNum = inpMat.dims; + + for (size_t i = 0; i < outputs.size(); i++) + { + std::vector inpIdx(dimsNum, 0); + std::vector outIdx(dimsNum, 0); + if (inpMat.type() == CV_16F) + getSliceRecursive(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx); + else if (inpMat.type() == CV_8S) + getSliceRecursive(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx); + else + getSliceRecursive(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx); + // flip for negative steps + flip(outputs[i]); + } + } + } + +#ifdef HAVE_CANN + virtual Ptr initCann(const std::vector >& inputs, + const std::vector >& outputs, + const std::vector >& nodes) CV_OVERRIDE + { + bool isSplit = sliceRanges.size() > 1; + auto x = inputs[0].dynamicCast(); + + if (isSplit) + { + // create operator + auto op = std::make_shared(name); + + // set attr + int n_split = static_cast(outputs.size()); + op->set_attr_num_split(n_split); + + // set inputs + // set inputs : x + auto op_x = nodes[0].dynamicCast()->getOp(); + op->set_input_x_by_name(*op_x, x->name.c_str()); + auto desc_x = x->getTensorDesc(); + op->update_input_desc_x(*desc_x); + // set inputs : size_splits + std::vector size_splits(n_split); + int cnt_split = 0; + for (size_t i = 0; i < sliceRanges.size() - 1; ++i) + { + auto target_range = sliceRanges[i].back(); + size_splits[i] = target_range.end - target_range.start; + cnt_split += size_splits[i]; + } + auto shape_x = desc_x->GetShape().GetDims(); + CV_CheckGT(shape_x[axis], cnt_split, "DNN/CANN: invalid splits"); + size_splits[n_split - 1] = shape_x[axis] - cnt_split; + std::vector shape_size_splits{ (int)size_splits.size() }; + Mat size_splits_mat(shape_size_splits, CV_32S, size_splits.data()); + auto op_const_size_splits = std::make_shared(size_splits_mat.data, size_splits_mat.type(), shape_size_splits, cv::format("%s_size_splits", name.c_str())); + op->set_input_size_splits(*(op_const_size_splits->getOp())); + op->update_input_desc_size_splits(*(op_const_size_splits->getTensorDesc())); + // set inputs : split_dim + Mat split_dim_mat(1, 1, CV_32S, Scalar(axis)); + std::vector split_dim_shape{ 1 }; + auto op_const_split_dim = std::make_shared(split_dim_mat.data, split_dim_mat.type(), split_dim_shape, cv::format("%s_split_dim", name.c_str())); + op->set_input_split_dim(*(op_const_split_dim->getOp())); + op->update_input_desc_split_dim(*(op_const_split_dim->getTensorDesc())); + + // set outputs + op->create_dynamic_output_y(n_split); + for (uint32_t i = 0; i < n_split; ++i) + { + auto desc_output_y_i = std::make_shared(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT); + op->update_dynamic_output_desc_y(i, *desc_output_y_i); + } + + return Ptr(new CannBackendNode(op)); + } + + // ONNX-Slice + CV_CheckEQ(sliceRanges.size(), (size_t)1, ""); + if (hasSteps) + { + CV_CheckEQ(sliceSteps.size(), (size_t)1, "DNN/CANN/Slice: no support to multiple slices"); + CV_CheckEQ(sliceRanges[0].size(), sliceSteps[0].size(), "DNN/CANN/Slice: number of slice ranges does not match number of slice steps"); + } + + const int dims = x->host->dims; + + // create operator + auto op = std::make_shared(name); + + // retrieve begins, ends, axes and steps + std::vector begins, ends, axes, steps; + for (int i = 0; i < sliceRanges[0].size(); i++) + { + begins.push_back(sliceRanges[0][i].start); + ends.push_back(sliceRanges[0][i].end); + axes.push_back(i); + if (hasSteps) + steps.push_back(sliceSteps[0][i]); + else + steps.push_back(1); // put 1 by default + } + std::vector shape_{ dims }; + + // set inputs + // set inputs : x + auto op_x = nodes[0].dynamicCast()->getOp(); + op->set_input_x_by_name(*op_x, x->name.c_str()); + auto x_desc = x->getTensorDesc(); + op->update_input_desc_x(*x_desc); + // set inputs : begin + Mat begin_mat(shape_, CV_32S, &begins[0]); + auto op_const_begin = std::make_shared(begin_mat.data, begin_mat.type(), shape_, cv::format("%s_begin", name.c_str())); + op->set_input_begin(*(op_const_begin->getOp())); + op->update_input_desc_begin(*(op_const_begin->getTensorDesc())); + // set inputs : end + Mat end_mat(shape_, CV_32S, &ends[0]); + auto op_const_end = std::make_shared(end_mat.data, end_mat.type(), shape_, cv::format("%s_end", name.c_str())); + op->set_input_end(*(op_const_end->getOp())); + op->update_input_desc_end(*(op_const_end->getTensorDesc())); + // set inputs : axes + Mat axes_mat(shape_, CV_32S, &axes[0]); + auto op_const_axes = std::make_shared(axes_mat.data, axes_mat.type(), shape_, cv::format("%s_axes", name.c_str())); + op->set_input_axes(*(op_const_axes->getOp())); + op->update_input_desc_axes(*(op_const_axes->getTensorDesc())); + // set inputs : strides + Mat strides_mat(shape_, CV_32S, &steps[0]); + auto op_const_strides = std::make_shared(strides_mat.data, strides_mat.type(), shape_, cv::format("%s_strides", name.c_str())); + op->set_input_strides(*(op_const_strides->getOp())); + op->update_input_desc_strides(*(op_const_strides->getTensorDesc())); + + // set outputs + auto output_desc = std::make_shared(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT); + op->update_output_desc_y(*output_desc); + + return Ptr(new CannBackendNode(op)); + } +#endif + +#ifdef HAVE_DNN_NGRAPH + virtual Ptr initNgraph(const std::vector >& inputs, + const std::vector >& nodes) CV_OVERRIDE + { + CV_Assert_N(nodes.size() <= 2); + auto& ieInpNode = nodes[0].dynamicCast()->node; + CV_Assert(finalSliceRanges[0].size() == ieInpNode.get_shape().size()); + + std::vector offsets, dims, steps; + for (int i = 0; i < finalSliceRanges[0].size(); ++i) + { + offsets.push_back(finalSliceRanges[0][i].start); + dims.push_back(finalSliceRanges[0][i].end); + } + if (hasSteps) + steps = std::vector(sliceSteps[0].begin(), sliceSteps[0].end()); + else + steps = std::vector((int64_t)dims.size(), 1); + + auto lower_bounds = std::make_shared(ov::element::i64, + ov::Shape{ offsets.size() }, offsets.data()); + auto upper_bounds = std::make_shared(ov::element::i64, + ov::Shape{ dims.size() }, dims.data()); + auto strides = std::make_shared(ov::element::i64, + ov::Shape{ dims.size() }, steps); + + auto slice = std::make_shared(ieInpNode, + lower_bounds, upper_bounds, strides, std::vector{}, std::vector{}); + + return Ptr(new InfEngineNgraphNode(slice)); + } +#endif // HAVE_DNN_NGRAPH + + +#ifdef HAVE_CUDA + Ptr initCUDA( + void* context_, + const std::vector>& inputs, + const std::vector>& outputs + ) override + { + auto context = reinterpret_cast(context_); + + std::vector> offsets; + for (const auto& ranges : finalSliceRanges) + { + std::vector offsets_i; + for (const auto& range : ranges) + offsets_i.push_back(range.start); + offsets.push_back(std::move(offsets_i)); + } + + return make_cuda_node(preferableTarget, std::move(context->stream), std::move(offsets)); + } +#endif + + bool tryQuantize(const std::vector >& scales, + const std::vector >& zeropoints, LayerParams& params) CV_OVERRIDE + { + const int numOutputs = scales[1].size(); + for (int i = 0; i < numOutputs; i++) + { + if (scales[1][i] != scales[0][0]) + return false; + } + return true; + } + + private: + template + void getSliceRecursive(const Mat& inpMat, std::vector& inpIdx, + const std::vector& sliceRanges, + const std::vector& sliceSteps, int dim, int dimsNum, + Mat& outputs, std::vector& outIdx) + { + int begin = sliceRanges[dim].start; + int end = sliceRanges[dim].end; + int step = !sliceSteps.empty() ? sliceSteps[dim] : 1; + + // TODO optimization is required (for 2D tail case at least) + for (int k = begin, j = 0; k < end; k += step, j++) + { + inpIdx[dim] = k; + outIdx[dim] = j; + + if (dim + 1 < dimsNum) + getSliceRecursive(inpMat, inpIdx, sliceRanges, sliceSteps, dim + 1, dimsNum, outputs, outIdx); + else + outputs.at(outIdx.data()) = inpMat.at(inpIdx.data()); + } + } + + void flip(Mat& output) // break if 1d tensor? + { + for (int i = 0; i < neg_step_dims.size(); ++i) + cv::flipND(output, output, neg_step_dims[i]); + } + protected: + // The actual non-negative values determined from @p sliceRanges depends on input size. + std::vector > finalSliceRanges; + std::vector neg_step_dims; + bool hasDynamicShapes; + bool shapesInitialized; + bool hasSteps; + }; + + class CropCenterLayerImpl CV_FINAL : public SliceLayerImpl + { + public: + CropCenterLayerImpl(const LayerParams& params) : SliceLayerImpl(LayerParams()) + { + setParamsFrom(params); + axis = params.get("axis", 2); + const DictValue* paramOffset = params.ptr("offset"); + + if (paramOffset) + { + for (int i = 0; i < paramOffset->size(); i++) + offset.push_back(paramOffset->get(i)); + } + + if (params.has("crop_size")) + { + const auto& paramCropSize = params.get("crop_size"); + const auto& str = paramCropSize.getStringValue(); + + const int s = atoi(str.c_str()); + offset.resize(4); + offset[0] = 0; + offset[1] = 0; + offset[2] = s; + offset[3] = s; + + axis = 0; + } + } + + bool getMemoryShapes(const std::vector& inputs, + const int requiredOutputs, + std::vector& outputs, + std::vector& internals) const CV_OVERRIDE + { + //CV_Assert(inputs.size() == 2); + + //MatShape dstShape = inputs[0]; + //int start = normalize_axis(axis, dstShape); + //for (int i = start; i < dstShape.size(); i++) + //{ + // dstShape[i] = inputs[1][i]; + //} + //outputs.resize(1, dstShape); + + MatShape dstShape = inputs[0]; + int start = normalize_axis(axis, dstShape); + for (int i = start; i < dstShape.size(); i++) + { + dstShape[i] = dstShape[i] - offset[i] * 2; + } + outputs.resize(1, dstShape); + + return false; + } + + void finalize(InputArrayOfArrays inputs_arr, OutputArrayOfArrays) CV_OVERRIDE + { + //std::vector inputs; + //inputs_arr.getMatVector(inputs); + //CV_Assert(2 == inputs.size()); + + //const Mat& inpBlob = inputs[0]; + //const Mat& inpSzBlob = inputs[1]; + + //int dims = inpBlob.dims; + //int start_axis = normalize_axis(axis, dims); + + //std::vector offset_final(dims, 0); + //if (offset.size() == 1) + //{ + // for (int i = start_axis; i < dims; i++) + // offset_final[i] = offset[0]; + //} + //else if (offset.size() > 1) + //{ + // if ((int)offset.size() != dims - start_axis) + // CV_Error(Error::StsBadArg, "number of offset values specified must be " + // "equal to the number of dimensions following axis."); + + // for (int i = start_axis; i < dims; i++) + // offset_final[i] = offset[i - start_axis]; + //} + + //finalSliceRanges.resize(1); + //finalSliceRanges[0].resize(dims); + //for (int i = 0; i < start_axis; i++) + //{ + // finalSliceRanges[0][i] = Range(0, inpBlob.size[i]); + //} + //for (int i = start_axis; i < dims; i++) + //{ + // if (offset_final[i] < 0 || offset_final[i] + inpSzBlob.size[i] > inpBlob.size[i]) + // CV_Error(Error::StsBadArg, "invalid crop parameters or blob sizes"); + + // finalSliceRanges[0][i] = Range(offset_final[i], offset_final[i] + inpSzBlob.size[i]); + //} + + + std::vector inputs; + inputs_arr.getMatVector(inputs); + CV_Assert(1 == inputs.size()); + + const Mat& inpBlob = inputs[0]; + + int dims = inpBlob.dims; + int start_axis = normalize_axis(axis, dims); + + std::vector offset_final(dims, 0); + if (offset.size() == 1) + { + for (int i = start_axis; i < dims; i++) + offset_final[i] = offset[0]; + } + else if (offset.size() > 1) + { + if ((int)offset.size() != dims - start_axis) + CV_Error(Error::StsBadArg, "number of offset values specified must be " + "equal to the number of dimensions following axis."); + + for (int i = start_axis; i < dims; i++) + offset_final[i] = offset[i - start_axis]; + } + + finalSliceRanges.resize(1); + finalSliceRanges[0].resize(dims); + for (int i = 0; i < start_axis; i++) + { + finalSliceRanges[0][i] = Range(0, inpBlob.size[i]); + } + for (int i = start_axis; i < dims; i++) + { + int w = inpBlob.size[i] - offset_final[i] * 2; + if (offset_final[i] < 0 || w < 0) + CV_Error(Error::StsBadArg, "invalid crop parameters or blob sizes"); + + finalSliceRanges[0][i] = Range(offset_final[i], offset_final[i] + w); + } + } + + static cv::Ptr create(cv::dnn::LayerParams& params) + { + return cv::Ptr(new CropCenterLayerImpl(params)); + } + + private: + std::vector offset; + }; + + } +} + +# include + +void reg() +{ + CV_DNN_REGISTER_LAYER_CLASS(CropCenter, cv::dnn::CropCenterLayerImpl); +} \ No newline at end of file diff --git a/waifu2x-caffe/waifu2x-caffe.vcxproj b/waifu2x-caffe/waifu2x-caffe.vcxproj index 3881221..2efe32e 100644 --- a/waifu2x-caffe/waifu2x-caffe.vcxproj +++ b/waifu2x-caffe/waifu2x-caffe.vcxproj @@ -14,19 +14,19 @@ {7C406EE4-2309-4D4C-98BB-CB7BA865FC41} Win32Proj waifu2x-caffe - 8.1 + 10.0 Application true - v140 + v143 Unicode Application false - v140 + v143 true Unicode @@ -42,15 +42,15 @@ true - $(CUDA_PATH_V11_0)\include;$(SolutionDir)lib\include;$(USERPROFILE)\.caffe\dependencies\libraries_v140_x64_py27_1.1.0\libraries\include;$(USERPROFILE)\.caffe\dependencies\libraries_v140_x64_py27_1.1.0\libraries\include\boost-1_61;$(SolutionDir)rapidjson\include;$(SolutionDir)stb;$(SolutionDir)include;$(SolutionDir)msgpack-c\include;$(IncludePath) - $(CUDA_PATH_V11_0)\lib\$(PlatformName);$(SolutionDir)lib\$(PlatformName)\vc15\staticlib;$(SolutionDir)lib\lib;$(USERPROFILE)\.caffe\dependencies\libraries_v140_x64_py27_1.1.0\libraries\lib;$(LibraryPath) + $(CUDA_PATH_V13_0)\include;$(SolutionDir)lib\include;$(SolutionDir)lib\include\opencv2;$(SolutionDir)rapidjson\include;$(SolutionDir)stb;$(SolutionDir)include;$(SolutionDir)msgpack-c\include;$(SolutionDir)opencv\modules\dnn\src;$(IncludePath) + $(CUDA_PATH_V13_0)\lib\$(PlatformName);$(SolutionDir)lib\$(PlatformName)\vc17\staticlib;$(SolutionDir)lib\lib;$(LibraryPath) $(SolutionDir)bin\ $(ProjectName)d false - $(CUDA_PATH_V11_0)\include;$(SolutionDir)lib\include;$(USERPROFILE)\.caffe\dependencies\libraries_v140_x64_py27_1.1.0\libraries\include;$(USERPROFILE)\.caffe\dependencies\libraries_v140_x64_py27_1.1.0\libraries\include\boost-1_61;$(SolutionDir)rapidjson\include;$(SolutionDir)stb;$(SolutionDir)include;$(SolutionDir)msgpack-c\include;$(IncludePath) - $(CUDA_PATH_V11_0)\lib\$(PlatformName);$(SolutionDir)lib\$(PlatformName)\vc15\staticlib;$(SolutionDir)lib\lib;$(USERPROFILE)\.caffe\dependencies\libraries_v140_x64_py27_1.1.0\libraries\lib;$(LibraryPath) + $(CUDA_PATH_V13_0)\include;$(SolutionDir)lib\include;$(SolutionDir)lib\include\opencv2;$(SolutionDir)rapidjson\include;$(SolutionDir)stb;$(SolutionDir)include;$(SolutionDir)msgpack-c\include;$(SolutionDir)opencv\modules\dnn\src;$(IncludePath) + $(CUDA_PATH_V13_0)\lib\$(PlatformName);$(SolutionDir)lib\$(PlatformName)\vc17\staticlib;$(SolutionDir)lib\lib;$(LibraryPath) $(SolutionDir)bin\ @@ -59,8 +59,9 @@ Level3 Disabled - BOOST_ALL_NO_LIB;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) MultiThreadedDebugDLL + stdcpp17 Console @@ -77,8 +78,9 @@ MaxSpeed true true - BOOST_ALL_NO_LIB;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) MultiThreadedDLL + stdcpp17 Console @@ -90,15 +92,31 @@ - - - - + + true + true + + + true + true + + + true + true + + + + + true + true + + + diff --git a/waifu2x-caffe/waifu2x-caffe.vcxproj.filters b/waifu2x-caffe/waifu2x-caffe.vcxproj.filters index eedf660..5407249 100644 --- a/waifu2x-caffe/waifu2x-caffe.vcxproj.filters +++ b/waifu2x-caffe/waifu2x-caffe.vcxproj.filters @@ -30,6 +30,15 @@ common + + 繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ + + + 繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ + + + 繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ + @@ -41,5 +50,8 @@ common + + 繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ + \ No newline at end of file