1. 程式人生 > >OpenCV 3.3版本釋出,載入CAFFE/Darknet深度學習模型

OpenCV 3.3版本釋出,載入CAFFE/Darknet深度學習模型

作者:mingo_敏  
來源:CSDN  
原文:https://blog.csdn.net/shanglianlm/article/details/80030569  
版權宣告:本文為博主原創文章,轉載請附上博文連結!

OpenCV 3.3版本釋出,對深度學習(dnn模組)提供了更好的支援,dnn模組目前支援Caffe、TensorFlow、Torch、PyTorch等深度學習框架。
1 載入模型成網路
1-1 呼叫caffe模型

核心程式碼:

String modelDesc = "../face/deploy.prototxt";
String modelBinary = "../face/res10_300x300_ssd_iter_140000.caffemodel";
// 初始化網路
dnn::Net net = readNetFromCaffe(modelDesc, modelBinary);
if (net.empty()){
    printf("could not load net...\n");
    return -1;
}

   

呼叫caffe示例
OpenCV基於殘差網路實現人臉檢測
http://blog.51cto.com/gloomyfish/2094611?lb=
1-2 呼叫TensorFlow模型

a TensorFlow訓練模型,並儲存成.pb檔案
b 使用opencv的readNetFromTensorflow函式載入.pb檔案
核心程式碼:

String labels_txt_file ="../inception5h/imagenet_comp_graph_label_strings.txt";
String tf_pb_file ="../inception5h/tensorflow_inception_graph.pb";

// 載入網路  
Net net =readNetFromTensorflow(tf_pb_file);   
if(net.empty()){
    printf("read caffe model data failure...\n");
    return -1;
}

    1
    2
    3
    4
    5
    6
    7
    8
    9

呼叫TensorFlow示例
OpenCV 基於Inception模型影象分類
https://mp.weixin.qq.com/s?__biz=MzA4MDExMDEyMw==&mid=2247484278&idx=1&sn=e5074be2ba35c17bf34685864b6d34d7&chksm=9fa87432a8dffd246f1c88fea1dc7e348abb3d93c93e0834da881852dcfea68f5609ce927038&mpshare=1&scene=23&srcid=0421tNU3Tvp8N4oEUip7LYE9#rd
1-3 呼叫Darknet模型

核心程式碼:

String modelConfiguration = "../yolov2-tiny-voc/yolov2-tiny-voc.cfg";
String modelBinary = "../yolov2-tiny-voc/yolov2-tiny-voc.weights";
dnn::Net net = readNetFromDarknet(modelConfiguration, modelBinary);
if (net.empty())
{
    printf("Could not load net...\n");
    return;
}

呼叫Darknet示例
OpenCV DNN之YOLO實時物件檢測
http://blog.51cto.com/gloomyfish/2095418
2 載入測試資料

blobFromImage 轉換資料為四維Blob圖片。
核心程式碼:

// 載入影象
Mat frame = imread("../123.jpg");
Mat inputBlob = blobFromImage(frame, 1/255.F, Size(416, 416), Scalar(), true, false);
net.setInput(inputBlob, "data");

blobFromImage函式解釋

Mat blobFromImage(InputArray image, double scalefactor=1.0, const Size& size = Size(),  const Scalar& mean = Scalar(), bool swapRB=true, bool crop=true);  
    /** @brief Creates 4-dimensional blob from image. Optionally resizes and crops @p image from center,  
    *  subtract @p mean values, scales values by @p scalefactor, swap Blue and Red channels.  
    *  @param image input image (with 1-, 3- or 4-channels).  
    *  @param size spatial size for output image  
    *  @param mean scalar with mean values which are subtracted from channels. Values are intended  
    *  to be in (mean-R, mean-G, mean-B) order if @p image has BGR ordering and @p swapRB is true.  
    *  @param scalefactor multiplier for @p image values.  
    *  @param swapRB flag which indicates that swap first and last channels  
    *  in 3-channel image is necessary.  
    *  @param crop flag which indicates whether image will be cropped after resize or not  
    *  @details if @p crop is true, input image is resized so one side after resize is equal to corresponding  
    *  dimension in @p size and another one is equal or larger. Then, crop from the center is performed.  
    *  If @p crop is false, direct resize without cropping and preserving aspect ratio is performed.  
    *  @returns 4-dimansional Mat with NCHW dimensions order.  
    */  

第一個引數,InputArray image,表示輸入的影象,可以是opencv的mat資料型別。
第二個引數,scalefactor,這個引數很重要的,如果訓練時,是歸一化到0-1之間,那麼這個引數就應該為0.00390625f (1/256),否則為1.0
第三個引數,size,應該與訓練時的輸入影象尺寸保持一致。
第四個引數,mean,這個主要在caffe中用到,caffe中經常會用到訓練資料的均值。tf中貌似沒有用到均值檔案。
第五個引數,swapRB,是否交換影象第1個通道和最後一個通道的順序。
第六個引數,crop,如果為true,就是裁剪影象,如果為false,就是等比例放縮影象。

3 輸出結果

//檢測 darknet
Mat detectionMat = net.forward("detection_out");

 

//分類 Inception
prob =net.forward("softmax2");

 

//tf
pred = net.forward("fc2/prob"); 

4 用到的一些函式

4-1 在dnn中從磁碟載入圖片##

    cv2.dnn.blobFromImage
    cv2.dnn.blobFromImages

4-2 用create方法直接從各種框架中匯出模型

    cv2.dnn.createCaffeImporter
    cv2.dnn.createTensorFlowImporter
    cv2.dnn.createTorchImporter

4-3 使用read方法從磁碟直接載入序列化模型

    cv2.dnn.readNetFromCaffe
    cv2.dnn.readNetFromTensorFlow
    cv2.dnn.readNetFromTorch
    cv2.dnn.readhTorchBlob

從磁碟載入完模型之後,可以用.forward方法來向前傳播我們的影象,獲取結果。

參考資料
1 OpenCV Tutorials
https://docs.opencv.org/3.4.1/d9/df8/tutorial_root.html
2 opencv呼叫tf訓練好的模型
https://blog.csdn.net/hust_bochu_xuchao/article/details/79428759
3 Deep Learning with OpenCVhttps://www.pyimagesearch.com/2017/08/21/deep-learning-with-opencv/
4 opencv的dnn解析
https://blog.csdn.net/langb2014/article/details/51286828