Commit 6e085b40 by Alexander Pivovarov Committed by Zhi

Fix TF resize for dynamic size models (#4510)

parent 7e3bf12e
...@@ -416,9 +416,11 @@ def _expand_dims(): ...@@ -416,9 +416,11 @@ def _expand_dims():
extras={'axis': int(axis), 'num_newaxis': 1})(inputs, attr) extras={'axis': int(axis), 'num_newaxis': 1})(inputs, attr)
return _impl return _impl
def _resize_bilinear(bilinear_op): def _resize(method):
def _impl(inputs, attr, params): def _impl(inputs, attr, params):
size = attr['_output_shapes'][0][1:3] output_shape0 = attr['_output_shapes'][0]
# Dynamic size models might have _output_shapes attr equal to [None] here
size = output_shape0[1:3] if output_shape0 is not None else [-1, -1]
# Important that the size is defined. If an axis is not, we need to infer what # Important that the size is defined. If an axis is not, we need to infer what
# the shape should be. # the shape should be.
if -1 in size: if -1 in size:
...@@ -431,7 +433,7 @@ def _resize_bilinear(bilinear_op): ...@@ -431,7 +433,7 @@ def _resize_bilinear(bilinear_op):
# Ignore the new attributes from TF2.0, for now. # Ignore the new attributes from TF2.0, for now.
return AttrCvt(op_name='resize', return AttrCvt(op_name='resize',
ignores=['Tdim', 'half_pixel_centers'], ignores=['Tdim', 'half_pixel_centers'],
extras={'method': bilinear_op})(inputs, attr) extras={'method': method})(inputs, attr)
return _impl return _impl
def _check_numerics(): def _check_numerics():
...@@ -1488,9 +1490,9 @@ _convert_map = { ...@@ -1488,9 +1490,9 @@ _convert_map = {
'Relu' : AttrCvt('relu'), 'Relu' : AttrCvt('relu'),
'Relu6' : _relu6(), 'Relu6' : _relu6(),
'Reshape' : _reshape(), 'Reshape' : _reshape(),
'ResizeBilinear' : _resize_bilinear('bilinear'), 'ResizeBilinear' : _resize('bilinear'),
'ResizeBicubic' : _resize_bilinear('bilinear'), 'ResizeBicubic' : _resize('bilinear'),
'ResizeNearestNeighbor' : _resize_bilinear('nearest_neighbor'), 'ResizeNearestNeighbor' : _resize('nearest_neighbor'),
'ReverseV2' : _reverse_v2(), 'ReverseV2' : _reverse_v2(),
'RightShift' : AttrCvt('right_shift'), 'RightShift' : AttrCvt('right_shift'),
'Round' : AttrCvt('round'), 'Round' : AttrCvt('round'),
......
...@@ -28,7 +28,6 @@ from tensorflow.python.framework import graph_util ...@@ -28,7 +28,6 @@ from tensorflow.python.framework import graph_util
from tensorflow.python.ops import nn_ops from tensorflow.python.ops import nn_ops
from tensorflow.python.ops import nn from tensorflow.python.ops import nn
from tensorflow.python.ops import array_ops from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import math_ops from tensorflow.python.ops import math_ops
from tensorflow.python.ops import variable_scope from tensorflow.python.ops import variable_scope
from tensorflow.python.ops import variables from tensorflow.python.ops import variables
...@@ -1438,8 +1437,8 @@ def _test_resize_bilinear_from_tensor(in_shape, align_corners): ...@@ -1438,8 +1437,8 @@ def _test_resize_bilinear_from_tensor(in_shape, align_corners):
with tf.Graph().as_default(): with tf.Graph().as_default():
in_data = array_ops.placeholder( in_data = array_ops.placeholder(
shape=[in_shape[0], in_shape[1], None, None], dtype=data.dtype) shape=[in_shape[0], None, None, in_shape[3]], dtype=data.dtype)
to_shape = tf.shape(in_data)[2:] to_shape = tf.shape(in_data)[1:3]
tf.image.resize_bilinear( tf.image.resize_bilinear(
in_data, to_shape, align_corners=align_corners) in_data, to_shape, align_corners=align_corners)
...@@ -1462,14 +1461,29 @@ def _test_resize_nearest_neighbor(in_shape, to_shape): ...@@ -1462,14 +1461,29 @@ def _test_resize_nearest_neighbor(in_shape, to_shape):
compare_tf_with_tvm(data, 'Placeholder:0', 'resize_nearest_neighbor:0') compare_tf_with_tvm(data, 'Placeholder:0', 'resize_nearest_neighbor:0')
def _test_resize_nearest_neighbor_dynamic_shape(in_shape, scale):
""" One iteration of resize nearest neighbor for graph with dynamic input shape """
data = np.random.uniform(size=in_shape).astype('float32')
with tf.Graph().as_default():
in_data = array_ops.placeholder(shape=None, dtype=data.dtype)
# multiply input shape by scale factor
new_shape = tf.shape(in_data)[1:3] * tf.constant(scale, dtype=tf.int32)
tf.image.resize_nearest_neighbor(
in_data, new_shape, name='resize_nearest_neighbor')
compare_tf_with_tvm(data, 'Placeholder:0', 'resize_nearest_neighbor:0')
def test_forward_resize(): def test_forward_resize():
""" Resize Bilinear, Nearest_Neighbor """ """ Resize Bilinear, Nearest_Neighbor """
# TF default layout is NHWC
_test_resize_bilinear((4, 16, 32, 32), [50, 50], False) _test_resize_bilinear((4, 32, 32, 3), [50, 50], False)
_test_resize_bilinear((6, 32, 64, 64), [20, 20], True) _test_resize_bilinear((6, 32, 32, 3), [20, 20], True)
_test_resize_bilinear_from_tensor((4, 16, 32, 32), False) _test_resize_bilinear_from_tensor((4, 32, 32, 3), False)
_test_resize_bilinear_from_tensor((6, 32, 50, 50), True) _test_resize_bilinear_from_tensor((6, 50, 50, 3), True)
_test_resize_nearest_neighbor((6, 32, 64, 64), [20, 20]) _test_resize_nearest_neighbor((6, 32, 32, 3), [20, 20])
_test_resize_nearest_neighbor_dynamic_shape((1, 16, 16, 3), scale=[2, 2])
####################################################################### #######################################################################
......
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