test: Add ObjectDetection test 12/262712/2
authorKwang Son <k.son@samsung.com>
Wed, 18 Aug 2021 08:15:32 +0000 (04:15 -0400)
committerKwang Son <k.son@samsung.com>
Wed, 18 Aug 2021 08:20:02 +0000 (04:20 -0400)
Add inference dog object detection test.
Move common shared code to helper test class.

Change-Id: I7c02d62fa1c45422ecd5550853adaf39f7fc6a5a
Signed-off-by: Kwang Son <k.son@samsung.com>
test/CMakeLists.txt
test/testsuites/machine_learning/inference/test_image_classification.cpp
test/testsuites/machine_learning/inference/test_inference_helper.cpp
test/testsuites/machine_learning/inference/test_inference_helper.hpp
test/testsuites/machine_learning/inference/test_object_detection.cpp [new file with mode: 0644]

index 43c7e23..d2fcd07 100644 (file)
@@ -9,6 +9,7 @@ add_executable(${PROJECT_NAME}
     testsuites/barcode/test_barcode.cpp
     testsuites/machine_learning/inference/test_inference_helper.cpp
     testsuites/machine_learning/inference/test_image_classification.cpp
+    testsuites/machine_learning/inference/test_object_detection.cpp
 )
 target_link_libraries(${PROJECT_NAME} ${${PROJECT_NAME}_DEP_LIBRARIES} mv_inference mv_image_helper mv_barcode_detector)
 install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_BINDIR})
\ No newline at end of file
index 7c6bc0c..2a13d06 100644 (file)
@@ -45,24 +45,10 @@ void _image_classified_cb(mv_source_h source, const int number_of_classes,
        EXPECT_TRUE(answer_found);
 }
 
-class TestImageClassification : public ::testing::Test
+class TestImageClassification : public TestInference
 {
 public:
-       TestImageClassification()
-       {
-               EXPECT_EQ(mv_create_engine_config(&engine_cfg),
-                                 MEDIA_VISION_ERROR_NONE);
-               EXPECT_EQ(mv_inference_create(&infer), MEDIA_VISION_ERROR_NONE);
-               EXPECT_EQ(mv_create_source(&mv_source), MEDIA_VISION_ERROR_NONE);
-       }
-       ~TestImageClassification()
-       {
-               EXPECT_EQ(mv_destroy_source(mv_source), MEDIA_VISION_ERROR_NONE);
-               EXPECT_EQ(mv_inference_destroy(infer), MEDIA_VISION_ERROR_NONE);
-               EXPECT_EQ(mv_destroy_engine_config(engine_cfg),
-                                 MEDIA_VISION_ERROR_NONE);
-       }
-       void inference_banana()
+       void inferenceBanana()
        {
                ASSERT_EQ(mv_inference_configure(infer, engine_cfg),
                                  MEDIA_VISION_ERROR_NONE);
@@ -74,9 +60,6 @@ public:
                                                                                          _image_classified_cb, NULL),
                                  MEDIA_VISION_ERROR_NONE);
        }
-       mv_engine_config_h engine_cfg;
-       mv_inference_h infer;
-       mv_source_h mv_source;
 };
 
 TEST_F(TestImageClassification, CPU_TFLITE_MobilenetV1)
@@ -84,7 +67,7 @@ TEST_F(TestImageClassification, CPU_TFLITE_MobilenetV1)
        engine_config_hosted_cpu_tflite_user_model(
                        engine_cfg, IC_TFLITE_WEIGHT_MOBILENET_V1_224_PATH,
                        IC_LABEL_MOBILENET_V1_224_PATH);
-       inference_banana();
+       inferenceBanana();
 }
 
 TEST_F(TestImageClassification, CPU_TFLITE_MobilenetV2)
@@ -92,7 +75,7 @@ TEST_F(TestImageClassification, CPU_TFLITE_MobilenetV2)
        engine_config_hosted_cpu_tflite_user_model(
                        engine_cfg, IC_TFLITE_WEIGHT_MOBILENET_V2_224_PATH,
                        IC_LABEL_MOBILENET_V1_224_PATH);
-       inference_banana();
+       inferenceBanana();
 }
 
 TEST_F(TestImageClassification, CPU_TFLITE_Densenet)
@@ -100,7 +83,7 @@ TEST_F(TestImageClassification, CPU_TFLITE_Densenet)
        engine_config_hosted_cpu_tflite_user_model(
                        engine_cfg, IC_TFLITE_WEIGHT_DENSENET_224_PATH,
                        IC_LABEL_MOBILENET_V1_224_PATH);
-       inference_banana();
+       inferenceBanana();
 }
 
 TEST_F(TestImageClassification, CPU_TFLITE_Nasnet)
@@ -108,7 +91,7 @@ TEST_F(TestImageClassification, CPU_TFLITE_Nasnet)
        engine_config_hosted_cpu_tflite_user_model(engine_cfg,
                                                                                           IC_TFLITE_WEIGHT_NASNET_224_PATH,
                                                                                           IC_LABEL_MOBILENET_V1_224_PATH);
-       inference_banana();
+       inferenceBanana();
 }
 
 TEST_F(TestImageClassification, CPU_TFLITE_MNasnet)
@@ -116,7 +99,7 @@ TEST_F(TestImageClassification, CPU_TFLITE_MNasnet)
        engine_config_hosted_cpu_tflite_user_model(
                        engine_cfg, IC_TFLITE_WEIGHT_MNASNET_224_PATH,
                        IC_LABEL_MOBILENET_V1_224_PATH);
-       inference_banana();
+       inferenceBanana();
 }
 
 TEST_F(TestImageClassification, CPU_TFLITE_Squeezenet)
@@ -124,7 +107,7 @@ TEST_F(TestImageClassification, CPU_TFLITE_Squeezenet)
        engine_config_hosted_cpu_tflite_user_model(
                        engine_cfg, IC_TFLITE_WEIGHT_SQUEEZENET_224_PATH,
                        IC_LABEL_MOBILENET_V1_224_PATH);
-       inference_banana();
+       inferenceBanana();
 }
 
 TEST_F(TestImageClassification, CPU_TFLITE_QUANT_MobilenetV1)
@@ -132,5 +115,5 @@ TEST_F(TestImageClassification, CPU_TFLITE_QUANT_MobilenetV1)
        engine_config_hosted_cpu_tflite_user_model(
                        engine_cfg, IC_TFLITE_WEIGHT_QUANT_MOBILENET_V1_224_PATH,
                        IC_LABEL_MOBILENET_V1_224_PATH);
-       inference_banana();
+       inferenceBanana();
 }
\ No newline at end of file
index 4fc1038..d34d383 100644 (file)
@@ -2,6 +2,19 @@
 #include <image_helper.h>
 #include "test_inference_helper.hpp"
 
+TestInference::TestInference()
+{
+       EXPECT_EQ(mv_create_engine_config(&engine_cfg), MEDIA_VISION_ERROR_NONE);
+       EXPECT_EQ(mv_inference_create(&infer), MEDIA_VISION_ERROR_NONE);
+       EXPECT_EQ(mv_create_source(&mv_source), MEDIA_VISION_ERROR_NONE);
+}
+TestInference::~TestInference()
+{
+       EXPECT_EQ(mv_destroy_source(mv_source), MEDIA_VISION_ERROR_NONE);
+       EXPECT_EQ(mv_inference_destroy(infer), MEDIA_VISION_ERROR_NONE);
+       EXPECT_EQ(mv_destroy_engine_config(engine_cfg), MEDIA_VISION_ERROR_NONE);
+}
+
 void engine_config_hosted_cpu_tflite(mv_engine_config_h handle,
                                                                         const char *tf_weight)
 {
index 05cab7a..16bb4c6 100644 (file)
@@ -3,6 +3,16 @@
 
 #include <mv_inference.h>
 
+class TestInference : public ::testing::Test
+{
+public:
+       TestInference();
+       virtual ~TestInference();
+       mv_engine_config_h engine_cfg;
+       mv_inference_h infer;
+       mv_source_h mv_source;
+};
+
 void engine_config_hosted_cpu_tflite(mv_engine_config_h handle,
                                                                         const char *tf_weight);
 
diff --git a/test/testsuites/machine_learning/inference/test_object_detection.cpp b/test/testsuites/machine_learning/inference/test_object_detection.cpp
new file mode 100644 (file)
index 0000000..345fb8f
--- /dev/null
@@ -0,0 +1,54 @@
+#include <gtest/gtest.h>
+#include <string>
+#include <ImageHelper.h>
+#include "test_inference_helper.hpp"
+
+#define OD_LABEL_MOBILENET_V1_SSD_300_PATH \
+       MV_CONFIG_PATH                         \
+       "/models/OD/tflite/od_mobilenet_v1_ssd_postop_label.txt"
+#define OD_TFLITE_WEIGHT_MOBILENET_V1_SSD_300_PATH \
+       MV_CONFIG_PATH                                 \
+       "/models/OD/tflite/od_mobilenet_v1_ssd_postop_300x300.tflite"
+#define IMG_DOG    \
+       MV_CONFIG_PATH \
+       "/res/inference/images/dog2.jpg"
+
+void _object_detected_cb(mv_source_h source, const int number_of_objects,
+                                                const int *indices, const char **names,
+                                                const float *confidences,
+                                                const mv_rectangle_s *locations, void *user_data)
+{
+       const std::string answer = "Dog";
+       auto answer_found = false;
+       for (int i = 0; i < number_of_objects; i++) {
+               if (answer == names[i]) {
+                       answer_found = true;
+               }
+       }
+       EXPECT_TRUE(answer_found);
+}
+
+class TestObjectDetection : public TestInference
+{
+public:
+       void inferenceDog()
+       {
+               ASSERT_EQ(mv_inference_configure(infer, engine_cfg),
+                                 MEDIA_VISION_ERROR_NONE);
+               ASSERT_EQ(mv_inference_prepare(infer), MEDIA_VISION_ERROR_NONE);
+               ASSERT_EQ(MediaVision::Common::ImageHelper::loadImageToSource(
+                                                 IMG_DOG, mv_source),
+                                 MEDIA_VISION_ERROR_NONE);
+               ASSERT_EQ(mv_inference_object_detect(mv_source, infer,
+                                                                                        _object_detected_cb, NULL),
+                                 MEDIA_VISION_ERROR_NONE);
+       }
+};
+
+TEST_F(TestObjectDetection, CPU_TFLITE_MobilenetV1_SSD)
+{
+       engine_config_hosted_cpu_tflite_user_model(
+                       engine_cfg, OD_TFLITE_WEIGHT_MOBILENET_V1_SSD_300_PATH,
+                       OD_LABEL_MOBILENET_V1_SSD_300_PATH);
+       inferenceDog();
+}
\ No newline at end of file