Unverified Commit 5893d611 authored by srihari-humbarwadi's avatar srihari-humbarwadi
Browse files

rescale offest masks before resizing

parent 277f935a
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
Note that the postprocessing class and the supporting functions are branched Note that the postprocessing class and the supporting functions are branched
from https://github.com/google-research/deeplab2/blob/main/model/post_processor/panoptic_deeplab.py from https://github.com/google-research/deeplab2/blob/main/model/post_processor/panoptic_deeplab.py
with minor changes.
""" """
import functools import functools
...@@ -24,6 +25,7 @@ from typing import List, Tuple, Dict, Text ...@@ -24,6 +25,7 @@ from typing import List, Tuple, Dict, Text
import tensorflow as tf import tensorflow as tf
from official.vision.beta.projects.panoptic_maskrcnn.ops import mask_ops
def _add_zero_padding(input_tensor: tf.Tensor, kernel_size: int, def _add_zero_padding(input_tensor: tf.Tensor, kernel_size: int,
rank: int) -> tf.Tensor: rank: int) -> tf.Tensor:
...@@ -468,6 +470,36 @@ class PostProcessor(tf.keras.layers.Layer): ...@@ -468,6 +470,36 @@ class PostProcessor(tf.keras.layers.Layer):
self._config_dict['output_size'][1]) self._config_dict['output_size'][1])
return mask return mask
def _resize_and_pad_offset_mask(self, mask, image_info):
"""Rescales and resizes offset masks to match the original image shape and
pads to`output_size`.
Args:
mask: a padded offset mask tensor.
image_info: a tensor that holds information about original and
preprocessed images.
Returns:
rescaled, resized and padded masks: tf.Tensor.
"""
rescale_size = tf.cast(
tf.math.ceil(image_info[1, :] / image_info[2, :]), tf.int32)
image_shape = tf.cast(image_info[0, :], tf.int32)
offsets = tf.cast(image_info[3, :], tf.int32)
mask = mask_ops.resize_and_rescale_offsets(
tf.expand_dims(mask, axis=0),
rescale_size)[0]
mask = tf.image.crop_to_bounding_box(
mask,
offsets[0], offsets[1],
image_shape[0],
image_shape[1])
mask = tf.image.pad_to_bounding_box(
mask, 0, 0,
self._config_dict['output_size'][0],
self._config_dict['output_size'][1])
return mask
def call( def call(
self, self,
result_dict: Dict[Text, tf.Tensor], result_dict: Dict[Text, tf.Tensor],
...@@ -490,21 +522,21 @@ class PostProcessor(tf.keras.layers.Layer): ...@@ -490,21 +522,21 @@ class PostProcessor(tf.keras.layers.Layer):
- instance_score - instance_score
""" """
if self._config_dict['rescale_predictions']: if self._config_dict['rescale_predictions']:
def _batch_resize_and_pad_masks(mask): segmentation_outputs = tf.map_fn(
mask = tf.map_fn( fn=lambda x: self._resize_and_pad_masks(x[0], x[1]),
fn=lambda x: self._resize_and_pad_masks(x[0], x[1]), elems=(result_dict['segmentation_outputs'], image_info),
elems=(mask, image_info), fn_output_signature=tf.float32,
fn_output_signature=tf.float32, parallel_iterations=32)
parallel_iterations=32) instance_centers_heatmap = tf.map_fn(
return mask fn=lambda x: self._resize_and_pad_masks(x[0], x[1]),
elems=(result_dict['instance_centers_heatmap'], image_info),
segmentation_outputs = _batch_resize_and_pad_masks( fn_output_signature=tf.float32,
result_dict['segmentation_outputs']) parallel_iterations=32)
instance_centers_heatmap = _batch_resize_and_pad_masks( instance_centers_offset = tf.map_fn(
result_dict['instance_centers_heatmap']) fn=lambda x: self._resize_and_pad_offset_mask(x[0], x[1]),
instance_centers_offset = _batch_resize_and_pad_masks( elems=(result_dict['instance_centers_offset'], image_info),
result_dict['instance_centers_offset']) fn_output_signature=tf.float32,
parallel_iterations=32)
else: else:
segmentation_outputs = tf.image.resize( segmentation_outputs = tf.image.resize(
result_dict['segmentation_outputs'], result_dict['segmentation_outputs'],
...@@ -514,10 +546,9 @@ class PostProcessor(tf.keras.layers.Layer): ...@@ -514,10 +546,9 @@ class PostProcessor(tf.keras.layers.Layer):
result_dict['instance_centers_heatmap'], result_dict['instance_centers_heatmap'],
size=self._config_dict['output_size'], size=self._config_dict['output_size'],
method='bilinear') method='bilinear')
instance_centers_offset = tf.image.resize( instance_centers_offset = mask_ops.resize_and_rescale_offsets(
result_dict['instance_centers_offset'], result_dict['instance_centers_offset'],
size=self._config_dict['output_size'], target_size=self._config_dict['output_size'])
method='bilinear')
processed_dict = {} processed_dict = {}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment