def test_fill_non_constant_inputs_with_specific_mapping_not_all_image_in_batch_matched_raise_config_error( self): input_feeder = InputFeeder([{ 'name': 'input1', 'type': 'INPUT', 'value': '0' }, { 'name': 'input2', 'type': 'INPUT', 'value': '1' }], { 'input1': (1, 3, 10, 10), 'input2': (1, 3, 10, 10) }) with pytest.raises(ConfigError): input_feeder.fill_non_constant_inputs([ DataRepresentation( [np.zeros((10, 10, 3)), np.ones((10, 10, 3))], identifier=['0', '1']), DataRepresentation( [np.zeros((10, 10, 3)), np.ones((10, 10, 3))], identifier=['0', '2']) ])
def test_fill_non_constant_inputs_with_specific_mapping_batch_2(self): input_feeder = InputFeeder( [{ 'name': 'input1', 'type': 'INPUT', 'value': '0' }, { 'name': 'input2', 'type': 'INPUT', 'value': '1' }], { 'input1': InputInfo_test(shape=(1, 3, 10, 10)), 'input2': InputInfo_test(shape=(1, 3, 10, 10)) }) result = input_feeder.fill_non_constant_inputs([ DataRepresentation([np.zeros( (10, 10, 3)), np.ones((10, 10, 3))], identifier=['0', '1']), DataRepresentation([np.zeros( (10, 10, 3)), np.ones((10, 10, 3))], identifier=['0', '1']) ])[0] expected_data = [np.zeros((2, 3, 10, 10)), np.ones((2, 3, 10, 10))] assert 'input1' in result assert np.array_equal(result['input1'], expected_data[0]) assert 'input2' in result assert np.array_equal(result['input2'], expected_data[1])
def test_fill_non_constant_input_without_specific_mapping_batch_2(self): input_feeder = InputFeeder( [], {'input': InputInfo_test(shape=(1, 3, 10, 10))}) result = input_feeder.fill_non_constant_inputs([ DataRepresentation(np.zeros((10, 10, 3)), identifier='0'), DataRepresentation(np.zeros((10, 10, 3)), identifier='1') ])[0] expected_data = np.zeros((2, 3, 10, 10)) assert 'input' in result assert np.array_equal(result['input'], expected_data)
def test_fill_non_constant_input_with_specific_mapping_not_all_image_in_batch_matched_raise_config_error( self): input_feeder = InputFeeder( [{ 'name': 'input', 'type': 'INPUT', 'value': '0+' }], {'input': InputInfo_test(shape=(1, 3, 10, 10))}) with pytest.raises(ConfigError): input_feeder.fill_non_constant_inputs([ DataRepresentation(np.zeros((10, 10, 3)), identifier='0'), DataRepresentation(np.zeros((10, 10, 3)), identifier='1') ])
def test_bgr_to_rgb(self): image = np.random.randint(0, 255, (30, 40, 3)).astype(np.uint8) expected_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) bgr_to_rgb = BgrToRgb({'type': 'bgr_to_rgb'}) assert np.array_equal( expected_image, bgr_to_rgb.process(DataRepresentation(image)).data)
def test_crop_less(self): crop = Crop({'dst_width': 151, 'dst_height': 42, 'type': 'crop'}) image = np.zeros((30, 30, 3)) image_rep = crop(DataRepresentation(image)) assert image_rep.data.shape == (42, 151, 3) assert image_rep.metadata == {'image_size': (30, 30, 3)}
def test_crop_to_size(self): crop = Crop({'size': 50, 'type': 'crop'}) image = np.zeros((100, 100, 3)) image_rep = crop(DataRepresentation(image)) assert image_rep.data.shape == (50, 50, 3) assert image_rep.metadata == {'image_size': (100, 100, 3)}
def test_crop__higher(): crop = Crop({'dst_width': 50, 'dst_height': 33, 'type': 'crop'}) image = np.zeros((100, 100, 3)) image_rep = crop(DataRepresentation(image)) assert image_rep.data.shape == (33, 50, 3) assert image_rep.metadata == {}
def test_crop__higher_non_symmetric(): crop = Crop({'dst_width': 50, 'dst_height': 12, 'type': 'crop'}) image = np.zeros((70, 50, 3)) image_rep = crop(DataRepresentation(image)) assert image_rep.data.shape == (12, 50, 3) assert image_rep.metadata == {}
def test_crop__less_non_symmetric(): crop = Crop({'dst_width': 42, 'dst_height': 151, 'type': 'crop'}) image = np.zeros((30, 40, 3)) image_rep = crop(DataRepresentation(image)) assert image_rep.data.shape == (151, 42, 3) assert image_rep.metadata == {}
def test_default_extend_around_rect_without_rect(self): image = np.random.randint(0, 255, (30, 40, 3)).astype(np.uint8) expected_image = image extend_image_around_rect = ExtendAroundRect( {'type': 'extend_around_rect'}) assert np.array_equal( expected_image, extend_image_around_rect(DataRepresentation(image), {}).data)
def test_crop_rect_if_rect_equal_image(self): image = np.zeros((30, 40, 3)) crop_rect = CropRect({'type': 'crop_rect'}) assert np.array_equal( image, crop_rect(DataRepresentation(image), { 'rect': [0, 0, 40, 30] }).data)
def test_infer(dlsdk_test_model, data_dir): c, h, w = dlsdk_test_model.inputs['data'] img_raw = cv2.imread(str(data_dir / '1.jpg')) img_resized = cv2.resize(img_raw, (w, h)) res = dlsdk_test_model.predict(['1.jpg'], [DataRepresentation(img_resized)]) assert res[0].label == 6
def test_fill_non_const_input_with_multi_infer_not_consistent_data_batch_2( self): input_feeder = InputFeeder({}, {'input': (2, 3, 10, 10)}) result = input_feeder.fill_non_constant_inputs([ DataRepresentation([np.zeros((10, 10, 3))], {'multi_infer': True}, identifier='0'), DataRepresentation([np.zeros( (10, 10, 3)), np.ones((10, 10, 3))], {'multi_infer': True}, identifier='1'), ]) expected = [{ 'input': np.zeros((2, 3, 10, 10)) }, { 'input': np.ones((1, 3, 10, 10)) }] assert len(result) == len(expected) assert np.array_equal(result[0]['input'], expected[0]['input']) assert np.array_equal(result[1]['input'], expected[1]['input'])
def test_infer(self, data_dir, models_dir): caffe_test_model = get_caffe_test_model(models_dir) c, h, w = caffe_test_model.inputs['data'] img_raw = cv2.imread(str(data_dir / '1.jpg')) img_resized = cv2.resize(img_raw, (w, h)) res = caffe_test_model.predict(['1.jpg'], [DataRepresentation(img_resized)]) assert res[0].label == 6
def test_resize_without_save_aspect_ratio(self): name = 'mock_preprocessor' config = {'type': 'resize', 'dst_width': 150, 'dst_height': 150} input_image = np.ones((100, 50, 3)) resize = Preprocessor.provide('resize', config, name) result = resize(DataRepresentation(input_image)).data assert result.shape == (150, 150, 3)
def test_crop_rect_more_image_size_coordinates_of_rect(self): image = np.zeros((30, 40, 3)) image[:, 20:, :] = 1 expected_image = np.ones((30, 20, 3)) crop_rect = CropRect({'type': 'crop_rect'}) assert np.array_equal( expected_image, crop_rect(DataRepresentation(image), { 'rect': [20, 0, 40, 50] }).data)
def test_point_alignment_not_provided_points_im_meta(self): input_image = np.ones((100, 50, 3)) point_aligner = PointAligner({ 'type': 'point_alignment', 'dst_width': 100, 'dst_height': 100 }) result = point_aligner(DataRepresentation(input_image), {}).data assert result.shape == (100, 50, 3)
def test_fill_non_constant_input_with_specific_mapping_batch_1(self): input_feeder = InputFeeder( [{ 'name': 'input', 'type': 'INPUT', 'value': '.' }], {'input': InputInfo_test(shape=(1, 3, 10, 10))}) result = input_feeder.fill_non_constant_inputs( [DataRepresentation(np.zeros((10, 10, 3)), identifier='0')])[0] expected_data = np.zeros((1, 3, 10, 10)) assert 'input' in result assert np.array_equal(result['input'], expected_data)
def test_resize_save_aspect_ratio_width(self): input_image = np.ones((100, 50, 3)) resize = Preprocessor.provide( 'resize', { 'type': 'resize', 'dst_width': 150, 'dst_height': 150, 'aspect_ratio_scale': 'width' }) result = resize(DataRepresentation(input_image)).data assert result.shape == (150, 75, 3)
def test_custom_normalization_with_std(self): name = 'mock_preprocessor' config = {'type': 'normalization', 'std': '(1, 2, 3)'} normalization = Preprocessor.provide('normalization', config, name) input = np.full_like((3, 300, 300), 100) input_ref = input.copy() / (1, 2, 3) res = normalization(DataRepresentation(input)) assert normalization.mean is None assert normalization.std == (1, 2, 3) assert np.all(input_ref == res.data)
def test_resize_save_aspect_ratio_for_greater_dim(self): name = 'mock_preprocessor' config = { 'type': 'resize', 'dst_width': 100, 'dst_height': 150, 'aspect_ratio_scale': 'greater' } input_image = np.ones((100, 50, 3)) resize = Preprocessor.provide('resize', config, name) result = resize(DataRepresentation(input_image)).data assert result.shape == (300, 100, 3)
def test_extend_around_rect_with_negative_augmentation(self): image = np.random.randint(0, 255, (30, 40, 3)).astype(np.uint8) expected_image = image extend_image_around_rect = ExtendAroundRect({ 'type': 'extend_around_rect', 'augmentation_param': -0.5 }) assert np.array_equal( expected_image, extend_image_around_rect(DataRepresentation(image), { 'rect': [20, 0, 40, 30] }).data)
def test_custom_normalization_with_mean(self): normalization = Preprocessor.provide('normalization', { 'type': 'normalization', 'mean': '(1, 2, 3)' }) source = np.full_like((3, 300, 300), 100) input_ref = source.copy() - (1, 2, 3) result = normalization(DataRepresentation(source)) assert normalization.mean == (1, 2, 3) assert normalization.std is None assert np.all(input_ref == result.data) assert result.metadata == {'image_size': (3, )}
def test_custom_normalization_with_mean_and_std(self): name = 'mock_preprocessor' config = {'type': 'normalization', 'mean': '(1, 2, 3)', 'std': '(4, 5, 6)'} normalization = Preprocessor.provide('normalization', config, name) input_ = np.full_like((3, 300, 300), 100) input_ref = (input_ - (1, 2, 3)) / (4, 5, 6) res = normalization(DataRepresentation(input_)) assert normalization.mean == (1, 2, 3) assert normalization.std == (4, 5, 6) assert np.all(input_ref == res.data) assert res.metadata == {}
def test_default_normalization(self): name = 'mock_preprocessor' config = {'type': 'normalization'} normalization = Preprocessor.provide('normalization', config, name) input = np.full_like((3, 300, 300), 100) input_copy = input.copy() res = normalization(DataRepresentation(input)) assert normalization.mean is None assert normalization.std is None assert np.all(input_copy == res.data) assert res.metadata == {}
def test_extend_around_rect_more_image_size_coordinates_of_rect(self): image = np.random.randint(0, 255, (30, 40, 3)).astype(np.uint8) expected_image = cv2.copyMakeBorder(image, int(15.5), int(31), int(0), int(11), cv2.BORDER_REPLICATE) extend_image_around_rect = ExtendAroundRect({ 'type': 'extend_around_rect', 'augmentation_param': 0.5 }) assert np.array_equal( expected_image, extend_image_around_rect(DataRepresentation(image), { 'rect': [20, 0, 40, 50] }).data)
def test_custom_normalization_with_std_as_scalar(self): normalization = Preprocessor.provide('normalization', { 'type': 'normalization', 'std': '2' }) source = np.full_like((3, 300, 300), 100) input_ref = source.copy() / 2 result = normalization(DataRepresentation(source)) assert normalization.mean is None assert normalization.std == (2.0, ) assert np.all(input_ref == result.data) assert result.metadata == {'image_size': (3, )}
def test_crop_with_both_provided_size_and_dst_height_dst_width_warn(self): image = np.zeros((30, 40, 3)) with pytest.warns(None) as warnings: crop = Crop({ 'dst_width': 100, 'dst_height': 100, 'size': 200, 'type': 'crop' }) assert len(warnings) == 1 result = crop.process(DataRepresentation(image)) assert result.data.shape == (200, 200, 3) assert result.metadata == {'image_size': (30, 40, 3)}
def test_default_resize(self, mocker): cv2_resize_mock = mocker.patch('accuracy_checker.preprocessor.preprocessors.cv2.resize') name = 'mock_preprocessor' config = {'type': 'resize', 'size': 200} resize = Preprocessor.provide('resize', config, name) input_mock = mocker.Mock() resize(DataRepresentation(input_mock)) assert not resize.use_pil assert resize.dst_width == 200 assert resize.dst_height == 200 cv2_resize_mock.assert_called_once_with(input_mock, (200, 200), interpolation=Resize.OPENCV_INTERPOLATION['LINEAR'])