"tests/models/fnet/test_tokenization_fnet.py" did not exist on "0eabe49204e4fb1cf393283ee7c16706b42a9abb"
test_image_processing_donut.py 8.39 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
# 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

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

24
from ...test_image_processing_common import 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 DonutImageProcessor
NielsRogge's avatar
NielsRogge committed
34
35


36
class DonutImageProcessingTester(unittest.TestCase):
NielsRogge's avatar
NielsRogge committed
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,
46
        size=None,
NielsRogge's avatar
NielsRogge committed
47
48
49
50
51
52
53
        do_thumbnail=True,
        do_align_axis=False,
        do_pad=True,
        do_normalize=True,
        image_mean=[0.5, 0.5, 0.5],
        image_std=[0.5, 0.5, 0.5],
    ):
54
        super().__init__()
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.image_size = image_size
        self.min_resolution = min_resolution
        self.max_resolution = max_resolution
        self.do_resize = do_resize
62
        self.size = size if size is not None else {"height": 18, "width": 20}
NielsRogge's avatar
NielsRogge committed
63
64
65
66
67
68
69
        self.do_thumbnail = do_thumbnail
        self.do_align_axis = do_align_axis
        self.do_pad = do_pad
        self.do_normalize = do_normalize
        self.image_mean = image_mean
        self.image_std = image_std

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

82
83
84
85
86
87
88
89
90
91
92
93
94
95
    def expected_output_image_shape(self, images):
        return self.num_channels, self.size["height"], self.size["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
96
97
98

@require_torch
@require_vision
99
class DonutImageProcessingTest(ImageProcessingTestMixin, unittest.TestCase):
100
    image_processing_class = DonutImageProcessor if is_vision_available() else None
NielsRogge's avatar
NielsRogge committed
101
102

    def setUp(self):
amyeroberts's avatar
amyeroberts committed
103
        super().setUp()
104
        self.image_processor_tester = DonutImageProcessingTester(self)
NielsRogge's avatar
NielsRogge committed
105
106

    @property
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
    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, "do_resize"))
        self.assertTrue(hasattr(image_processing, "size"))
        self.assertTrue(hasattr(image_processing, "do_thumbnail"))
        self.assertTrue(hasattr(image_processing, "do_align_long_axis"))
        self.assertTrue(hasattr(image_processing, "do_pad"))
        self.assertTrue(hasattr(image_processing, "do_normalize"))
        self.assertTrue(hasattr(image_processing, "image_mean"))
        self.assertTrue(hasattr(image_processing, "image_std"))

    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, {"height": 18, "width": 20})

        image_processor = self.image_processing_class.from_dict(self.image_processor_dict, size=42)
        self.assertEqual(image_processor.size, {"height": 42, "width": 42})
127
128

        # Previous config had dimensions in (width, height) order
129
130
        image_processor = self.image_processing_class.from_dict(self.image_processor_dict, size=(42, 84))
        self.assertEqual(image_processor.size, {"height": 84, "width": 42})
131

132
    @is_flaky()
NielsRogge's avatar
NielsRogge committed
133
    def test_call_pil(self):
134
135
        # Initialize image_processing
        image_processing = self.image_processing_class(**self.image_processor_dict)
NielsRogge's avatar
NielsRogge committed
136
        # create random PIL images
137
        image_inputs = self.image_processor_tester.prepare_image_inputs(equal_resolution=False)
NielsRogge's avatar
NielsRogge committed
138
139
140
141
        for image in image_inputs:
            self.assertIsInstance(image, Image.Image)

        # Test not batched input
142
        encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
NielsRogge's avatar
NielsRogge committed
143
144
145
146
        self.assertEqual(
            encoded_images.shape,
            (
                1,
147
148
149
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
NielsRogge's avatar
NielsRogge committed
150
151
152
153
            ),
        )

        # Test batched
154
        encoded_images = image_processing(image_inputs, return_tensors="pt").pixel_values
NielsRogge's avatar
NielsRogge committed
155
156
157
        self.assertEqual(
            encoded_images.shape,
            (
158
159
160
161
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
NielsRogge's avatar
NielsRogge committed
162
163
164
            ),
        )

165
    @is_flaky()
NielsRogge's avatar
NielsRogge committed
166
    def test_call_numpy(self):
167
168
        # Initialize image_processing
        image_processing = self.image_processing_class(**self.image_processor_dict)
NielsRogge's avatar
NielsRogge committed
169
        # create random numpy tensors
170
        image_inputs = self.image_processor_tester.prepare_image_inputs(equal_resolution=False, numpify=True)
NielsRogge's avatar
NielsRogge committed
171
172
173
174
        for image in image_inputs:
            self.assertIsInstance(image, np.ndarray)

        # Test not batched input
175
        encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
NielsRogge's avatar
NielsRogge committed
176
177
178
179
        self.assertEqual(
            encoded_images.shape,
            (
                1,
180
181
182
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
NielsRogge's avatar
NielsRogge committed
183
184
185
186
            ),
        )

        # Test batched
187
        encoded_images = image_processing(image_inputs, return_tensors="pt").pixel_values
NielsRogge's avatar
NielsRogge committed
188
189
190
        self.assertEqual(
            encoded_images.shape,
            (
191
192
193
194
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
NielsRogge's avatar
NielsRogge committed
195
196
197
            ),
        )

198
    @is_flaky()
NielsRogge's avatar
NielsRogge committed
199
    def test_call_pytorch(self):
200
201
        # Initialize image_processing
        image_processing = self.image_processing_class(**self.image_processor_dict)
NielsRogge's avatar
NielsRogge committed
202
        # create random PyTorch tensors
203
        image_inputs = self.image_processor_tester.prepare_image_inputs(equal_resolution=False, torchify=True)
NielsRogge's avatar
NielsRogge committed
204
205
206
207
        for image in image_inputs:
            self.assertIsInstance(image, torch.Tensor)

        # Test not batched input
208
        encoded_images = image_processing(image_inputs[0], return_tensors="pt").pixel_values
NielsRogge's avatar
NielsRogge committed
209
210
211
212
        self.assertEqual(
            encoded_images.shape,
            (
                1,
213
214
215
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
NielsRogge's avatar
NielsRogge committed
216
217
218
219
            ),
        )

        # Test batched
220
        encoded_images = image_processing(image_inputs, return_tensors="pt").pixel_values
NielsRogge's avatar
NielsRogge committed
221
222
223
        self.assertEqual(
            encoded_images.shape,
            (
224
225
226
227
                self.image_processor_tester.batch_size,
                self.image_processor_tester.num_channels,
                self.image_processor_tester.size["height"],
                self.image_processor_tester.size["width"],
NielsRogge's avatar
NielsRogge committed
228
229
            ),
        )