OpenCV DNNへの移行テスト中

This commit is contained in:
lltcggie 2026-03-25 22:21:49 +09:00
parent 807cfcb78f
commit 0a2fdd7cfc
26 changed files with 2415 additions and 392 deletions

3
.gitignore vendored
View File

@ -243,5 +243,8 @@ ModelManifest.xml
# FAKE - F# Make
.fake/
*.caffemodel
*.protobin
/lib/
/opencv_contrib-*/

4
.gitmodules vendored
View File

@ -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

View File

@ -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"

1
caffe

@ -1 +0,0 @@
Subproject commit 9d5aa36c65a0082768f10c1d474c56d8a7cc9934

View File

@ -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

View File

@ -29,7 +29,7 @@ static bool readFile(boost::iostreams::stream<boost::iostreams::file_descriptor_
}
template<typename BufType>
static bool readFile(const boost::filesystem::path &path, std::vector<BufType> &buf)
static bool readFile(const std::filesystem::path &path, std::vector<BufType> &buf)
{
boost::iostreams::stream<boost::iostreams::file_descriptor_source> is;
@ -45,7 +45,7 @@ static bool readFile(const boost::filesystem::path &path, std::vector<BufType> &
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<boost::iostreams::file_descriptor_source> 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<boost::iostreams::file_descriptor> 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<boost::iostreams::file_descriptor_source> 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<char> &jsonBuf)
Waifu2x::eWaifu2xError ReadJson(const std::filesystem::path &info_path, rapidjson::Document &d, std::vector<char> &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<char> 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 &param_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 &param_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(), &param_caffemodel))
return Waifu2x::eWaifu2xError_FailedParseModelFile;
mNet = boost::shared_ptr<caffe::Net<float>>(new caffe::Net<float>(param_model));
mNet = std::shared_ptr<caffe::Net<float>>(new caffe::Net<float>(param_model));
mNet->CopyTrainedLayersFrom(param_caffemodel);
}
else
@ -437,8 +437,8 @@ Waifu2x::eWaifu2xError cNet::SetParameter(caffe::NetParameter &param, const std:
return Waifu2x::eWaifu2xError_OK;
}
Waifu2x::eWaifu2xError cNet::LoadParameterFromJson(const boost::filesystem::path &model_path, const boost::filesystem::path &param_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 &param_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<caffe::Net<float>>(new caffe::Net<float>(param));
mNet = std::shared_ptr<caffe::Net<float>>(new caffe::Net<float>(param));
rapidjson::Document d;
std::vector<char> jsonBuf;
@ -509,7 +509,7 @@ Waifu2x::eWaifu2xError cNet::LoadParameterFromJson(const boost::filesystem::path
if (inputPlane != outputPlane)
return Waifu2x::eWaifu2xError_FailedParseModelFile;
std::vector<boost::shared_ptr<caffe::Layer<float>>> list;
std::vector<std::shared_ptr<caffe::Layer<float>>> 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;

View File

@ -9,7 +9,7 @@ class cNet
private:
Waifu2x::eWaifu2xModelType mMode;
boost::shared_ptr<caffe::Net<float>> mNet;
std::shared_ptr<caffe::Net<float>> mNet;
int mModelScale; // モデルが対象とする拡大率
int mInnerScale; // ネット内部で拡大される倍率
@ -19,17 +19,17 @@ private:
private:
void LoadParamFromInfo(const Waifu2x::eWaifu2xModelType mode, const Waifu2x::stInfo& info);
Waifu2x::eWaifu2xError LoadParameterFromJson(const boost::filesystem::path &model_path, const boost::filesystem::path &param_path
, const boost::filesystem::path &modelbin_path, const boost::filesystem::path &caffemodel_path, const std::string &process);
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 &param_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;
@ -41,5 +41,5 @@ public:
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);
};

View File

@ -1,7 +1,7 @@
#include "stImage.h"
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/algorithm/string.hpp>
//#include <boost/iostreams/stream.hpp>
//#include <boost/iostreams/device/file_descriptor.hpp>
//#include <boost/algorithm/string.hpp>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
@ -11,10 +11,10 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
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::stOutputExtentionElement> stImage::OutputExtentionList =
{
{L".png",{8, 16}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".bmp",{8}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".png",{8, 16}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".bmp",{8}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".jpg",{8}, 0, 100, 95, cv::IMWRITE_JPEG_QUALITY},
{L".jp2",{8, 16}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".sr",{8}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".tif",{8, 16, 32}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".hdr",{8, 16, 32}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".exr",{8, 16, 32}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".ppm",{8, 16}, boost::optional<int>(), boost::optional<int>(), boost::optional<int>(), boost::optional<int>()},
{L".jp2",{8, 16}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".sr",{8}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".tif",{8, 16, 32}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".hdr",{8, 16, 32}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".exr",{8, 16, 32}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".ppm",{8, 16}, std::optional<int>(), std::optional<int>(), std::optional<int>(), std::optional<int>()},
{L".webp",{8}, 1, 100, 100, cv::IMWRITE_WEBP_QUALITY},
{L".tga",{8}, 0, 1, 0, 0},
};
template<typename BufType>
static bool readFile(boost::iostreams::stream<boost::iostreams::file_descriptor_source> &is, std::vector<BufType> &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<typename BufType>
static bool readFile(const boost::filesystem::path &path, std::vector<BufType> &buf)
static bool readFile(const std::filesystem::path& path, std::vector<BufType>& buf)
{
boost::iostreams::stream<boost::iostreams::file_descriptor_source> is;
@ -73,21 +56,7 @@ static bool readFile(const boost::filesystem::path &path, std::vector<BufType> &
}
template<typename BufType>
static bool writeFile(boost::iostreams::stream<boost::iostreams::file_descriptor> &os, const std::vector<BufType> &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<typename BufType>
static bool writeFile(const boost::filesystem::path &path, std::vector<BufType> &buf)
static bool writeFile(const std::filesystem::path& path, std::vector<BufType>& buf)
{
boost::iostreams::stream<boost::iostreams::file_descriptor> os;
@ -219,7 +188,7 @@ Waifu2x::eWaifu2xError stImage::AlphaMakeBorder(std::vector<cv::Mat> &planes, co
}
// 画像を読み込んで値を0.0f1.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());
@ -354,7 +323,7 @@ void stImage::Clear()
mEndImage.release();
}
Waifu2x::eWaifu2xError stImage::Load(const boost::filesystem::path &input_file)
Waifu2x::eWaifu2xError stImage::Load(const std::filesystem::path& input_file)
{
Clear();
@ -369,8 +338,8 @@ Waifu2x::eWaifu2xError stImage::Load(const boost::filesystem::path &input_file)
mOrgChannel = im.channels();
mOrgSize = im.size();
const boost::filesystem::path ip(input_file);
const boost::filesystem::path ipext(ip.extension());
const std::filesystem::path ip(input_file);
const std::filesystem::path ipext(ip.extension());
const bool isJpeg = boost::iequals(ipext.string(), ".jpg") || boost::iequals(ipext.string(), ".jpeg");
@ -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();
@ -881,14 +850,14 @@ cv::Mat stImage::GetEndImage() const
return mEndImage;
}
Waifu2x::eWaifu2xError stImage::Save(const boost::filesystem::path &output_file, const boost::optional<int> &output_quality)
Waifu2x::eWaifu2xError stImage::Save(const std::filesystem::path& output_file, const std::optional<int>& 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<int> &output_quality)
Waifu2x::eWaifu2xError stImage::WriteMat(const cv::Mat& im, const std::filesystem::path& output_file, const std::optional<int>& 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"))
@ -970,8 +939,8 @@ 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<int> params;

View File

@ -28,10 +28,10 @@ public:
{
std::wstring ext;
std::vector<int> depthList;
boost::optional<int> imageQualityStart;
boost::optional<int> imageQualityEnd;
boost::optional<int> imageQualityDefault;
boost::optional<int> imageQualitySettingVolume;
std::optional<int> imageQualityStart;
std::optional<int> imageQualityEnd;
std::optional<int> imageQualityDefault;
std::optional<int> imageQualitySettingVolume;
};
const static std::vector<stOutputExtentionElement> 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<int> &output_quality);
static Waifu2x::eWaifu2xError WriteMat(const cv::Mat &im, const std::filesystem::path &output_file, const std::optional<int> &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<int> &output_quality);
Waifu2x::eWaifu2xError Save(const std::filesystem::path &output_file, const std::optional<int> &output_quality);
};

View File

@ -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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
Waifu2x::eWaifu2xError Waifu2x::waifu2x(const std::filesystem::path &input_file, const std::filesystem::path &output_file,
const std::optional<double> scale_ratio, const std::optional<int> scale_width, const std::optional<int> scale_height,
const waifu2xCancelFunc cancel_func, const int crop_w, const int crop_h,
const boost::optional<int> output_quality, const int output_depth, const bool use_tta,
const std::optional<int> 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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
Factor Waifu2x::CalcScaleRatio(const std::optional<double> scale_ratio, const std::optional<int> scale_width, const std::optional<int> 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;
}

View File

@ -5,9 +5,9 @@
#include <vector>
#include <utility>
#include <functional>
#include <boost/shared_ptr.hpp>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <memory>
#include <filesystem>
#include <optional>
#include <opencv2/core.hpp>
#define CUDNN_DLL_NAME "cudnn64_8.dll"
@ -150,10 +150,10 @@ private:
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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
static Factor CalcScaleRatio(const std::optional<double> scale_ratio, const std::optional<int> scale_width, const std::optional<int> scale_height,
const stImage& image);
static int GetcuDNNAlgorithm(const char* layer_name, int num_input, int num_output, int batch_size,
@ -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<double> scale_ratio, const boost::optional<int> scale_width, const boost::optional<int> scale_height,
eWaifu2xError waifu2x(const std::filesystem::path& input_file, const std::filesystem::path& output_file,
const std::optional<double> scale_ratio, const std::optional<int> scale_width, const std::optional<int> scale_height,
const waifu2xCancelFunc cancel_func = nullptr, const int crop_w = 128, const int crop_h = 128,
const boost::optional<int> output_quality = boost::optional<int>(), const int output_depth = 8, const bool use_tta = false,
const std::optional<int> output_quality = std::optional<int>(), const int output_depth = 8, const bool use_tta = false,
const int batch_size = 1);
// factor: ”{—¦
@ -207,6 +207,6 @@ 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);
static std::string GetModelName(const std::filesystem::path& model_dir);
static bool GetInfo(const std::filesystem::path& model_dir, stInfo& info);
};

2
opencv

@ -1 +1 @@
Subproject commit b38c50b3d0c31e82294315ec44b54b7ef559ef12
Subproject commit 6d889ee74c94124f6492eb8f0d50946d9c31d8e9

View File

@ -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

View File

@ -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<int>(), boost::optional<int>(), model_dir, process, boost::optional<int>(), output_depth, use_tta, crop_size, batch_size) != Waifu2x::eWaifu2xError_OK)
// if (obj->Init(1, argv, mode, noise_level, 2.0, std::optional<int>(), std::optional<int>(), model_dir, process, std::optional<int>(), 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<int>(), boost::optional<int>(), model_dir, process, boost::optional<int>(), output_depth, use_tta, crop_size, batch_size) != Waifu2x::eWaifu2xError_OK)
// if (obj->Init(1, argv, mode, noise_level, 2.0, std::optional<int>(), std::optional<int>(), model_dir, process, std::optional<int>(), 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;

View File

@ -14,19 +14,19 @@
<ProjectGuid>{DFF94FEB-78AB-41B1-9B92-4D8B7D799E04}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>waifu2x-caffe-dll</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
@ -61,6 +61,7 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>BOOST_ALL_NO_LIB;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -79,6 +80,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>BOOST_ALL_NO_LIB;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>

View File

@ -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<double> ScaleRatio;
boost::optional<int> ScaleWidth, ScaleHeight;
std::optional<double> ScaleRatio;
std::optional<int> 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;
}

View File

@ -7,8 +7,8 @@
#include <string>
#include <thread>
#include <atomic>
#include <boost/filesystem.hpp>
#include <boost/optional.hpp>
#include <filesystem>
#include <optional>
#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<int> CropSizeList;
tstring input_str;
@ -96,7 +96,7 @@ private:
bool use_tta;
boost::optional<int> output_quality;
std::optional<int> 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();

View File

@ -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();
}

View File

@ -14,19 +14,19 @@
<ProjectGuid>{63FB3EFC-63B0-401C-BB54-F3A984DC233F}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>waifu2xcaffegui</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
@ -61,6 +61,7 @@
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>BOOST_ALL_NO_LIB;WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
@ -82,6 +83,7 @@
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>BOOST_ALL_NO_LIB;WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>

View File

@ -1,11 +1,8 @@
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <filesystem>
#include <functional>
#include <boost/tokenizer.hpp>
#include <boost/tokenizer.hpp>
#include <glog/logging.h>
#include <codecvt>
#include "../common/waifu2x.h"
@ -17,6 +14,7 @@
#include <fcntl.h>
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 <tclap/CmdLine.h>
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"));
@ -222,9 +213,9 @@ int main(int argc, char** argv)
return 1;
}
boost::optional<double> ScaleRatio;
boost::optional<int> ScaleWidth;
boost::optional<int> ScaleHeight;
std::optional<double> ScaleRatio;
std::optional<int> ScaleWidth;
std::optional<int> 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<std::pair<tstring, tstring>> 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)"))
{
@ -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,34 +312,45 @@ int main(int argc, char** argv)
{
// input_extention_listを文字列の配列にする
typedef boost::char_separator<TCHAR> char_separator;
typedef boost::tokenizer<char_separator, tstring::const_iterator, tstring> 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<TCHAR> char_separator;
//typedef boost::tokenizer<char_separator, tstring::const_iterator, tstring> 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)
const auto func = [&extList, &input_path, &output_path, &outputExt, &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()))
for (const auto& i : std::filesystem::recursive_directory_iterator(path))
{
if (boost::filesystem::is_directory(p))
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;
if (!boost::filesystem::exists(out_absolute))
if (!std::filesystem::exists(out_absolute))
{
if (!boost::filesystem::create_directory(out_absolute))
if (!std::filesystem::create_directory(out_absolute))
{
tprintf(TEXT("エラー: 出力フォルダ「%s」の作成に失敗しました\n"), path_to_tstring(out_absolute).c_str());
return false;
@ -364,7 +366,7 @@ int main(int argc, char** argv)
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;
const auto out = path_to_tstring(out_absolute.parent_path() / out_absolute.stem()) + outputExt;
file_paths.emplace_back(path_to_tstring(p), out);
}
@ -465,7 +467,7 @@ int main(int argc, char** argv)
{
const Waifu2x::eWaifu2xError ret = w.waifu2x(p.first, p.second, ScaleRatio, ScaleWidth, ScaleHeight, nullptr,
crop_w, crop_h,
cmdOutputQuality.getValue() == -1 ? boost::optional<int>() : cmdOutputQuality.getValue(), cmdOutputDepth.getValue(), use_tta, cmdBatchSizeFile.getValue());
cmdOutputQuality.getValue() == -1 ? std::optional<int>() : cmdOutputQuality.getValue(), cmdOutputDepth.getValue(), use_tta, cmdBatchSizeFile.getValue());
if (ret != Waifu2x::eWaifu2xError_OK)
{
switch (ret)

471
waifu2x-caffe/Test.cpp Normal file
View File

@ -0,0 +1,471 @@
# include <iostream>
# include <fstream>
# include <iomanip>
# include <opencv2/dnn.hpp>
# include <opencv2/imgproc.hpp>
# include <opencv2/imgcodecs.hpp>
# include <opencv2/dnn/layer.details.hpp>
# include <opencv2/dnn/all_layers.hpp>
#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<int> cropSize;
cv::Ptr<cv::dnn::Layer> 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<cv::dnn::Layer> create(cv::dnn::LayerParams& params)
{
return cv::Ptr<CropCenterLayer>(new CropCenterLayer(params));
}
// Override virtual functions from cv::dnn::Layer and delegate to cropLayer
CV_DEPRECATED_EXTERNAL virtual void finalize(const std::vector<cv::Mat*>& input, std::vector<cv::Mat>& output) override
{
cropLayer->finalize(input, output);
}
virtual void finalize(cv::InputArrayOfArrays inputs_arr, cv::OutputArrayOfArrays outputs_arr) override
{
std::vector<cv::Mat> 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<cv::Mat*>& input, std::vector<cv::Mat>& output, std::vector<cv::Mat>& 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<std::vector<float>>& scales,
const std::vector<std::vector<int>>& zeropoints, cv::dnn::LayerParams& params) override
{
return cropLayer->tryQuantize(scales, zeropoints, params);
}
CV_DEPRECATED_EXTERNAL void finalize(const std::vector<cv::Mat>& inputs, CV_OUT std::vector<cv::Mat>& outputs)
{
cropLayer->finalize(inputs, outputs);
}
CV_DEPRECATED std::vector<cv::Mat> finalize(const std::vector<cv::Mat>& inputs)
{
cropLayer->finalize(inputs);
}
CV_DEPRECATED CV_WRAP void run(const std::vector<cv::Mat>& inputs, CV_OUT std::vector<cv::Mat>& outputs,
CV_IN_OUT std::vector<cv::Mat>& 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<cv::dnn::BackendNode> initHalide(const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& inputs) override
{
// preferableTargetを反映させるタイミングがここしかないっぽいので反映させる
cropLayer->preferableTarget = preferableTarget;
return cropLayer->initHalide(inputs);
}
virtual cv::Ptr<cv::dnn::BackendNode> initNgraph(const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& inputs,
const std::vector<cv::Ptr<cv::dnn::BackendNode>>& nodes) override
{
// preferableTargetを反映させるタイミングがここしかないっぽいので反映させる
cropLayer->preferableTarget = preferableTarget;
return cropLayer->initNgraph(inputs, nodes);
}
virtual cv::Ptr<cv::dnn::BackendNode> initVkCom(const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& inputs,
std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& outputs) override
{
// preferableTargetを反映させるタイミングがここしかないっぽいので反映させる
cropLayer->preferableTarget = preferableTarget;
return cropLayer->initVkCom(inputs, outputs);
}
virtual cv::Ptr<cv::dnn::BackendNode> initWebnn(const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& inputs,
const std::vector<cv::Ptr<cv::dnn::BackendNode>>& nodes) override
{
// preferableTargetを反映させるタイミングがここしかないっぽいので反映させる
cropLayer->preferableTarget = preferableTarget;
return cropLayer->initWebnn(inputs, nodes);
}
virtual cv::Ptr<cv::dnn::BackendNode> initCUDA(void* context,
const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& inputs,
const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& outputs) override
{
// preferableTargetを反映させるタイミングがここしかないっぽいので反映させる
cropLayer->preferableTarget = preferableTarget;
return cropLayer->initCUDA(context, inputs, outputs);
}
virtual cv::Ptr<cv::dnn::BackendNode> initTimVX(void* timVxInfo,
const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& inputsWrapper,
const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& outputsWrapper,
bool isLast) override
{
// preferableTargetを反映させるタイミングがここしかないっぽいので反映させる
cropLayer->preferableTarget = preferableTarget;
return cropLayer->initTimVX(timVxInfo, inputsWrapper, outputsWrapper, isLast);
}
virtual cv::Ptr<cv::dnn::BackendNode> initCann(const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& inputs,
const std::vector<cv::Ptr<cv::dnn::BackendWrapper>>& outputs,
const std::vector<cv::Ptr<cv::dnn::BackendNode>>& nodes) override
{
// preferableTargetを反映させるタイミングがここしかないっぽいので反映させる
cropLayer->preferableTarget = preferableTarget;
return cropLayer->initCann(inputs, outputs, nodes);
}
virtual void applyHalideScheduler(cv::Ptr<cv::dnn::BackendNode>& node,
const std::vector<cv::Mat*>& inputs,
const std::vector<cv::Mat>& outputs,
int targetId) const override
{
cropLayer->applyHalideScheduler(node, inputs, outputs, targetId);
}
virtual cv::Ptr<cv::dnn::BackendNode> tryAttach(const cv::Ptr<cv::dnn::BackendNode>& node) override
{
return cropLayer->tryAttach(node);
}
virtual bool setActivation(const cv::Ptr<cv::dnn::ActivationLayer>& layer) override
{
return cropLayer->setActivation(layer);
}
virtual bool tryFuse(cv::Ptr<cv::dnn::Layer>& 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<cv::dnn::MatShape>& inputs,
const int requiredOutputs,
std::vector<cv::dnn::MatShape>& outputs,
std::vector<cv::dnn::MatShape>& internals) const override
{
CV_Assert(inputs.size() == 1);
const auto& srcShape = inputs[0];
std::vector<int> 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<cv::dnn::MatShape>& 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<typename T>
static void printRec(const cv::Mat& m, std::vector<int>& 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<T>(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<T>(m, idx, d + 1);
if (i + 1 < m.size[d]) std::cout << ",\n";
}
std::cout << "]";
}
}
template<typename T>
static void printMatND_T(const cv::Mat& m) {
// 浮動小数は小数桁を控えめに
if (std::is_floating_point<T>::value) {
std::cout << std::fixed << std::setprecision(6);
}
std::vector<int> idx(m.dims, 0);
printRec<T>(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<uchar>(m); break;
case CV_8S: printMatND_T<schar>(m); break;
case CV_16U: printMatND_T<uint16_t>(m); break;
case CV_16S: printMatND_T<int16_t>(m); break;
case CV_32S: printMatND_T<int32_t>(m); break;
case CV_32F: printMatND_T<float>(m); break;
case CV_64F: printMatND_T<double>(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<int> 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<int> probMatDim(probMat.size.p, probMat.size.p + probMat.size.dims());
auto sss = sumAllElements(probMat);
//printMatND(probMat);
std::vector<cv::Mat> outImgs;
cv::dnn::imagesFromBlob(probMat, outImgs);
//cv::dnn::imagesFromBlob(inputBlob, outImgs);
auto outImg = outImgs[0];
std::vector<int> outdim(outImg.size.p, outImg.size.p + outImg.size.dims());
printMatND(outImg);
//std::cout << cv::format(outImg, cv::Formatter::FMT_DEFAULT) << std::endl;
// 値を01にクリッピング
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<string> 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_<int>::const_iterator it = topk.begin<int>();
//while (it != topk.end<int>()) {
// cout << categoryList[*it] << " : " << probMat.at<float>(*it) * 100 << " %" << endl;
// ++it;
//}
}
catch (const cv::Exception& e) {
cerr << e.msg << endl;
}
return 0;
}

106
waifu2x-caffe/axpy.hpp Normal file
View File

@ -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 <op_cuda.hpp>
#include <opencv2/core.hpp>
#include <cuda4dnn/kernels/scale_shift.hpp>
#include <cstddef>
#include <utility>
namespace cv {
namespace dnn {
namespace cuda4dnn {
template <class T>
class AxpyOp final : public CUDABackendNode {
public:
using wrapper_type = GetCUDABackendWrapperType<T>;
AxpyOp(csl::Stream stream_)
: stream(std::move(stream_)), axis(0)
{
}
void forward(
const std::vector<cv::Ptr<BackendWrapper>>& inputs,
const std::vector<cv::Ptr<BackendWrapper>>& outputs,
csl::Workspace& workspace) override
{
CV_Assert(inputs.size() == 3);
CV_Assert(outputs.size() == 1);
auto input_wrapper = inputs[1].dynamicCast<wrapper_type>();
auto input = input_wrapper->getView();
auto output_wrapper = outputs[0].dynamicCast<wrapper_type>();
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<T> weights = inputs[0].dynamicCast<wrapper_type>()->getView();
parameter_batch_size = weights.get_axis_size(0);
CV_Assert(parameter_batch_size == input.get_axis_size(0));
csl::TensorView<T> bias = inputs[2].dynamicCast<wrapper_type>()->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<int>(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<T>(stream, output, input, inner_size, weights, bias);
else if (!weights.empty())
kernels::scaleN<T>(stream, output, input, inner_size, weights);
else
kernels::biasN<T>(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 */

View File

@ -0,0 +1,371 @@
#include <opencv2/dnn/all_layers.hpp>
//#include <opencv2/core/opencl/ocl_defs.hpp>
#include <cvconfig.h>
//#include <layers/layers_common.hpp>
#include <op_cuda.hpp>
#include <op_halide.hpp>
#include <op_inf_engine.hpp>
#include <ie_ngraph.hpp>
#include <op_webnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/core/utils/trace.hpp>
#ifdef HAVE_CUDA
//#include <cuda4dnn/primitives/scale_shift.hpp>
#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<MatShape>& inputs,
const int requiredOutputs,
std::vector<MatShape>& outputs,
std::vector<MatShape>& 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<Mat> 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<Mat> 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<float>(0), bias.at<float>(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<BackendNode> initCUDA(
void* context_,
const std::vector<Ptr<BackendWrapper>>& inputs,
const std::vector<Ptr<BackendWrapper>>& outputs
) override
{
auto context = reinterpret_cast<csl::CSLContext*>(context_);
CV_Assert(inputs.size() == 3);
return make_cuda_node<cuda4dnn::AxpyOp>(preferableTarget, std::move(context->stream));
}
#endif
virtual Ptr<BackendNode> tryAttach(const Ptr<BackendNode>& node) CV_OVERRIDE
{
switch (node->backendId)
{
case DNN_BACKEND_HALIDE:
{
#ifdef HAVE_HALIDE
auto base = node.dynamicCast<HalideBackendNode>();
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<BackendNode>(new HalideBackendNode(base, top));
#endif // HAVE_HALIDE
break;
}
}
return Ptr<BackendNode>();
}
virtual Ptr<BackendNode> initHalide(const std::vector<Ptr<BackendWrapper> >& inputs) CV_OVERRIDE
{
#ifdef HAVE_HALIDE
Halide::Buffer<float> 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<BackendNode>(new HalideBackendNode(top));
#endif // HAVE_HALIDE
return Ptr<BackendNode>();
}
#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<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inputs, const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE
{
auto ieInpNode0 = nodes[0].dynamicCast<InfEngineNgraphNode>()->node;
ov::Output<ov::Node> ieInpNode1;
if (nodes.size() > 1)
ieInpNode1 = nodes[1].dynamicCast<InfEngineNgraphNode>()->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<size_t> shape(ieInpNode0.get_shape().size(), 1);
int cAxis = normalize_axis(axis, shape.size());
shape[cAxis] = numChannels;
std::shared_ptr<ov::Node> node;
if (hasWeights)
{
ov::Output<ov::Node> weight = blobs.empty() ? ieInpNode1 :
std::make_shared<ov::op::v0::Constant>(ov::element::f32, ov::Shape(shape), blobs[0].data);
node = std::make_shared<ov::op::v1::Multiply>(ieInpNode0, weight, ov::op::AutoBroadcastType::NUMPY);
}
if (hasBias || !hasWeights)
{
ov::Output<ov::Node> bias;
if (hasBias)
{
bias = blobs.empty() ? ieInpNode1 :
std::make_shared<ov::op::v0::Constant>(ov::element::f32,
ov::Shape(shape), blobs.back().data);
}
else
bias = std::make_shared<ov::op::v0::Constant>(ov::element::f32,
ov::Shape(shape), std::vector<float>(numChannels, 0).data());
node = std::make_shared<ov::op::v1::Add>(node, bias, ov::op::AutoBroadcastType::NUMPY);
}
return Ptr<BackendNode>(new InfEngineNgraphNode(node));
}
#endif // HAVE_DNN_NGRAPH
#ifdef HAVE_WEBNN
virtual Ptr<BackendNode> initWebnn(const std::vector<Ptr<BackendWrapper> >& inputs, const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE
{
Ptr<WebnnBackendNode> node = nodes[0].dynamicCast<WebnnBackendNode>();
auto& webnnInpOperand0 = node->operand;
auto& webnnGraphBuilder = node->net->builder;
auto webnnInpOperand1 = nodes.size() > 1 ? nodes[1].dynamicCast<WebnnBackendNode>()->operand : nullptr;
auto webnnInpOperand2 = nodes.size() > 2 ? nodes[1].dynamicCast<WebnnBackendNode>()->operand : nullptr;
std::vector<int32_t> 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<BackendNode>(new WebnnBackendNode(operand));
}
#endif
void getScaleShift(Mat& scale, Mat& shift) const CV_OVERRIDE
{
scale = Mat();
shift = Mat();
}
//bool tryQuantize(const std::vector<std::vector<float> >& scales,
// const std::vector<std::vector<int> >& 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<MatShape>& inputs,
const std::vector<MatShape>& 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<AxpyFastLayerImpl> create(const LayerParams& params)
{
return Ptr<AxpyFastLayerImpl>(new AxpyFastLayerImpl(params));
}
};
} // namespace dnn
} // namespace cv
# include <opencv2/dnn/layer.details.hpp>
void reg2()
{
CV_DNN_REGISTER_LAYER_CLASS(AxpyFast, cv::dnn::AxpyFastLayerImpl);
}

View File

@ -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 <opencv2/dnn/all_layers.hpp>
#include <opencv2/dnn/shape_utils.hpp>
#include <opencv2/core/opencl/ocl_defs.hpp>
#include <opencv2/core/utils/logger.hpp>
#include <opencv2/core/utils/trace.hpp>
#include <op_cuda.hpp>
#ifdef HAVE_OPENCL
#include "opencl_kernels_dnn.hpp"
#endif
#ifdef HAVE_CUDA
#include <cuda4dnn/primitives/slice.hpp>
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<std::vector<cv::Range> >& sliceRanges, std::vector<std::vector<int> >& 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<std::vector<cv::Range> > finalizeSliceRange(const cv::dnn::MatShape& inpShape, int& axis,
const std::vector<std::vector<cv::Range> >& inputSliceRanges)
{
std::vector<std::vector<cv::Range> > sliceRanges = inputSliceRanges;
CV_Assert(inpShape.size() > 0);
bool axisNeg = (axis < 0);
axis = (axis + static_cast<int>(inpShape.size())) % inpShape.size();
for (size_t i = 0; i < sliceRanges.size(); ++i) {
std::vector<cv::Range>& 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<int>("axis", 1);
num_split = params.get<int>("num_split", 0);
hasDynamicShapes = params.get<bool>("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<Range>(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<int>(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<int>(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<int>(i);
int sizeOrEnd = sizesOrEnds.get<int>(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<MatShape>& inputs,
const int requiredOutputs,
std::vector<MatShape>& outputs,
std::vector<MatShape>& internals) const CV_OVERRIDE
{
CV_Assert(inputs.size() == 1);
MatShape inpShape = inputs[0];
std::vector<std::vector<int> > sliceSteps_ = sliceSteps;
std::vector<std::vector<cv::Range> > sliceRanges_ = sliceRanges;
if (hasSteps && !neg_step_dims.empty())
tranformForNegSteps(inpShape, sliceRanges_, sliceSteps_);
int axis_rw = axis;
std::vector<std::vector<cv::Range> > 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<MatShape>& 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<Mat> 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<Range>(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<OpenCLExecInfo> ocl_exec_cache;
void ocl_prepare(const std::vector<UMat>& inputs, const std::vector<UMat>& 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>& 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<UMat> inputs;
std::vector<UMat> 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<Mat> 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<int> inpIdx(dimsNum, 0);
std::vector<int> outIdx(dimsNum, 0);
if (inpMat.type() == CV_16F)
getSliceRecursive<int16_t>(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx);
else if (inpMat.type() == CV_8S)
getSliceRecursive<int8_t>(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx);
else
getSliceRecursive<float>(inpMat, inpIdx, finalSliceRanges[i], sliceSteps[i], 0, dimsNum, outputs[i], outIdx);
// flip for negative steps
flip(outputs[i]);
}
}
}
#ifdef HAVE_CANN
virtual Ptr<BackendNode> initCann(const std::vector<Ptr<BackendWrapper> >& inputs,
const std::vector<Ptr<BackendWrapper> >& outputs,
const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE
{
bool isSplit = sliceRanges.size() > 1;
auto x = inputs[0].dynamicCast<CannBackendWrapper>();
if (isSplit)
{
// create operator
auto op = std::make_shared<ge::op::SplitV>(name);
// set attr
int n_split = static_cast<int>(outputs.size());
op->set_attr_num_split(n_split);
// set inputs
// set inputs : x
auto op_x = nodes[0].dynamicCast<CannBackendNode>()->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<int> 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<int> 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<CannConstOp>(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<int> split_dim_shape{ 1 };
auto op_const_split_dim = std::make_shared<CannConstOp>(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::TensorDesc>(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT);
op->update_dynamic_output_desc_y(i, *desc_output_y_i);
}
return Ptr<BackendNode>(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<ge::op::StridedSliceV2>(name);
// retrieve begins, ends, axes and steps
std::vector<int> 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<int> shape_{ dims };
// set inputs
// set inputs : x
auto op_x = nodes[0].dynamicCast<CannBackendNode>()->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<CannConstOp>(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<CannConstOp>(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<CannConstOp>(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<CannConstOp>(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::TensorDesc>(ge::Shape(), ge::FORMAT_NCHW, ge::DT_FLOAT);
op->update_output_desc_y(*output_desc);
return Ptr<BackendNode>(new CannBackendNode(op));
}
#endif
#ifdef HAVE_DNN_NGRAPH
virtual Ptr<BackendNode> initNgraph(const std::vector<Ptr<BackendWrapper> >& inputs,
const std::vector<Ptr<BackendNode> >& nodes) CV_OVERRIDE
{
CV_Assert_N(nodes.size() <= 2);
auto& ieInpNode = nodes[0].dynamicCast<InfEngineNgraphNode>()->node;
CV_Assert(finalSliceRanges[0].size() == ieInpNode.get_shape().size());
std::vector<int64_t> 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<int64_t>(sliceSteps[0].begin(), sliceSteps[0].end());
else
steps = std::vector<int64_t>((int64_t)dims.size(), 1);
auto lower_bounds = std::make_shared<ov::op::v0::Constant>(ov::element::i64,
ov::Shape{ offsets.size() }, offsets.data());
auto upper_bounds = std::make_shared<ov::op::v0::Constant>(ov::element::i64,
ov::Shape{ dims.size() }, dims.data());
auto strides = std::make_shared<ov::op::v0::Constant>(ov::element::i64,
ov::Shape{ dims.size() }, steps);
auto slice = std::make_shared<ov::op::v1::StridedSlice>(ieInpNode,
lower_bounds, upper_bounds, strides, std::vector<int64_t>{}, std::vector<int64_t>{});
return Ptr<BackendNode>(new InfEngineNgraphNode(slice));
}
#endif // HAVE_DNN_NGRAPH
#ifdef HAVE_CUDA
Ptr<BackendNode> initCUDA(
void* context_,
const std::vector<Ptr<BackendWrapper>>& inputs,
const std::vector<Ptr<BackendWrapper>>& outputs
) override
{
auto context = reinterpret_cast<csl::CSLContext*>(context_);
std::vector<std::vector<std::size_t>> offsets;
for (const auto& ranges : finalSliceRanges)
{
std::vector<std::size_t> offsets_i;
for (const auto& range : ranges)
offsets_i.push_back(range.start);
offsets.push_back(std::move(offsets_i));
}
return make_cuda_node<cuda4dnn::SliceOp>(preferableTarget, std::move(context->stream), std::move(offsets));
}
#endif
bool tryQuantize(const std::vector<std::vector<float> >& scales,
const std::vector<std::vector<int> >& 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 <typename T>
void getSliceRecursive(const Mat& inpMat, std::vector<int>& inpIdx,
const std::vector<Range>& sliceRanges,
const std::vector<int>& sliceSteps, int dim, int dimsNum,
Mat& outputs, std::vector<int>& 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<T>(inpMat, inpIdx, sliceRanges, sliceSteps, dim + 1, dimsNum, outputs, outIdx);
else
outputs.at<T>(outIdx.data()) = inpMat.at<T>(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<std::vector<Range> > finalSliceRanges;
std::vector<int> 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<int>("axis", 2);
const DictValue* paramOffset = params.ptr("offset");
if (paramOffset)
{
for (int i = 0; i < paramOffset->size(); i++)
offset.push_back(paramOffset->get<int>(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<MatShape>& inputs,
const int requiredOutputs,
std::vector<MatShape>& outputs,
std::vector<MatShape>& 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<Mat> 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<int> 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<Mat> 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<int> 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<cv::dnn::Layer> create(cv::dnn::LayerParams& params)
{
return cv::Ptr<CropCenterLayerImpl>(new CropCenterLayerImpl(params));
}
private:
std::vector<int> offset;
};
}
}
# include <opencv2/dnn/layer.details.hpp>
void reg()
{
CV_DNN_REGISTER_LAYER_CLASS(CropCenter, cv::dnn::CropCenterLayerImpl);
}

View File

@ -14,19 +14,19 @@
<ProjectGuid>{7C406EE4-2309-4D4C-98BB-CB7BA865FC41}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>waifu2x-caffe</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
@ -42,15 +42,15 @@
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
<IncludePath>$(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)</IncludePath>
<LibraryPath>$(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)</LibraryPath>
<IncludePath>$(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)</IncludePath>
<LibraryPath>$(CUDA_PATH_V13_0)\lib\$(PlatformName);$(SolutionDir)lib\$(PlatformName)\vc17\staticlib;$(SolutionDir)lib\lib;$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)bin\</OutDir>
<TargetName>$(ProjectName)d</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(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)</IncludePath>
<LibraryPath>$(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)</LibraryPath>
<IncludePath>$(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)</IncludePath>
<LibraryPath>$(CUDA_PATH_V13_0)\lib\$(PlatformName);$(SolutionDir)lib\$(PlatformName)\vc17\staticlib;$(SolutionDir)lib\lib;$(LibraryPath)</LibraryPath>
<OutDir>$(SolutionDir)bin\</OutDir>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -59,8 +59,9 @@
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>BOOST_ALL_NO_LIB;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -77,8 +78,9 @@
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>BOOST_ALL_NO_LIB;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>_CRT_SECURE_NO_WARNINGS;_SILENCE_CXX17_CODECVT_HEADER_DEPRECATION_WARNING;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
@ -90,15 +92,31 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\common\cNet.cpp" />
<ClCompile Include="..\common\stImage.cpp" />
<ClCompile Include="..\common\waifu2x.cpp" />
<ClCompile Include="Source.cpp" />
<ClCompile Include="..\common\cNet.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\common\stImage.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="..\common\waifu2x.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="axpy_fast_layer.cpp" />
<ClCompile Include="slice_layer.cpp" />
<ClCompile Include="Source.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|x64'">true</ExcludedFromBuild>
</ClCompile>
<ClCompile Include="Test.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\common\cNet.h" />
<ClInclude Include="..\common\stImage.h" />
<ClInclude Include="..\common\waifu2x.h" />
<ClInclude Include="axpy.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">

View File

@ -30,6 +30,15 @@
<ClCompile Include="..\common\stImage.cpp">
<Filter>common</Filter>
</ClCompile>
<ClCompile Include="Test.cpp">
<Filter>ソース ファイル</Filter>
</ClCompile>
<ClCompile Include="slice_layer.cpp">
<Filter>ソース ファイル</Filter>
</ClCompile>
<ClCompile Include="axpy_fast_layer.cpp">
<Filter>ソース ファイル</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\common\waifu2x.h">
@ -41,5 +50,8 @@
<ClInclude Include="..\common\stImage.h">
<Filter>common</Filter>
</ClInclude>
<ClInclude Include="axpy.hpp">
<Filter>ソース ファイル</Filter>
</ClInclude>
</ItemGroup>
</Project>