From c7d020639ea40446fde3418943a5820d7bf9d2f0 Mon Sep 17 00:00:00 2001 From: lltcggie Date: Sat, 29 Apr 2017 12:41:42 +0900 Subject: [PATCH] =?UTF-8?q?=E7=B8=A6=E5=B9=85=E3=81=8B=E6=A8=AA=E5=B9=85?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=81=AE=E6=8B=A1=E5=A4=A7=E3=81=A7=E3=80=81?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E3=82=B5=E3=82=A4=E3=82=BA=E3=81=8B=E3=82=89?= =?UTF-8?q?1px=E3=81=8F=E3=82=89=E3=81=84=E3=81=9A=E3=82=8C=E3=82=8B?= =?UTF-8?q?=E3=81=93=E3=81=A8=E3=81=8C=E3=81=82=E3=82=8B=E3=81=AE=E3=82=92?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=20#84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/stImage.cpp | 21 +++++++++++---------- common/stImage.h | 8 ++++---- common/waifu2x.cpp | 33 +++++++++++++++++---------------- common/waifu2x.h | 35 +++++++++++++++++++++++++++++++++-- 4 files changed, 65 insertions(+), 32 deletions(-) diff --git a/common/stImage.cpp b/common/stImage.cpp index 840e8ac..4b16be7 100644 --- a/common/stImage.cpp +++ b/common/stImage.cpp @@ -404,14 +404,14 @@ Waifu2x::eWaifu2xError stImage::Load(const void* source, const int width, const return Waifu2x::eWaifu2xError_OK; } -double stImage::GetScaleFromWidth(const int width) const +Factor stImage::GetScaleFromWidth(const int width) const { - return (double)width / (double)mOrgSize.width; + return Factor((double)width, (double)mOrgSize.width); } -double stImage::GetScaleFromHeight(const int height) const +Factor stImage::GetScaleFromHeight(const int height) const { - return (double)height / (double)mOrgSize.height; + return Factor((double)height, (double)mOrgSize.height); } bool stImage::RequestDenoise() const @@ -619,7 +619,7 @@ void stImage::SetReconstructedImage(cv::Mat &dst, cv::Mat &src, const cv::Size_< src.release(); } -void stImage::Postprocess(const int input_plane, const double scale, const int depth) +void stImage::Postprocess(const int input_plane, const Factor scale, const int depth) { DeconvertFromNetFormat(input_plane); ShrinkImage(scale); @@ -742,20 +742,21 @@ void stImage::DeconvertFromNetFormat(const int input_plane) } } -void stImage::ShrinkImage(const double scale) +void stImage::ShrinkImage(const Factor scale) { // TODO: scale = 1.0 でも悪影響を及ぼさないか調べる const int scaleBase = 2; // TODO: モデルの拡大率によって可変できるようにする - const int scaleNum = ceil(log(scale) / log(scaleBase)); - const double shrinkRatio = scale >= 1.0 ? scale / std::pow(scaleBase, scaleNum) : scale; + const auto Width = scale.MultiNumerator(mOrgSize.width); + const auto Height = scale.MultiNumerator(mOrgSize.height); - const cv::Size_ ns(mOrgSize.width * scale, mOrgSize.height * scale); + //const cv::Size_ ns(mOrgSize.width * scale, mOrgSize.height * scale); + const cv::Size_ ns((int)Width.toDouble(), (int)Height.toDouble()); if (mEndImage.size().width != ns.width || mEndImage.size().height != ns.height) { int argo = cv::INTER_CUBIC; - if (scale < 0.5) + if (scale.toDouble() < 0.5) argo = cv::INTER_AREA; cv::resize(mEndImage, mEndImage, ns, 0.0, 0.0, argo); diff --git a/common/stImage.h b/common/stImage.h index 49973d0..0272224 100644 --- a/common/stImage.h +++ b/common/stImage.h @@ -68,7 +68,7 @@ private: void SetReconstructedImage(cv::Mat &dst, cv::Mat &src, const cv::Size_ &size, const int inner_scale); void DeconvertFromNetFormat(const int input_plane); - void ShrinkImage(const double scale); + void ShrinkImage(const Factor scale); void ShrinkImage(const int width, const int height); static int DepthBitToCVDepth(const int depth_bit); @@ -94,8 +94,8 @@ public: // sourceはPostprocess()が終わるまで存在している必要がある Waifu2x::eWaifu2xError Load(const void* source, const int width, const int height, const int channel, const int stride); - double GetScaleFromWidth(const int width) const; - double GetScaleFromHeight(const int width) const; + Factor GetScaleFromWidth(const int width) const; + Factor GetScaleFromHeight(const int width) const; bool RequestDenoise() const; @@ -125,7 +125,7 @@ public: // size: GetScalePaddingedImage()で取得したsize void SetReconstructedA(cv::Mat &im, const cv::Size_ &size, const int inner_scale); - void Postprocess(const int input_plane, const double scale, const int depth); + void Postprocess(const int input_plane, const Factor scale, const int depth); void Postprocess(const int input_plane, const int width, const int height, const int depth); cv::Mat GetEndImage() const; diff --git a/common/waifu2x.cpp b/common/waifu2x.cpp index b531726..75955cb 100644 --- a/common/waifu2x.cpp +++ b/common/waifu2x.cpp @@ -769,18 +769,18 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(const boost::filesystem::path &input_fil const bool isReconstructNoise = mMode == eWaifu2xModelTypeNoise || mMode == eWaifu2xModelTypeNoiseScale || (mMode == eWaifu2xModelTypeAutoScale && image.RequestDenoise()); const bool isReconstructScale = mMode == eWaifu2xModelTypeScale || mMode == eWaifu2xModelTypeNoiseScale || mMode == eWaifu2xModelTypeAutoScale; - double Factor = CalcScaleRatio(scale_ratio, scale_width, scale_height, image); + auto factor = CalcScaleRatio(scale_ratio, scale_width, scale_height, image); if (!isReconstructScale) - Factor = 1.0; + factor = Factor(1.0, 1.0); cv::Mat reconstruct_image; - ret = ReconstructImage(Factor, crop_w, crop_h, use_tta, batch_size, isReconstructNoise, isReconstructScale, cancel_func, image); + ret = ReconstructImage(factor, crop_w, crop_h, use_tta, batch_size, isReconstructNoise, isReconstructScale, cancel_func, image); if (ret != Waifu2x::eWaifu2xError_OK) return ret; if(!scale_width || !scale_height) - image.Postprocess(mInputPlane, Factor, output_depth); + image.Postprocess(mInputPlane, factor, output_depth); else image.Postprocess(mInputPlane, *scale_width, *scale_height, output_depth); @@ -822,16 +822,16 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(const double factor, const void* source, const bool isReconstructNoise = mMode == eWaifu2xModelTypeNoise || mMode == eWaifu2xModelTypeNoiseScale; const bool isReconstructScale = mMode == eWaifu2xModelTypeScale || mMode == eWaifu2xModelTypeNoiseScale || mMode == eWaifu2xModelTypeAutoScale; - double Factor = factor; + Factor nowFactor = Factor(factor, 1.0); if (!isReconstructScale) - Factor = 1.0; + nowFactor = Factor(1.0, 1.0); cv::Mat reconstruct_image; - ret = ReconstructImage(Factor, crop_w, crop_h, use_tta, batch_size, isReconstructNoise, isReconstructScale, nullptr, image); + ret = ReconstructImage(nowFactor, crop_w, crop_h, use_tta, batch_size, isReconstructNoise, isReconstructScale, nullptr, image); if (ret != Waifu2x::eWaifu2xError_OK) return ret; - image.Postprocess(mInputPlane, Factor, 8); + image.Postprocess(mInputPlane, nowFactor, 8); cv::Mat out_bgr_image = image.GetEndImage(); image.Clear(); @@ -854,18 +854,18 @@ Waifu2x::eWaifu2xError Waifu2x::waifu2x(const double factor, const void* source, return Waifu2x::eWaifu2xError_OK; } -double Waifu2x::CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, +Factor Waifu2x::CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, const stImage &image) { if (scale_ratio) - return *scale_ratio; + return Factor(*scale_ratio, 1.0); if (scale_width && scale_height) { const auto d1 = image.GetScaleFromWidth(*scale_width); const auto d2 = image.GetScaleFromWidth(*scale_height); - return d1 >= d2 ? d1 : d2; + return d1.toDouble() >= d2.toDouble() ? d1 : d2; } if (scale_width) @@ -874,7 +874,7 @@ double Waifu2x::CalcScaleRatio(const boost::optional scale_ratio, const if(scale_height) return image.GetScaleFromHeight(*scale_height); - return 1.0; + return Factor(1.0, 1.0); } int Waifu2x::GetcuDNNAlgorithm(const char * layer_name, int num_input, int num_output, int batch_size, @@ -899,12 +899,12 @@ void Waifu2x::SetcuDNNAlgorithm(int algo, const char * layer_name, int num_input return g_DeconvCcuDNNAlgorithm.SetAlgorithm(algo, num_input, num_output, batch_size, width, height, kernel_w, kernel_h, pad_w, pad_h, stride_w, stride_h); } -Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(const double factor, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, +Waifu2x::eWaifu2xError Waifu2x::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) { Waifu2x::eWaifu2xError ret; - double Factor = factor; + Factor nowFactor = factor; if (isReconstructNoise) { @@ -926,14 +926,15 @@ Waifu2x::eWaifu2xError Waifu2x::ReconstructImage(const double factor, const int if (ret != Waifu2x::eWaifu2xError_OK) return ret; - Factor /= mNoiseNet->GetInnerScale(); + //nowFactor /= mNoiseNet->GetInnerScale(); + nowFactor = nowFactor.MultiDenominator(mNoiseNet->GetInnerScale()); } } if (cancel_func && cancel_func()) return Waifu2x::eWaifu2xError_Cancel; - const int scaleNum = ceil(log(Factor) / log(ScaleBase)); + const int scaleNum = ceil(log(nowFactor.toDouble()) / log(ScaleBase)); if (isReconstructScale) { diff --git a/common/waifu2x.h b/common/waifu2x.h index f746c7e..babff6e 100644 --- a/common/waifu2x.h +++ b/common/waifu2x.h @@ -25,6 +25,37 @@ class cNet; class stImage; +class Factor +{ +private: + double mNumerator; // 分子 + double mDenominator; // 母数 + +public: + Factor() : mNumerator(1.0), mDenominator(1.0) + { + } + + Factor(const double numerator, const double denominator) : mNumerator(numerator), mDenominator(denominator) + { + } + + Factor MultiNumerator(const double numerator) const + { + return Factor(mNumerator * numerator, mDenominator); + } + + Factor MultiDenominator(const double denominator) const + { + return Factor(mNumerator, mDenominator * denominator); + } + + double toDouble() const + { + return mNumerator / mDenominator; + } +}; + class Waifu2x { public: @@ -97,7 +128,7 @@ private: static boost::filesystem::path GetModeDirPath(const boost::filesystem::path &model_dir); static boost::filesystem::path GetInfoPath(const boost::filesystem::path &model_dir); - static double CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, + static Factor CalcScaleRatio(const boost::optional scale_ratio, const boost::optional scale_width, const boost::optional scale_height, const stImage &image); static int GetcuDNNAlgorithm(const char *layer_name, int num_input, int num_output, int batch_size, @@ -106,7 +137,7 @@ private: 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 double factor, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, + 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); 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);