CoolFace
Apppublic

karolmajek/Axial-DeepLab-SWideRNet

sourceHugging Faceupdated 5y agoView on Hugging Face
0likes
preprocess_utils_test.py350 linesDownload Raw Back to preprocessing
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 preprocess_utils."""17import numpy as np18import tensorflow as tf19 20from deeplab2.data.preprocessing import preprocess_utils21 22 23class PreprocessUtilsTest(tf.test.TestCase):24 25  def testNoFlipWhenProbIsZero(self):26    numpy_image = np.dstack([[[5., 6.],27                              [9., 0.]],28                             [[4., 3.],29                              [3., 5.]]])30    image = tf.convert_to_tensor(numpy_image)31 32    actual, is_flipped = preprocess_utils.flip_dim([image], prob=0, dim=0)33    self.assertAllEqual(numpy_image, actual)34    self.assertFalse(is_flipped)35    actual, is_flipped = preprocess_utils.flip_dim([image], prob=0, dim=1)36    self.assertAllEqual(numpy_image, actual)37    self.assertFalse(is_flipped)38    actual, is_flipped = preprocess_utils.flip_dim([image], prob=0, dim=2)39    self.assertAllEqual(numpy_image, actual)40    self.assertFalse(is_flipped)41 42  def testFlipWhenProbIsOne(self):43    numpy_image = np.dstack([[[5., 6.],44                              [9., 0.]],45                             [[4., 3.],46                              [3., 5.]]])47    dim0_flipped = np.dstack([[[9., 0.],48                               [5., 6.]],49                              [[3., 5.],50                               [4., 3.]]])51    dim1_flipped = np.dstack([[[6., 5.],52                               [0., 9.]],53                              [[3., 4.],54                               [5., 3.]]])55    dim2_flipped = np.dstack([[[4., 3.],56                               [3., 5.]],57                              [[5., 6.],58                               [9., 0.]]])59    image = tf.convert_to_tensor(numpy_image)60 61    actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=0)62    self.assertAllEqual(dim0_flipped, actual)63    self.assertTrue(is_flipped)64    actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=1)65    self.assertAllEqual(dim1_flipped, actual)66    self.assertTrue(is_flipped)67    actual, is_flipped = preprocess_utils.flip_dim([image], prob=1, dim=2)68    self.assertAllEqual(dim2_flipped, actual)69    self.assertTrue(is_flipped)70 71  def testFlipMultipleImagesConsistentlyWhenProbIsOne(self):72    numpy_image = np.dstack([[[5., 6.],73                              [9., 0.]],74                             [[4., 3.],75                              [3., 5.]]])76    numpy_label = np.dstack([[[0., 1.],77                              [2., 3.]]])78    image_dim1_flipped = np.dstack([[[6., 5.],79                                     [0., 9.]],80                                    [[3., 4.],81                                     [5., 3.]]])82    label_dim1_flipped = np.dstack([[[1., 0.],83                                     [3., 2.]]])84    image = tf.convert_to_tensor(numpy_image)85    label = tf.convert_to_tensor(numpy_label)86 87    image, label, is_flipped = preprocess_utils.flip_dim(88        [image, label], prob=1, dim=1)89    self.assertAllEqual(image_dim1_flipped, image)90    self.assertAllEqual(label_dim1_flipped, label)91    self.assertTrue(is_flipped)92 93  def testReturnRandomFlipsOnMultipleEvals(self):94    numpy_image = np.dstack([[[5., 6.],95                              [9., 0.]],96                             [[4., 3.],97                              [3., 5.]]])98    dim1_flipped = np.dstack([[[6., 5.],99                               [0., 9.]],100                              [[3., 4.],101                               [5., 3.]]])102    image = tf.convert_to_tensor(numpy_image)103    original_image, not_flipped = preprocess_utils.flip_dim(104        [image], prob=0, dim=1)105    flip_image, is_flipped = preprocess_utils.flip_dim(106        [image], prob=1.0, dim=1)107    self.assertAllEqual(numpy_image, original_image)108    self.assertFalse(not_flipped)109    self.assertAllEqual(dim1_flipped, flip_image)110    self.assertTrue(is_flipped)111 112  def testReturnCorrectCropOfSingleImage(self):113    np.random.seed(0)114 115    height, width = 10, 20116    image = np.random.randint(0, 256, size=(height, width, 3))117 118    crop_height, crop_width = 2, 4119 120    [cropped] = preprocess_utils.random_crop([tf.convert_to_tensor(image)],121                                             crop_height,122                                             crop_width)123 124    # Ensure we can find the cropped image in the original:125    is_found = False126    for x in range(0, width - crop_width + 1):127      for y in range(0, height - crop_height + 1):128        if np.isclose(image[y:y+crop_height, x:x+crop_width, :],129                      cropped).all():130          is_found = True131          break132 133    self.assertTrue(is_found)134 135  def testRandomCropMaintainsNumberOfChannels(self):136    np.random.seed(0)137 138    crop_height, crop_width = 10, 20139    image = np.random.randint(0, 256, size=(100, 200, 3))140 141    tf.random.set_seed(37)142    [cropped] = preprocess_utils.random_crop(143        [tf.convert_to_tensor(image)], crop_height, crop_width)144 145    self.assertListEqual(cropped.shape.as_list(), [crop_height, crop_width, 3])146 147  def testReturnDifferentCropAreasOnTwoEvals(self):148    tf.random.set_seed(0)149 150    crop_height, crop_width = 2, 3151    image = np.random.randint(0, 256, size=(100, 200, 3))152    [cropped0] = preprocess_utils.random_crop(153        [tf.convert_to_tensor(image)], crop_height, crop_width)154    [cropped1] = preprocess_utils.random_crop(155        [tf.convert_to_tensor(image)], crop_height, crop_width)156 157    self.assertFalse(np.isclose(cropped0.numpy(), cropped1.numpy()).all())158 159  def testReturnConsistenCropsOfImagesInTheList(self):160    tf.random.set_seed(0)161 162    height, width = 10, 20163    crop_height, crop_width = 2, 3164    labels = np.linspace(0, height * width-1, height * width)165    labels = labels.reshape((height, width, 1))166    image = np.tile(labels, (1, 1, 3))167 168    [cropped_image, cropped_label] = preprocess_utils.random_crop(169        [tf.convert_to_tensor(image), tf.convert_to_tensor(labels)],170        crop_height, crop_width)171 172    for i in range(3):173      self.assertAllEqual(cropped_image[:, :, i], tf.squeeze(cropped_label))174 175  def testDieOnRandomCropWhenImagesWithDifferentWidth(self):176    crop_height, crop_width = 2, 3177    image1 = tf.convert_to_tensor(np.random.rand(4, 5, 3))178    image2 = tf.convert_to_tensor(np.random.rand(4, 6, 1))179 180    with self.assertRaises(tf.errors.InvalidArgumentError):181      _ = preprocess_utils.random_crop([image1, image2], crop_height,182                                       crop_width)183 184  def testDieOnRandomCropWhenImagesWithDifferentHeight(self):185    crop_height, crop_width = 2, 3186    image1 = tf.convert_to_tensor(np.random.rand(4, 5, 3))187    image2 = tf.convert_to_tensor(np.random.rand(5, 5, 1))188 189    with self.assertRaises(tf.errors.InvalidArgumentError):190      _ = preprocess_utils.random_crop([image1, image2], crop_height,191                                       crop_width)192 193  def testDieOnRandomCropWhenCropSizeIsGreaterThanImage(self):194    crop_height, crop_width = 5, 9195    image1 = tf.convert_to_tensor(np.random.rand(4, 5, 3))196    image2 = tf.convert_to_tensor(np.random.rand(4, 5, 1))197 198    with self.assertRaises(tf.errors.InvalidArgumentError):199      _ = preprocess_utils.random_crop([image1, image2], crop_height,200                                       crop_width)201 202  def testRandomScaleFitsInRange(self):203    scale_value = preprocess_utils.get_random_scale(1., 2., 0.)204    self.assertGreaterEqual(scale_value, 1.)205    self.assertLessEqual(scale_value, 2.)206 207  def testDeterminedRandomScaleReturnsNumber(self):208    scale = preprocess_utils.get_random_scale(1., 1., 0.)209    self.assertEqual(scale, 1.)210 211  def testResizeTensorsToRange(self):212    test_shapes = [[60, 40],213                   [15, 30],214                   [15, 50]]215    min_size = 50216    max_size = 100217    factor = None218    expected_shape_list = [(75, 50, 3),219                           (50, 100, 3),220                           (30, 100, 3)]221    for i, test_shape in enumerate(test_shapes):222      image = tf.random.normal([test_shape[0], test_shape[1], 3])223      new_tensor_list = preprocess_utils.resize_to_range(224          image=image,225          label=None,226          min_size=min_size,227          max_size=max_size,228          factor=factor,229          align_corners=True)230      self.assertEqual(new_tensor_list[0].shape, expected_shape_list[i])231 232  def testResizeTensorsToRangeWithFactor(self):233    test_shapes = [[60, 40],234                   [15, 30],235                   [15, 50]]236    min_size = 50237    max_size = 98238    factor = 8239    expected_image_shape_list = [(81, 57, 3),240                                 (49, 97, 3),241                                 (33, 97, 3)]242    expected_label_shape_list = [(81, 57, 1),243                                 (49, 97, 1),244                                 (33, 97, 1)]245    for i, test_shape in enumerate(test_shapes):246      image = tf.random.normal([test_shape[0], test_shape[1], 3])247      label = tf.random.normal([test_shape[0], test_shape[1], 1])248      new_tensor_list = preprocess_utils.resize_to_range(249          image=image,250          label=label,251          min_size=min_size,252          max_size=max_size,253          factor=factor,254          align_corners=True)255      self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i])256      self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i])257 258  def testResizeTensorsToRangeWithSimilarMinMaxSizes(self):259    test_shapes = [[60, 40],260                   [15, 30],261                   [15, 50]]262    # Values set so that one of the side = 97.263    min_size = 96264    max_size = 98265    factor = 8266    expected_image_shape_list = [(97, 65, 3),267                                 (49, 97, 3),268                                 (33, 97, 3)]269    expected_label_shape_list = [(97, 65, 1),270                                 (49, 97, 1),271                                 (33, 97, 1)]272    for i, test_shape in enumerate(test_shapes):273      image = tf.random.normal([test_shape[0], test_shape[1], 3])274      label = tf.random.normal([test_shape[0], test_shape[1], 1])275      new_tensor_list = preprocess_utils.resize_to_range(276          image=image,277          label=label,278          min_size=min_size,279          max_size=max_size,280          factor=factor,281          align_corners=True)282      self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i])283      self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i])284 285  def testResizeTensorsToRangeWithEqualMaxSize(self):286    test_shapes = [[97, 38],287                   [96, 97]]288    # Make max_size equal to the larger value of test_shapes.289    min_size = 97290    max_size = 97291    factor = 8292    expected_image_shape_list = [(97, 41, 3),293                                 (97, 97, 3)]294    expected_label_shape_list = [(97, 41, 1),295                                 (97, 97, 1)]296    for i, test_shape in enumerate(test_shapes):297      image = tf.random.normal([test_shape[0], test_shape[1], 3])298      label = tf.random.normal([test_shape[0], test_shape[1], 1])299      new_tensor_list = preprocess_utils.resize_to_range(300          image=image,301          label=label,302          min_size=min_size,303          max_size=max_size,304          factor=factor,305          align_corners=True)306      self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i])307      self.assertEqual(new_tensor_list[1].shape, expected_label_shape_list[i])308 309  def testResizeTensorsToRangeWithPotentialErrorInTFCeil(self):310    test_shape = [3936, 5248]311    # Make max_size equal to the larger value of test_shapes.312    min_size = 1441313    max_size = 1441314    factor = 16315    expected_image_shape = (1089, 1441, 3)316    expected_label_shape = (1089, 1441, 1)317    image = tf.random.normal([test_shape[0], test_shape[1], 3])318    label = tf.random.normal([test_shape[0], test_shape[1], 1])319    new_tensor_list = preprocess_utils.resize_to_range(320        image=image,321        label=label,322        min_size=min_size,323        max_size=max_size,324        factor=factor,325        align_corners=True)326    self.assertEqual(new_tensor_list[0].shape, expected_image_shape)327    self.assertEqual(new_tensor_list[1].shape, expected_label_shape)328 329  def testResizeTensorWithOnlyMaxSize(self):330    test_shapes = [[97, 38],331                   [96, 18]]332 333    max_size = (97, 28)334    # Since the second test shape already fits max size, do nothing.335    expected_image_shape_list = [(71, 28, 3),336                                 (96, 18, 3)]337    for i, test_shape in enumerate(test_shapes):338      image = tf.random.normal([test_shape[0], test_shape[1], 3])339      new_tensor_list = preprocess_utils.resize_to_range(340          image=image,341          label=None,342          min_size=None,343          max_size=max_size,344          align_corners=True)345      self.assertEqual(new_tensor_list[0].shape, expected_image_shape_list[i])346 347 348if __name__ == '__main__':349  tf.test.main()350