Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions python/tvm/relay/frontend/tflite.py
Original file line number Diff line number Diff line change
Expand Up @@ -2211,6 +2211,7 @@ def convert_detection_postprocess(self, op):
assert len(inputs) == 3, "inputs length should be 3"
cls_pred = self.get_expr(inputs[1].tensor_idx)
loc_prob = self.get_expr(inputs[0].tensor_idx)
batch_size = inputs[1].tensor.Shape(0)
anchor_values = self.get_tensor_value(inputs[2])
anchor_boxes = len(anchor_values)
anchor_type = self.get_tensor_type_str(inputs[2].tensor.Type())
Expand Down Expand Up @@ -2238,7 +2239,7 @@ def convert_detection_postprocess(self, op):
loc_prob = _op.concatenate(
[loc_coords[1], loc_coords[0], loc_coords[3], loc_coords[2]], axis=2
)
loc_prob = _op.reshape(loc_prob, [1, anchor_boxes*4])
loc_prob = _op.reshape(loc_prob, [batch_size, anchor_boxes*4])

# anchor coords are in yxhw format
# need to convert to ltrb
Expand Down Expand Up @@ -2281,10 +2282,14 @@ def convert_detection_postprocess(self, op):
ret = _op.vision.non_max_suppression(ret[0], ret[1], **non_max_suppression_attrs)
ret = _op.vision.get_valid_counts(ret, 0)
valid_count = ret[0]
# keep only the top 'max_detections' rows
ret = _op.strided_slice(ret[1],
[0, 0, 0],
[batch_size, custom_options["max_detections"], anchor_boxes])
# the output needs some reshaping to match tflite
ret = _op.split(ret[1], 6, axis=2)
cls_ids = ret[0]
scores = ret[1]
ret = _op.split(ret, 6, axis=2)
cls_ids = _op.reshape(ret[0], [batch_size, -1])
scores = _op.reshape(ret[1], [batch_size, -1])
boxes = _op.concatenate([ret[3], ret[2], ret[5], ret[4]], axis=2)
ret = _expr.TupleWrapper(_expr.Tuple([boxes, cls_ids, scores, valid_count]), size=4)
return ret
Expand Down
7 changes: 7 additions & 0 deletions tests/python/frontend/tflite/test_forward.py
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,14 @@ def test_detection_postprocess():
["raw_outputs/box_encodings", "raw_outputs/class_predictions"], num_output=4)
# check valid count is the same
assert tvm_output[3] == tflite_output[3]
# check all the output shapes are the same
assert tvm_output[0].shape == tflite_output[0].shape
assert tvm_output[1].shape == tflite_output[1].shape
assert tvm_output[2].shape == tflite_output[2].shape
valid_count = tvm_output[3][0]
# only check the valid detections are the same
# tvm has a different convention to tflite for invalid detections, it uses all -1s whereas
# tflite appears to put in nonsense data instead
tvm_boxes = tvm_output[0][0][:valid_count]
tvm_classes = tvm_output[1][0][:valid_count]
tvm_scores = tvm_output[2][0][:valid_count]
Expand Down