image_utils_test.py 3.97 KB
Newer Older
Jiageng Zhang's avatar
Jiageng Zhang 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
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
#
# 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.

"""Tests for image_utils."""
import imghdr
from unittest import mock
from absl.testing import parameterized
import tensorflow as tf

from official.vision.data import fake_feature_generator
from official.vision.data import image_utils


class ImageUtilsTest(parameterized.TestCase, tf.test.TestCase):

  @parameterized.named_parameters(
      ('RGB_PNG', 128, 64, 3, 'PNG'), ('RGB_JPEG', 2, 1, 3, 'JPEG'),
      ('GREY_BMP', 32, 32, 1, 'BMP'), ('GREY_PNG', 128, 128, 1, 'png'))
  def test_encode_image_then_decode_image(self, height, width, num_channels,
                                          image_format):
    image_np = fake_feature_generator.generate_image_np(height, width,
                                                        num_channels)
    image_str = image_utils.encode_image(image_np, image_format)
    actual_image_np = image_utils.decode_image(image_str)

    # JPEG encoding does not keep the pixel value.
    if image_format != 'JPEG':
      self.assertAllClose(actual_image_np, image_np)
    self.assertEqual(actual_image_np.shape, image_np.shape)

Jiageng Zhang's avatar
Jiageng Zhang committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  @parameterized.named_parameters(
      ('RGB_RAW', 128, 64, 3, tf.bfloat16.as_numpy_dtype),
      ('GREY_RAW', 32, 32, 1, tf.uint8.as_numpy_dtype))
  def test_encode_raw_image_then_decode_raw_image(self, height, width,
                                                  num_channels, image_dtype):
    image_np = fake_feature_generator.generate_image_np(height, width,
                                                        num_channels)
    image_np = image_np.astype(image_dtype)
    image_str = image_utils.encode_image(image_np, 'RAW')
    actual_image_np = image_utils.decode_image(image_str, 'RAW', image_dtype)
    actual_image_np = actual_image_np.reshape([height, width, num_channels])

    self.assertAllClose(actual_image_np, image_np)
    self.assertEqual(actual_image_np.shape, image_np.shape)

Jiageng Zhang's avatar
Jiageng Zhang committed
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
  @parameterized.named_parameters(
      ('RGB_PNG', 128, 64, 3, 'PNG'), ('RGB_JPEG', 64, 128, 3, 'JPEG'),
      ('GREY_BMP', 32, 32, 1, 'BMP'), ('GREY_PNG', 128, 128, 1, 'png'))
  def test_encode_image_then_decode_image_metadata(self, height, width,
                                                   num_channels, image_format):
    image_np = fake_feature_generator.generate_image_np(height, width,
                                                        num_channels)
    image_str = image_utils.encode_image(image_np, image_format)
    (actual_height, actual_width, actual_num_channels, actual_format) = (
        image_utils.decode_image_metadata(image_str))

    self.assertEqual(actual_height, height)
    self.assertEqual(actual_width, width)
    self.assertEqual(actual_num_channels, num_channels)
    self.assertEqual(actual_format, image_format.upper())

  def test_encode_image_raise_error_with_invalid_image_format(self):
    with self.assertRaisesRegex(ValueError, 'Image format is invalid: foo'):
      image_np = fake_feature_generator.generate_image_np(2, 2, 1)
      image_utils.encode_image(image_np, 'foo')

  @mock.patch.object(imghdr, 'what', return_value='foo', autospec=True)
  def test_decode_image_raise_error_with_invalid_image_format(self, _):
    image_np = fake_feature_generator.generate_image_np(1, 1, 3)
    image_str = image_utils.encode_image(image_np, 'PNG')
    with self.assertRaisesRegex(ValueError, 'Image format is invalid: foo'):
      image_utils.decode_image_metadata(image_str)


if __name__ == '__main__':
  tf.test.main()