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_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_with_specific_mapping_batch_2(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'), 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_inputs_without_specific_mapping_batch_2(self): input_feeder = InputFeeder([], {'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)), identifier='0'), DataRepresentation(np.zeros((10, 10, 3)), identifier='1') ])[0] expected_data = np.zeros((2, 3, 10, 10)) assert 'input1' in result assert np.array_equal(result['input1'], expected_data) assert 'input2' in result assert np.array_equal(result['input2'], expected_data)
def test_resize_save_aspect_ratio_frcnn_keep_aspect_ratio(self): input_image = np.ones((480, 640, 3)) resize = Preprocessor.provide('resize', { 'type': 'resize', 'dst_width': 100, 'dst_height': 150, 'aspect_ratio_scale': 'frcnn_keep_aspect_ratio' }) result = resize(DataRepresentation(input_image)) assert result.data.shape == (100, 133, 3) assert result.metadata == { 'geometric_operations': [ GeometricOperationMetadata( type='resize', parameters={ 'scale_x': 0.2078125, 'scale_y': 0.20833333333333334, 'image_info': [100, 133, 1], 'original_width': 640, 'original_height': 480, 'preferable_width': 133, 'preferable_height': 150 } ) ], 'image_info': [100, 133, 1], 'image_size': (480, 640, 3), 'original_height': 480, 'original_width': 640, 'preferable_height': 150, 'preferable_width': 133, 'scale_x': 0.2078125, 'scale_y': 0.20833333333333334 }
def test_mxnet_launcher_provide_input_shape_to_adapter( self, mocker, models_dir): mocker.patch('mxnet.mod.Module.forward', return_value={'fc3': 0}) launcher = get_mx_test_model(models_dir) zeros = DataRepresentation(np.zeros((1, 3, 32, 32))) launcher.predict([{'data': zeros.data}], [zeros.metadata]) assert zeros.metadata['input_shape'] == {'data': (1, 3, 32, 32)}
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_default_extend_around_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), {'rect': [20, 0, 40, 30]}).data )
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_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_default_auto_resize(self, mocker): cv2_resize_mock = mocker.patch( 'accuracy_checker.preprocessor.geometric_transformations.cv2.resize' ) resize = Preprocessor.provide('auto_resize', {'type': 'auto_resize'}) resize.set_input_shape({'data': (1, 3, 200, 200)}) input_data = np.zeros((100, 100, 3)) input_rep = DataRepresentation(input_data) expected_meta = { 'preferable_width': 200, 'preferable_height': 200, 'image_info': [200, 200, 1], 'scale_x': 2.0, 'scale_y': 2.0, 'original_width': 100, 'original_height': 100, } resize(input_rep) assert resize.dst_width == 200 assert resize.dst_height == 200 cv2_resize_mock.assert_called_once_with(input_data, (200, 200)) for key, value in expected_meta.items(): assert key in input_rep.metadata assert input_rep.metadata[key] == value
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_point_alignment_both_provided_size_and_dst_height_dst_width_warn(self): input_image = np.ones((100, 50, 3)) with pytest.warns(None) as warnings: point_aligner = PointAligner({'type': 'point_alignment', 'dst_width': 100, 'dst_height': 100, 'size': 200}) assert len(warnings) == 1 result = point_aligner(DataRepresentation(input_image), {}).data assert result.shape == (100, 50, 3)
def test_crop_central_fraction_non_symmetric(self): crop = Crop({'central_fraction': 0.5, 'type': 'crop'}) image = np.zeros((80, 40, 3)) image_rep = crop(DataRepresentation(image)) assert image_rep.data.shape == (40, 20, 3) assert image_rep.metadata == {'image_size': (80, 40, 3), 'geometric_operations': [GeometricOperationMetadata(type='crop', parameters={})]}
def test_crop_less_non_symmetric(self): 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 == {'image_size': (30, 40, 3), 'geometric_operations': [GeometricOperationMetadata(type='crop', parameters={})]}
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), 'geometric_operations': [GeometricOperationMetadata(type='crop', parameters={})]}
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_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), 'geometric_operations': [GeometricOperationMetadata(type='crop', parameters={})]}
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_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_resize_with_both_provided_size_and_dst_height_dst_width_warn(self): input_image = np.ones((100, 50, 3)) with pytest.warns(None) as warnings: resize = Preprocessor.provide( 'resize', {'type': 'resize', 'dst_width': 100, 'dst_height': 100, 'size': 200} ) assert len(warnings) == 1 result = resize(DataRepresentation(input_image)).data assert result.shape == (200, 200, 3)
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_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_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_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_default_auto_resize(self, mocker): cv2_resize_mock = mocker.patch('accuracy_checker.preprocessor.geometric_transformations.cv2.resize') resize = Preprocessor.provide('auto_resize', {'type': 'auto_resize'}) resize.set_input_shape({'data': (1, 3, 200, 200)}) input_mock = mocker.Mock() resize(DataRepresentation(input_mock)) assert resize.dst_width == 200 assert resize.dst_height == 200 cv2_resize_mock.assert_called_once_with(input_mock, (200, 200))
def test_custom_normalization_with_precomputed_std(self): normalization = Preprocessor.provide('normalization', {'type': 'normalization', 'std': 'cifar10'}) source = np.full_like((3, 300, 300), 100) input_ref = source.copy() / normalization.PRECOMPUTED_STDS['cifar10'] result = normalization(DataRepresentation(source)) assert normalization.mean is None assert normalization.std == normalization.PRECOMPUTED_STDS['cifar10'] assert np.all(input_ref == result.data) assert result.metadata == {'image_size': (3,)}
def test_custom_normalization_with_mean_and_std_as_scalars(self): normalization = Preprocessor.provide('normalization', {'type': 'normalization', 'mean': '2', 'std': '5'}) input_ = np.full_like((3, 300, 300), 100) input_ref = (input_ - (2, )) / (5, ) result = normalization(DataRepresentation(input_)) assert normalization.mean == (2, ) assert normalization.std == (5, ) assert np.all(input_ref == result.data) assert result.metadata == {'image_size': (3,)}
def test_fill_non_constant_input_and_input_info_with_one_input_without_specific_mapping_batch_1(self): input_feeder = InputFeeder( [{'name': 'info', 'type': 'IMAGE_INFO'}], {'input': InputInfo_test(shape=(1, 3, 10, 10)), 'info': InputInfo_test(shape=(1, 3))} ) 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) assert 'info' in result assert np.array_equal(result['info'], np.array([[10, 10, 1]]))