Skip to content

Commit 93bf302

Browse files
committed
ALL DONE
1 parent 22dec95 commit 93bf302

File tree

10 files changed

+321
-176
lines changed

10 files changed

+321
-176
lines changed

app/Graph/CMakeLists.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,8 @@ file(DOWNLOAD
6060
)
6161

6262
file(DOWNLOAD
63-
"https://storage.googleapis.com/kagglesdsdata/datasets/1513816/2500032/test_224/10008.jpg?X-Goog-Algorithm=GOOG4-RSA-SHA256&X-Goog-Credential=databundle-worker-v2%40kaggle-161607.iam.gserviceaccount.com%2F20250916%2Fauto%2Fstorage%2Fgoog4_request&X-Goog-Date=20250916T192850Z&X-Goog-Expires=345600&X-Goog-SignedHeaders=host&X-Goog-Signature=90e54a1e36e1b1be1cda07bcd00eb4cdcf504358bf9ce4eccdf0dc6af6adb19ab9fa82689878a3b26cea4e4295501fdba76e8e5dff3ee0aefe8220abd67ced9667d6f4538a7617bbe4e762a6f97907cab112949353f50276d1911c71dab11ce56370694756a2db16f08c8f819c2dbc8e6c11b131f08481962abfad3347a3ff94469310eb22db163b9036b81ce5efc720b2e175e9bb84beb87e849c2158830697328daa344f03f852ab7dad15c3bc13743f8f185dcfffc9898b7ee449800a188b1809d62f9caeb7343a94c24e7b0cae50abb93cd99a2ee679706eccd5cc093c5f4a9d0f096dcbe76be2c891f75541e11d28f47931cb8bef2dc2fea40ce1ffb391"
64-
"${CMAKE_SOURCE_DIR}/docs/input/224/test1.png"
65-
SHOW_PROGRESS
66-
STATUS status_code
67-
LOG log_file
68-
)
69-
70-
file(DOWNLOAD
71-
"https://cs13.pikabu.ru/avatars/3329/x3329282-693120225.png"
72-
"${CMAKE_SOURCE_DIR}/docs/input/256/test1.png"
63+
"blob:https://ru.pinterest.com/63b88674-b4a6-4ef3-85b2-ab57ef7bb8e7"
64+
"${CMAKE_SOURCE_DIR}/docs/input/Imagenet_test/tench.png"
7365
SHOW_PROGRESS
7466
STATUS status_code
7567
LOG log_file

app/Graph/acc_check_mnist.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int main(int argc, char* argv[]) {
5555
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
5656
Tensor t = make_tensor<float>(res, sh);
5757
input = t;
58-
build_graph(input, output, false, parallel);
58+
build_graph_linear(input, output, false, parallel);
5959
std::vector<std::vector<float>> tmp_output =
6060
softmax<float>(*output.as<float>(), 10);
6161
std::vector<size_t> indices;

app/Graph/build.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
#include <unordered_map>
66
#include <unordered_set>
77

8-
void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
9-
const std::string& json_path, bool comments,
8+
void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output, bool comments,
109
bool parallel) {
1110
if (comments) {
1211
for (size_t i = 0; i < input.get_shape().dims(); i++) {
@@ -32,7 +31,7 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
3231
std::vector<std::shared_ptr<it_lab_ai::Layer>> layers;
3332
std::vector<bool> layerpostop;
3433

35-
std::string json_file = json_path;
34+
std::string json_file = MODEL_PATH_H5;
3635
it_lab_ai::json model_data = it_lab_ai::read_json(json_file);
3736

3837
if (comments) std::cout << "Loaded model data from JSON." << std::endl;
@@ -78,7 +77,7 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
7877
it_lab_ai::Tensor tmp_values = tensor;
7978
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
8079
auto conv_layer = std::make_shared<it_lab_ai::ConvolutionalLayer>(
81-
1, pads, 1, tmp_values, tmp_bias, impl2, 1);
80+
1, pads, 1, tmp_values, tmp_bias, impl2, 1, true);
8281
conv_layer->setName(it_lab_ai::kConvolution);
8382
layers.push_back(conv_layer);
8483
layerpostop.push_back(false);
@@ -94,18 +93,6 @@ void build_graph_linear(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
9493
}
9594
if (layer_type.find("Dense") != std::string::npos) {
9695
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
97-
it_lab_ai::Tensor tmp_tensor = it_lab_ai::Tensor(
98-
it_lab_ai::Shape({tensor.get_shape()[1], tensor.get_shape()[0]}),
99-
it_lab_ai::Type::kFloat);
100-
// kernel is always transposed ?
101-
for (size_t h = 0; h < tensor.get_shape()[0]; h++) {
102-
for (size_t w = 0; w < tensor.get_shape()[1]; w++) {
103-
tmp_tensor.set<float>(std::vector<size_t>({w, h}),
104-
tensor.get<float>({h, w}));
105-
}
106-
}
107-
//
108-
tensor = tmp_tensor;
10996
auto fc_layer = std::make_shared<it_lab_ai::FCLayer>(tensor, tmp_bias);
11097
fc_layer->setName(it_lab_ai::kFullyConnected);
11198
layers.push_back(fc_layer);

app/Graph/graph_build.cpp

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <algorithm>
1+
#include <algorithm>
22
#include <numeric>
33
#include <unordered_map>
44

@@ -62,29 +62,28 @@ std::vector<int> get_input_shape_from_json(const std::string& json_path) {
6262
}
6363
}
6464
}
65-
66-
throw std::runtime_error("Could not determine input shape from JSON");
65+
return {28};
6766
}
6867

6968
std::vector<float> process_model_output(const std::vector<float>& output,
7069
const std::string& model_name) {
7170
bool is_yolo = (model_name.find("yolo") != std::string::npos);
7271

7372
if (!is_yolo) {
74-
// Äëÿ íå-YOLO ìîäåëåé èñïîëüçóåì ñòàíäàðòíûé softmax
73+
// Для не-YOLO моделей используем стандартный softmax
7574
return softmax<float>(output);
7675
}
7776

78-
// Äëÿ YOLO ìîäåëåé àíàëèçèðóåì âûõîäíûå äàííûå
77+
// Для YOLO моделей анализируем выходные данные
7978
float sum_val = std::accumulate(output.begin(), output.end(), 0.0f);
8079

81-
// Åñëè ñóììà áëèçêà ê 1, âåðîÿòíîñòè óæå íîðìàëèçîâàíû
80+
// Если сумма близка к 1, вероятности уже нормализованы
8281
if (std::abs(sum_val - 1.0f) < 0.01f) {
8382
std::cout << "YOLO output already normalized, using as-is" << std::endl;
8483
return output;
8584
}
8685

87-
// Èíà÷å ïðèìåíÿåì softmax
86+
// Иначе применяем softmax
8887
std::cout << "Applying softmax to YOLO output" << std::endl;
8988
return softmax<float>(output);
9089
}
@@ -104,15 +103,15 @@ it_lab_ai::Tensor prepare_image(const cv::Mat& image,
104103
cv::Mat processed_image;
105104
cv::Size target_size(width, height);
106105

107-
bool is_yolo_model =
108-
(model_name.find("yolo") != std::string::npos || model_name.find("Google"));
106+
bool is_yolo_model = (model_name.find("yolo") != std::string::npos ||
107+
model_name.find("Google"));
109108

110109
if (image.rows == height && image.cols == width) {
111110
processed_image = image.clone();
112111
std::cout << "Image already at target size - no resize needed" << std::endl;
113112
} else {
114113
if (is_yolo_model) {
115-
// Äëÿ YOLO: ðåñàéç ñ ñîõðàíåíèåì ñîîòíîøåíèÿ ñòîðîí
114+
// Для YOLO: ресайз с сохранением соотношения сторон
116115
double scale = std::min(static_cast<double>(width) / image.cols,
117116
static_cast<double>(height) / image.rows);
118117
int new_width = static_cast<int>(image.cols * scale);
@@ -145,11 +144,11 @@ it_lab_ai::Tensor prepare_image(const cv::Mat& image,
145144
processed_image.convertTo(float_image, CV_32FC3);
146145

147146
if (is_yolo_model) {
148-
// Äëÿ YOLO: ïðîñòàÿ íîðìàëèçàöèÿ 0-1
147+
// Для YOLO: простая нормализация 0-1
149148
float_image /= 255.0;
150149
std::cout << "YOLO normalization: 0-1 range" << std::endl;
151150
} else {
152-
// ImageNet íîðìàëèçàöèÿ äëÿ äðóãèõ ìîäåëåé
151+
// ImageNet нормализация для других моделей
153152
float_image /= 255.0;
154153

155154
if (channels == 3) {
@@ -192,6 +191,23 @@ it_lab_ai::Tensor prepare_image(const cv::Mat& image,
192191
return it_lab_ai::make_tensor(data, shape);
193192
}
194193

194+
it_lab_ai::Tensor prepare_mnist_image(const cv::Mat& image) {
195+
cv::Mat gray_image;
196+
cv::cvtColor(image, gray_image, cv::COLOR_BGR2GRAY);
197+
std::vector<cv::Mat> channels;
198+
cv::split(image, channels);
199+
200+
std::vector<float> res(28 * 28);
201+
for (int i = 0; i < 28; ++i) {
202+
for (int j = 0; j < 28; ++j) {
203+
res[i * 28 + j] = channels[0].at<uchar>(j, i);
204+
}
205+
}
206+
207+
Shape sh({1, 1, 28, 28});
208+
return it_lab_ai::make_tensor(res, sh);
209+
}
210+
195211
int main(int argc, char* argv[]) {
196212
std::string model_name = "alexnet_mnist";
197213
bool parallel = false;
@@ -207,20 +223,15 @@ int main(int argc, char* argv[]) {
207223
std::string json_path = model_paths[model_name];
208224

209225
std::vector<int> input_shape;
210-
try {
211-
input_shape = get_input_shape_from_json(json_path);
212-
std::cout << "Input shape: [";
213-
for (size_t i = 0; i < input_shape.size(); ++i) {
214-
std::cout << input_shape[i];
215-
if (i < input_shape.size() - 1) std::cout << ", ";
216-
}
217-
std::cout << "]" << std::endl;
218-
} catch (const std::exception& e) {
219-
std::cerr << "Error reading input shape: " << e.what() << std::endl;
220-
return 1;
221-
}
226+
input_shape = get_input_shape_from_json(json_path);
222227

223-
std::string image_folder = IMAGENET_PATH;
228+
std::string image_folder;
229+
if (model_name == "alexnet_mnist") {
230+
image_folder = IMAGE28_PATH;
231+
}
232+
else {
233+
image_folder = IMAGENET_PATH;
234+
}
224235
std::cout << "Using image folder: " << image_folder << std::endl;
225236

226237
std::vector<std::string> image_paths;
@@ -254,33 +265,59 @@ int main(int argc, char* argv[]) {
254265
std::cout << "Original size: " << image.cols << "x" << image.rows
255266
<< ", channels: " << image.channels() << std::endl;
256267

257-
it_lab_ai::Tensor input = prepare_image(image, input_shape, model_name);
258-
259268
if (model_name == "alexnet_mnist") {
269+
// Специальная обработка для MNIST
270+
it_lab_ai::Tensor input = prepare_mnist_image(image);
271+
272+
// Создаем выходной тензор (заглушка - форма не важна для
273+
// build_graph_linear)
260274
it_lab_ai::Shape sh1({1, 5, 5, 3});
261275
std::vector<float> vec(75, 3);
262276
it_lab_ai::Tensor output = it_lab_ai::make_tensor(vec, sh1);
263277

264-
build_graph_linear(input, output, json_path, true, parallel);
278+
build_graph_linear(input, output, true, parallel);
265279

280+
// Получаем реальные выходы (10 классов для MNIST)
266281
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
267-
for (size_t i = 0; i < tmp_output.size(); i++) {
268-
if (tmp_output[i] >= 1e-6) {
269-
std::cout << "Image: " << image_path << " -> Class: " << i
270-
<< std::endl;
271-
}
282+
283+
// Выводим топ-3 предсказания для MNIST
284+
int top_n = std::min(3, static_cast<int>(tmp_output.size()));
285+
std::vector<int> indices(tmp_output.size());
286+
std::iota(indices.begin(), indices.end(), 0);
287+
std::partial_sort(
288+
indices.begin(), indices.begin() + top_n, indices.end(),
289+
[&](int a, int b) { return tmp_output[a] > tmp_output[b]; });
290+
291+
std::cout << "Top " << top_n << " predictions for MNIST:" << std::endl;
292+
for (int i = 0; i < top_n; i++) {
293+
int idx = indices[i];
294+
std::cout << " " << (i + 1) << ". Class " << idx << ": "
295+
<< std::fixed << std::setprecision(6)
296+
<< tmp_output[idx] * 100 << "%" << std::endl;
272297
}
298+
299+
// Итоговый результат
300+
int max_class = indices[0];
301+
float max_prob = tmp_output[max_class];
302+
std::cout << "Image: " << fs::path(image_path).filename().string()
303+
<< " -> Predicted digit: " << max_class
304+
<< " (probability: " << std::fixed << std::setprecision(6)
305+
<< max_prob * 100 << "%)" << std::endl;
306+
273307
} else {
308+
// Обычная обработка для других моделей
309+
it_lab_ai::Tensor input = prepare_image(image, input_shape, model_name);
310+
274311
size_t output_classes = 1000;
275312
it_lab_ai::Tensor output({1, output_classes}, it_lab_ai::Type::kFloat);
276313

277314
build_graph(input, output, json_path, true, parallel);
278315

279-
// Èñïîëüçóåì óëó÷øåííóþ îáðàáîòêó âûõîäîâ
316+
// Используем улучшенную обработку выходов
280317
std::vector<float> tmp_output =
281318
process_model_output(*output.as<float>(), model_name);
282319

283-
// Íàõîäèì òîï-5 êëàññîâ
320+
// Находим топ-5 классов
284321
int top_n = std::min(5, static_cast<int>(tmp_output.size()));
285322
std::vector<int> indices(tmp_output.size());
286323
std::iota(indices.begin(), indices.end(), 0);
@@ -300,7 +337,7 @@ int main(int argc, char* argv[]) {
300337
std::cout << std::endl;
301338
}
302339

303-
// Èòîãîâûé ðåçóëüòàò
340+
// Итоговый результат
304341
int max_class = indices[0];
305342
float max_prob = tmp_output[max_class];
306343
std::cout << "Image: " << fs::path(image_path).filename().string()

include/graph/graph.hpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,26 +137,27 @@ class Graph {
137137
for (size_t i = 0; i < traversal.size(); ++i) {
138138
int current_layer = traversal[i];
139139

140-
// Ïðîñòîé âûâîä
141-
/*std::string layer_name = getLayerName(current_layer);
142-
std::cout << "Processing layer #" << current_layer << " (" << layer_name
143-
<< ")" << std::endl;
144-
if (!inten_.empty()) {
145-
std::cout << "Input shape: ";
146-
for (size_t d = 0; d < inten_[0].get_shape().dims(); ++d) {
147-
std::cout << inten_[0].get_shape()[d] << " ";
148-
}
149-
std::cout << std::endl;
150-
}
151-
std::cout << "Layer #" << current_layer << " ("
152-
<< getLayerName(current_layer) << ") has "
153-
<< in_edges_[current_layer].size() << " input connections"
154-
<< std::endl;
155-
156-
for (int input_id : in_edges_[current_layer]) {
157-
std::cout << " - From layer #" << input_id << " ("
158-
<< getLayerName(input_id) << ")" << std::endl;
159-
}*/
140+
//// Ïðîñòîé âûâîä
141+
//std::string layer_name = getLayerName(current_layer);
142+
//std::cout << "Processing layer #" << current_layer << " (" << layer_name
143+
// << ")" << std::endl;
144+
//if (!inten_.empty()) {
145+
// std::cout << "Input shape: ";
146+
// for (size_t d = 0; d < inten_[0].get_shape().dims(); ++d) {
147+
// std::cout << inten_[0].get_shape()[d] << " ";
148+
// }
149+
// std::cout << std::endl;
150+
//}
151+
152+
//std::cout << "Layer #" << current_layer << " ("
153+
// << getLayerName(current_layer) << ") has "
154+
// << in_edges_[current_layer].size() << " input connections"
155+
// << std::endl;
156+
157+
//for (int input_id : in_edges_[current_layer]) {
158+
// std::cout << " - From layer #" << input_id << " ("
159+
// << getLayerName(input_id) << ")" << std::endl;
160+
//}
160161
#ifdef ENABLE_STATISTIC_TIME
161162
auto start = std::chrono::high_resolution_clock::now();
162163
#endif

0 commit comments

Comments
 (0)