Skip to content

Commit

Permalink
inference completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Semyon1104 committed Nov 29, 2024
1 parent 46dd5a7 commit d95d3b6
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 179 deletions.
131 changes: 130 additions & 1 deletion app/Graph/build.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,132 @@
#include "build.hpp"

#include <stdexcept>
void build_graph(Tensor input, Tensor output) {
std::vector<std::shared_ptr<Layer>> layers;

std::string json_file = MODEL_PATH;
json model_data = read_json(json_file);

std::cout << "Loaded model data from JSON." << std::endl;

for (const auto& layer_data : model_data) {
std::string layer_type = layer_data["type"];
std::cout << "Processing layer of type: " << layer_type << std::endl;

Tensor tensor =
create_tensor_from_json(layer_data["weights"], Type::kFloat);

if (layer_type.find("Conv") != std::string::npos) {
Shape shape = tensor.get_shape();
std::cout << "PoolingLayer shape: ";
for (size_t i = 0; i < shape.dims(); ++i) {
std::cout << shape[i] << " ";
}
std::cout << std::endl;

Tensor tmp_values = tensor;
Tensor tmp_bias = make_tensor(tensor.get_bias());

auto conv_layer =
std::make_shared<ConvolutionalLayer>(1, 0, 1, tmp_values, tmp_bias);
conv_layer->setName(kConvolution);
layers.push_back(conv_layer);
std::cout << "ConvLayer added to layers." << std::endl;
}

if (layer_type.find("Dense") != std::string::npos) {
Tensor tmp_values = tensor;
std::vector<float> Values_vector = *tensor.as<float>();
std::vector<std::vector<float>> Values_vector_2d(
tensor.get_shape()[0],
std::vector<float>(tensor.get_shape()[1], 0.0f));
int q = 0;
for (int i = 0; i < Values_vector.size(); i++) {
Values_vector_2d[q][i - (q * tensor.get_shape()[1])] = Values_vector[i];
if ((i + 1) % tensor.get_shape()[1] == 0) {
q++;
}
}
std::vector<std::vector<float>> Values_vector_2d_2(
tensor.get_shape()[1],
std::vector<float>(tensor.get_shape()[0], 0.0f));

for (int i = 0; i < tensor.get_shape()[0]; ++i) {
for (int j = 0; j < tensor.get_shape()[1]; ++j) {
Values_vector_2d_2[j][i] = Values_vector_2d[i][j];
}
}
std::vector<float> Values_vector_1d(
tensor.get_shape()[0] * tensor.get_shape()[1], 0.0f);
int index_1d = 0;

for (int j = 0; j < tensor.get_shape()[1]; ++j) {
for (int k = 0; k < tensor.get_shape()[0]; ++k) {
Values_vector_1d[index_1d++] = Values_vector_2d_2[j][k];
}
}

Shape shape_fc({tensor.get_shape()[1], tensor.get_shape()[0]});
Tensor values = make_tensor<float>(Values_vector_1d, shape_fc);
Tensor tmp_bias = make_tensor(tensor.get_bias());

auto fc_layer = std::make_shared<FCLayer>(values, tmp_bias);
fc_layer->setName(kFullyConnected);
layers.push_back(fc_layer);
std::cout << "DenseLayer added to layers." << std::endl;
}

if (layer_type.find("Pool") != std::string::npos) {
Shape shape = {2, 2};
std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1]
<< std::endl;
auto pool_layer = std::make_shared<PoolingLayer>(shape);
pool_layer->setName(kPooling);
layers.push_back(pool_layer);
std::cout << "PoolingLayer added to layers." << std::endl;
}

if (layer_type.find("Flatten") != std::string::npos) {
auto flatten_layer = std::make_shared<FlattenLayer>();
flatten_layer->setName(kFlatten);
layers.push_back(flatten_layer);
std::cout << "FlattenLayer added to layers." << std::endl;
}

if (layer_type.find("Dropout") != std::string::npos) {
auto dropout_layer = std::make_shared<DropOutLayer>(0.5);
dropout_layer->setName(kDropout);
layers.push_back(dropout_layer);
std::cout << "DropOutLayer added to layers with probability 0.5."
<< std::endl;
}
}
std::cout << "number of layers - " << layers.size() + 1 << std::endl;
Graph graph(static_cast<int>(layers.size()));
InputLayer a1(kNhwc, kNchw, 1, 2);

std::cout << "InputLayer created." << std::endl;

graph.setInput(a1, input);
std::cout << "Input set in graph." << std::endl;

graph.makeConnection(a1, *layers[0]);
std::cout << "Connection made between InputLayer and first layer."
<< std::endl;

for (size_t i = 0; i < layers.size() - 1; ++i) {
graph.makeConnection(*layers[i], *layers[i + 1]);
}

graph.setOutput(*layers.back(), output);
std::cout << "Output set in graph." << std::endl;

std::cout << "Starting inference..." << std::endl;
graph.inference();
std::cout << "Inference completed." << std::endl;

std::vector<float> tmp = *output.as<float>();
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
for (float i : tmp) {
std::cout << i << " ";
}
}
16 changes: 15 additions & 1 deletion app/Graph/build.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
#pragma once
#include <iostream>
#include <stdexcept>
#include <variant>
#include <vector>
#include <opencv2/opencv.hpp>
#include "Weights_Reader/reader_weights.hpp"
#include "graph/graph.hpp"
#include "layers/ConvLayer.hpp"
#include "layers/DropOutLayer.hpp"
#include "layers/EWLayer.hpp"
#include "layers/FCLayer.hpp"
#include "layers/FlattenLayer.hpp"
#include "layers/InputLayer.hpp"
#include "layers/OutputLayer.hpp"
#include "layers/PoolingLayer.hpp"

void build_graph(Tensor input, Tensor output);
144 changes: 11 additions & 133 deletions app/Graph/graph_build.cpp
Original file line number Diff line number Diff line change
@@ -1,152 +1,30 @@
#include <iostream>
#include <stdexcept>
#include <variant>
#include <vector>

#include "Weights_Reader/reader_weights.hpp"
#include "build.hpp"
#include "graph/graph.hpp"
#include "layers/ConvLayer.hpp"
#include "layers/DropOutLayer.hpp"
#include "layers/EWLayer.hpp"
#include "layers/FCLayer.hpp"
#include "layers/FlattenLayer.hpp"
#include "layers/InputLayer.hpp"
#include "layers/OutputLayer.hpp"
#include "layers/PoolingLayer.hpp"
#include "build.cpp"

using namespace itlab_2023;

void build_graph(Tensor input, Tensor output) {
std::vector<std::shared_ptr<Layer>> layers;

std::string json_file = MODEL_PATH;
json model_data = read_json(json_file);

std::cout << "Loaded model data from JSON." << std::endl;

for (const auto& layer_data : model_data) {
std::string layer_type = layer_data["type"];
std::cout << "Processing layer of type: " << layer_type << std::endl;

Tensor tensor =
create_tensor_from_json(layer_data["weights"], Type::kFloat);

if (layer_type.find("Conv") != std::string::npos) {
Shape shape = tensor.get_shape();
std::cout << "PoolingLayer shape: ";
for (size_t i = 0; i < shape.dims(); ++i) {
std::cout << shape[i] << " ";
}
std::cout << std::endl;

Tensor tmp_values = tensor;
Tensor tmp_bias = make_tensor(tensor.get_bias());

auto conv_layer =
std::make_shared<ConvolutionalLayer>(1, 0, 0, tmp_values, tmp_bias);
conv_layer->setName(kConvolution);
layers.push_back(conv_layer);
std::cout << "ConvLayer added to layers." << std::endl;
}

if (layer_type.find("Dense") != std::string::npos) {
Tensor tmp_values = tensor;
Tensor tmp_bias = make_tensor(tensor.get_bias());

auto fc_layer = std::make_shared<FCLayer>(tmp_values, tmp_bias);
fc_layer->setName(kFullyConnected);
layers.push_back(fc_layer);
std::cout << "DenseLayer added to layers." << std::endl;
}

if (layer_type.find("Pool") != std::string::npos) {
Shape shape = {2, 2};
std::cout << "PoolingLayer shape: " << shape[0] << "x" << shape[1]
<< std::endl;
auto pool_layer = std::make_shared<PoolingLayer>(shape);
pool_layer->setName(kPooling);
layers.push_back(pool_layer);
std::cout << "PoolingLayer added to layers." << std::endl;
}

if (layer_type.find("Flatten") != std::string::npos) {
auto flatten_layer = std::make_shared<FlattenLayer>();
flatten_layer->setName(kFlatten);
layers.push_back(flatten_layer);
std::cout << "FlattenLayer added to layers." << std::endl;
}

if (layer_type.find("Dropout") != std::string::npos) {
auto dropout_layer = std::make_shared<DropOutLayer>(0.5);
dropout_layer->setName(kDropout);
layers.push_back(dropout_layer);
std::cout << "DropOutLayer added to layers with probability 0.5."
<< std::endl;
}
}
std::cout << "number of layers - " << layers.size() + 1<< std::endl;
Graph graph(static_cast<int>(layers.size()));
InputLayer a1(kNhwc, kNchw, 1, 2);

std::cout << "InputLayer created." << std::endl;

graph.setInput(a1, input);
std::cout << "Input set in graph." << std::endl;

graph.makeConnection(a1, *layers[0]);
std::cout << "Connection made between InputLayer and first layer."
<< std::endl;

for (size_t i = 0; i < layers.size() - 1; ++i) {
graph.makeConnection(*layers[i], *layers[i + 1]);
std::cout << "Connection made between layer " << i << " ("
<< layerTypeToString(layers[i]->getName()) << ")"
<< " and layer " << i + 1 << " ("
<< layerTypeToString(layers[i + 1]->getName()) << ")"
<< std::endl;
}



graph.setOutput(*layers.back(), output);
std::cout << "Output set in graph." << std::endl;

std::cout << "Starting inference..." << std::endl;
graph.inference();
std::cout << "Inference completed." << std::endl;

std::vector<float> tmp = *output.as<float>();
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
for (float i : tmp) {
std::cout << i << " ";
}
}

int main() {
std::string image_path = IMAGE1_PATH;
cv::Mat image = cv::imread(image_path);
if (image.empty()) {
throw std::runtime_error("Failed to load image");
}
cv::Mat resized_image;
cv::resize(image, resized_image, cv::Size(227, 227));
cv::cvtColor(image, image, cv::COLOR_BGR2GRAY);
cv::resize(image, resized_image, cv::Size(28, 28));
std::vector<cv::Mat> channels;

cv::split(resized_image, channels);

int count_pic = 1;
std::vector<float> res(count_pic * 227 * 227 * 3);
int c = 0;
for (int i = 0; i < 227; ++i) {
for (int j = 0; j < 227; ++j) {
res[c] = channels[2].at<uchar>(i, j);
c++;
res[c] = channels[1].at<uchar>(i, j);
c++;
res[c] = channels[0].at<uchar>(i, j);
c++;
std::vector<float> res(count_pic * 28 * 28);

for (int i = 0; i < 28; ++i) {
for (int j = 0; j < 28; ++j) {
res[i * 28 + j] = channels[0].at<uchar>(i,j);
}
}
Shape sh({static_cast<size_t>(count_pic), 227, 227, 3});
Shape sh({static_cast<size_t>(count_pic), 28, 28, 1});
Tensor t = make_tensor<float>(res, sh);
Tensor input = t;

Expand Down
2 changes: 1 addition & 1 deletion include/Weights_Reader/reader_weights.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ json read_json(const std::string& filename);
void extract_values_without_bias(const json& j, std::vector<float>& values);
void extract_values_from_json(const json& j, std::vector<float>& values);
void parse_json_shape(const json& j, std::vector<size_t>& shape,
size_t dim = 0);
size_t dim);
Tensor create_tensor_from_json(const json& j, Type type);
void extract_bias_from_json(const json& j, std::vector<float>& bias);
Loading

0 comments on commit d95d3b6

Please sign in to comment.