TEST_P(Test_TensorFlow_nets, MobileNet_SSD) { std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false); std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false); std::string imgPath = findDataFile("dnn/street.png", false); Mat inp; resize(imread(imgPath), inp, Size(300, 300)); inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true); std::vector<String> outNames(3); outNames[0] = "concat"; outNames[1] = "concat_1"; outNames[2] = "detection_out"; std::vector<Mat> target(outNames.size()); for (int i = 0; i < outNames.size(); ++i) { std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false); target[i] = blobFromNPY(path); } Net net = readNetFromTensorflow(netPath, netConfig); net.setPreferableTarget(GetParam()); net.setInput(inp); std::vector<Mat> output; net.forward(output, outNames); normAssert(target[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4); normAssert(target[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4); normAssert(target[2].reshape(1, 1), output[2].reshape(1, 1), "", 4e-5, 1e-2); }
// inp = cv.imread('opencv_extra/testdata/cv/ximgproc/sources/08.png') // inp = inp[:,:,[2, 1, 0]].astype(np.float32).reshape(1, 512, 512, 3) // outs = sess.run([sess.graph.get_tensor_by_name('feature_fusion/Conv_7/Sigmoid:0'), // sess.graph.get_tensor_by_name('feature_fusion/concat_3:0')], // feed_dict={'input_images:0': inp}) // scores = np.ascontiguousarray(outs[0].transpose(0, 3, 1, 2)) // geometry = np.ascontiguousarray(outs[1].transpose(0, 3, 1, 2)) // np.save('east_text_detection.scores.npy', scores) // np.save('east_text_detection.geometry.npy', geometry) TEST_P(Test_TensorFlow_nets, EAST_text_detection) { checkBackend(); #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_RELEASE < 2018030000 if (backend == DNN_BACKEND_INFERENCE_ENGINE && target == DNN_TARGET_MYRIAD) throw SkipTestException("Test is enabled starts from OpenVINO 2018R3"); #endif std::string netPath = findDataFile("dnn/frozen_east_text_detection.pb", false); std::string imgPath = findDataFile("cv/ximgproc/sources/08.png", false); std::string refScoresPath = findDataFile("dnn/east_text_detection.scores.npy", false); std::string refGeometryPath = findDataFile("dnn/east_text_detection.geometry.npy", false); Net net = readNet(findDataFile("dnn/frozen_east_text_detection.pb", false)); net.setPreferableBackend(backend); net.setPreferableTarget(target); Mat img = imread(imgPath); Mat inp = blobFromImage(img, 1.0, Size(), Scalar(123.68, 116.78, 103.94), true, false); net.setInput(inp); std::vector<Mat> outs; std::vector<String> outNames(2); outNames[0] = "feature_fusion/Conv_7/Sigmoid"; outNames[1] = "feature_fusion/concat_3"; net.forward(outs, outNames); Mat scores = outs[0]; Mat geometry = outs[1]; // Scores are in range [0, 1]. Geometry values are in range [-0.23, 290] double l1_scores = default_l1, lInf_scores = default_lInf; double l1_geometry = default_l1, lInf_geometry = default_lInf; if (target == DNN_TARGET_OPENCL_FP16) { lInf_scores = 0.11; l1_geometry = 0.28; lInf_geometry = 5.94; } else if (target == DNN_TARGET_MYRIAD) { lInf_scores = 0.214; l1_geometry = 0.47; lInf_geometry = 15.34; } else { l1_geometry = 1e-4, lInf_geometry = 3e-3; } normAssert(scores, blobFromNPY(refScoresPath), "scores", l1_scores, lInf_scores); normAssert(geometry, blobFromNPY(refGeometryPath), "geometry", l1_geometry, lInf_geometry); }
TEST(Test_TensorFlow, Mask_RCNN) { std::string proto = findDataFile("dnn/mask_rcnn_inception_v2_coco_2018_01_28.pbtxt", false); std::string model = findDataFile("dnn/mask_rcnn_inception_v2_coco_2018_01_28.pb", false); Net net = readNetFromTensorflow(model, proto); Mat img = imread(findDataFile("dnn/street.png", false)); Mat refDetections = blobFromNPY(path("mask_rcnn_inception_v2_coco_2018_01_28.detection_out.npy")); Mat refMasks = blobFromNPY(path("mask_rcnn_inception_v2_coco_2018_01_28.detection_masks.npy")); Mat blob = blobFromImage(img, 1.0f, Size(800, 800), Scalar(), true, false); net.setPreferableBackend(DNN_BACKEND_OPENCV); net.setInput(blob); // Mask-RCNN predicts bounding boxes and segmentation masks. std::vector<String> outNames(2); outNames[0] = "detection_out_final"; outNames[1] = "detection_masks"; std::vector<Mat> outs; net.forward(outs, outNames); Mat outDetections = outs[0]; Mat outMasks = outs[1]; normAssertDetections(refDetections, outDetections, "", /*threshold for zero confidence*/1e-5); // Output size of masks is NxCxHxW where // N - number of detected boxes // C - number of classes (excluding background) // HxW - segmentation shape const int numDetections = outDetections.size[2]; int masksSize[] = {1, numDetections, outMasks.size[2], outMasks.size[3]}; Mat masks(4, &masksSize[0], CV_32F); std::vector<cv::Range> srcRanges(4, cv::Range::all()); std::vector<cv::Range> dstRanges(4, cv::Range::all()); outDetections = outDetections.reshape(1, outDetections.total() / 7); for (int i = 0; i < numDetections; ++i) { // Get a class id for this bounding box and copy mask only for that class. int classId = static_cast<int>(outDetections.at<float>(i, 1)); srcRanges[0] = dstRanges[1] = cv::Range(i, i + 1); srcRanges[1] = cv::Range(classId, classId + 1); outMasks(srcRanges).copyTo(masks(dstRanges)); } cv::Range topRefMasks[] = {Range::all(), Range(0, numDetections), Range::all(), Range::all()}; normAssert(masks, refMasks(&topRefMasks[0])); }
TEST_P(Test_TensorFlow_nets, MobileNet_SSD) { checkBackend(); if ((backend == DNN_BACKEND_INFERENCE_ENGINE && target != DNN_TARGET_CPU) || (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)) throw SkipTestException(""); std::string netPath = findDataFile("dnn/ssd_mobilenet_v1_coco.pb", false); std::string netConfig = findDataFile("dnn/ssd_mobilenet_v1_coco.pbtxt", false); std::string imgPath = findDataFile("dnn/street.png", false); Mat inp; resize(imread(imgPath), inp, Size(300, 300)); inp = blobFromImage(inp, 1.0f / 127.5, Size(), Scalar(127.5, 127.5, 127.5), true); std::vector<String> outNames(3); outNames[0] = "concat"; outNames[1] = "concat_1"; outNames[2] = "detection_out"; std::vector<Mat> refs(outNames.size()); for (int i = 0; i < outNames.size(); ++i) { std::string path = findDataFile("dnn/tensorflow/ssd_mobilenet_v1_coco." + outNames[i] + ".npy", false); refs[i] = blobFromNPY(path); } Net net = readNetFromTensorflow(netPath, netConfig); net.setPreferableBackend(backend); net.setPreferableTarget(target); net.setInput(inp); std::vector<Mat> output; net.forward(output, outNames); normAssert(refs[0].reshape(1, 1), output[0].reshape(1, 1), "", 1e-5, 1.5e-4); normAssert(refs[1].reshape(1, 1), output[1].reshape(1, 1), "", 1e-5, 3e-4); normAssertDetections(refs[2], output[2], "", 0.2); }