Skip to content

Commit

Permalink
Add PAD operator to relay tflite frontend (#3310)
Browse files Browse the repository at this point in the history
  • Loading branch information
apivovarov authored and kevinthesun committed Jun 10, 2019
1 parent 3294d72 commit 8f219b9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
26 changes: 26 additions & 0 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(self, model, subgraph, exp_tab):
'ADD': self.convert_add,
'MUL': self.convert_mul,
'FULLY_CONNECTED': self.convert_fully_connected,
'PAD': self.convert_pad,
}

def check_unsupported_ops(self):
Expand Down Expand Up @@ -596,6 +597,31 @@ def convert_pool2d(self, op, pool_type):

return out

def convert_pad(self, op):
"""Convert TFLite PAD"""
try:
from tflite.Operator import Operator
except ImportError:
raise ImportError("The tflite package must be installed")

assert isinstance(op, Operator)
input_tensors = self.get_input_tensors(op)
assert len(input_tensors) == 2, "input tensors length should be 2"

# TFLite only support CONSTANT mode and does not support constant_values parameter.
# tensor
input_tensor = input_tensors[0]
in_expr = self.get_expr(input_tensor.tensor_idx)

# paddings
pad_list = self.get_tensor_value(input_tensors[1])
# convert list of lists to tuple of tuples
paddings = tuple(tuple(l) for l in pad_list)

# Use default pad_value 0 because TFLite does not support constant_values parameter
out = _op.nn.pad(in_expr, paddings)
return out

def get_expr(self, input_tensor_idx):
return self.exp_tab.get_expr(get_tensor_name(self.subgraph, input_tensor_idx))

Expand Down
30 changes: 30 additions & 0 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,35 @@ def test_forward_squeeze():
_test_squeeze(np.arange(6).reshape((1, 2, 1, 3)), [0, 2])
_test_squeeze(np.arange(6).reshape((2, 1, 3, 1)), [1, 3])


#######################################################################
# Pad
# ---

def _test_pad(data):
""" One iteration of PAD """

assert len(data) == 2

# Test with tensor and constant
with tf.Graph().as_default():
in_data = [array_ops.placeholder(shape=data[0].shape, dtype=data[0].dtype, name='in')]
out = array_ops.pad(in_data[0], ops.convert_to_tensor(data[1], dtype=data[1].dtype))
compare_tflite_with_tvm([data[0]], ['in:0'], in_data, [out])


def test_forward_pad():
""" Pad """
_test_pad([np.arange(1.0, 7.0, dtype=np.float32).reshape((2, 1, 1, 3)),
np.array([[1, 1], [2, 2], [1, 1], [2, 2]], dtype=np.int32)])
_test_pad([np.arange(1.0, 7.0, dtype=np.float32).reshape((2, 1, 3)),
np.array([[2, 2], [1, 1], [1, 1]], dtype=np.int32)])
_test_pad([np.arange(1.0, 7.0, dtype=np.float32).reshape((2, 3)),
np.array([[1, 1], [2, 2]], dtype=np.int32)])
_test_pad([np.arange(1.0, 4.0, dtype=np.float32).reshape((1, 3)),
np.array([[1, 1], [2, 2]], dtype=np.int32)])


#######################################################################
# Softmax
# -------
Expand Down Expand Up @@ -528,6 +557,7 @@ def test_forward_inception_v4_net():
if __name__ == '__main__':
# Transforms
test_forward_concatenation()
test_forward_pad()
test_forward_reshape()
test_forward_squeeze()

Expand Down

0 comments on commit 8f219b9

Please sign in to comment.