Skip to content

Commit 937d39a

Browse files
Semyon1104AndreySorokin7
andauthored
Add input as array of images (#168)
Co-authored-by: AndreySorokin7 <andrey_sorokin_nn@mail,ru>
1 parent 9a3c083 commit 937d39a

File tree

4 files changed

+51
-35
lines changed

4 files changed

+51
-35
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,14 @@ To start the testing process locally, you need to go to the directory
117117
# **Some files used to create the library**
118118
### *neural network models*
119119
You need to download [Alexnet-model.h5](https://github.com/moizahmed97/Convolutional-Neural-Net-Designer/blob/master/AlexNet-model.h5) to the folder *docs*
120-
# **How do I get a file with the weights of the model to launch the inference?**
121-
You need to run the script *parcer.py* that is located in *app/AlexNet* to read weights from a model *Alexnet-model.h5* and the json file with the weights will be stored in the *docs* folder.
120+
121+
# **How do I launch the inference?**
122+
* You need to run the script *parcer.py* that is located in app/AlexNet to read weights from a model *Alexnet-model.h5* and the json file with the weights will be stored in the *docs* folder.
123+
* Then put the test images in png format in the folder *docs/input*
124+
122125
# **Accuracy validation**
123126
To run accuracy validation you need to use the MNIST dataset, which you can download [here](https://github.com/DeepTrackAI/MNIST_dataset/tree/main/mnist/test) and put it in a folder *docs/mnist/mnist/test*
124127
Now you can run accuracy check - *build\bin\ACC_MNIST.exe*
128+
125129
# **Structure of our library**
126130
![Class diagram](./docs/class_diagram.svg)

app/Graph/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,13 @@ add_custom_command(TARGET ACC_MNIST POST_BUILD
4646
endif()
4747

4848
file(DOWNLOAD
49-
"https://raw.githubusercontent.com/opencv/opencv/4.x/samples/data/lena.jpg"
50-
"${CMAKE_CURRENT_BINARY_DIR}/image.jpg"
49+
"https://raw.githubusercontent.com/DeepTrackAI/MNIST_dataset/main/mnist/test/1_000008.png"
50+
"${CMAKE_SOURCE_DIR}/docs/input/test1.png"
5151
SHOW_PROGRESS
5252
STATUS status_code
5353
LOG log_file
5454
)
55-
add_definitions(-DIMAGE1_PATH="${CMAKE_CURRENT_BINARY_DIR}/image.jpg")
55+
56+
add_definitions(-DIMAGE1_PATH="${CMAKE_SOURCE_DIR}/docs/input/")
5657
add_definitions(-DMODEL_PATH="${CMAKE_SOURCE_DIR}/docs/model_data_alexnet_1.json")
5758
add_definitions(-DMNIST_PATH="${CMAKE_SOURCE_DIR}/docs/mnist/mnist/test")

app/Graph/build.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <filesystem>
12
#include <iostream>
23
#include <opencv2/opencv.hpp>
34
#include <stdexcept>

app/Graph/graph_build.cpp

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,56 @@
11
#include "build.cpp"
22
#include "build.hpp"
33

4+
namespace fs = std::filesystem;
45
using namespace itlab_2023;
56

67
int main() {
7-
std::string image_path = IMAGE1_PATH;
8-
cv::Mat image = cv::imread(image_path);
9-
if (image.empty()) {
10-
throw std::runtime_error("Failed to load image");
8+
std::string image_folder = IMAGE1_PATH;
9+
std::vector<std::string> image_paths;
10+
11+
for (const auto& entry : fs::directory_iterator(image_folder)) {
12+
if (entry.path().extension() == ".png") {
13+
image_paths.push_back(entry.path().string());
14+
}
1115
}
12-
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
13-
std::vector<cv::Mat> channels;
1416

15-
cv::split(image, channels);
17+
if (image_paths.empty()) {
18+
throw std::runtime_error("No PNG images found in the folder");
19+
}
20+
21+
for (const auto& image_path : image_paths) {
22+
cv::Mat image = cv::imread(image_path);
23+
if (image.empty()) {
24+
std::cerr << "Failed to load image: " << image_path << std::endl;
25+
continue;
26+
}
1627

17-
int count_pic = 1;
18-
std::vector<float> res(count_pic * 28 * 28);
28+
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
29+
std::vector<cv::Mat> channels;
30+
cv::split(image, channels);
1931

20-
for (int i = 0; i < 28; ++i) {
21-
for (int j = 0; j < 28; ++j) {
22-
res[i * 28 + j] = channels[0].at<uchar>(j, i);
32+
std::vector<float> res(28 * 28);
33+
for (int i = 0; i < 28; ++i) {
34+
for (int j = 0; j < 28; ++j) {
35+
res[i * 28 + j] = channels[0].at<uchar>(j, i);
36+
}
2337
}
24-
}
25-
Shape sh({static_cast<size_t>(count_pic), 1, 28, 28});
26-
// move to reshape layer
27-
Tensor t = make_tensor<float>(res, sh);
28-
Tensor input = t;
29-
30-
Shape sh1({1, 5, 5, 3});
31-
std::vector<float> vec;
32-
vec.reserve(75);
33-
for (int i = 0; i < 75; ++i) {
34-
vec.push_back(3);
35-
}
36-
Tensor output = make_tensor(vec, sh1);
3738

38-
build_graph(input, output, true);
39+
Shape sh({1, 1, 28, 28});
40+
Tensor input = make_tensor<float>(res, sh);
41+
42+
Shape sh1({1, 5, 5, 3});
43+
std::vector<float> vec(75, 3);
44+
Tensor output = make_tensor(vec, sh1);
45+
46+
build_graph(input, output, true);
3947

40-
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
41-
for (size_t i = 0; i < tmp_output.size(); i++) {
42-
if (tmp_output[i] >= 1e-6) {
43-
std::cout << i << std::endl;
48+
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
49+
for (size_t i = 0; i < tmp_output.size(); i++) {
50+
if (tmp_output[i] >= 1e-6) {
51+
std::cout << "Image: " << image_path << " -> Class: " << i << std::endl;
52+
}
4453
}
4554
}
55+
return 0;
4656
}

0 commit comments

Comments
 (0)