forked from openvinotoolkit/model_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_YOLOv8.cpp
82 lines (73 loc) · 2.84 KB
/
test_YOLOv8.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Copyright (C) 2020-2024 Intel Corporation
* SPDX-License-Identifier: Apache-2.0
*/
#include <gtest/gtest.h>
#include <models/detection_model.h>
#include <models/input_data.h>
#include <models/results.h>
#include <filesystem>
#include <fstream>
using namespace std;
namespace {
string data() {
// Get data from env var, not form cmd arg to stay aligned with Python version
static const char* const data = getenv("DATA");
EXPECT_NE(data, nullptr);
return data;
}
string model_path(const char model_name[]) {
return data() + "/ultralytics/" + model_name;
}
shared_ptr<DetectionModel> cached_model(const char model_name[]) {
static const char* prev_arg;
static shared_ptr<DetectionModel> prev_model;
if (model_name == prev_arg) {
return prev_model;
} else {
prev_arg = model_name;
filesystem::path xml;
for (auto const& dir_entry : filesystem::directory_iterator{model_path(model_name)}) {
const filesystem::path& path = dir_entry.path();
if (".xml" == path.extension()) {
EXPECT_TRUE(xml.empty());
xml = path;
}
}
bool preload = true;
prev_model = DetectionModel::create_model(xml.string(), {}, "", preload, "CPU");
return prev_model;
}
}
struct Param {
const char* model_name;
filesystem::path refpath;
};
class AccuracySuit : public testing::TestWithParam<Param> {};
TEST_P(AccuracySuit, TestDetector) {
Param param = GetParam();
ifstream file{param.refpath};
stringstream ss;
ss << file.rdbuf();
EXPECT_EQ(ss.str(),
string{*cached_model(param.model_name)
->infer(cv::imread(data() + "/coco128/images/train2017/" + param.refpath.stem().string() +
".jpg"))});
}
INSTANTIATE_TEST_SUITE_P(YOLOv8, AccuracySuit, testing::ValuesIn([] {
std::vector<Param> params;
for (const char* model_name : {"yolov5mu_openvino_model", "yolov8l_openvino_model"}) {
vector<filesystem::path> refpaths;
for (auto const& dir_entry :
filesystem::directory_iterator{model_path(model_name) + "/ref/"}) {
refpaths.push_back(dir_entry.path());
}
EXPECT_GT(refpaths.size(), 0);
sort(refpaths.begin(), refpaths.end());
for (const filesystem::path& refpath : refpaths) {
params.push_back({model_name, refpath});
}
}
return params;
}()));
} // namespace