@@ -22,7 +22,13 @@ struct TestData {
2222
2323NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE (TestData, image, reference);
2424
25- namespace cv {}
25+ cv::Mat load_image (const std::string& path, bool use_tiling, cv::Size size) {
26+ cv::Mat image = cv::imread (path);
27+ if (use_tiling) {
28+ cv::resize (image, image, size);
29+ }
30+ return image;
31+ }
2632
2733struct ModelData {
2834 std::string name;
@@ -83,26 +89,23 @@ TEST_P(ModelParameterizedTest, AccuracyTest) {
8389 auto data = GetParam ();
8490 auto model_path = DATA_DIR + ' /' + data.name ;
8591
92+ auto use_tiling = !data.input_res .empty ();
8693 if (data.type == " DetectionModel" ) {
87- auto use_tiling = !data.input_res .empty ();
8894 auto model = DetectionModel::load (model_path, {{" tiling" , use_tiling}});
8995
9096 for (auto & test_data : data.test_data ) {
9197 std::string image_path = DATA_DIR + ' /' + test_data.image ;
92- cv::Mat image = cv::imread (image_path);
93- if (use_tiling) {
94- cv::resize (image, image, data.input_res );
95- }
98+ auto image = load_image (image_path, use_tiling, data.input_res );
9699 auto result = model.infer (image);
97100 EXPECT_EQ (std::string{result}, test_data.reference [0 ]);
98101 }
99102
100103 } else if (data.type == " SegmentationModel" ) {
101- auto model = SemanticSegmentation::load (model_path);
104+ auto model = SemanticSegmentation::load (model_path, {{ " tiling " , use_tiling}} );
102105
103106 for (auto & test_data : data.test_data ) {
104107 std::string image_path = DATA_DIR + ' /' + test_data.image ;
105- cv::Mat image = cv::imread (image_path);
108+ auto image = load_image (image_path, use_tiling, data. input_res );
106109 auto result = model.infer (image);
107110
108111 EXPECT_EQ (format_test_output_to_string (model, result), test_data.reference [0 ]);
@@ -112,7 +115,7 @@ TEST_P(ModelParameterizedTest, AccuracyTest) {
112115
113116 for (auto & test_data : data.test_data ) {
114117 std::string image_path = DATA_DIR + ' /' + test_data.image ;
115- cv::Mat image = cv::imread (image_path);
118+ auto image = load_image (image_path, use_tiling, data. input_res );
116119 auto result = model.infer (image);
117120
118121 EXPECT_EQ (format_test_output_to_string (model, result), test_data.reference [0 ]);
@@ -121,7 +124,7 @@ TEST_P(ModelParameterizedTest, AccuracyTest) {
121124 auto model = Classification::load (model_path);
122125 for (auto & test_data : data.test_data ) {
123126 std::string image_path = DATA_DIR + ' /' + test_data.image ;
124- cv::Mat image = cv::imread (image_path);
127+ auto image = load_image (image_path, use_tiling, data. input_res );
125128 auto result = model.infer (image);
126129 EXPECT_EQ (std::string{result}, test_data.reference [0 ]);
127130 }
@@ -135,24 +138,21 @@ TEST_P(ModelParameterizedTest, SerializedAccuracyTest) {
135138
136139 const std::string& basename = data.name .substr (data.name .find_last_of (" /\\ " ) + 1 );
137140 auto model_path = DATA_DIR + " /serialized/" + basename;
141+ auto use_tiling = !data.input_res .empty ();
138142 if (data.type == " DetectionModel" ) {
139- auto use_tiling = !data.input_res .empty ();
140143 auto model = DetectionModel::load (model_path, {{" tiling" , use_tiling}});
141144 for (auto & test_data : data.test_data ) {
142145 std::string image_path = DATA_DIR + ' /' + test_data.image ;
143- cv::Mat image = cv::imread (image_path);
144- if (use_tiling) {
145- cv::resize (image, image, data.input_res );
146- }
146+ auto image = load_image (image_path, use_tiling, data.input_res );
147147 auto result = model.infer (image);
148148 EXPECT_EQ (std::string{result}, test_data.reference [0 ]);
149149 }
150150 } else if (data.type == " SegmentationModel" ) {
151- auto model = SemanticSegmentation::load (model_path);
151+ auto model = SemanticSegmentation::load (model_path, {{ " tiling " , use_tiling}} );
152152
153153 for (auto & test_data : data.test_data ) {
154154 std::string image_path = DATA_DIR + ' /' + test_data.image ;
155- cv::Mat image = cv::imread (image_path);
155+ auto image = load_image (image_path, use_tiling, data. input_res );
156156 auto result = model.infer (image);
157157
158158 EXPECT_EQ (format_test_output_to_string (model, result), test_data.reference [0 ]);
@@ -162,7 +162,7 @@ TEST_P(ModelParameterizedTest, SerializedAccuracyTest) {
162162
163163 for (auto & test_data : data.test_data ) {
164164 std::string image_path = DATA_DIR + ' /' + test_data.image ;
165- cv::Mat image = cv::imread (image_path);
165+ auto image = load_image (image_path, use_tiling, data. input_res );
166166 auto result = model.infer (image);
167167
168168 EXPECT_EQ (format_test_output_to_string (model, result), test_data.reference [0 ]);
@@ -172,7 +172,7 @@ TEST_P(ModelParameterizedTest, SerializedAccuracyTest) {
172172
173173 for (auto & test_data : data.test_data ) {
174174 std::string image_path = DATA_DIR + ' /' + test_data.image ;
175- cv::Mat image = cv::imread (image_path);
175+ auto image = load_image (image_path, use_tiling, data. input_res );
176176 auto result = model.infer (image);
177177
178178 EXPECT_EQ (std::string{result}, test_data.reference [0 ]);
@@ -188,26 +188,23 @@ TEST_P(ModelParameterizedTest, AccuracyTestBatch) {
188188 const std::string& basename = data.name .substr (data.name .find_last_of (" /\\ " ) + 1 );
189189 auto model_path = DATA_DIR + " /serialized/" + basename;
190190
191+ auto use_tiling = !data.input_res .empty ();
191192 if (data.type == " DetectionModel" ) {
192- auto use_tiling = !data.input_res .empty ();
193193 auto model = DetectionModel::load (model_path, {{" tiling" , use_tiling}});
194194
195195 for (auto & test_data : data.test_data ) {
196196 std::string image_path = DATA_DIR + ' /' + test_data.image ;
197- cv::Mat image = cv::imread (image_path);
198- if (use_tiling) {
199- cv::resize (image, image, data.input_res );
200- }
197+ auto image = load_image (image_path, use_tiling, data.input_res );
201198 auto result = model.inferBatch ({image});
202199 ASSERT_EQ (result.size (), 1 );
203200 EXPECT_EQ (std::string{result[0 ]}, test_data.reference [0 ]);
204201 }
205202 } else if (data.type == " SegmentationModel" ) {
206- auto model = SemanticSegmentation::load (model_path);
203+ auto model = SemanticSegmentation::load (model_path, {{ " tiling " , use_tiling}} );
207204
208205 for (auto & test_data : data.test_data ) {
209206 std::string image_path = DATA_DIR + ' /' + test_data.image ;
210- cv::Mat image = cv::imread (image_path);
207+ auto image = load_image (image_path, use_tiling, data. input_res );
211208 auto result = model.inferBatch ({image});
212209
213210 EXPECT_EQ (format_test_output_to_string (model, result[0 ]), test_data.reference [0 ]);
@@ -217,7 +214,7 @@ TEST_P(ModelParameterizedTest, AccuracyTestBatch) {
217214
218215 for (auto & test_data : data.test_data ) {
219216 std::string image_path = DATA_DIR + ' /' + test_data.image ;
220- cv::Mat image = cv::imread (image_path);
217+ auto image = load_image (image_path, use_tiling, data. input_res );
221218 auto result = model.inferBatch ({image});
222219
223220 ASSERT_EQ (result.size (), 1 );
@@ -228,7 +225,7 @@ TEST_P(ModelParameterizedTest, AccuracyTestBatch) {
228225
229226 for (auto & test_data : data.test_data ) {
230227 std::string image_path = DATA_DIR + ' /' + test_data.image ;
231- cv::Mat image = cv::imread (image_path);
228+ auto image = load_image (image_path, use_tiling, data. input_res );
232229 auto result = model.inferBatch ({image});
233230
234231 ASSERT_EQ (result.size (), 1 );
0 commit comments