def test_random_resized_crop_lazy(self): target_keys = ['imgs', 'crop_bbox', 'img_shape', 'lazy'] # There will be a slight difference because of rounding eps = 0.01 imgs = list(np.random.rand(2, 256, 341, 3)) results = dict(imgs=imgs) with pytest.raises(AssertionError): # area_range[0] > area_range[1], which is wrong random_crop = RandomResizedCrop(area_range=(0.9, 0.7), lazy=True) random_crop(results) with pytest.raises(AssertionError): # 0 > area_range[0] and area_range[1] > 1, which is wrong random_crop = RandomResizedCrop(aspect_ratio_range=(-0.1, 2.0), lazy=True) random_crop(results) random_crop = RandomResizedCrop(lazy=True) random_crop_result = random_crop(results) assert assert_dict_has_keys(random_crop_result, target_keys) assert id(imgs) == id(random_crop_result['imgs']) random_crop_result_fuse = Fuse()(random_crop_result) assert check_crop(imgs, random_crop_result_fuse['imgs'], results['crop_bbox']) h, w = random_crop_result['img_shape'] assert ((0.08 - eps <= h * w / 256 / 341) and (h * w / 256 / 341 <= 1 + eps)) assert (3. / 4. - eps <= h / w) and (h / w - eps <= 4. / 3.) assert repr(random_crop) == (f'{random_crop.__class__.__name__}' f'(area_range={(0.08, 1.0)}, ' f'aspect_ratio_range={(3 / 4, 4 / 3)}, ' f'lazy={True})') random_crop = RandomResizedCrop(area_range=(0.9, 0.9), aspect_ratio_range=(10.0, 10.1), lazy=True) # Test fallback cases by very big area range imgs = np.random.rand(2, 256, 341, 3) results = dict(imgs=imgs) random_crop_result = random_crop(results) assert assert_dict_has_keys(random_crop_result, target_keys) assert id(imgs) == id(random_crop_result['imgs']) random_crop_result_fuse = Fuse()(random_crop_result) assert check_crop(imgs, random_crop_result_fuse['imgs'], results['crop_bbox']) h, w = random_crop_result['img_shape'] assert h == w == 256
def test_random_resized_crop(): with pytest.raises(TypeError): # area_range must be a tuple of float RandomResizedCrop(area_range=0.5) with pytest.raises(TypeError): # aspect_ratio_range must be a tuple of float RandomResizedCrop(area_range=(0.08, 1.0), aspect_ratio_range=0.1) target_keys = ['imgs', 'crop_bbox', 'img_shape'] # There will be a slight difference because of rounding eps = 0.01 imgs = list(np.random.rand(2, 256, 341, 3)) results = dict(imgs=imgs) results['gt_bboxes'] = np.array([[0, 0, 340, 256]]) results['proposals'] = np.array([[0, 0, 340, 256]]) kp = np.array([[160, 120], [160, 120]]).reshape([1, 1, 2, 2]) results['keypoint'] = kp with pytest.raises(AssertionError): # area_range[0] > area_range[1], which is wrong random_crop = RandomResizedCrop(area_range=(0.9, 0.7)) random_crop(results) with pytest.raises(AssertionError): # 0 > area_range[0] and area_range[1] > 1, which is wrong random_crop = RandomResizedCrop(aspect_ratio_range=(-0.1, 2.0)) random_crop(results) random_crop = RandomResizedCrop() random_crop_result = random_crop(results) assert assert_dict_has_keys(random_crop_result, target_keys) assert check_crop(imgs, random_crop_result['imgs'], results['crop_bbox']) h, w = random_crop_result['img_shape'] assert ((0.08 - eps <= h * w / 256 / 341) and (h * w / 256 / 341 <= 1 + eps)) assert (3. / 4. - eps <= h / w) and (h / w - eps <= 4. / 3.) assert repr(random_crop) == (f'{random_crop.__class__.__name__}' f'(area_range={(0.08, 1.0)}, ' f'aspect_ratio_range={(3 / 4, 4 / 3)}, ' f'lazy={False})') random_crop = RandomResizedCrop(area_range=(0.9, 0.9), aspect_ratio_range=(10.0, 10.1)) # Test fallback cases by very big area range imgs = list(np.random.rand(2, 256, 341, 3)) results = dict(imgs=imgs) random_crop_result = random_crop(results) assert assert_dict_has_keys(random_crop_result, target_keys) assert check_crop(imgs, random_crop_result['imgs'], results['crop_bbox']) h, w = random_crop_result['img_shape'] assert h == w == 256