Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

file(GLOB_RECURSE HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
41 changes: 41 additions & 0 deletions include/graph/graph.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef GRAPH_H
#define GRAPH_H

#include <memory>
#include <unordered_map>
#include <vector>

#include "layer/layer.h"
#include "tensor/tensor.h"

class Graph {
private:
std::unordered_map<int, std::shared_ptr<Layer>> layers_;
Tensor<double> inputTensor_;
Tensor<double>* outputTensor_;
int start_ = -1;
int end_ = -1;
bool bfs_helper(int start, int vert, bool flag,
std::vector<int>* v_ord) const;

public:
Graph();

void addLayer(std::shared_ptr<Layer> lay);
void addEdge(std::shared_ptr<Layer> layPrev, std::shared_ptr<Layer> layNext);
void removeEdge(std::shared_ptr<Layer> layPrev,
std::shared_ptr<Layer> layNext);
void removeLayer(std::shared_ptr<Layer> lay);
int getLayers() const;
int getEdges() const;
bool empty() const;
bool hasPath(std::shared_ptr<Layer> layPrev,
std::shared_ptr<Layer> layNext) const;
std::vector<int> BFS(int start);
void setInput(std::shared_ptr<Layer> lay, Tensor<double>& vec);
void setOutput(std::shared_ptr<Layer> lay, Tensor<double>& vec);
void inference();
~Graph();
};

#endif
30 changes: 30 additions & 0 deletions include/layer/layer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#ifndef LAYER_H
#define LAYER_H

#include <list>
#include <memory>
#include <string>

#include "tensor/tensor.h"

class Layer {
public:
int id;
std::string name;
std::list<std::shared_ptr<Layer>> neighbors_;

Layer() = default;
Layer(const std::string& name);
Layer(std::string&& name);

virtual ~Layer() = default;
void setID(int id) { this->id = id; }
int getID() const { return id; }
virtual void run(const Tensor<double>& input, Tensor<double>& output) {}
virtual Shape get_output_shape() { return Shape({1, 1, 1, 1}); }

void addNeighbor(std::shared_ptr<Layer> neighbor);
void removeNeighbor(std::shared_ptr<Layer> neighbor);
};
#endif
32 changes: 32 additions & 0 deletions include/model_parser/model_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <string>
#include <unordered_map>
#include <vector>

#include "graph/graph.h"

class AnyLayer : public Layer {
public:
std::string Name;
std::unordered_map<std::string, std::string> Attributes;
std::vector<float> Weights;
std::vector<float> Bias;

AnyLayer(const std::string& name);
~AnyLayer() = default;

void addNeighbor(Layer* neighbor);
void removeNeighbor(Layer* neighbor);
};

enum Model { ONNX, PYTORCH, OPENCV };

class ModelParser {
protected:
std::string m_filename;

public:
virtual ~ModelParser() = default;
virtual Graph Parse() = 0;
};
31 changes: 31 additions & 0 deletions include/model_parser/onnx_model_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <onnx.pb.h>

#include <fstream>
#include <list>
#include <string>
#include <unordered_map>
#include <vector>

#include "graph/graph.h"
#include "model_parser/model_parser.h"

class ONNX_ModelParser : public ModelParser {
private:
onnx::ModelProto m_model;

std::unordered_map<std::string, const onnx::TensorProto*> m_weights;

public:
ONNX_ModelParser(const std::string& filename);
Graph Parse() override;

private:
bool LoadModel(const std::string& filename);
bool ParseAttributes(
const onnx::NodeProto& node,
std::unordered_map<std::string, std::string>& attributes);
bool ParseWeightsAndBias(const onnx::NodeProto& node,
const std::shared_ptr<AnyLayer>& any_layer);
};
Loading
Loading