test_image_processing_detr.py 13.6 KB
Newer Older
NielsRogge's avatar
NielsRogge committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# coding=utf-8
# Copyright 2021 HuggingFace Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


import json
import pathlib
import unittest

from transformers.testing_utils import require_torch, require_vision, slow
22
from transformers.utils import is_torch_available, is_vision_available
NielsRogge's avatar
NielsRogge committed
23

24
from ...test_image_processing_common import AnnotationFormatTestMixin, ImageProcessingTestMixin, prepare_image_inputs
NielsRogge's avatar
NielsRogge committed
25
26
27
28
29
30
31
32


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

33
    from transformers import DetrImageProcessor
NielsRogge's avatar
NielsRogge committed
34
35


36
class DetrImageProcessingTester(unittest.TestCase):
NielsRogge's avatar
NielsRogge committed
37
38
39
40
41
42
43
44
    def __init__(
        self,
        parent,
        batch_size=7,
        num_channels=3,
        min_resolution=30,
        max_resolution=400,
        do_resize=True,
45
46
47
        size=None,
        do_rescale=True,
        rescale_factor=1 / 255,
NielsRogge's avatar
NielsRogge committed
48
49
50
        do_normalize=True,
        image_mean=[0.5, 0.5, 0.5],
        image_std=[0.5, 0.5, 0.5],
51
        do_pad=True,
NielsRogge's avatar
NielsRogge committed
52
    ):
53
54
        # by setting size["longest_edge"] > max_resolution we're effectively not testing this :p
        size = size if size is not None else {"shortest_edge": 18, "longest_edge": 1333}
NielsRogge's avatar
NielsRogge committed
55
56
57
58
59
60
61
        self.parent = parent
        self.batch_size = batch_size
        self.num_channels = num_channels
        self.min_resolution = min_resolution
        self.max_resolution = max_resolution
        self.do_resize = do_resize
        self.size = size
62
63
        self.do_rescale = do_rescale
        self.rescale_factor = rescale_factor
NielsRogge's avatar
NielsRogge committed
64
65
66
        self.do_normalize = do_normalize
        self.image_mean = image_mean
        self.image_std = image_std
67
        self.do_pad = do_pad
NielsRogge's avatar
NielsRogge committed
68

69
    def prepare_image_processor_dict(self):
NielsRogge's avatar
NielsRogge committed
70
71
72
        return {
            "do_resize": self.do_resize,
            "size": self.size,
73
74
            "do_rescale": self.do_rescale,
            "rescale_factor": self.rescale_factor,
NielsRogge's avatar
NielsRogge committed
75
76
77
            "do_normalize": self.do_normalize,
            "image_mean": self.image_mean,
            "image_std": self.image_std,
78
            "do_pad": self.do_pad,
NielsRogge's avatar
NielsRogge committed
79
80
81
82
        }

    def get_expected_values(self, image_inputs, batched=False):
        """
83
        This function computes the expected height and width when providing images to DetrImageProcessor,
NielsRogge's avatar
NielsRogge committed
84
85
86
87
88
89
90
91
92
        assuming do_resize is set to True with a scalar size.
        """
        if not batched:
            image = image_inputs[0]
            if isinstance(image, Image.Image):
                w, h = image.size
            else:
                h, w = image.shape[1], image.shape[2]
            if w < h:
93
94
                expected_height = int(self.size["shortest_edge"] * h / w)
                expected_width = self.size["shortest_edge"]
NielsRogge's avatar
NielsRogge committed
95
            elif w > h:
96
97
                expected_height = self.size["shortest_edge"]
                expected_width = int(self.size["shortest_edge"] * w / h)
NielsRogge's avatar
NielsRogge committed
98
            else:
99
100
                expected_height = self.size["shortest_edge"]
                expected_width = self.size["shortest_edge"]
NielsRogge's avatar
NielsRogge committed
101
102
103
104
105
106
107
108
109
110
111

        else:
            expected_values = []
            for image in image_inputs:
                expected_height, expected_width = self.get_expected_values([image])
                expected_values.append((expected_height, expected_width))
            expected_height = max(expected_values, key=lambda item: item[0])[0]
            expected_width = max(expected_values, key=lambda item: item[1])[1]

        return expected_height, expected_width

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    def expected_output_image_shape(self, images):
        height, width = self.get_expected_values(images, batched=True)
        return self.num_channels, height, width

    def prepare_image_inputs(self, equal_resolution=False, numpify=False, torchify=False):
        return prepare_image_inputs(
            batch_size=self.batch_size,
            num_channels=self.num_channels,
            min_resolution=self.min_resolution,
            max_resolution=self.max_resolution,
            equal_resolution=equal_resolution,
            numpify=numpify,
            torchify=torchify,
        )

NielsRogge's avatar
NielsRogge committed
127
128
129

@require_torch
@require_vision
130
class DetrImageProcessingTest(AnnotationFormatTestMixin, ImageProcessingTestMixin, unittest.TestCase):
131
    image_processing_class = DetrImageProcessor if is_vision_available() else None
NielsRogge's avatar
NielsRogge committed
132
133

    def setUp(self):
134
        self.image_processor_tester = DetrImageProcessingTester(self)
NielsRogge's avatar
NielsRogge committed
135
136

    @property
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    def image_processor_dict(self):
        return self.image_processor_tester.prepare_image_processor_dict()

    def test_image_processor_properties(self):
        image_processing = self.image_processing_class(**self.image_processor_dict)
        self.assertTrue(hasattr(image_processing, "image_mean"))
        self.assertTrue(hasattr(image_processing, "image_std"))
        self.assertTrue(hasattr(image_processing, "do_normalize"))
        self.assertTrue(hasattr(image_processing, "do_rescale"))
        self.assertTrue(hasattr(image_processing, "rescale_factor"))
        self.assertTrue(hasattr(image_processing, "do_resize"))
        self.assertTrue(hasattr(image_processing, "size"))
        self.assertTrue(hasattr(image_processing, "do_pad"))

    def test_image_processor_from_dict_with_kwargs(self):
        image_processor = self.image_processing_class.from_dict(self.image_processor_dict)
        self.assertEqual(image_processor.size, {"shortest_edge": 18, "longest_edge": 1333})
        self.assertEqual(image_processor.do_pad, True)

        image_processor = self.image_processing_class.from_dict(
            self.image_processor_dict, size=42, max_size=84, pad_and_return_pixel_mask=False
158
        )
159
160
        self.assertEqual(image_processor.size, {"shortest_edge": 42, "longest_edge": 84})
        self.assertEqual(image_processor.do_pad, False)
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
    def test_should_raise_if_annotation_format_invalid(self):
        image_processor_dict = self.image_processor_tester.prepare_image_processor_dict()

        with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt", "r") as f:
            detection_target = json.loads(f.read())

        annotations = {"image_id": 39769, "annotations": detection_target}

        params = {
            "images": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"),
            "annotations": annotations,
            "return_tensors": "pt",
        }

        image_processor_params = {**image_processor_dict, **{"format": "_INVALID_FORMAT_"}}
        image_processor = self.image_processing_class(**image_processor_params)

        with self.assertRaises(ValueError) as e:
            image_processor(**params)

        self.assertTrue(str(e.exception).startswith("_INVALID_FORMAT_ is not a valid AnnotationFormat"))

    def test_valid_coco_detection_annotations(self):
        # prepare image and target
        image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
        with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt", "r") as f:
            target = json.loads(f.read())

        params = {"image_id": 39769, "annotations": target}

        # encode them
        image_processing = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")

        # legal encodings (single image)
        _ = image_processing(images=image, annotations=params, return_tensors="pt")
        _ = image_processing(images=image, annotations=[params], return_tensors="pt")

        # legal encodings (batch of one image)
        _ = image_processing(images=[image], annotations=params, return_tensors="pt")
        _ = image_processing(images=[image], annotations=[params], return_tensors="pt")

        # legal encoding (batch of more than one image)
        n = 5
        _ = image_processing(images=[image] * n, annotations=[params] * n, return_tensors="pt")

        # example of an illegal encoding (missing the 'image_id' key)
        with self.assertRaises(ValueError) as e:
            image_processing(images=image, annotations={"annotations": target}, return_tensors="pt")

        self.assertTrue(str(e.exception).startswith("Invalid COCO detection annotations"))

        # example of an illegal encoding (unequal lengths of images and annotations)
        with self.assertRaises(ValueError) as e:
            image_processing(images=[image] * n, annotations=[params] * (n - 1), return_tensors="pt")

        self.assertTrue(str(e.exception) == "The number of images (5) and annotations (4) do not match.")

NielsRogge's avatar
NielsRogge committed
219
220
221
222
223
224
225
226
227
228
    @slow
    def test_call_pytorch_with_coco_detection_annotations(self):
        # prepare image and target
        image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
        with open("./tests/fixtures/tests_samples/COCO/coco_annotations.txt", "r") as f:
            target = json.loads(f.read())

        target = {"image_id": 39769, "annotations": target}

        # encode them
229
230
        image_processing = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
        encoding = image_processing(images=image, annotations=target, return_tensors="pt")
NielsRogge's avatar
NielsRogge committed
231
232
233
234
235
236

        # verify pixel values
        expected_shape = torch.Size([1, 3, 800, 1066])
        self.assertEqual(encoding["pixel_values"].shape, expected_shape)

        expected_slice = torch.tensor([0.2796, 0.3138, 0.3481])
237
        self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3], expected_slice, atol=1e-4))
NielsRogge's avatar
NielsRogge committed
238
239
240

        # verify area
        expected_area = torch.tensor([5887.9600, 11250.2061, 489353.8438, 837122.7500, 147967.5156, 165732.3438])
241
        self.assertTrue(torch.allclose(encoding["labels"][0]["area"], expected_area))
NielsRogge's avatar
NielsRogge committed
242
243
        # verify boxes
        expected_boxes_shape = torch.Size([6, 4])
244
        self.assertEqual(encoding["labels"][0]["boxes"].shape, expected_boxes_shape)
NielsRogge's avatar
NielsRogge committed
245
        expected_boxes_slice = torch.tensor([0.5503, 0.2765, 0.0604, 0.2215])
246
        self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0], expected_boxes_slice, atol=1e-3))
NielsRogge's avatar
NielsRogge committed
247
248
        # verify image_id
        expected_image_id = torch.tensor([39769])
249
        self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"], expected_image_id))
NielsRogge's avatar
NielsRogge committed
250
251
        # verify is_crowd
        expected_is_crowd = torch.tensor([0, 0, 0, 0, 0, 0])
252
        self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"], expected_is_crowd))
NielsRogge's avatar
NielsRogge committed
253
254
        # verify class_labels
        expected_class_labels = torch.tensor([75, 75, 63, 65, 17, 17])
255
        self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], expected_class_labels))
NielsRogge's avatar
NielsRogge committed
256
257
        # verify orig_size
        expected_orig_size = torch.tensor([480, 640])
258
        self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], expected_orig_size))
NielsRogge's avatar
NielsRogge committed
259
260
        # verify size
        expected_size = torch.tensor([800, 1066])
261
        self.assertTrue(torch.allclose(encoding["labels"][0]["size"], expected_size))
NielsRogge's avatar
NielsRogge committed
262
263
264
265
266
267
268
269
270
271
272
273
274

    @slow
    def test_call_pytorch_with_coco_panoptic_annotations(self):
        # prepare image, target and masks_path
        image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
        with open("./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt", "r") as f:
            target = json.loads(f.read())

        target = {"file_name": "000000039769.png", "image_id": 39769, "segments_info": target}

        masks_path = pathlib.Path("./tests/fixtures/tests_samples/COCO/coco_panoptic")

        # encode them
275
276
        image_processing = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50-panoptic")
        encoding = image_processing(images=image, annotations=target, masks_path=masks_path, return_tensors="pt")
NielsRogge's avatar
NielsRogge committed
277
278
279
280
281
282

        # verify pixel values
        expected_shape = torch.Size([1, 3, 800, 1066])
        self.assertEqual(encoding["pixel_values"].shape, expected_shape)

        expected_slice = torch.tensor([0.2796, 0.3138, 0.3481])
283
        self.assertTrue(torch.allclose(encoding["pixel_values"][0, 0, 0, :3], expected_slice, atol=1e-4))
NielsRogge's avatar
NielsRogge committed
284
285
286

        # verify area
        expected_area = torch.tensor([147979.6875, 165527.0469, 484638.5938, 11292.9375, 5879.6562, 7634.1147])
287
        self.assertTrue(torch.allclose(encoding["labels"][0]["area"], expected_area))
NielsRogge's avatar
NielsRogge committed
288
289
        # verify boxes
        expected_boxes_shape = torch.Size([6, 4])
290
        self.assertEqual(encoding["labels"][0]["boxes"].shape, expected_boxes_shape)
NielsRogge's avatar
NielsRogge committed
291
        expected_boxes_slice = torch.tensor([0.2625, 0.5437, 0.4688, 0.8625])
292
        self.assertTrue(torch.allclose(encoding["labels"][0]["boxes"][0], expected_boxes_slice, atol=1e-3))
NielsRogge's avatar
NielsRogge committed
293
294
        # verify image_id
        expected_image_id = torch.tensor([39769])
295
        self.assertTrue(torch.allclose(encoding["labels"][0]["image_id"], expected_image_id))
NielsRogge's avatar
NielsRogge committed
296
297
        # verify is_crowd
        expected_is_crowd = torch.tensor([0, 0, 0, 0, 0, 0])
298
        self.assertTrue(torch.allclose(encoding["labels"][0]["iscrowd"], expected_is_crowd))
NielsRogge's avatar
NielsRogge committed
299
300
        # verify class_labels
        expected_class_labels = torch.tensor([17, 17, 63, 75, 75, 93])
301
        self.assertTrue(torch.allclose(encoding["labels"][0]["class_labels"], expected_class_labels))
NielsRogge's avatar
NielsRogge committed
302
        # verify masks
303
        expected_masks_sum = 822873
304
        self.assertEqual(encoding["labels"][0]["masks"].sum().item(), expected_masks_sum)
NielsRogge's avatar
NielsRogge committed
305
306
        # verify orig_size
        expected_orig_size = torch.tensor([480, 640])
307
        self.assertTrue(torch.allclose(encoding["labels"][0]["orig_size"], expected_orig_size))
NielsRogge's avatar
NielsRogge committed
308
309
        # verify size
        expected_size = torch.tensor([800, 1066])
310
        self.assertTrue(torch.allclose(encoding["labels"][0]["size"], expected_size))