|
1 |
| -#include <iostream> |
2 |
| -#include <stdexcept> |
3 |
| -#include <variant> |
4 |
| -#include <vector> |
5 |
| - |
6 |
| -#include "Weights_Reader/reader_weights.hpp" |
7 | 1 | #include "build.hpp"
|
8 |
| -#include "graph/graph.hpp" |
9 |
| -#include "layers/ConvLayer.hpp" |
10 |
| -#include "layers/DropOutLayer.hpp" |
11 |
| -#include "layers/EWLayer.hpp" |
12 |
| -#include "layers/FCLayer.hpp" |
13 |
| -#include "layers/FlattenLayer.hpp" |
14 |
| -#include "layers/InputLayer.hpp" |
15 |
| -#include "layers/OutputLayer.hpp" |
16 |
| -#include "layers/PoolingLayer.hpp" |
| 2 | +#include "build.cpp" |
17 | 3 |
|
18 | 4 | using namespace itlab_2023;
|
19 | 5 |
|
20 |
| -void build_graph(Tensor input, Tensor output) { |
21 |
| - std::vector<std::shared_ptr<Layer>> layers; |
22 |
| - |
23 |
| - std::string json_file = MODEL_PATH; |
24 |
| - json model_data = read_json(json_file); |
25 |
| - |
26 |
| - std::cout << "Loaded model data from JSON." << std::endl; |
27 |
| - |
28 |
| - for (const auto& layer_data : model_data) { |
29 |
| - std::string layer_type = layer_data["type"]; |
30 |
| - std::cout << "Processing layer of type: " << layer_type << std::endl; |
31 |
| - |
32 |
| - Tensor tensor = |
33 |
| - create_tensor_from_json(layer_data["weights"], Type::kFloat); |
34 |
| - |
35 |
| - if (layer_type.find("Conv") != std::string::npos) { |
36 |
| - Shape shape = tensor.get_shape(); |
37 |
| - std::cout << "PoolingLayer shape: "; |
38 |
| - for (size_t i = 0; i < shape.dims(); ++i) { |
39 |
| - std::cout << shape[i] << " "; |
40 |
| - } |
41 |
| - std::cout << std::endl; |
42 |
| - |
43 |
| - Tensor tmp_values = tensor; |
44 |
| - Tensor tmp_bias = make_tensor(tensor.get_bias()); |
45 |
| - |
46 |
| - auto conv_layer = |
47 |
| - std::make_shared<ConvolutionalLayer>(1, 0, 0, tmp_values, tmp_bias); |
48 |
| - conv_layer->setName(kConvolution); |
49 |
| - layers.push_back(conv_layer); |
50 |
| - std::cout << "ConvLayer added to layers." << std::endl; |
51 |
| - } |
52 |
| - |
53 |
| - if (layer_type.find("Dense") != std::string::npos) { |
54 |
| - Tensor tmp_values = tensor; |
55 |
| - Tensor tmp_bias = make_tensor(tensor.get_bias()); |
56 |
| - |
57 |
| - auto fc_layer = std::make_shared<FCLayer>(tmp_values, tmp_bias); |
58 |
| - fc_layer->setName(kFullyConnected); |
59 |
| - layers.push_back(fc_layer); |
60 |
| - std::cout << "DenseLayer added to layers." << std::endl; |
61 |
| - } |
62 |
| - |
63 |
| - if (layer_type.find("Pool") != std::string::npos) { |
64 |
| - Shape shape = {2, 2}; |
65 |
| - std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1] |
66 |
| - << std::endl; |
67 |
| - auto pool_layer = std::make_shared<PoolingLayer>(shape); |
68 |
| - pool_layer->setName(kPooling); |
69 |
| - layers.push_back(pool_layer); |
70 |
| - std::cout << "PoolingLayer added to layers." << std::endl; |
71 |
| - } |
72 |
| - |
73 |
| - if (layer_type.find("Flatten") != std::string::npos) { |
74 |
| - auto flatten_layer = std::make_shared<FlattenLayer>(); |
75 |
| - flatten_layer->setName(kFlatten); |
76 |
| - layers.push_back(flatten_layer); |
77 |
| - std::cout << "FlattenLayer added to layers." << std::endl; |
78 |
| - } |
79 |
| - |
80 |
| - if (layer_type.find("Dropout") != std::string::npos) { |
81 |
| - auto dropout_layer = std::make_shared<DropOutLayer>(0.5); |
82 |
| - dropout_layer->setName(kDropout); |
83 |
| - layers.push_back(dropout_layer); |
84 |
| - std::cout << "DropOutLayer added to layers with probability 0.5." |
85 |
| - << std::endl; |
86 |
| - } |
87 |
| - } |
88 |
| - std::cout << "number of layers - " << layers.size() + 1<< std::endl; |
89 |
| - Graph graph(static_cast<int>(layers.size())); |
90 |
| - InputLayer a1(kNhwc, kNchw, 1, 2); |
91 |
| - |
92 |
| - std::cout << "InputLayer created." << std::endl; |
93 |
| - |
94 |
| - graph.setInput(a1, input); |
95 |
| - std::cout << "Input set in graph." << std::endl; |
96 |
| - |
97 |
| - graph.makeConnection(a1, *layers[0]); |
98 |
| - std::cout << "Connection made between InputLayer and first layer." |
99 |
| - << std::endl; |
100 |
| - |
101 |
| - for (size_t i = 0; i < layers.size() - 1; ++i) { |
102 |
| - graph.makeConnection(*layers[i], *layers[i + 1]); |
103 |
| - std::cout << "Connection made between layer " << i << " (" |
104 |
| - << layerTypeToString(layers[i]->getName()) << ")" |
105 |
| - << " and layer " << i + 1 << " (" |
106 |
| - << layerTypeToString(layers[i + 1]->getName()) << ")" |
107 |
| - << std::endl; |
108 |
| - } |
109 |
| - |
110 |
| - |
111 |
| - |
112 |
| - graph.setOutput(*layers.back(), output); |
113 |
| - std::cout << "Output set in graph." << std::endl; |
114 |
| - |
115 |
| - std::cout << "Starting inference..." << std::endl; |
116 |
| - graph.inference(); |
117 |
| - std::cout << "Inference completed." << std::endl; |
118 |
| - |
119 |
| - std::vector<float> tmp = *output.as<float>(); |
120 |
| - std::vector<float> tmp_output = softmax<float>(*output.as<float>()); |
121 |
| - for (float i : tmp) { |
122 |
| - std::cout << i << " "; |
123 |
| - } |
124 |
| -} |
125 |
| - |
126 | 6 | int main() {
|
127 | 7 | std::string image_path = IMAGE1_PATH;
|
128 | 8 | cv::Mat image = cv::imread(image_path);
|
129 | 9 | if (image.empty()) {
|
130 | 10 | throw std::runtime_error("Failed to load image");
|
131 | 11 | }
|
132 | 12 | cv::Mat resized_image;
|
133 |
| - cv::resize(image, resized_image, cv::Size(227, 227)); |
| 13 | + cv::cvtColor(image, image, cv::COLOR_BGR2GRAY); |
| 14 | + cv::resize(image, resized_image, cv::Size(28, 28)); |
134 | 15 | std::vector<cv::Mat> channels;
|
| 16 | + |
135 | 17 | cv::split(resized_image, channels);
|
| 18 | + |
136 | 19 | int count_pic = 1;
|
137 |
| - std::vector<float> res(count_pic * 227 * 227 * 3); |
138 |
| - int c = 0; |
139 |
| - for (int i = 0; i < 227; ++i) { |
140 |
| - for (int j = 0; j < 227; ++j) { |
141 |
| - res[c] = channels[2].at<uchar>(i, j); |
142 |
| - c++; |
143 |
| - res[c] = channels[1].at<uchar>(i, j); |
144 |
| - c++; |
145 |
| - res[c] = channels[0].at<uchar>(i, j); |
146 |
| - c++; |
| 20 | + std::vector<float> res(count_pic * 28 * 28); |
| 21 | + |
| 22 | + for (int i = 0; i < 28; ++i) { |
| 23 | + for (int j = 0; j < 28; ++j) { |
| 24 | + res[i * 28 + j] = channels[0].at<uchar>(i,j); |
147 | 25 | }
|
148 | 26 | }
|
149 |
| - Shape sh({static_cast<size_t>(count_pic), 227, 227, 3}); |
| 27 | + Shape sh({static_cast<size_t>(count_pic), 28, 28, 1}); |
150 | 28 | Tensor t = make_tensor<float>(res, sh);
|
151 | 29 | Tensor input = t;
|
152 | 30 |
|
|
0 commit comments