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 dataset_utils."""17 18import numpy as np19import tensorflow as tf20 21from deeplab2.data import dataset_utils22 23 24class DatasetUtilsTest(tf.test.TestCase):25 26 def _get_test_labels(self, num_classes, shape, label_divisor):27 num_ids_per_class = 3528 semantic_labels = np.random.randint(num_classes, size=shape)29 panoptic_labels = np.random.randint(30 num_ids_per_class, size=shape) + semantic_labels * label_divisor31 32 semantic_labels = tf.convert_to_tensor(semantic_labels, dtype=tf.int32)33 panoptic_labels = tf.convert_to_tensor(panoptic_labels, dtype=tf.int32)34 35 return panoptic_labels, semantic_labels36 37 def setUp(self):38 super().setUp()39 self._first_thing_class = 940 self._num_classes = 1941 self._dataset_info = {42 'panoptic_label_divisor': 1000,43 'class_has_instances_list': tf.range(self._first_thing_class,44 self._num_classes)45 }46 self._num_ids = 3747 self._labels, self._semantic_classes = self._get_test_labels(48 self._num_classes, [2, 33, 33],49 self._dataset_info['panoptic_label_divisor'])50 51 def test_get_panoptic_and_semantic_label(self):52 # Note: self._labels contains one crowd instance per class.53 (returned_sem_labels, returned_pan_labels, returned_thing_mask,54 returned_crowd_region) = (55 dataset_utils.get_semantic_and_panoptic_label(56 self._dataset_info, self._labels, ignore_label=255))57 58 expected_semantic_labels = self._semantic_classes59 condition = self._labels % self._dataset_info['panoptic_label_divisor'] == 060 condition = tf.logical_and(61 condition,62 tf.math.greater_equal(expected_semantic_labels,63 self._first_thing_class))64 expected_crowd_labels = tf.where(condition, 1.0, 0.0)65 expected_pan_labels = tf.where(66 condition, 255 * self._dataset_info['panoptic_label_divisor'],67 self._labels)68 expected_thing_mask = tf.where(69 tf.math.greater_equal(expected_semantic_labels,70 self._first_thing_class), 1.0, 0.0)71 72 self.assertListEqual(returned_sem_labels.shape.as_list(),73 expected_semantic_labels.shape.as_list())74 self.assertListEqual(returned_pan_labels.shape.as_list(),75 expected_pan_labels.shape.as_list())76 self.assertListEqual(returned_crowd_region.shape.as_list(),77 expected_crowd_labels.shape.as_list())78 self.assertListEqual(returned_thing_mask.shape.as_list(),79 expected_thing_mask.shape.as_list())80 np.testing.assert_equal(returned_sem_labels.numpy(),81 expected_semantic_labels.numpy())82 np.testing.assert_equal(returned_pan_labels.numpy(),83 expected_pan_labels.numpy())84 np.testing.assert_equal(returned_crowd_region.numpy(),85 expected_crowd_labels.numpy())86 np.testing.assert_equal(returned_thing_mask.numpy(),87 expected_thing_mask.numpy())88 89if __name__ == '__main__':90 tf.test.main()91 