"torchvision/csrc/roi_align.cpp" did not exist on "bb88c4520b835e79d5d3c4423eb7ff7c26fa2043"
test_data_transforms_crop.py 8.45 KB
Newer Older
facebook-github-bot's avatar
facebook-github-bot committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved


import unittest

import d2go.data.transforms.box_utils as bu
import numpy as np
import torch
from d2go.data.transforms import crop as tf_crop


class TestDataTransformsCrop(unittest.TestCase):
    def test_transform_crop_extent_transform(self):
        img_wh = (16, 11)

        sem_seg = np.zeros([img_wh[1], img_wh[0]], dtype=np.uint8)
        # h, w
        sem_seg[5, 4] = 1
        sem_seg[10, 13] = 1
        sem_seg[5:11, 4:14] = 1

        # src_rect: [x0, y0, x1, y1] in pixel coordinate, output_size: [h, w]
        trans = tf_crop.ExtentTransform(src_rect=[4, 5, 14, 11], output_size=[6, 10])
        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, torch.Tensor([6, 10]))
        self.assertArrayEqual(np.unique(out_mask), torch.Tensor([1]))

        trans = tf_crop.ExtentTransform(src_rect=[3, 4, 15, 11], output_size=[7, 12])
        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, torch.Tensor([7, 12]))
        self.assertArrayEqual(np.unique(out_mask), torch.Tensor([0, 1]))
        self.assertArrayEqual(np.unique(out_mask[1:, 1:-1]), torch.Tensor([1]))
        self.assertEqual(out_mask[:, 0].sum(), 0)
        self.assertArrayEqual(out_mask[0, :].sum(), 0)
        self.assertArrayEqual(out_mask[:, -1].sum(), 0)

    def test_transform_crop_random_crop_fixed_aspect_ratio(self):
        aug = tf_crop.RandomCropFixedAspectRatio([1.0 / 2])
        img_wh = (16, 11)

        img = np.ones([img_wh[1], img_wh[0], 3], dtype=np.uint8)
        sem_seg = np.zeros([img_wh[1], img_wh[0]], dtype=np.uint8)
        sem_seg[5, 4] = 1
        sem_seg[10, 13] = 1
        mask_xywh = bu.get_box_from_mask(sem_seg)
        self.assertArrayEqual(mask_xywh, torch.Tensor([4, 5, 10, 6]))

        trans = aug.get_transform(img, sem_seg)
        self.assertArrayEqual(trans.src_rect, torch.Tensor([4, -2, 14, 18]))
        self.assertArrayEqual(trans.output_size, torch.Tensor([20, 10]))

        out_img = trans.apply_image(img)
        self.assertArrayEqual(out_img.shape, torch.Tensor([20, 10, 3]))

        self.assertArrayEqual(np.unique(out_img[2:13, :, :]), torch.Tensor([1]))
        self.assertArrayEqual(np.unique(out_img[0:2, :, :]), torch.Tensor([0]))
        self.assertArrayEqual(np.unique(out_img[13:, :, :]), torch.Tensor([0]))

        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, torch.Tensor([20, 10]))
        self.assertEqual(out_mask[7, 0], 1)
        self.assertEqual(out_mask[12, -1], 1)

    def test_transform_crop_random_crop_fixed_aspect_ratio_scale_offset(self):
        aug = tf_crop.RandomCropFixedAspectRatio(
            [1.0 / 2], scale_range=[0.5, 0.5], offset_scale_range=[-0.5, -0.5]
        )
        img_wh = (16, 11)

        img = np.ones([img_wh[1], img_wh[0], 3], dtype=np.uint8)
        sem_seg = np.zeros([img_wh[1], img_wh[0]], dtype=np.uint8)
        sem_seg[5, 4] = 1
        sem_seg[10, 13] = 1
        sem_seg[5:11, 4:14] = 1
        mask_xywh = bu.get_box_from_mask(sem_seg)
        self.assertArrayEqual(mask_xywh, torch.Tensor([4, 5, 10, 6]))

        trans = aug.get_transform(img, sem_seg)
        self.assertArrayEqual(trans.src_rect, torch.Tensor([1.5, 0.0, 6.5, 10.0]))
        self.assertArrayEqual(trans.output_size, torch.Tensor([10, 5]))

        out_img = trans.apply_image(img)
        self.assertArrayEqual(out_img.shape, torch.Tensor([10, 5, 3]))
        self.assertEqual(np.unique(out_img), 1)

        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, torch.Tensor([10, 5]))
        self.assertEqual(np.unique(out_mask[6:, 3:]), 1)

    def test_transform_crop_random_crop_fixed_aspect_ratio_empty_mask(self):
        """The sem_mask is empty (the whole image is background)"""
        aug = tf_crop.RandomCropFixedAspectRatio([1.0 / 2])
        img_wh = (16, 11)

        img = np.ones([img_wh[1], img_wh[0], 3], dtype=np.uint8)
        sem_seg = np.zeros([img_wh[1], img_wh[0]], dtype=np.uint8)

        mask_xywh = bu.get_box_from_mask(sem_seg)
        self.assertEqual(mask_xywh, None)

        trans = aug.get_transform(img, sem_seg)
        self.assertIsInstance(trans, tf_crop.NoOpTransform)

        out_img = trans.apply_image(img)
        self.assertArrayEqual(out_img.shape, img.shape)

        out_mask = trans.apply_segmentation(sem_seg)
        self.assertArrayEqual(out_mask.shape, sem_seg.shape)

    def test_pad_transform(self):
        crop_w, crop_h = 4, 3
        full_w, full_h = 11, 9
        crop_x, crop_y = 5, 6
        trans = tf_crop.PadTransform(crop_x, crop_y, crop_w, crop_h, full_w, full_h)
        img = np.ones([crop_h, crop_w])
        trans_img = trans.apply_image(img)
        self.assertArrayEqual(trans_img.shape, [full_h, full_w])
        self.assertArrayEqual(np.unique(trans_img), [0, 1])
        full_img_gt = np.zeros([full_h, full_w])
        full_img_gt[crop_y : (crop_y + crop_h), crop_x : (crop_x + crop_w)] = 1
        self.assertArrayEqual(full_img_gt, trans_img)

    def test_crop_transform_inverse(self):
        crop_w, crop_h = 4, 3
        full_w, full_h = 11, 9
        crop_x, crop_y = 5, 6
        trans = tf_crop.InvertibleCropTransform(
            crop_x, crop_y, crop_w, crop_h, full_w, full_h
        )

        full_img_gt = np.zeros([full_h, full_w])
        full_img_gt[crop_y : (crop_y + crop_h), crop_x : (crop_x + crop_w)] = 1
        crop_img_gt = np.ones([crop_h, crop_w])

        self.assertArrayEqual(trans.apply_image(full_img_gt), crop_img_gt)
        self.assertArrayEqual(trans.inverse().apply_image(crop_img_gt), full_img_gt)
        self.assertArrayEqual(
            trans.inverse().inverse().apply_image(full_img_gt), crop_img_gt
        )

    def test_pad_transform(self):
        img_h, img_w = 10, 7
        divisibility = 8

        aug = tf_crop.PadBorderDivisible(divisibility)

        img = np.ones([img_h, img_w, 3]) * 3
        trans = aug.get_transform(img)
        pad_img = trans.apply_image(img)
        self.assertEqual(pad_img.shape, (16, 8, 3))
        inverse_img = trans.inverse().apply_image(pad_img)
        self.assertEqual(inverse_img.shape, (10, 7, 3))
        self.assertArrayEqual(img, inverse_img)

        mask = np.ones([img_h, img_w]) * 2
        pad_mask = trans.apply_segmentation(mask)
        self.assertEqual(pad_mask.shape, (16, 8))
        inverse_mask = trans.inverse().apply_segmentation(pad_mask)
        self.assertEqual(inverse_mask.shape, (10, 7))
        self.assertArrayEqual(mask, inverse_mask)

    def test_random_instance_crop(self):
        from detectron2.data import detection_utils as du
        from detectron2.data.transforms.augmentation import (
            AugInput,
            AugmentationList,
        )
        from detectron2.structures import BoxMode

        aug = tf_crop.RandomInstanceCrop([1.0, 1.0])

        img_w, img_h = 10, 7
        annotations = [
            {
                "category_id": 0,
                "bbox": [1, 1, 4, 3],
                "bbox_mode": BoxMode.XYWH_ABS,
            },
            {
                "category_id": 0,
                "bbox": [2, 2, 4, 3],
                "bbox_mode": BoxMode.XYWH_ABS,
            },
            {
                "category_id": 0,
                "bbox": [6, 5, 3, 2],
                "bbox_mode": BoxMode.XYWH_ABS,
            },
        ]

        img = np.ones([img_h, img_w, 3]) * 3

        inputs = AugInput(image=img)
        # pass additional arguments
        inputs.annotations = annotations
        transforms = AugmentationList([aug])(inputs)

        self.assertIn(
            inputs.image.shape, [torch.Size([3, 4, 3]), torch.Size([2, 3, 3])]
        )

        # from dataset mapper unused annotations will be filtered out due to the
        #  iscrowd flag
        image_shape = inputs.image.shape[:2]
        annos = [
            du.transform_instance_annotations(
                obj,
                transforms,
                image_shape,
            )
            for obj in annotations
            if obj.get("iscrowd", 0) == 0
        ]
        instances = du.annotations_to_instances(annos, image_shape)
        filtered_instances = du.filter_empty_instances(instances)
        self.assertEqual(len(filtered_instances), 1)
        self.assertArrayEqual(
            filtered_instances.gt_boxes.tensor.tolist(),
            [[0, 0, image_shape[1], image_shape[0]]],
        )

    def assertArrayEqual(self, a1, a2):
        self.assertTrue(np.array_equal(a1, a2))