From 6effe0dfaa28ebeffa26a1b607812019a6ec8afe Mon Sep 17 00:00:00 2001 From: lltcggie Date: Thu, 25 Oct 2018 04:15:53 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=A2=E3=83=87=E3=83=AB=E3=81=AE=E3=81=8A?= =?UTF-8?q?=E3=81=99=E3=81=99=E3=82=81=E3=81=AECropSize=E3=82=92=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=E3=81=99=E3=82=8B=E6=A9=9F=E8=83=BD=E8=BF=BD=E5=8A=A0?= =?UTF-8?q?=E3=80=81UpResNet10=E3=83=A2=E3=83=87=E3=83=AB=E3=81=AB?= =?UTF-8?q?=E3=81=8A=E3=81=99=E3=81=99=E3=82=81=E3=81=AECropSize=E8=A8=AD?= =?UTF-8?q?=E5=AE=9A=20CropSize=E3=81=AB=E3=82=88=E3=81=A3=E3=81=A6?= =?UTF-8?q?=E5=87=BA=E5=8A=9B=E3=81=8C=E5=A4=89=E3=82=8F=E3=82=8B=E3=83=A2?= =?UTF-8?q?=E3=83=87=E3=83=AB=E7=94=A8=E3=81=AE=E8=A8=AD=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/models/upresnet10/info.json | 2 +- common/cNet.cpp | 12 +++++++----- common/cNet.h | 24 +++--------------------- common/waifu2x.cpp | 11 +++++++++++ common/waifu2x.h | 20 ++++++++++++++++++++ waifu2x-caffe-gui/MainDialog.cpp | 19 +++++++++++++++++-- 6 files changed, 59 insertions(+), 29 deletions(-) diff --git a/bin/models/upresnet10/info.json b/bin/models/upresnet10/info.json index 024fb70..a434a50 100644 --- a/bin/models/upresnet10/info.json +++ b/bin/models/upresnet10/info.json @@ -1,3 +1,3 @@ {"name":"UpResNet10","arch_name":"upresnet10","has_noise_scale":true,"channels":3, -"scale_factor":2,"offset":26 +"scale_factor":2,"offset":26,"recommended_crop_size":38 } \ No newline at end of file diff --git a/common/cNet.cpp b/common/cNet.cpp index dd46ad8..0e84f3c 100644 --- a/common/cNet.cpp +++ b/common/cNet.cpp @@ -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, stInfo &info) +Waifu2x::eWaifu2xError cNet::GetInfo(const boost::filesystem::path & info_path, Waifu2x::stInfo &info) { rapidjson::Document d; std::vector jsonBuf; @@ -191,11 +191,13 @@ Waifu2x::eWaifu2xError cNet::GetInfo(const boost::filesystem::path & info_path, const auto arch_name = d["arch_name"].GetString(); const bool has_noise_scale = d.HasMember("has_noise_scale") && d["has_noise_scale"].GetBool() ? true : false; const int channels = d["channels"].GetInt(); + const int recommended_crop_size = d.HasMember("recommended_crop_size") ? d["recommended_crop_size"].GetInt() : -1; info.name = name; info.arch_name = arch_name; info.has_noise_scale = has_noise_scale; info.channels = channels; + info.recommended_crop_size = recommended_crop_size; if (d.HasMember("offset")) { @@ -261,7 +263,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 stInfo &info, const std::string &process) +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 ret; @@ -321,11 +323,11 @@ Waifu2x::eWaifu2xError cNet::ConstractNet(const Waifu2x::eWaifu2xModelType mode, return Waifu2x::eWaifu2xError_OK; } -void cNet::LoadParamFromInfo(const Waifu2x::eWaifu2xModelType mode, const stInfo &info) +void cNet::LoadParamFromInfo(const Waifu2x::eWaifu2xModelType mode, const Waifu2x::stInfo &info) { mModelScale = 2; // TODO: 動的に設定するようにする - stInfo::stParam param; + Waifu2x::stInfo::stParam param; switch (mode) { @@ -824,7 +826,7 @@ std::string cNet::GetModelName(const boost::filesystem::path &info_path) { Waifu2x::eWaifu2xError ret; - stInfo info; + Waifu2x::stInfo info; ret = GetInfo(info_path, info); if (ret != Waifu2x::eWaifu2xError_OK) return std::string(); diff --git a/common/cNet.h b/common/cNet.h index cbd8ff1..fe7cf2e 100644 --- a/common/cNet.h +++ b/common/cNet.h @@ -4,24 +4,6 @@ #include "waifu2x.h" -struct stInfo -{ - struct stParam - { - int scale_factor; - int offset; - }; - - std::string name; - std::string arch_name; - bool has_noise_scale; - int channels; - - stParam noise; - stParam scale; - stParam noise_scale; -}; - class cNet { private: @@ -36,7 +18,7 @@ private: bool mHasNoiseScaleModel; private: - void LoadParamFromInfo(const Waifu2x::eWaifu2xModelType mode, const stInfo &info); + 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; @@ -45,9 +27,9 @@ public: cNet(); ~cNet(); - static Waifu2x::eWaifu2xError GetInfo(const boost::filesystem::path &info_path, stInfo &info); + static Waifu2x::eWaifu2xError GetInfo(const boost::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 stInfo &info, const std::string &process); + 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); int GetInputPlane() const; int GetInnerScale() const; diff --git a/common/waifu2x.cpp b/common/waifu2x.cpp index 093ce5c..d485cb7 100644 --- a/common/waifu2x.cpp +++ b/common/waifu2x.cpp @@ -1149,3 +1149,14 @@ std::string Waifu2x::GetModelName(const boost::filesystem::path & model_dir) return cNet::GetModelName(info_path); } + +bool Waifu2x::GetInfo(const boost::filesystem::path &model_dir, stInfo &info) +{ + const boost::filesystem::path mode_dir_path(GetModeDirPath(model_dir)); + if (!boost::filesystem::exists(mode_dir_path)) + return false; + + const boost::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 dd246b8..fa23ad9 100644 --- a/common/waifu2x.h +++ b/common/waifu2x.h @@ -59,6 +59,25 @@ public: class Waifu2x { public: + struct stInfo + { + struct stParam + { + int scale_factor; + int offset; + }; + + std::string name; + std::string arch_name; + bool has_noise_scale; + int channels; + int recommended_crop_size; + + stParam noise; + stParam scale; + stParam noise_scale; + }; + enum eWaifu2xModelType { eWaifu2xModelTypeNoise = 0, @@ -183,4 +202,5 @@ public: 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); }; diff --git a/waifu2x-caffe-gui/MainDialog.cpp b/waifu2x-caffe-gui/MainDialog.cpp index 054e535..60205a5 100644 --- a/waifu2x-caffe-gui/MainDialog.cpp +++ b/waifu2x-caffe-gui/MainDialog.cpp @@ -527,6 +527,18 @@ void DialogEvent::SetCropSizeList(const boost::filesystem::path & input_path) } ), list.end()); + bool isRecommendedCropSize = false; + Waifu2x::stInfo info; + if (Waifu2x::GetInfo(model_dir, info) && info.recommended_crop_size > 0) + { + tstring str(to_tstring(info.recommended_crop_size)); + SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str()); + isRecommendedCropSize = true; + } + + if (list.size() > 0) + SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)TEXT("-----------------------")); + int mindiff = INT_MAX; int defaultIndex = -1; for (int i = 0; i < list.size(); i++) @@ -534,13 +546,13 @@ void DialogEvent::SetCropSizeList(const boost::filesystem::path & input_path) const int n = list[i]; tstring str(to_tstring(n)); - SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str()); + const int index = SendMessage(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str()); const int diff = abs(DefaultCommonDivisor - n); if (DefaultCommonDivisorRange.first <= n && n <= DefaultCommonDivisorRange.second && diff < mindiff) { mindiff = diff; - defaultIndex = i; + defaultIndex = index; } } @@ -565,6 +577,9 @@ void DialogEvent::SetCropSizeList(const boost::filesystem::path & input_path) if (defaultIndex == -1) defaultIndex = defaultListIndex; + if(isRecommendedCropSize) + defaultIndex = 0; + if (GetWindowTextLength(hcrop) == 0) SendMessage(hcrop, CB_SETCURSEL, defaultIndex, 0); }