-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmobilenet_v3_small.cpp
77 lines (66 loc) · 2.52 KB
/
mobilenet_v3_small.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
#include <chrono>
#include <cstddef>
#include <exception>
#include <filesystem>
#include <ratio>
#include <vector>
#include <fmt/color.h>
#include "edgerunner/model.hpp"
#include "imageClassifier.hpp"
auto main() -> int {
const std::filesystem::path modelPath {
"models/tflite/mobilenet_v3_small.tflite"};
const std::filesystem::path labelListPath {
"models/common/imagenet_labels.txt"};
ImageClassifier imageClassifier(modelPath, labelListPath);
/* use the best delegate available based on the build configuration */
#if defined(EDGERUNNER_QNN)
imageClassifier.setDelegate(edge::DELEGATE::NPU);
#elif defined(EDGERUNNER_GPU)
imageClassifier.setDelegate(edge::DELEGATE::GPU);
#endif
const size_t numPredictions = 5;
const std::vector<std::filesystem::path> imagePaths = {
"images/keyboard.jpg",
"images/dog.jpg",
};
for (const auto& imagePath : imagePaths) {
try {
if (imageClassifier.loadImage(imagePath) != edge::STATUS::SUCCESS) {
continue;
}
const auto start = std::chrono::high_resolution_clock::now();
const auto [predictions, inferenceTime] =
imageClassifier.predict(numPredictions);
const auto end = std::chrono::high_resolution_clock::now();
const auto predictionTime =
std::chrono::duration<double, std::milli>(end - start).count();
fmt::print(stderr,
fmt::fg(fmt::color::green),
"predictions for {}:\n",
imagePath.filename().string());
for (const auto& prediction : predictions) {
fmt::print(stderr,
fmt::fg(fmt::color::green),
"\t{} ({:.2f}%)\n",
prediction.first,
100.0F * prediction.second);
}
fmt::print(stderr,
fmt::fg(fmt::color::yellow),
"prediction time: {}ms\n",
predictionTime);
fmt::print(stderr,
fmt::fg(fmt::color::yellow),
"inference time: {}ms\n",
inferenceTime);
} catch (std::exception& ex) {
fmt::print(stderr,
fmt::fg(fmt::color::red),
"{} example failed: {}\n",
imagePath.stem().string(),
ex.what());
}
}
return 0;
}