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) expoected_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 expoected_meta.items(): assert key in input_rep.metadata assert input_rep.metadata[key] == value
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_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_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_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_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_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_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_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_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(input) assert normalization.mean is None assert normalization.std is None assert np.all(input_copy == res)
def test_default_resize(self, mocker): cv2_resize_mock = mocker.patch('accuracy_checker.preprocessor.geometric_transformations.cv2.resize') resize = Preprocessor.provide('resize', {'type': 'resize', 'size': 200}) input_mock = mocker.Mock() input_mock.shape = (480, 640, 3) 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), interpolation=_OpenCVResizer.supported_interpolations()['LINEAR'] )
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_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_custom_normalization_with_mean(self): name = 'mock_preprocessor' config = {'type': 'normalization', 'mean': '(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(input) assert normalization.mean == (1, 2, 3) assert normalization.std is None assert np.all(input_ref == res)
def test_custom_normalization_with_mean_and_std(self): normalization = Preprocessor.provide( 'normalization', {'type': 'normalization', 'mean': '(1, 2, 3)', 'std': '(4, 5, 6)'} ) input_ = np.full_like((3, 300, 300), 100) input_ref = (input_ - (1, 2, 3)) / (4, 5, 6) result = normalization(DataRepresentation(input_)) assert normalization.mean == (1, 2, 3) assert normalization.std == (4, 5, 6) 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_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'])
def test_custom_normalization_multi_input(self): normalization = Preprocessor.provide('normalization', { 'type': 'normalization', 'mean': '2', 'std': '2' }) source = np.full_like((300, 300, 3), 100) input_ref = (source.copy() - 2) / 2 result = normalization(DataRepresentation([source, source])) assert normalization.mean == (2, ) assert normalization.std == (2, ) assert len(result.data) == 2 assert np.all(input_ref == result.data[0]) assert np.all(input_ref == result.data[1]) assert result.metadata == {'image_size': (3, )}
def test_custom_resize(self, mocker): cv2_resize_mock = mocker.patch('accuracy_checker.preprocessor.geometric_transformations.cv2.resize') resize = Preprocessor.provide( 'resize', {'type': 'resize', 'dst_width': 126, 'dst_height': 128, 'interpolation': 'CUBIC'} ) input_mock = mocker.Mock() input_mock.shape = (480, 640, 3) resize(DataRepresentation(input_mock)) assert resize.dst_width == 126 assert resize.dst_height == 128 cv2_resize_mock.assert_called_once_with( input_mock, (126, 128), interpolation=_OpenCVResizer.supported_interpolations()['CUBIC'] )
def test_custom_resize(self, mocker): cv2_resize_mock = mocker.patch('accuracy_checker.preprocessor.preprocessors.cv2.resize') name = 'mock_preprocessor' config = { 'type': 'resize', 'dst_width': 126, 'dst_height': 128, 'interpolation': 'CUBIC' } resize = Preprocessor.provide('resize', config, name) input_mock = mocker.Mock() resize(DataRepresentation(input_mock)) assert not resize.use_pil assert resize.dst_width == 126 assert resize.dst_height == 128 cv2_resize_mock.assert_called_once_with(input_mock, (126, 128), interpolation=Resize.OPENCV_INTERPOLATION['CUBIC'])
def test_normalization_with_zero_as_std_value_raise_config_error(self): with pytest.raises(ConfigError): Preprocessor.provide('normalization', { 'type': 'normalization', 'std': '0' })
def test_normalization_without_mean_and_std_raise_config_error(self): with pytest.raises(ConfigError): Preprocessor.provide('normalization', {'type': 'normalization'})
def test_auto_resize_empty_input_shapes_raise_config_error(self): with pytest.raises(ConfigError): Preprocessor.provide('auto_resize', { 'type': 'auto_resize' }).set_input_shape({})
def test_auto_resize_with_non_image_input_raise_config_error(self): with pytest.raises(ConfigError): Preprocessor.provide('auto_resize', { 'type': 'auto_resize' }).set_input_shape({'im_info': [200, 200, 1]})
def test_auto_resize_input_shape_not_provided_raise_config_error( self, mocker): input_mock = mocker.Mock() with pytest.raises(ConfigError): Preprocessor.provide('auto_resize', {'type': 'auto_resize'})( DataRepresentation(input_mock))
def test_resize_provided_only_dst_width_raise_config_error(self): with pytest.raises(ValueError): Preprocessor.provide('resize', { 'type': 'resize', 'dst_width': 100 })
def test_resize_to_negative_size_raise_config_error(self): with pytest.raises(ConfigError): Preprocessor.provide('resize', {'type': 'resize', 'size': -100})
def test_point_alignment_raise_config_error_on_extra_args(self): with pytest.raises(ConfigError): Preprocessor.provide('point_alignment', { 'type': 'point_alignment', 'something_extra': 'extra' })
def test_extend_around_rect_raise_config_error_on_extra_args(self): with pytest.raises(ConfigError): Preprocessor.provide('extend_around_rect', { 'type': 'extend_around_rect', 'something_extra': 'extra' })