def gen_crop_transform_with_instance(crop_size, image_size, instances, crop_box=True): """ Generate a CropTransform so that the cropping region contains the center of the given instance. Args: crop_size (tuple): h, w in pixels image_size (tuple): h, w instance (dict): an annotation dict of one instance, in Detectron2's dataset format. """ instance = (np.random.choice(instances), ) instance = instance[0] crop_size = np.asarray(crop_size, dtype=np.int32) bbox = BoxMode.convert(instance["bbox"], instance["bbox_mode"], BoxMode.XYXY_ABS) center_yx = (bbox[1] + bbox[3]) * 0.5, (bbox[0] + bbox[2]) * 0.5 # print(image_size[0], center_yx[0], image_size[1], center_yx[1]) # if not (image_size[0] >= center_yx[0] and image_size[1] >= center_yx[1]): # center_yx = (bbox[1] + bbox[3]) * 0.1, (bbox[0] + bbox[2]) * 0.1 assert (image_size[0] >= center_yx[0] and image_size[1] >= center_yx[1] ), "The annotation bounding box is outside of the image!" assert (image_size[0] >= crop_size[0] and image_size[1] >= crop_size[1] ), "Crop size is larger than image size!" min_yx = np.maximum(np.floor(center_yx).astype(np.int32) - crop_size, 0) max_yx = np.maximum(np.asarray(image_size, dtype=np.int32) - crop_size, 0) max_yx = np.minimum(max_yx, np.ceil(center_yx).astype(np.int32)) y0 = np.random.randint(min_yx[0], max_yx[0] + 1) x0 = np.random.randint(min_yx[1], max_yx[1] + 1) # if some instance is cropped extend the box if not crop_box: num_modifications = 0 modified = True # convert crop_size to float crop_size = crop_size.astype(np.float32) while modified: modified, x0, y0, crop_size = adjust_crop(x0, y0, crop_size, instances) num_modifications += 1 if num_modifications > 100: raise ValueError( "Cannot finished cropping adjustment within 100 tries (#instances {})." .format(len(instances))) return T.CropTransform(0, 0, image_size[1], image_size[0]) return T.CropTransform(*map(int, (x0, y0, crop_size[1], crop_size[0])))
def test_register(self): """ Test register. """ dtype = "int" def add1(t, x): return x + 1 def flip_sub_width(t, x): return x - t.width T.Transform.register_type(dtype, add1) T.HFlipTransform.register_type(dtype, flip_sub_width) transforms = T.TransformList([ T.ScaleTransform(0, 0, 0, 0, 0), T.CropTransform(0, 0, 0, 0), T.HFlipTransform(3), ]) self.assertEqual(transforms.apply_int(3), 2) # Testing __add__, __iadd__, __radd__, __len__. transforms = transforms + transforms transforms += transforms transforms = T.NoOpTransform() + transforms self.assertEqual(len(transforms), 13) with self.assertRaises(AssertionError): T.HFlipTransform.register_type(dtype, lambda x: 1) with self.assertRaises(AttributeError): transforms.no_existing
def test_crop_invalid_polygons(self): # Ensure that invalid polygons are skipped. transform = T.CropTransform(3, 4, 20, 20) polygon = [ (0, 0), (0, 3), (3, 3), (3, 0), (2, 0), (2, 2), (1, 2), (1, 1), (2, 1), (2, 0), (0, 0), ] cropped_polygons = transform.apply_polygons([polygon]) self.assertEqual(0, len(cropped_polygons))
def test_register(self): """ Test register. """ dtype = "int" def add1(t, x): return x + 1 def flip_sub_width(t, x): return x - t.width T.Transform.register_type(dtype, add1) T.HFlipTransform.register_type(dtype, flip_sub_width) transforms = T.TransformList([ T.ScaleTransform(0, 0, 0, 0, 0), T.CropTransform(0, 0, 0, 0), T.HFlipTransform(3), ]) self.assertEqual(transforms.apply_int(3), 2) with self.assertRaises(AssertionError): T.HFlipTransform.register_type(dtype, lambda x: 1)
def test_transformlist_flatten(self): t0 = T.HFlipTransform(width=100) t1 = T.ScaleTransform(3, 4, 5, 6) t2 = T.CropTransform(4, 5, 6, 7) t = T.TransformList([T.TransformList([t0, t1]), t2]) self.assertEqual(len(t.transforms), 3)