2015-05-29 01:47:26 +09:00
|
|
|
|
#pragma once
|
|
|
|
|
|
2015-06-02 01:04:20 +09:00
|
|
|
|
#include <stdint.h>
|
2015-05-29 01:47:26 +09:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <utility>
|
|
|
|
|
#include <functional>
|
2015-06-03 03:01:56 +09:00
|
|
|
|
#include <boost/shared_ptr.hpp>
|
2015-12-06 18:48:37 +09:00
|
|
|
|
#include <boost/filesystem.hpp>
|
2015-12-27 06:39:13 +09:00
|
|
|
|
#include <boost/optional.hpp>
|
2016-05-11 22:55:16 +09:00
|
|
|
|
#include <opencv2/core.hpp>
|
2015-05-29 01:47:26 +09:00
|
|
|
|
|
2020-09-05 16:25:18 +09:00
|
|
|
|
#define CUDNN_DLL_NAME "cudnn64_8.dll"
|
|
|
|
|
#define CUDNN_REQUIRE_VERION_TEXT "v8.0.3"
|
|
|
|
|
#define CUDNN_REQUIRE_VERION 8003
|
2015-12-04 00:48:55 +09:00
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
|
|
|
|
|
namespace caffe
|
2015-05-29 01:47:26 +09:00
|
|
|
|
{
|
2015-06-03 03:01:56 +09:00
|
|
|
|
template <typename Dtype>
|
|
|
|
|
class Net;
|
2015-06-24 01:07:27 +09:00
|
|
|
|
class NetParameter;
|
2015-05-29 01:47:26 +09:00
|
|
|
|
};
|
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
class cNet;
|
|
|
|
|
class stImage;
|
|
|
|
|
|
|
|
|
|
|
2017-04-29 12:41:42 +09:00
|
|
|
|
class Factor
|
|
|
|
|
{
|
|
|
|
|
private:
|
|
|
|
|
double mNumerator; // <20><><EFBFBD>q
|
|
|
|
|
double mDenominator; // <20>ꐔ
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
class Waifu2x
|
|
|
|
|
{
|
|
|
|
|
public:
|
2018-10-25 04:15:53 +09:00
|
|
|
|
struct stInfo
|
|
|
|
|
{
|
|
|
|
|
struct stParam
|
|
|
|
|
{
|
|
|
|
|
int scale_factor;
|
|
|
|
|
int offset;
|
2018-11-23 22:02:06 +09:00
|
|
|
|
int recommended_crop_size;
|
2018-11-23 22:53:35 +09:00
|
|
|
|
|
|
|
|
|
stParam() : scale_factor(1), offset(0), recommended_crop_size(-1) {}
|
2018-10-25 04:15:53 +09:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
|
std::string arch_name;
|
|
|
|
|
bool has_noise_scale;
|
2018-11-23 23:49:22 +09:00
|
|
|
|
bool has_noise_only;
|
2018-10-25 04:15:53 +09:00
|
|
|
|
int channels;
|
2018-12-01 15:25:41 +09:00
|
|
|
|
int force_divisible_crop_size;
|
2018-10-25 04:15:53 +09:00
|
|
|
|
|
|
|
|
|
stParam noise;
|
|
|
|
|
stParam scale;
|
|
|
|
|
stParam noise_scale;
|
|
|
|
|
};
|
|
|
|
|
|
2016-07-03 17:13:02 +09:00
|
|
|
|
enum eWaifu2xModelType
|
|
|
|
|
{
|
2016-07-03 22:36:06 +09:00
|
|
|
|
eWaifu2xModelTypeNoise = 0,
|
|
|
|
|
eWaifu2xModelTypeScale = 1,
|
|
|
|
|
eWaifu2xModelTypeNoiseScale = 2,
|
|
|
|
|
eWaifu2xModelTypeAutoScale = 3,
|
2016-07-03 17:13:02 +09:00
|
|
|
|
};
|
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
enum eWaifu2xError
|
|
|
|
|
{
|
|
|
|
|
eWaifu2xError_OK = 0,
|
|
|
|
|
eWaifu2xError_Cancel,
|
|
|
|
|
eWaifu2xError_NotInitialized,
|
|
|
|
|
eWaifu2xError_InvalidParameter,
|
|
|
|
|
eWaifu2xError_FailedOpenInputFile,
|
|
|
|
|
eWaifu2xError_FailedOpenOutputFile,
|
|
|
|
|
eWaifu2xError_FailedOpenModelFile,
|
|
|
|
|
eWaifu2xError_FailedParseModelFile,
|
2015-12-06 18:48:37 +09:00
|
|
|
|
eWaifu2xError_FailedWriteModelFile,
|
2015-06-03 03:01:56 +09:00
|
|
|
|
eWaifu2xError_FailedConstructModel,
|
|
|
|
|
eWaifu2xError_FailedProcessCaffe,
|
2015-06-08 03:34:42 +09:00
|
|
|
|
eWaifu2xError_FailedCudaCheck,
|
2016-04-29 15:30:02 +09:00
|
|
|
|
eWaifu2xError_FailedUnknownType,
|
2015-06-08 03:34:42 +09:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum eWaifu2xCudaError
|
|
|
|
|
{
|
|
|
|
|
eWaifu2xCudaError_OK = 0,
|
|
|
|
|
eWaifu2xCudaError_NotFind,
|
|
|
|
|
eWaifu2xCudaError_OldVersion,
|
2016-05-07 17:25:24 +09:00
|
|
|
|
eWaifu2xCudaError_OldDevice,
|
2015-06-03 03:01:56 +09:00
|
|
|
|
};
|
|
|
|
|
|
2015-06-03 13:51:48 +09:00
|
|
|
|
enum eWaifu2xcuDNNError
|
|
|
|
|
{
|
|
|
|
|
eWaifu2xcuDNNError_OK = 0,
|
|
|
|
|
eWaifu2xcuDNNError_NotFind,
|
|
|
|
|
eWaifu2xcuDNNError_OldVersion,
|
|
|
|
|
eWaifu2xcuDNNError_CannotCreate,
|
|
|
|
|
};
|
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
typedef std::function<bool()> waifu2xCancelFunc;
|
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
static std::string ExeDir;
|
2015-12-27 06:39:13 +09:00
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
private:
|
2016-07-03 13:37:26 +09:00
|
|
|
|
bool mIsInited;
|
2015-06-20 03:54:15 +09:00
|
|
|
|
|
2016-07-03 17:13:02 +09:00
|
|
|
|
eWaifu2xModelType mMode;
|
2016-07-03 13:37:26 +09:00
|
|
|
|
int mNoiseLevel;
|
|
|
|
|
std::string mProcess;
|
2016-07-03 22:23:54 +09:00
|
|
|
|
int mGPUNo;
|
2015-06-20 03:54:15 +09:00
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
bool mIsCuda;
|
2015-06-24 01:07:27 +09:00
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
std::shared_ptr<cNet> mNoiseNet;
|
|
|
|
|
std::shared_ptr<cNet> mScaleNet;
|
2015-06-05 01:40:03 +09:00
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
int mInputPlane; // <20>l<EFBFBD>b<EFBFBD>g<EFBFBD>ւ̓<D682><CC93>̓`<60><><EFBFBD><EFBFBD><EFBFBD>l<EFBFBD><6C><EFBFBD><EFBFBD>
|
|
|
|
|
int mMaxNetOffset; // <20>l<EFBFBD>b<EFBFBD>g<EFBFBD>ɓ<EFBFBD><C993>͂<EFBFBD><CD82><EFBFBD><EFBFBD>Ƃǂꂭ<C782>炢<EFBFBD><E782A2><EFBFBD><EFBFBD><EFBFBD>邩
|
2018-11-23 23:49:22 +09:00
|
|
|
|
bool mHasNoiseScaleOnly;
|
2015-05-29 01:47:26 +09:00
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
float *mOutputBlock;
|
|
|
|
|
size_t mOutputBlockSize;
|
2016-06-08 11:11:36 +09:00
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
private:
|
2016-07-03 15:36:32 +09:00
|
|
|
|
static boost::filesystem::path GetModeDirPath(const boost::filesystem::path &model_dir);
|
|
|
|
|
static boost::filesystem::path GetInfoPath(const boost::filesystem::path &model_dir);
|
2016-02-06 00:52:49 +09:00
|
|
|
|
|
2017-04-29 12:41:42 +09:00
|
|
|
|
static Factor CalcScaleRatio(const boost::optional<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
|
2016-07-03 21:55:20 +09:00
|
|
|
|
const stImage &image);
|
|
|
|
|
|
2016-07-06 18:41:13 +09:00
|
|
|
|
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,
|
|
|
|
|
int width, int height, int kernel_w, int kernel_h, int pad_w, int pad_h, int stride_w, int stride_h);
|
|
|
|
|
|
2017-04-29 12:41:42 +09:00
|
|
|
|
Waifu2x::eWaifu2xError ReconstructImage(const Factor factor, const int crop_w, const int crop_h, const bool use_tta, const int batch_size,
|
2016-07-03 13:37:26 +09:00
|
|
|
|
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);
|
2016-07-03 17:13:02 +09:00
|
|
|
|
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);
|
2016-07-03 13:37:26 +09:00
|
|
|
|
Waifu2x::eWaifu2xError ReconstructByNet(std::shared_ptr<cNet> 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<cNet> net, const int crop_w, const int crop_h, const bool use_tta, const int batch_size, cv::Mat &im);
|
2015-12-27 07:20:55 +09:00
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
public:
|
|
|
|
|
Waifu2x();
|
|
|
|
|
~Waifu2x();
|
|
|
|
|
|
2015-06-08 03:34:42 +09:00
|
|
|
|
static eWaifu2xCudaError can_use_CUDA();
|
2016-07-03 13:37:26 +09:00
|
|
|
|
static eWaifu2xcuDNNError can_use_cuDNN();
|
2015-06-03 03:01:56 +09:00
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
static void init_liblary(int argc, char** argv);
|
2015-12-04 00:48:55 +09:00
|
|
|
|
static void quit_liblary();
|
2016-07-10 20:09:46 +09:00
|
|
|
|
static void quit_thread_liblary();
|
2015-12-04 00:48:55 +09:00
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
// mode: noise or scale or noise_scale or auto_scale
|
|
|
|
|
// process: cpu or gpu or cudnn
|
2016-07-03 17:13:02 +09:00
|
|
|
|
eWaifu2xError Init(const eWaifu2xModelType mode, const int noise_level,
|
2016-07-03 22:23:54 +09:00
|
|
|
|
const boost::filesystem::path &model_dir, const std::string &process, const int gpu_no = 0);
|
2015-06-03 03:01:56 +09:00
|
|
|
|
|
2015-12-06 18:48:37 +09:00
|
|
|
|
eWaifu2xError waifu2x(const boost::filesystem::path &input_file, const boost::filesystem::path &output_file,
|
2016-07-03 21:55:20 +09:00
|
|
|
|
const boost::optional<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
|
|
|
|
|
const waifu2xCancelFunc cancel_func = nullptr, const int crop_w = 128, const int crop_h = 128,
|
2016-07-03 13:37:26 +09:00
|
|
|
|
const boost::optional<int> output_quality = boost::optional<int>(), const int output_depth = 8, const bool use_tta = false,
|
|
|
|
|
const int batch_size = 1);
|
2015-06-03 03:01:56 +09:00
|
|
|
|
|
2016-02-06 00:52:49 +09:00
|
|
|
|
// factor: <20>{<7B><>
|
2016-02-06 01:14:20 +09:00
|
|
|
|
// source: (4<>`<60><><EFBFBD><EFBFBD><EFBFBD>l<EFBFBD><6C><EFBFBD>̏ꍇ<CC8F><EA8D87>)RGBA<42>ȉ<EFBFBD><C889>f<EFBFBD>z<EFBFBD><7A>
|
|
|
|
|
// dest: (4<>`<60><><EFBFBD><EFBFBD><EFBFBD>l<EFBFBD><6C><EFBFBD>̏ꍇ<CC8F><EA8D87>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>RGBA<42>ȉ<EFBFBD><C889>f<EFBFBD>z<EFBFBD><7A>
|
2016-02-06 00:52:49 +09:00
|
|
|
|
// in_stride: source<63>̃X<CC83>g<EFBFBD><67><EFBFBD>C<EFBFBD>h(<28>o<EFBFBD>C<EFBFBD>g<EFBFBD>P<EFBFBD><50>)
|
|
|
|
|
// out_stride: dest<73>̃X<CC83>g<EFBFBD><67><EFBFBD>C<EFBFBD>h(<28>o<EFBFBD>C<EFBFBD>g<EFBFBD>P<EFBFBD><50>)
|
2016-07-03 13:37:26 +09:00
|
|
|
|
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);
|
2016-02-06 00:52:49 +09:00
|
|
|
|
|
2016-07-03 13:37:26 +09:00
|
|
|
|
void Destroy();
|
2016-02-06 00:52:49 +09:00
|
|
|
|
|
2015-06-03 03:01:56 +09:00
|
|
|
|
const std::string& used_process() const;
|
2015-07-10 04:09:22 +09:00
|
|
|
|
|
2016-07-03 15:36:32 +09:00
|
|
|
|
static std::string GetModelName(const boost::filesystem::path &model_dir);
|
2018-10-25 04:15:53 +09:00
|
|
|
|
static bool GetInfo(const boost::filesystem::path &model_dir, stInfo &info);
|
2015-06-03 03:01:56 +09:00
|
|
|
|
};
|