-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraph_build.cpp
More file actions
182 lines (159 loc) · 6.68 KB
/
Copy pathgraph_build.cpp
File metadata and controls
182 lines (159 loc) · 6.68 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <algorithm>
#include <numeric>
#include <unordered_map>
#include "build.hpp"
namespace fs = std::filesystem;
using namespace it_lab_ai;
int main(int argc, char* argv[]) {
std::string model_name = "alexnet_mnist";
RuntimeOptions options;
for (int i = 1; i < argc; ++i) {
if (std::string(argv[i]) == "--model" && i + 1 < argc) {
model_name = argv[++i];
} else if (std::string(argv[i]) == "--onednn") {
options.backend = Backend::kOneDnn;
if (options.isParallel()) {
std::cout << "Warning: oneDNN backend is not compatible with parallel "
"execution. Disabling parallelism."
<< '\n';
options.setParallelBackend(ParBackend::kSeq);
}
} else if (std::string(argv[i]) == "--parallel" && i + 1 < argc) {
if (options.backend == Backend::kOneDnn) {
std::cout << "Warning: Parallel execution is not compatible with "
"oneDNN backend. Ignoring --parallel option."
<< '\n';
i++;
continue;
}
std::string backend_str = argv[++i];
if (backend_str == "tbb") {
options.setParallelBackend(ParBackend::kTbb);
} else if (backend_str == "threads" || backend_str == "stl") {
options.setParallelBackend(ParBackend::kThreads);
} else if (backend_str == "omp") {
options.setParallelBackend(ParBackend::kOmp);
} else {
std::cerr << "Unknown parallel backend: " << backend_str
<< ". Using default (Threads)." << '\n';
options.setParallelBackend(ParBackend::kThreads);
}
} else if (std::string(argv[i]) == "--threads" && i + 1 < argc) {
options.threads = std::stoi(argv[++i]);
}
}
std::string json_path = model_paths[model_name];
std::vector<int> input_shape;
input_shape = get_input_shape_from_json(json_path);
std::string image_folder;
if (model_name == "alexnet_mnist") {
image_folder = IMAGE28_PATH;
} else {
image_folder = IMAGENET_PATH;
}
std::vector<std::string> image_paths;
for (const auto& entry : fs::directory_iterator(image_folder)) {
if (entry.path().extension() == ".png" ||
entry.path().extension() == ".jpg" ||
entry.path().extension() == ".jpeg") {
image_paths.push_back(entry.path().string());
}
}
std::unordered_map<int, std::string> class_names;
try {
class_names = load_class_names(IMAGENET_LABELS);
} catch (const std::exception& e) {
std::cerr << "Warning: " << e.what() << '\n';
}
for (const auto& image_path : image_paths) {
cv::Mat image = cv::imread(image_path);
if (image.empty()) {
std::cerr << "Failed to load image: " << image_path << '\n';
continue;
}
try {
if (model_name == "alexnet_mnist") {
it_lab_ai::Tensor input = prepare_mnist_image(image);
it_lab_ai::Shape sh1({1, 5, 5, 3});
std::vector<float> vec(75, 3);
it_lab_ai::Tensor output = it_lab_ai::make_tensor(vec, sh1);
Graph graph;
build_graph_linear(graph, input, output, options, true);
std::cout << "Starting inference..." << '\n';
try {
graph.inference(options);
std::cout << "Inference completed successfully." << '\n';
} catch (const std::exception& e) {
std::cerr << "ERROR during inference: " << e.what() << '\n';
}
print_time_stats(graph);
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
int top_n = std::min(3, static_cast<int>(tmp_output.size()));
std::vector<int> indices(tmp_output.size());
std::iota(indices.begin(), indices.end(), 0);
std::partial_sort(
indices.begin(), indices.begin() + top_n, indices.end(),
[&](int a, int b) { return tmp_output[a] > tmp_output[b]; });
std::cout << "Top " << top_n << " predictions for MNIST:" << '\n';
for (int i = 0; i < top_n; i++) {
int idx = indices[i];
std::cout << " " << (i + 1) << ". Class " << idx << ": "
<< std::fixed << std::setprecision(6)
<< tmp_output[idx] * 100 << "%" << '\n';
}
int max_class = indices[0];
float max_prob = tmp_output[max_class];
std::cout << "Image: " << fs::path(image_path).filename().string()
<< " -> Predicted digit: " << max_class
<< " (probability: " << std::fixed << std::setprecision(6)
<< max_prob * 100 << "%)" << '\n';
} else {
it_lab_ai::Tensor input = prepare_image(image, input_shape, model_name);
size_t output_classes = 1000;
it_lab_ai::Tensor output({1, output_classes}, it_lab_ai::Type::kFloat);
Graph graph;
build_graph(graph, input, output, json_path, options, false);
std::cout << "Starting inference..." << '\n';
try {
graph.inference(options);
std::cout << "Inference completed successfully." << '\n';
} catch (const std::exception& e) {
std::cerr << "ERROR during inference: " << e.what() << '\n';
}
print_time_stats(graph);
std::vector<float> tmp_output =
process_model_output(*output.as<float>(), model_name);
int top_n = std::min(5, static_cast<int>(tmp_output.size()));
std::vector<int> indices(tmp_output.size());
std::iota(indices.begin(), indices.end(), 0);
std::partial_sort(
indices.begin(), indices.begin() + top_n, indices.end(),
[&](int a, int b) { return tmp_output[a] > tmp_output[b]; });
std::cout << "Top " << top_n << " predictions:" << '\n';
for (int i = 0; i < top_n; i++) {
int idx = indices[i];
std::cout << " " << (i + 1) << ". Class " << idx << ": "
<< std::fixed << std::setprecision(6) << tmp_output[idx];
if (class_names.find(idx) != class_names.end()) {
std::cout << " (" << class_names[idx] << ")";
}
std::cout << '\n';
}
int max_class = indices[0];
float max_prob = tmp_output[max_class];
std::cout << "Image: " << fs::path(image_path).filename().string()
<< " -> Predicted class: " << max_class;
if (class_names.find(max_class) != class_names.end()) {
std::cout << " (" << class_names[max_class] << ")";
}
std::cout << " (probability: " << std::fixed << std::setprecision(6)
<< max_prob << ")" << '\n';
}
std::cout << "----------------------------------------" << '\n';
} catch (const std::exception& e) {
std::cerr << "Error processing image " << image_path << ": " << e.what()
<< '\n';
}
}
return 0;
}