karolmajek/maxdeeplab
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"""This file contains utility function for handling the dataset."""17 18import tensorflow as tf19 20 21def get_semantic_and_panoptic_label(dataset_info, label, ignore_label):22 """Helper function to get semantic and panoptic label from panoptic label.23 24 This functions gets the semantic and panoptic label from panoptic label for25 different datasets. The labels must be encoded with semantic_label *26 label_divisor + instance_id. For thing classes, the instance ID 0 is reserved27 for crowd regions. Please note, the returned panoptic label has replaced28 the crowd region with ignore regions. Yet, the semantic label makes use of29 these regions.30 31 Args:32 dataset_info: A dictionary storing dataset information.33 label: A Tensor of panoptic label.34 ignore_label: An integer specifying the ignore_label.35 36 Returns:37 semantic_label: A Tensor of semantic segmentation label.38 panoptic_label: A Tensor of panoptic segmentation label, which follows the39 Cityscapes annotation where40 panoptic_label = semantic_label * panoptic_label_divisor + instance_id.41 thing_mask: A boolean Tensor specifying the thing regions. Zero if no thing.42 crowd_region: A boolean Tensor specifying crowd region. Zero if no crowd43 annotation.44 45 Raises:46 ValueError: An error occurs when the ignore_label is not in range47 [0, label_divisor].48 """49 panoptic_label_divisor = dataset_info['panoptic_label_divisor']50 if ignore_label >= panoptic_label_divisor or ignore_label < 0:51 raise ValueError('The ignore_label must be in [0, label_divisor].')52 53 semantic_label = label // panoptic_label_divisor54 # Find iscrowd region if any and set to ignore for panoptic labels.55 # 1. Find thing mask.56 thing_mask = tf.zeros_like(semantic_label, tf.bool)57 for thing_id in dataset_info['class_has_instances_list']:58 thing_mask = tf.logical_or(59 thing_mask,60 tf.equal(semantic_label, thing_id))61 # 2. Find crowd region (thing label that have instance_id == 0).62 crowd_region = tf.logical_and(63 thing_mask,64 tf.equal(label % panoptic_label_divisor, 0))65 # 3. Set crowd region to ignore label.66 panoptic_label = tf.where(67 crowd_region,68 tf.ones_like(label) * ignore_label * panoptic_label_divisor,69 label)70 71 return semantic_label, panoptic_label, thing_mask, crowd_region72 