-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgraph_build.cpp
More file actions
154 lines (132 loc) · 5.51 KB
/
Copy pathgraph_build.cpp
File metadata and controls
154 lines (132 loc) · 5.51 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
#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";
bool onednn = false;
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") {
onednn = true;
}
}
it_lab_ai::LayerFactory::configure(onednn);
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() << std::endl;
}
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 << std::endl;
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, true);
std::cout << "Starting inference..." << std::endl;
try {
graph.inference();
std::cout << "Inference completed successfully." << std::endl;
} catch (const std::exception& e) {
std::cerr << "ERROR during inference: " << e.what() << std::endl;
}
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:" << std::endl;
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 << "%" << std::endl;
}
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 << "%)" << std::endl;
} 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, false);
std::cout << "Starting inference..." << std::endl;
try {
graph.inference();
std::cout << "Inference completed successfully." << std::endl;
} catch (const std::exception& e) {
std::cerr << "ERROR during inference: " << e.what() << std::endl;
}
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:" << std::endl;
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 << std::endl;
}
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 << ")" << std::endl;
}
std::cout << "----------------------------------------" << std::endl;
} catch (const std::exception& e) {
std::cerr << "Error processing image " << image_path << ": " << e.what()
<< std::endl;
}
}
return 0;
}