test_image_processing_levit.py 7.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# coding=utf-8
# Copyright 2022 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 unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

24
from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs
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 LevitImageProcessor
34
35


36
class LevitImageProcessingTester(unittest.TestCase):
37
38
39
40
41
42
43
44
45
    def __init__(
        self,
        parent,
        batch_size=7,
        num_channels=3,
        image_size=18,
        min_resolution=30,
        max_resolution=400,
        do_resize=True,
amyeroberts's avatar
amyeroberts committed
46
        size=None,
47
        do_center_crop=True,
amyeroberts's avatar
amyeroberts committed
48
        crop_size=None,
49
50
51
52
        do_normalize=True,
        image_mean=[0.5, 0.5, 0.5],
        image_std=[0.5, 0.5, 0.5],
    ):
amyeroberts's avatar
amyeroberts committed
53
54
        size = size if size is not None else {"shortest_edge": 18}
        crop_size = crop_size if crop_size is not None else {"height": 18, "width": 18}
55
56
57
58
59
60
61
62
63
        self.parent = parent
        self.batch_size = batch_size
        self.num_channels = num_channels
        self.image_size = image_size
        self.min_resolution = min_resolution
        self.max_resolution = max_resolution
        self.do_resize = do_resize
        self.size = size
        self.do_center_crop = do_center_crop
amyeroberts's avatar
amyeroberts committed
64
        self.crop_size = crop_size
65
66
67
68
        self.do_normalize = do_normalize
        self.image_mean = image_mean
        self.image_std = image_std

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


@require_torch
@require_vision
83
84
class LevitImageProcessingTest(ImageProcessingSavingTestMixin, unittest.TestCase):
    image_processing_class = LevitImageProcessor if is_vision_available() else None
85
86

    def setUp(self):
87
        self.image_processor_tester = LevitImageProcessingTester(self)
88
89

    @property
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    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_resize"))
        self.assertTrue(hasattr(image_processing, "do_center_crop"))
        self.assertTrue(hasattr(image_processing, "size"))

    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})
        self.assertEqual(image_processor.crop_size, {"height": 18, "width": 18})

        image_processor = self.image_processing_class.from_dict(self.image_processor_dict, size=42, crop_size=84)
        self.assertEqual(image_processor.size, {"shortest_edge": 42})
        self.assertEqual(image_processor.crop_size, {"height": 84, "width": 84})
110

111
112
113
114
    def test_batch_feature(self):
        pass

    def test_call_pil(self):
115
116
        # Initialize image_processing
        image_processing = self.image_processing_class(**self.image_processor_dict)
117
        # create random PIL images
118
        image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False)
119
120
121
122
        for image in image_inputs:
            self.assertIsInstance(image, Image.Image)

        # Test not batched input
123
        encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
124
125
126
127
        self.assertEqual(
            encoded_images.shape,
            (
                1,
128
129
130
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
131
132
133
134
            ),
        )

        # Test batched
135
        encoded_images = image_processing(image_inputs, return_tensors="pt").pixel_values
136
137
138
        self.assertEqual(
            encoded_images.shape,
            (
139
140
141
142
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
143
144
145
146
            ),
        )

    def test_call_numpy(self):
147
148
        # Initialize image_processing
        image_processing = self.image_processing_class(**self.image_processor_dict)
149
        # create random numpy tensors
150
        image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False, numpify=True)
151
152
153
154
        for image in image_inputs:
            self.assertIsInstance(image, np.ndarray)

        # Test not batched input
155
        encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
156
157
158
159
        self.assertEqual(
            encoded_images.shape,
            (
                1,
160
161
162
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
163
164
165
166
            ),
        )

        # Test batched
167
        encoded_images = image_processing(image_inputs, return_tensors="pt").pixel_values
168
169
170
        self.assertEqual(
            encoded_images.shape,
            (
171
172
173
174
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
175
176
177
178
            ),
        )

    def test_call_pytorch(self):
179
180
        # Initialize image_processing
        image_processing = self.image_processing_class(**self.image_processor_dict)
181
        # create random PyTorch tensors
182
        image_inputs = prepare_image_inputs(self.image_processor_tester, equal_resolution=False, torchify=True)
183
184
185
186
        for image in image_inputs:
            self.assertIsInstance(image, torch.Tensor)

        # Test not batched input
187
        encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
188
189
190
191
        self.assertEqual(
            encoded_images.shape,
            (
                1,
192
193
194
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
195
196
197
198
            ),
        )

        # Test batched
199
        encoded_images = image_processing(image_inputs, return_tensors="pt").pixel_values
200
201
202
        self.assertEqual(
            encoded_images.shape,
            (
203
204
205
206
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.crop_size["height"],
                self.image_processor_tester.crop_size["width"],
207
208
            ),
        )