- 我是用的是opencv4.1.1,加载的模型是bvlc_reference_caffenet.caffemodel,本来想找单独的猫狗分类模型的,结果没有找到,就用这个代替了,deploy.prototxt用的是caffe-windows\models\bvlc_alexnet\deploy.prototxt。
- 分别把这两个文件放到测试程序的model文件夹下,bvlc_reference_caffenet.caffemodel重命名成model.caffemodel。并找了一张猫的图片放到image文件夹下。
- bvlc_reference_caffenet.caffemodel模型大小232M.分类可以得到正确的结果,使用SqueezeNet_v1.1也可以得到正确分类结果,模型只有4.72M。SqueezeNet_v1.1模型下载链接:https://github.com/forresti/SqueezeNet
//#include "pch.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <opencv2/opencv.hpp>
#include <opencv2/dnn.hpp> //dnn模块
#include <time.h>
using namespace std;
using namespace cv;
using namespace ::dnn; //调用DNN命名空间
clock_t start, finish;
String model_file = "model/model.caffemodel"; //模型结构文件
String model_text = "model/deploy.prototxt"; //模型数据
//图像深度学习检测
double detect_NN(Mat detectImg, Net net)
{
if (net.empty())
{
cout << "no model!" << endl;
return -1;
}
//initialize images(输入图像初始化)
Mat src = detectImg.clone();
if (src.empty())
{
return -1;
}
//图像识别转换
//第一个参数输入图像,第二个参数图像放缩大小,第三个参数输入图像尺寸,第四个参数模型训练图像三个通道RGB的均值(均值文件)
start = clock();
Mat inputBlob;
//resize(src, src, Size(227, 227));
// 参数分别为输入图像,归一化参数,模型大小,BGR均值
inputBlob = blobFromImage(src, 1.0, Size(227, 227), Scalar(92.71, 106.44, 118.11));
Mat prob; //输出结果
//循环
for (int i = 0; i < 1; i++)
{
net.setInput(inputBlob, "data");
prob = net.forward("prob"); //输出层2
}
Mat probMat = prob.reshape(1, 1); //转化为1行2列
Point classNumber; //最大值的位置
double classProb;
//最大值多少
//最大最小值查找,忽略最小值
minMaxLoc(probMat, NULL, &classProb, NULL, &classNumber);
int classidx = classNumber.x;
printf("classidx is:%d\n", classidx);
printf("prob is %f\n", classProb);
finish = clock();
double duration = (double)(finish - start);
printf("run time is %f ms\n", duration);
return duration;
}
int main()
{
Net net = readNetFromCaffe(model_text, model_file);
Mat detectImg = imread("image/cat.jpg");
double runTime = detect_NN(detectImg, net);
return 0;
}
分类结果:
Attempting to upgrade input file specified using deprecated transformation parameters: model/model.caffemodel
Successfully upgraded file specified using deprecated data transformation parameters.
Note that future Caffe releases will only support transform_param messages for transformation fields.
Attempting to upgrade input file specified using deprecated V1LayerParameter: model/model.caffemodel
Successfully upgraded file specified using deprecated V1LayerParameter
classidx is:287
prob is 0.939753
run time is 2177.000000 ms
可见分类结果为 287,查找imagenet1000里面对应的是 'lynx, catamount',即 山猫,野猫
我的输入图片:
参考:[深度学习] caffe分类模型训练、结果可视化、部署及量化笔记
参考:https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a#file-imagenet1000_clsid_to_human-txt