+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

2020-04(18)

2020-05(28)

2020-06(39)

2020-07(23)

2020-08(43)

Opencv dnn tpb模型(tensorflow)调用——分类

发布于2021-02-27 14:56     阅读(279)     评论(0)     点赞(20)     收藏(4)


0

1

2

3

4

5

自建模型:python调用成功,

          C++官网调用成功,但是自己写的调用失败,太坑了

1.官方分类pb模型调用检测:(模型下载链接)-翻墙自己下,翻不了下这个

  1. #####################################################################
  2. # #
  3. # ###### dnn 分类模型pb调用 ###### #
  4. #####################################################################
  5. import cv2 # opencv-python 4.2.xx
  6. import time
  7. import numpy as np # numpy 1.15.0
  8. # 1.图片读取
  9. image = cv2.imread("./101.jpg")
  10. image = cv2.normalize(image, dst=image, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
  11. # 2.读取模型
  12. net = cv2.dnn.readNetFromTensorflow("./models/inception_v3_2016_08_28_frozen.pb")
  13. # 3.获得所有层名称与索引
  14. layerNames = net.getLayerNames()
  15. for ind, layername in enumerate(layerNames):
  16. lastLayerId = net.getLayerId(layername)
  17. lastLayer = net.getLayer(lastLayerId)
  18. if ind==0:
  19. print(ind, lastLayer.type)
  20. pass
  21. pass
  22. # 4.模型检测
  23. start = time.clock()
  24. input_blob = cv2.dnn.blobFromImage(image, size=(299, 299))
  25. net.setInput(input_blob)
  26. cvOut = np.squeeze(net.forward())
  27. stop = time.clock()
  28. # 5.结果输出
  29. with open("./models/imagenet_slim_labels.txt") as f:
  30. label_name = f.read().splitlines()
  31. res_index = np.argmax(cvOut, axis=-1)
  32. cvOut,label_name[res_index]
  33. print(label_name[res_index], stop-start, cvOut)

 2.自建分类pb模型调用检测:(python版本)

 

  1. #####################################################################
  2. # #
  3. # ###### dnn 分类模型pb调用 ###### #
  4. #####################################################################
  5. import cv2 # opencv-python 4.2.xx
  6. import time
  7. import numpy as np # numpy 1.15.0
  8. # 1.图片读取
  9. image = cv2.imread("./101.jpg")
  10. image = cv2.normalize(image, dst=image, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX)
  11. # 2.读取模型
  12. net = cv2.dnn.readNetFromTensorflow("./models/best_model_cls_1.0000.pb")
  13. # 3.获得所有层名称与索引
  14. layerNames = net.getLayerNames()
  15. for ind, layername in enumerate(layerNames):
  16. lastLayerId = net.getLayerId(layername)
  17. lastLayer = net.getLayer(lastLayerId)
  18. if ind==0:
  19. print(ind, lastLayer.type)
  20. pass
  21. pass
  22. # 4.模型检测
  23. start = time.clock()
  24. input_blob = cv2.dnn.blobFromImage(image, size=(256, 256))
  25. net.setInput(input_blob)
  26. cvOut = np.squeeze(net.forward())
  27. stop = time.clock()
  28. # 5.结果输出
  29. with open("./models/labels.txt") as f:
  30. label_name = f.read().splitlines()
  31. res_index = np.argmax(cvOut, axis=-1)
  32. cvOut,label_name[res_index]
  33. print(label_name[res_index], stop-start, cvOut)

3.自建分类pb模型调用检测:(C++版本)

  1. /*前向推理出问题了,但是使用官网的就可以*/
  2. #include <opencv2/opencv.hpp>
  3. #include <opencv2/dnn.hpp>
  4. #include <iostream>
  5. using namespace cv;
  6. using namespace cv::dnn;
  7. using namespace std;
  8. #ifdef _DEBUG
  9. #pragma comment( lib, "opencv_world420d.lib" )
  10. #else
  11. #pragma comment( lib, "opencv_world420.lib" )
  12. #endif
  13. int main(int argc, char** argv) {
  14. string model = "./models/best_model_cls_1.0000.pb";
  15. Mat image = imread("101.jpg", IMREAD_COLOR); //读取图片
  16. if (image.empty()) {
  17. printf("Load image failed");
  18. }
  19. // 2.模型读取
  20. Net net = readNetFromTensorflow(model);
  21. net.setPreferableBackend(DNN_BACKEND_INFERENCE_ENGINE);
  22. net.setPreferableTarget(DNN_TARGET_CPU);
  23. // 3.
  24. Mat blob = blobFromImage(image, 1.0, Size(256,256), Scalar(), true, false, CV_32F);
  25. cout << "input_blob: " << blob.depth() << endl;
  26. net.setInput(blob, "inputs");
  27. // 4.
  28. Mat result = net.forward();
  29. system("pause");
  30. return 0;
  31. }

 

原文链接:https://blog.csdn.net/weixin_41275726/article/details/114030494

0

1

2

3

4



所属网站分类: 技术文章 > 博客

作者:从新来过

链接: https://www.pythonheidong.com/blog/article/860107/e4025c4184f7acf00a4e/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

20 0
收藏该文
已收藏

评论内容:(最多支持255个字符)