mirror of
https://github.com/lltcggie/waifu2x-caffe.git
synced 2025-11-24 10:34:54 +00:00
テスト
This commit is contained in:
parent
e6270364ed
commit
65becbc242
107
waifu2x-caffe/Test.cpp
Normal file
107
waifu2x-caffe/Test.cpp
Normal file
@ -0,0 +1,107 @@
|
||||
# include <iostream>
|
||||
# include <fstream>
|
||||
# include <opencv2/dnn.hpp>
|
||||
# include <opencv2/imgproc.hpp>
|
||||
# include <opencv2/imgcodecs.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;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// ImageNet Caffeリファレンスモデル
|
||||
string protoFile = "bvlc_reference_caffenet/deploy.prototxt";
|
||||
string modelFile = "bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel";
|
||||
|
||||
// 画像ファイル
|
||||
string imageFile = (argc > 1) ? argv[1] : "images/cat.jpg";
|
||||
// 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);
|
||||
}
|
||||
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 = 224;
|
||||
cv::resize(img, img, cv::Size(cropSize, cropSize));
|
||||
// Caffeで扱うBlob形式に変換 (実体はcv::Matのラッパークラス)
|
||||
const auto inputBlob = cv::dnn::blobFromImage(img);
|
||||
// 入力層に画像を入力
|
||||
net.setInput(inputBlob, "data");
|
||||
// フォワードパス(順伝播)の計算&出力層(Softmax)の出力を取得, ここに予測結果が格納されている
|
||||
// ImageNet 1000クラス毎の確率(32bits浮動小数点値)が格納された1x1000の行列(ベクトル)
|
||||
const auto probMat = net.forward("prob");
|
||||
// 確率(信頼度)の高い順にソートして、上位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;
|
||||
}
|
||||
@ -92,10 +92,23 @@
|
||||
</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="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" />
|
||||
|
||||
@ -30,6 +30,9 @@
|
||||
<ClCompile Include="..\common\stImage.cpp">
|
||||
<Filter>common</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Test.cpp">
|
||||
<Filter>ソース ファイル</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="..\common\waifu2x.h">
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user