tflite-support icon indicating copy to clipboard operation
tflite-support copied to clipboard

Replace custom JSON expected result with the protobuf JSON format

Open khanhlvg opened this issue 3 years ago • 11 comments

Investigate if we write expected_detections using the proto json format directly, something like this, which should be equivalent to what generated from here (expected_result_dict).

@kinaryml Could you take a look?

khanhlvg avatar Apr 19 '22 14:04 khanhlvg

@khanhlvg I believe it adds the protobuf module dependency and we can use text_format.Merge to do it as showcased here

from google.protobuf import text_format

expected_detections_text_proto = """
detections {
  bounding_box { origin_x: 54 origin_y: 396 width: 393 height: 196 }
  classes { index: 16 score: 0.64453125 class_name: "cat" }
}
...
"""
...
detection_result = detections_pb2.DetectionResult()
text_format.Merge(expected_detections_text_proto, detection_result)

I can apply these changes to the existing PR if you'd like. Let me know if I should wait for the existing PR to be merged and send another PR.

kinarr avatar Apr 19 '22 15:04 kinarr

It should cut down on using the _build_test_data private module in a lot of tests FYI if we apply the change.

kinarr avatar Apr 19 '22 15:04 kinarr

Thanks Kinar!

Also, we can hopefully simply the following test code:

classification_result = classifications_pb2.ClassificationResult()
    classification_result.ParseFromString(image_result.SerializeToString())
    self.assertProtoEquals(classification_result, expected_result)

with

self.assertProtoEquals(image_result, expected_result)

Could you please make those changes as well?

lu-wang-g avatar Apr 19 '22 21:04 lu-wang-g

Thanks Kinar!

Also, we can hopefully simply the following test code:

classification_result = classifications_pb2.ClassificationResult()
    classification_result.ParseFromString(image_result.SerializeToString())
    self.assertProtoEquals(classification_result, expected_result)

with

self.assertProtoEquals(image_result, expected_result)

Could you please make those changes as well?

The tests will run into the following error if we happen to remove that test code:

AssertionError: Can't compare protos of type <class 'DetectionResult'> and <class 'tensorflow_lite_support.cc.task.processor.proto.detections_pb2.DetectionResult'>.

I think that is still required as the returned object (image_result) needs to be a DetectionResult C++ proto object in the processor namespace.

kinarr avatar Apr 19 '22 22:04 kinarr

@khanhlvg @lu-wang-g I've updated the PR with some changes. PTAL.

kinarr avatar Apr 19 '22 22:04 kinarr

Thanks Kinar!

Also, we can hopefully simply the following test code:

classification_result = classifications_pb2.ClassificationResult()
    classification_result.ParseFromString(image_result.SerializeToString())
    self.assertProtoEquals(classification_result, expected_result)

with

self.assertProtoEquals(image_result, expected_result)

Could you please make those changes as well?

I think swapping the order of the parameters should do it. i.e.,

self.assertProtoEquals(expected_result_text_proto, image_result)

kinarr avatar Apr 19 '22 23:04 kinarr

This should directly compare the expected_proto_text ASCII string with the image classification, object detection and audio classification result protobuf message objects.

kinarr avatar Apr 19 '22 23:04 kinarr

Thanks Kinar. Unfortunately the PR has been imported and adapted to internal code structure so it's non-trivial to add your change in the same PR. I went ahead and merge the PR so could you pull the master branch, re-apply the change and create a new branch?

AssertionError: Can't compare protos of type <class 'DetectionResult'> and <class 'tensorflow_lite_support.cc.task.processor.proto.detections_pb2.DetectionResult'>.

Last time we already added the conversion logic to pybind so that the Python API returns the proto in the Python's processor folder. Could you investigate why this still happen and fix it accordingly?

khanhlvg avatar Apr 20 '22 00:04 khanhlvg

Thanks Kinar. Unfortunately the PR has been imported and adapted to internal code structure so it's non-trivial to add your change in the same PR. I went ahead and merge the PR so could you pull the master branch, re-apply the change and create a new branch?

AssertionError: Can't compare protos of type <class 'DetectionResult'> and <class 'tensorflow_lite_support.cc.task.processor.proto.detections_pb2.DetectionResult'>.

Last time we already added the conversion logic to pybind so that the Python API returns the proto in the Python's processor folder. Could you investigate why this still happen and fix it accordingly?

I think it doesn't matter if we convert the protos in the pybinds. I believe Python can't resolve the namespace at runtime. It would still be 'DetectionResult' even for a vision DetectionResult.

kinarr avatar Apr 20 '22 03:04 kinarr

@khanhlvg I've raised a PR here. PTAL.

kinarr avatar Apr 20 '22 03:04 kinarr

Thanks Kinar. Unfortunately the PR has been imported and adapted to internal code structure so it's non-trivial to add your change in the same PR. I went ahead and merge the PR so could you pull the master branch, re-apply the change and create a new branch?

AssertionError: Can't compare protos of type <class 'DetectionResult'> and <class 'tensorflow_lite_support.cc.task.processor.proto.detections_pb2.DetectionResult'>.

Last time we already added the conversion logic to pybind so that the Python API returns the proto in the Python's processor folder. Could you investigate why this still happen and fix it accordingly?

I think it doesn't matter if we convert the protos in the pybinds. I believe Python can't resolve the namespace at runtime. It would still be 'DetectionResult' even for a vision DetectionResult.

Meanwhile, tf.test does verify if the internal function detect in the pybinds actually returns DetectionResult from the processor namespace before invokation as we have the type-hint specified here already.

If we happen to return the vision proto from the pybinds and keep the processor proto as the type-hint in Python, it should fail as expected.

kinarr avatar Apr 20 '22 05:04 kinarr