From 6f4d9e050f127aad9312c9691510b883bab27ba7 Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Tue, 17 Oct 2023 19:26:29 +1300 Subject: [PATCH 1/4] add path to image, so gryolo can read it from disk. --- lib/image_inference_impl.cc | 3 ++- python/iqtlabs/qa_image_inference.py | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/image_inference_impl.cc b/lib/image_inference_impl.cc index fc87463a..9a022dfb 100644 --- a/lib/image_inference_impl.cc +++ b/lib/image_inference_impl.cc @@ -326,7 +326,8 @@ void image_inference_impl::output_image_() { std::stringstream ss("", std::ios_base::app | std::ios_base::out); ss << "{" << "\"ts\": " << host_now_str_(output_item.ts) - << ", \"rx_freq\": " << output_item.rx_freq; + << ", \"rx_freq\": " << output_item.rx_freq + << ", \"image_path\": \"" << full_image_file_png << "\""; // TODO: synchronous requests for testing. Should be parallel. if (host_.size() && port_.size()) { boost::asio::io_context ioc; diff --git a/python/iqtlabs/qa_image_inference.py b/python/iqtlabs/qa_image_inference.py index b8f765d7..d86b730f 100755 --- a/python/iqtlabs/qa_image_inference.py +++ b/python/iqtlabs/qa_image_inference.py @@ -314,6 +314,7 @@ def test_instance(self): with open(test_file) as f: for line in f.readlines(): result = json.loads(line) + self.assertTrue(os.path.exists(result["image_path"]) self.assertEqual(result["predictions"], predictions_result) From 148c4b78385332fb3630cbb810a73e62c9144db0 Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Tue, 17 Oct 2023 19:46:42 +1300 Subject: [PATCH 2/4] Handle multiline JSON from torchserve. --- lib/image_inference_impl.cc | 3 ++- python/iqtlabs/qa_image_inference.py | 19 ++++++++++++------- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/lib/image_inference_impl.cc b/lib/image_inference_impl.cc index 9a022dfb..1005b31d 100644 --- a/lib/image_inference_impl.cc +++ b/lib/image_inference_impl.cc @@ -358,7 +358,8 @@ void image_inference_impl::output_image_() { ss << ", \"error\": \"" << ex.what() << "\""; } } - ss << "}" << std::endl; + // double new line to faciliate json parsing, since prediction may contain new lines. + ss << "}\n" << std::endl; const std::string s = ss.str(); out_buf_.insert(out_buf_.end(), s.begin(), s.end()); delete_output_(); diff --git a/python/iqtlabs/qa_image_inference.py b/python/iqtlabs/qa_image_inference.py index d86b730f..591ebc4d 100755 --- a/python/iqtlabs/qa_image_inference.py +++ b/python/iqtlabs/qa_image_inference.py @@ -241,8 +241,8 @@ def simulate_torchserve(self, port, model_name, result): # nosemgrep:github.workflows.config.useless-inner-function @app.route(f"/predictions/{model_name}", methods=["POST"]) - def predictions_test(): - return json.dumps(result), 200 + def predictions_test(): + return json.dumps(result, indent=2), 200 try: app.run(host="127.0.0.1", port=11001) @@ -252,7 +252,7 @@ def predictions_test(): def test_instance(self): port = 11001 model_name = "testmodel" - predictions_result = {"modulation": 999} + predictions_result = {"modulation": [{"conf": 0.9, "xywh": [1, 2, 3, 4]}]} if self.pid == 0: self.simulate_torchserve(port, model_name, predictions_result) return @@ -312,10 +312,15 @@ def test_instance(self): self.assertEqual(imghdr.what(image_file), "png") self.assertTrue(os.stat(test_file).st_size) with open(test_file) as f: - for line in f.readlines(): - result = json.loads(line) - self.assertTrue(os.path.exists(result["image_path"]) - self.assertEqual(result["predictions"], predictions_result) + content = f.read() + json_raw_all = content.split("\n\n") + self.assertTrue(json_raw_all) + for json_raw in json_raw_all: + if not json_raw: + continue + result = json.loads(json_raw) + self.assertTrue(os.path.exists(result["image_path"])) + self.assertEqual(result["predictions"], predictions_result) if __name__ == "__main__": From 115f2bf24af3f937f94ebb3b4f90cc52bbdd4f6c Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Tue, 17 Oct 2023 19:49:45 +1300 Subject: [PATCH 3/4] format. --- lib/image_inference_impl.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/image_inference_impl.cc b/lib/image_inference_impl.cc index 1005b31d..a9307fbf 100644 --- a/lib/image_inference_impl.cc +++ b/lib/image_inference_impl.cc @@ -358,7 +358,8 @@ void image_inference_impl::output_image_() { ss << ", \"error\": \"" << ex.what() << "\""; } } - // double new line to faciliate json parsing, since prediction may contain new lines. + // double new line to faciliate json parsing, since prediction may contain new + // lines. ss << "}\n" << std::endl; const std::string s = ss.str(); out_buf_.insert(out_buf_.end(), s.begin(), s.end()); From 726788079c883800f4c65458166f8aba0e7aeeb3 Mon Sep 17 00:00:00 2001 From: Josh Bailey Date: Tue, 17 Oct 2023 19:52:15 +1300 Subject: [PATCH 4/4] format. --- lib/image_inference_impl.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/image_inference_impl.cc b/lib/image_inference_impl.cc index a9307fbf..739aa8d1 100644 --- a/lib/image_inference_impl.cc +++ b/lib/image_inference_impl.cc @@ -326,8 +326,8 @@ void image_inference_impl::output_image_() { std::stringstream ss("", std::ios_base::app | std::ios_base::out); ss << "{" << "\"ts\": " << host_now_str_(output_item.ts) - << ", \"rx_freq\": " << output_item.rx_freq - << ", \"image_path\": \"" << full_image_file_png << "\""; + << ", \"rx_freq\": " << output_item.rx_freq << ", \"image_path\": \"" + << full_image_file_png << "\""; // TODO: synchronous requests for testing. Should be parallel. if (host_.size() && port_.size()) { boost::asio::io_context ioc;