diff --git a/waifu2x-caffe/Test.cpp b/waifu2x-caffe/Test.cpp new file mode 100644 index 0000000..d88b968 --- /dev/null +++ b/waifu2x-caffe/Test.cpp @@ -0,0 +1,107 @@ +# include +# include +# include +# include +# include + +#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 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_::const_iterator it = topk.begin(); + while (it != topk.end()) { + cout << categoryList[*it] << " : " << probMat.at(*it) * 100 << " %" << endl; + ++it; + } + } + catch (const cv::Exception& e) { + cerr << e.msg << endl; + } + return 0; +} diff --git a/waifu2x-caffe/waifu2x-caffe.vcxproj b/waifu2x-caffe/waifu2x-caffe.vcxproj index 2af00c3..2731830 100644 --- a/waifu2x-caffe/waifu2x-caffe.vcxproj +++ b/waifu2x-caffe/waifu2x-caffe.vcxproj @@ -92,10 +92,23 @@ - - - - + + true + true + + + true + true + + + true + true + + + true + true + + diff --git a/waifu2x-caffe/waifu2x-caffe.vcxproj.filters b/waifu2x-caffe/waifu2x-caffe.vcxproj.filters index eedf660..0e782dc 100644 --- a/waifu2x-caffe/waifu2x-caffe.vcxproj.filters +++ b/waifu2x-caffe/waifu2x-caffe.vcxproj.filters @@ -30,6 +30,9 @@ common + + 繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ +