def test_input_3d(self): model = self.find_dnn_file('dnn/onnx/models/hidden_lstm.onnx') input_file = self.find_dnn_file('dnn/onnx/data/input_hidden_lstm.npy') output_file = self.find_dnn_file( 'dnn/onnx/data/output_hidden_lstm.npy') if model is None: raise unittest.SkipTest( "Missing DNN test files (dnn/onnx/models/hidden_lstm.onnx). " "Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter.") if input_file is None or output_file is None: raise unittest.SkipTest( "Missing DNN test files (dnn/onnx/data/{input/output}_hidden_lstm.npy). " "Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter.") net = cv.dnn.readNet(model) input = np.load(input_file) # we have to expand the shape of input tensor because Python bindings cut 3D tensors to 2D # it should be fixed in future. see : https://github.com/opencv/opencv/issues/19091 # please remove `expand_dims` after that input = np.expand_dims(input, axis=3) gold_output = np.load(output_file) net.setInput(input) for backend, target in self.dnnBackendsAndTargets: printParams(backend, target) net.setPreferableBackend(backend) net.setPreferableTarget(target) real_output = net.forward() normAssert(self, real_output, gold_output, "", getDefaultThreshold(target))
def test_export_inner_class_of_class_exported_with_different_name(self): if not hasattr(cv.utils.nested, "ExportClassName"): raise unittest.SkipTest( "Outer class with export alias is not registered in the submodule" ) self.assertTrue( hasattr(cv.utils.nested.ExportClassName, "Params"), msg="Inner class with export alias is not registered in " "the outer class") self.assertTrue( hasattr(cv, "utils_nested_ExportClassName_Params"), msg="Inner class with export alias is not registered in " "global module") params = cv.utils.nested.ExportClassName.Params() params.int_value = 45 params.float_value = 4.5 instance = cv.utils.nested.ExportClassName.create(params) self.assertTrue( isinstance(instance, cv.utils.nested.ExportClassName), msg="Factory function returns wrong class instance: {}".format( type(instance))) self.assertEqual( params.int_value, instance.getIntParam(), msg= "Class initialized with wrong integer parameter. Expected: {}. Actual: {}" .format(params.int_value, instance.getIntParam())) self.assertEqual( params.float_value, instance.getFloatParam(), msg= "Class initialized with wrong integer parameter. Expected: {}. Actual: {}" .format(params.float_value, instance.getFloatParam()))
def test_can_get_ocv_version(self): import sys if sys.version_info[0] < 3: raise unittest.SkipTest('Python 2.x is not supported') self.assertEqual(cv.misc.get_ocv_version(), cv.__version__, "Can't get package version using Python misc module")
def test_perf_detection_softmax_model(self): img_path = self.find_dnn_file("dnn/person-detector.png") weights = self.find_dnn_file("dnn/squeezenet_softmax.caffemodel", required=False) config = self.find_dnn_file("dnn/squeezenet_softmax.prototxt", required=False) if weights is None or config is None: raise unittest.SkipTest( "Missing DNN test files (dnn/squeezenet_softmax.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter." ) frame = cv.imread(img_path) model = cv.dnn_DetectionModel(weights, config) model.setInputParams(size=(224, 224), mean=(0, 0, 0), scale=1.0) outputLayer = "softmaxout" weightsMemory, blobsMemory = model.getMemoryConsumption( (1, 3, 224, 224)) flops = model.getFLOPS((1, 3, 224, 224)) model.forward(outputLayer) print("Memory consumption:") print(" Weights(parameters): ", (weightsMemory + (1 << 20) - 1) / (1 << 20), " Mb") print(" Blobs: ", (blobsMemory + (1 << 20) - 1) / (1 << 20), " Mb") print("Calculation complexity: ", flops * 1e-9, " GFlops")
def test_detection_softmax_model(self): img_path = self.find_dnn_file("dnn/person-detector.png") weights = self.find_dnn_file("dnn/squeezenet_softmax.caffemodel", required=False) config = self.find_dnn_file("dnn/squeezenet_softmax.prototxt") if weights is None or config is None: raise unittest.SkipTest( "Missing DNN test files (dnn/squeezenet_v1.1.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter." ) frame = cv.imread(img_path).astype(np.uint8) model = cv.dnn_ClassificationModel(config, weights) model.setInputSize(224, 224) model.setInputCrop(True) out = model.predict(frame) for i in range(10): frame2 = np.clip(frame + np.random.randint(0, 2, frame.shape), 0, 255).astype(np.uint8) out2 = model.predict(frame2) ref = out2 try: normAssert(self, out[0], ref[0], msg="prediction probility in max. " + str(np.max(np.abs(out[0] - ref[0])))) except Exception as e: print(e.args)
def test_model(self): img_path = self.find_dnn_file("dnn/street.png") weights = self.find_dnn_file("dnn/MobileNetSSD_deploy.caffemodel", required=False) config = self.find_dnn_file("dnn/MobileNetSSD_deploy.prototxt", required=False) if weights is None or config is None: raise unittest.SkipTest("Missing DNN test files (dnn/MobileNetSSD_deploy.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter.") frame = cv.imread(img_path) model = cv.dnn_DetectionModel(weights, config) model.setInputParams(size=(300, 300), mean=(127.5, 127.5, 127.5), scale=1.0/127.5) iouDiff = 0.05 confThreshold = 0.0001 nmsThreshold = 0 scoreDiff = 1e-3 classIds, confidences, boxes = model.detect(frame, confThreshold, nmsThreshold) refClassIds = (7, 15) refConfidences = (0.9998, 0.8793) refBoxes = ((328, 238, 85, 102), (101, 188, 34, 138)) normAssertDetections(self, refClassIds, refConfidences, refBoxes, classIds, confidences, boxes,confThreshold, scoreDiff, iouDiff) for box in boxes: cv.rectangle(frame, box, (0, 255, 0)) cv.rectangle(frame, np.array(box), (0, 255, 0)) cv.rectangle(frame, tuple(box), (0, 255, 0)) cv.rectangle(frame, list(box), (0, 255, 0))
def test_async(self): timeout = 5000 # in milliseconds testdata_required = bool( os.environ.get('OPENCV_DNN_TEST_REQUIRE_TESTDATA', False)) proto = self.find_dnn_file('dnn/layers/layer_convolution.prototxt', required=testdata_required) model = self.find_dnn_file('dnn/layers/layer_convolution.caffemodel', required=testdata_required) if proto is None or model is None: raise unittest.SkipTest( "Missing DNN test files (dnn/layers/layer_convolution.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter." ) print('\n') for backend, target in self.dnnBackendsAndTargets: if backend != cv.dnn.DNN_BACKEND_INFERENCE_ENGINE: continue printParams(backend, target) netSync = cv.dnn.readNet(proto, model) netSync.setPreferableBackend(backend) netSync.setPreferableTarget(target) netAsync = cv.dnn.readNet(proto, model) netAsync.setPreferableBackend(backend) netAsync.setPreferableTarget(target) # Generate inputs numInputs = 10 inputs = [] for _ in range(numInputs): inputs.append( np.random.standard_normal([2, 6, 75, 113]).astype(np.float32)) # Run synchronously refs = [] for i in range(numInputs): netSync.setInput(inputs[i]) refs.append(netSync.forward()) # Run asynchronously. To make test more robust, process inputs in the reversed order. outs = [] for i in reversed(range(numInputs)): netAsync.setInput(inputs[i]) outs.insert(0, netAsync.forwardAsync()) for i in reversed(range(numInputs)): ret = outs[i].wait_for(timeout) if ret == 1: self.fail("Timeout") self.assertEqual(ret, 0) # is ready normAssert(self, refs[i], outs[i].get(), 'Index: %d' % i, 1e-10)
def test_face_detection(self): testdata_required = bool( os.environ.get('OPENCV_DNN_TEST_REQUIRE_TESTDATA', False)) proto = self.find_dnn_file('dnn/opencv_face_detector.prototxt', required=testdata_required) model = self.find_dnn_file('dnn/opencv_face_detector.caffemodel', required=testdata_required) if proto is None or model is None: raise unittest.SkipTest( "Missing DNN test files (dnn/opencv_face_detector.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter." ) img = self.get_sample('gpu/lbpcascade/er.png') blob = cv.dnn.blobFromImage(img, mean=(104, 177, 123), swapRB=False, crop=False) ref = [ [0, 1, 0.99520785, 0.80997437, 0.16379407, 0.87996572, 0.26685631], [0, 1, 0.9934696, 0.2831718, 0.50738752, 0.345781, 0.5985168], [0, 1, 0.99096733, 0.13629119, 0.24892329, 0.19756334, 0.3310290], [0, 1, 0.98977017, 0.23901358, 0.09084064, 0.29902688, 0.1769477], [0, 1, 0.97203469, 0.67965847, 0.06876482, 0.73999709, 0.1513494], [0, 1, 0.95097077, 0.51901293, 0.45863652, 0.5777427, 0.5347801] ] print('\n') for backend, target in self.dnnBackendsAndTargets: printParams(backend, target) net = cv.dnn.readNet(proto, model) net.setPreferableBackend(backend) net.setPreferableTarget(target) net.setInput(blob) out = net.forward().reshape(-1, 7) scoresDiff = 4e-3 if target in [ cv.dnn.DNN_TARGET_OPENCL_FP16, cv.dnn.DNN_TARGET_MYRIAD ] else 1e-5 iouDiff = 2e-2 if target in [ cv.dnn.DNN_TARGET_OPENCL_FP16, cv.dnn.DNN_TARGET_MYRIAD ] else 1e-4 ref = np.array(ref, np.float32) refClassIds, testClassIds = ref[:, 1], out[:, 1] refScores, testScores = ref[:, 2], out[:, 2] refBoxes, testBoxes = ref[:, 3:], out[:, 3:] normAssertDetections(self, refClassIds, refScores, refBoxes, testClassIds, testScores, testBoxes, 0.5, scoresDiff, iouDiff)
def test_native_method_can_be_patched(self): import sys if sys.version_info[0] < 3: raise unittest.SkipTest('Python 2.x is not supported') res = cv.utils.testOverwriteNativeMethod(10) self.assertTrue(isinstance(res, Sequence), msg="Overwritten method should return sequence. " "Got: {} of type {}".format(res, type(res))) self.assertSequenceEqual(res, (11, 10), msg="Failed to overwrite native method") res = cv.utils._native.testOverwriteNativeMethod(123) self.assertEqual(res, 123, msg="Failed to call native method implementation")
def test_classification_model(self): img_path = self.find_dnn_file("dnn/googlenet_0.png") weights = self.find_dnn_file("dnn/squeezenet_v1.1.caffemodel", required=False) config = self.find_dnn_file("dnn/squeezenet_v1.1.prototxt") ref = np.load(self.find_dnn_file("dnn/squeezenet_v1.1_prob.npy")) if weights is None or config is None: raise unittest.SkipTest("Missing DNN test files (dnn/squeezenet_v1.1.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter.") frame = cv.imread(img_path) model = cv.dnn_ClassificationModel(config, weights) model.setInputSize(227, 227) model.setInputCrop(True) out = model.predict(frame) normAssert(self, out, ref)
def test_async(self): timeout = 10 * 1000 * 10**6 # in nanoseconds (10 sec) proto = self.find_dnn_file('dnn/layers/layer_convolution.prototxt') model = self.find_dnn_file('dnn/layers/layer_convolution.caffemodel') if proto is None or model is None: raise unittest.SkipTest( "Missing DNN test files (dnn/layers/layer_convolution.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter." ) print('\n') for backend, target in self.dnnBackendsAndTargets: if backend != cv.dnn.DNN_BACKEND_INFERENCE_ENGINE: continue printParams(backend, target) netSync = cv.dnn.readNet(proto, model) netSync.setPreferableBackend(backend) netSync.setPreferableTarget(target) netAsync = cv.dnn.readNet(proto, model) netAsync.setPreferableBackend(backend) netAsync.setPreferableTarget(target) # Generate inputs numInputs = 10 inputs = [] for _ in range(numInputs): inputs.append( np.random.standard_normal([2, 6, 75, 113]).astype(np.float32)) # Run synchronously refs = [] for i in range(numInputs): netSync.setInput(inputs[i]) refs.append(netSync.forward()) # Run asynchronously. To make test more robust, process inputs in the reversed order. outs = [] for i in reversed(range(numInputs)): netAsync.setInput(inputs[i]) outs.insert(0, netAsync.forwardAsync()) for i in reversed(range(numInputs)): ret, result = outs[i].get(timeoutNs=float(timeout)) self.assertTrue(ret) normAssert(self, refs[i], result, 'Index: %d' % i, 1e-10)
def test_textdetection_model(self): img_path = self.find_dnn_file("dnn/text_det_test1.png") weights = self.find_dnn_file("dnn/onnx/models/DB_TD500_resnet50.onnx", required=False) if weights is None: raise unittest.SkipTest( "Missing DNN test files (onnx/models/DB_TD500_resnet50.onnx). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter." ) frame = cv.imread(img_path) scale = 1.0 / 255.0 size = (736, 736) mean = (122.67891434, 116.66876762, 104.00698793) model = cv.dnn_TextDetectionModel_DB(weights) model.setInputParams(scale, size, mean) out, _ = model.detect(frame) self.assertTrue(type(out) == list) self.assertTrue(np.array(out).shape == (2, 4, 2))
def test_ssd_model(self): img_path = self.find_dnn_file("dnn/person-detector.png") weights = self.find_dnn_file("dnn/SqueezeNetSSD-5Class.caffemodel", required=False) config = self.find_dnn_file("dnn/SqueezeNetSSD-5Class.prototxt", required=False) if weights is None or config is None: raise unittest.SkipTest( "Missing DNN test files (dnn/SqueezeNetSSD-5Class.{prototxt/caffemodel}). Verify OPENCV_DNN_TEST_DATA_PATH configuration parameter." ) frame = cv.imread(img_path) model = cv.dnn_DetectionModel(weights, config) model.setInputParams(size=(224, 224), mean=(0, 0, 0), scale=1.0) iouDiff = 0.5 confThreshold = 0.4 nmsThreshold = 2 scoreDiff = 5e-3 classIds, confidences, boxes = model.detect(frame, confThreshold, nmsThreshold) refClassIds = (5, 5) refConfidences = (0.481543, 0.481456) refBoxes = ((644, 18, 224, 623), (644, 18, 224, 623)) normAssertDetections(self, refClassIds, refConfidences, refBoxes, classIds, confidences, boxes, confThreshold, scoreDiff, iouDiff) for box in boxes: cv.rectangle(frame, box, (0, 255, 0)) cv.rectangle(frame, np.array(box), (0, 255, 0)) cv.rectangle(frame, tuple(box), (0, 255, 0)) cv.rectangle(frame, list(box), (0, 255, 0)) cv.putText(frame, "Box: " + str(confidences[0][0]), tuple((np.array(box[:2]) + 100).tolist()), cv.FONT_HERSHEY_SIMPLEX, 0.6, (0, 0, 0), 1, cv.LINE_AA) cv.imwrite("accuracy-tested-person-detector.png", frame)