Opencv呼び出しCaffe,TensorFlow,Torch,PyTorch訓練したモデル
本住所:https://blog.csdn.net/shanglianlm/article/details/80030569
OpenCV 3.3リリースにより、深度学習(dnnモジュール)により良いサポートが提供され、dnnモジュールは現在Caffe、TensorFlow、Torch、PyTorchなどの深度学習フレームワークをサポートしている.
1モデルをネットワークにロード
1-1 caffeモデルを呼び出す
コアコード:
呼び出しcaffe例OpenCV残差ネットワークに基づいて顔検出を実現http://blog.51cto.com/gloomyfish/2094611?lb=
1-2 TensorFlowモデルを呼び出す##
a TensorFlowはモデルを訓練し、保存した.pbファイルbはopencvのreadNetFromTensorflow関数を用いてロードする.pbファイルコアコード:
呼び出し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モデルを呼び出す##
コアコード:
DarknetサンプルOpenCV DNNを呼び出すYOLOリアルタイムオブジェクト検出http://blog.51cto.com/gloomyfish/2095418
2テストデータのロード
blobFromImageはデータを4次元Blobピクチャに変換します.コアコード:
blobFromImage関数の説明
3出力結果
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 Tutorialshttps://docs.opencv.org/3.4.1/d9/df8/tutorial_root.html2 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
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...
");
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...
");
return -1;
}
呼び出し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...
");
return;
}
DarknetサンプルOpenCV DNNを呼び出すYOLOリアルタイムオブジェクト検出http://blog.51cto.com/gloomyfish/2095418
2テストデータのロード
blobFromImageはデータを4次元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 Tutorialshttps://docs.opencv.org/3.4.1/d9/df8/tutorial_root.html2 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