karolmajek/Axial-DeepLab-SWideRNet
0
1# coding=utf-82# Copyright 2021 The Deeplab2 Authors.3#4# Licensed under the Apache License, Version 2.0 (the "License");5# you may not use this file except in compliance with the License.6# You may obtain a copy of the License at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# distributed under the License is distributed on an "AS IS" BASIS,12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13# See the License for the specific language governing permissions and14# limitations under the License.15 16"""Tests for input_preprocessing."""17 18import numpy as np19import tensorflow as tf20 21from deeplab2.data.preprocessing import input_preprocessing22 23 24class InputPreprocessingTest(tf.test.TestCase):25 26 def setUp(self):27 super().setUp()28 self._image = tf.convert_to_tensor(np.random.randint(256, size=[33, 33, 3]))29 self._label = tf.convert_to_tensor(np.random.randint(19, size=[33, 33, 1]))30 31 def test_cropping(self):32 crop_height = np.random.randint(33)33 crop_width = np.random.randint(33)34 35 original_image, processed_image, processed_label, prev_image, prev_label = (36 input_preprocessing.preprocess_image_and_label(37 image=self._image,38 label=self._label,39 prev_image=tf.identity(self._image),40 prev_label=tf.identity(self._label),41 crop_height=crop_height,42 crop_width=crop_width,43 ignore_label=255))44 45 self.assertListEqual(original_image.shape.as_list(),46 [33, 33, 3])47 self.assertListEqual(processed_image.shape.as_list(),48 [crop_height, crop_width, 3])49 self.assertListEqual(processed_label.shape.as_list(),50 [crop_height, crop_width, 1])51 np.testing.assert_equal(processed_image.numpy(), prev_image.numpy())52 np.testing.assert_equal(processed_label.numpy(), prev_label.numpy())53 54 def test_resizing(self):55 height, width = 65, 6556 57 original_image, processed_image, processed_label, prev_image, prev_label = (58 input_preprocessing.preprocess_image_and_label(59 image=self._image,60 label=self._label,61 prev_image=tf.identity(self._image),62 prev_label=tf.identity(self._label),63 crop_height=height,64 crop_width=width,65 min_resize_value=65,66 max_resize_value=65,67 resize_factor=32,68 ignore_label=255))69 70 self.assertListEqual(original_image.shape.as_list(),71 [height, width, 3])72 self.assertListEqual(processed_image.shape.as_list(),73 [height, width, 3])74 self.assertListEqual(processed_label.shape.as_list(),75 [height, width, 1])76 np.testing.assert_equal(processed_image.numpy(), prev_image.numpy())77 np.testing.assert_equal(processed_label.numpy(), prev_label.numpy())78 79 def test_scaling(self):80 height, width = 65, 6581 82 original_image, processed_image, processed_label, prev_image, prev_label = (83 input_preprocessing.preprocess_image_and_label(84 image=self._image,85 label=self._label,86 prev_image=tf.identity(self._image),87 prev_label=tf.identity(self._label),88 crop_height=height,89 crop_width=width,90 min_scale_factor=0.5,91 max_scale_factor=2.0,92 ignore_label=255))93 94 self.assertListEqual(original_image.shape.as_list(),95 [33, 33, 3])96 self.assertListEqual(processed_image.shape.as_list(),97 [height, width, 3])98 self.assertListEqual(processed_label.shape.as_list(),99 [height, width, 1])100 np.testing.assert_equal(processed_image.numpy(), prev_image.numpy())101 np.testing.assert_equal(processed_label.numpy(), prev_label.numpy())102 103 def test_return_padded_image_and_label(self):104 image = np.dstack([[[5, 6], [9, 0]], [[4, 3], [3, 5]], [[7, 8], [1, 2]]])105 image = tf.convert_to_tensor(image, dtype=tf.float32)106 label = np.array([[[1], [2]], [[3], [4]]])107 expected_image = np.dstack([[[127.5, 127.5, 127.5, 127.5, 127.5],108 [127.5, 127.5, 127.5, 127.5, 127.5],109 [127.5, 5, 6, 127.5, 127.5],110 [127.5, 9, 0, 127.5, 127.5],111 [127.5, 127.5, 127.5, 127.5, 127.5]],112 [[127.5, 127.5, 127.5, 127.5, 127.5],113 [127.5, 127.5, 127.5, 127.5, 127.5],114 [127.5, 4, 3, 127.5, 127.5],115 [127.5, 3, 5, 127.5, 127.5],116 [127.5, 127.5, 127.5, 127.5, 127.5]],117 [[127.5, 127.5, 127.5, 127.5, 127.5],118 [127.5, 127.5, 127.5, 127.5, 127.5],119 [127.5, 7, 8, 127.5, 127.5],120 [127.5, 1, 2, 127.5, 127.5],121 [127.5, 127.5, 127.5, 127.5, 127.5]]])122 expected_label = np.array([[[255], [255], [255], [255], [255]],123 [[255], [255], [255], [255], [255]],124 [[255], [1], [2], [255], [255]],125 [[255], [3], [4], [255], [255]],126 [[255], [255], [255], [255], [255]]])127 128 padded_image, padded_label = input_preprocessing._pad_image_and_label(129 image, label, 2, 1, 5, 5, 255)130 np.testing.assert_allclose(padded_image.numpy(), expected_image)131 np.testing.assert_allclose(padded_label.numpy(), expected_label)132 133 def test_return_original_image_when_target_size_is_equal_to_image_size(self):134 height, width, _ = tf.shape(self._image)135 padded_image, _ = input_preprocessing._pad_image_and_label(136 self._image, None, 0, 0, height, width)137 np.testing.assert_allclose(padded_image.numpy(), self._image)138 139 def test_die_on_target_size_greater_than_image_size(self):140 height, width, _ = tf.shape(self._image)141 with self.assertRaises(tf.errors.InvalidArgumentError):142 _ = input_preprocessing._pad_image_and_label(self._image, None, 0, 0,143 height, width - 1)144 145 with self.assertRaises(tf.errors.InvalidArgumentError):146 _ = input_preprocessing._pad_image_and_label(self._image, None, 0, 0,147 height - 1, width)148 149 def test_die_if_target_size_not_possible_with_given_offset(self):150 height, width, _ = tf.shape(self._image)151 with self.assertRaises(tf.errors.InvalidArgumentError):152 _ = input_preprocessing._pad_image_and_label(self._image, None, 3, 3,153 height + 2, width + 2)154 155 def test_set_min_resize_value_only_during_training(self):156 crop_height = np.random.randint(33)157 crop_width = np.random.randint(33)158 159 _, processed_image, _, _, _ = (160 input_preprocessing.preprocess_image_and_label(161 image=self._image,162 label=self._label,163 crop_height=crop_height,164 crop_width=crop_width,165 min_resize_value=[10],166 max_resize_value=None,167 ignore_label=255))168 169 self.assertListEqual(processed_image.shape.as_list(),170 [crop_height, crop_width, 3])171 172 173if __name__ == '__main__':174 tf.test.main()175 