opencv3.3调用 caffe mnist 模型进行手写数字的分类(该博主的博客有很多关于OpenCV DNN模块的文章)

转载链接:https://blog.csdn.net/hust_bochu_xuchao/article/details/78931972

上一篇博文,正确跑通了mnist数据集,得到了lenet_iter_5000.caffemodel  lenet_iter_5000.solverstate  lenet_iter_10000.caffemodel  lenet_iter_10000.solverstate 四个文件


一、按照下面步骤修改caffe-master\examples\mnist文件夹中的  lenet_train_test.prototxt 文件

1、去掉数据输入层,即将top 为 “data” 的layers 去掉。 即将下面的内容删掉   

  1. layer {
  2. name: "mnist"
  3. type: "Data"
  4. top: "data"
  5. top: "label"
  6. include {
  7. phase: TRAIN
  8. }
  9. transform_param {
  10. scale: 0.00390625
  11. }
  12. data_param {
  13. source: "examples/mnist/mnist_train_lmdb"
  14. batch_size: 64
  15. backend: LMDB
  16. }
  17. }
  18. layer {
  19. name: "mnist"
  20. type: "Data"
  21. top: "data"
  22. top: "label"
  23. include {
  24. phase: TEST
  25. }
  26. transform_param {
  27. scale: 0.00390625
  28. }
  29. data_param {
  30. source: "examples/mnist/mnist_test_lmdb"
  31. batch_size: 100
  32. backend: LMDB
  33. }
  34. }
2、重新建立输入,即添加下面的内容
  1. input: "data"
  2. input_shape {
  3. dim: 1 # batchsize
  4. dim: 1 # number of channels
  5. dim: 28 # width
  6. dim: 28 # height
  7. }

3、去掉输出层,即将bottom 为 “label” 的layers 去掉,即将下面的内容删掉

  1. layer {
  2. name: "accuracy"
  3. type: "Accuracy"
  4. bottom: "ip2"
  5. bottom: "label"
  6. top: "accuracy"
  7. include {
  8. phase: TEST
  9. }
  10. }
  11. layer {
  12. name: "loss"
  13. type: "SoftmaxWithLoss"
  14. bottom: "ip2"
  15. bottom: "label"
  16. top: "loss"
  17. }
4、重新建立输出,即添加下面的内容

  1. layer {
  2. name: "prob"
  3. type: "Softmax"
  4. bottom: "ip2"
  5. top: "prob"
  6. }


二、使用cv::dnn 里的API加载model, 输入图片,进行测试

  1. #include <iostream>
  2. #include <opencv2/opencv.hpp>
  3. #include <opencv2/dnn.hpp>
  4. using namespace std;
  5. using namespace cv;
  6. using namespace cv::dnn;
  7. /* Find best class for the blob (i. e. class with maximal probability) */
  8. static void getMaxClass(const Mat &probBlob, int *classId, double *classProb)
  9. {
  10. Mat probMat = probBlob.reshape( 1, 1);
  11. Point classNumber;
  12. minMaxLoc(probMat, NULL, classProb, NULL, &classNumber);
  13. *classId = classNumber.x;
  14. }
  15. int main()
  16. {
  17. string modelTxt = "lenet_train_test.prototxt";
  18. string modelBin = "lenet_iter_10000.caffemodel";
  19. //read image
  20. Mat img = imread( "4.bmp", 0);
  21. Mat inputBlob = dnn::blobFromImage(img, 0.00390625f, Size( 28, 28), Scalar(), false); //Convert Mat to batch of images
  22. dnn::Net net;
  23. try{
  24. net = dnn::readNetFromCaffe(modelTxt, modelBin);
  25. }
  26. catch (cv::Exception &ee){
  27. cerr << "Exception: " << ee.what() << endl;
  28. if (net.empty()){
  29. cout << "Can't load the network by using the flowing files:" << endl;
  30. cout << "modelTxt: " << modelTxt << endl;
  31. cout << "modelBin: " << modelBin << endl; exit( -1);
  32. }
  33. }
  34. Mat pred;
  35. net.setInput(inputBlob, "data"); //set the network input, "data" is the name of the input layer
  36. pred = net.forward( "prob"); //compute output, "prob" is the name of the output layer
  37. cout << pred << endl; int classId; double classProb; getMaxClass(pred, &classId, &classProb);
  38. cout << "Best Class: " << classId << endl;
  39. cout << "Probability: " << classProb * 100 << "%" << endl;
  40. }


三、参考博文

http://blog.csdn.net/sushiqian/article/details/78555891
https://www.cnblogs.com/messier/p/7997951.html#_caption_1

https://docs.opencv.org/master/d5/de7/tutorial_dnn_googlenet.html

工程代码及caffemodel的下载 

猜你喜欢

转载自blog.csdn.net/maweifei/article/details/81005034
今日推荐