Skip to content
Merged
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: 1 addition & 1 deletion app/Converters/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tensorflow==2.19.0
tensorflow>=2.19.0
onnx>=1.15.0
ultralytics>=8.0.0
numpy>=1.21.0
Expand Down
7 changes: 0 additions & 7 deletions app/Graph/build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,12 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
it_lab_ai::Tensor tmp_bias = it_lab_ai::make_tensor(tensor.get_bias());
auto conv_layer = std::make_shared<it_lab_ai::ConvolutionalLayer>(
1, pads, 1, tmp_values, tmp_bias, impl2);
conv_layer->setName(it_lab_ai::kConvolution);
layers.push_back(conv_layer);
layerpostop.push_back(false);
if (comments) std::cout << "ConvLayer added to layers." << std::endl;
}
if (layer_type.find("relu") != std::string::npos) {
auto ew_layer = std::make_shared<it_lab_ai::EWLayer>("relu");
ew_layer->setName(it_lab_ai::kElementWise);
layers.push_back(ew_layer);
layerpostop.push_back(true);
if (comments)
Expand All @@ -101,7 +99,6 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
//
tensor = tmp_tensor;
auto fc_layer = std::make_shared<it_lab_ai::FCLayer>(tensor, tmp_bias);
fc_layer->setName(it_lab_ai::kFullyConnected);
layers.push_back(fc_layer);
layerpostop.push_back(false);
if (comments) std::cout << "DenseLayer added to layers." << std::endl;
Expand All @@ -120,7 +117,6 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
<< std::endl;
auto pool_layer =
std::make_shared<it_lab_ai::PoolingLayer>(shape, pooltype, impl1);
pool_layer->setName(it_lab_ai::kPooling);
layers.push_back(pool_layer);
layerpostop.push_back(false);
if (comments) std::cout << "PoolingLayer added to layers." << std::endl;
Expand All @@ -129,15 +125,13 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
if (layer_type.find("Flatten") != std::string::npos) {
auto flatten_layer = std::make_shared<it_lab_ai::FlattenLayer>(
std::vector<size_t>({0, 3, 2, 1}));
flatten_layer->setName(it_lab_ai::kFlatten);
layers.push_back(flatten_layer);
layerpostop.push_back(false);
if (comments) std::cout << "FlattenLayer added to layers." << std::endl;
}

if (layer_type.find("Dropout") != std::string::npos) {
auto dropout_layer = std::make_shared<it_lab_ai::DropOutLayer>(0.0);
dropout_layer->setName(it_lab_ai::kDropout);
layers.push_back(dropout_layer);
layerpostop.push_back(false);
if (comments)
Expand All @@ -151,7 +145,6 @@ void build_graph(it_lab_ai::Tensor& input, it_lab_ai::Tensor& output,
std::cout << "number of layers - " << layers.size() + 1 << std::endl;
it_lab_ai::Graph graph(static_cast<int>(layers.size()));
it_lab_ai::InputLayer a1(it_lab_ai::kNchw, it_lab_ai::kNchw);
a1.setName(it_lab_ai::kInput);

if (comments) std::cout << "InputLayer created." << std::endl;

Expand Down
3 changes: 3 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
file(GLOB_RECURSE graph_headers graph/*.h graph/*.hpp)
set(GRAPH_HEADERS "${graph_headers}" PARENT_SCOPE)

file(GLOB_RECURSE graphT_headers graph_transformations/*.h graph_transformations/*.hpp)
set(GRAPHT_HEADERS "${graphT_headers}" PARENT_SCOPE)

file(GLOB_RECURSE layers_headers layers/*.h layers/*.hpp)
set(LAYERS_HEADERS "${layers_headers}" PARENT_SCOPE)

Expand Down
62 changes: 57 additions & 5 deletions include/graph/graph.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ struct BranchState {

class Graph {
int BiggestSize_;
int V_;
std::vector<Layer*> layers_;
std::vector<int> arrayV_;
std::vector<int> arrayE_;
int V_; // amount of ids
std::vector<Layer*> layers_; // layers vector with some ids
std::vector<int> arrayV_; // vertices (id -> vertex number)
std::vector<int> arrayE_; // edges (vertex number -> id)
std::vector<Tensor> inten_;
std::vector<Tensor> outten_;
Tensor* outtenres_;
int start_;
int end_;
std::list<BranchState> branch_list_;
std::vector<std::vector<int>> in_edges_;
std::vector<std::vector<int>> in_edges_; // next -> prev
std::vector<std::vector<std::pair<int, int>>> split_distribution_;
int count_used_split_distribution_;
#ifdef ENABLE_STATISTIC_TENSORS
Expand Down Expand Up @@ -67,6 +67,35 @@ class Graph {
in_edges_.clear();
}

int getVertexValue(size_t layerID) const {
if (layerID >= arrayV_.size()) {
throw std::invalid_argument("ArrayV does not contain this ID.");
}
return arrayV_[layerID];
}

int getEdgeValue(size_t pos) const {
if (pos >= arrayE_.size()) {
throw std::invalid_argument("ArrayE does not contain this.");
}
return arrayE_[pos];
}

size_t getInputsSize(size_t layerID) const {
if (layerID >= in_edges_.size()) {
throw std::invalid_argument("Input edges array do not contain this ID.");
}
return in_edges_[layerID].size();
}

int getLayersCount() const { return V_; }
const Layer& getLayerFromID(size_t layerID) const {
if (layerID >= layers_.size()) {
throw std::invalid_argument("Layers do not contain this ID.");
}
return *layers_[layerID];
}

void setInput(Layer& lay, Tensor& vec) {
lay.setID(0);
layers_.push_back(&lay);
Expand All @@ -76,6 +105,29 @@ class Graph {
V_++;
in_edges_.resize(1);
}

void addSingleLayer(Layer& lay) {
bool layer_exists = false;
for (const auto* layer : layers_) {
if (layer == &lay) {
layer_exists = true;
break;
}
}

if (!layer_exists) {
lay.setID(V_);
layers_.push_back(&lay);
arrayV_.push_back(static_cast<int>(arrayE_.size()));

if (V_ >= static_cast<int>(in_edges_.size())) {
in_edges_.resize(V_ + 1);
}

V_++;
}
}

void makeConnection(const Layer& layPrev, Layer& layNext) {
bool layer_exists = false;
for (const auto* layer : layers_) {
Expand Down
16 changes: 16 additions & 0 deletions include/graph_transformations/graph_transformations.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <algorithm>
#include <vector>

#include "graph/graph.hpp"
#include "layers/Layer.hpp"

namespace it_lab_ai {
std::vector<std::vector<int>> find_subgraphs(const Graph& graph,
const Graph& subgraph);
bool has_edge(const Graph& graph, int id_from, int id_to);
bool is_root(const Graph& graph, int id);
bool is_leaf(const Graph& graph, int id);
bool run_search(const Graph& graph, const Graph& subgraph,
std::vector<int>& assignments,
std::vector<std::vector<int>>& results);
} // namespace it_lab_ai
7 changes: 3 additions & 4 deletions include/layers/BinaryOpLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ class BinaryOpLayer : public Layer {
public:
enum class Operation : uint8_t { kMul, kAdd, kSub, kDiv };

BinaryOpLayer() = default;
explicit BinaryOpLayer(Operation op) : op_(op) {}
BinaryOpLayer() : Layer(kBinaryOp), op_(Operation::kMul) {}
explicit BinaryOpLayer(Operation op) : Layer(kBinaryOp), op_(op) {}

static std::string get_name() { return "Binary Operation Layer"; }
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
static bool is_scalar_tensor(const Tensor& t);
Expand All @@ -30,7 +29,7 @@ class BinaryOpLayer : public Layer {
#endif

private:
Operation op_ = Operation::kMul;
Operation op_;

template <typename ValueType>
void run_with_scalar_impl(const Tensor& input, ValueType scalar,
Expand Down
4 changes: 1 addition & 3 deletions include/layers/ConcatLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ namespace it_lab_ai {

class ConcatLayer : public Layer {
public:
explicit ConcatLayer(int64_t axis = 0) : axis_(axis) {}
explicit ConcatLayer(int64_t axis = 0) : Layer(kConcat), axis_(axis) {}

void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;

static std::string get_name() { return "ConcatLayer"; }

#ifdef ENABLE_STATISTIC_WEIGHTS
Tensor get_weights() override { return Tensor(); }
#endif
Expand Down
10 changes: 8 additions & 2 deletions include/layers/ConvLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@ class ConvolutionalLayer : public Layer {
ImplType implType_;

public:
ConvolutionalLayer() = default;
ConvolutionalLayer() : Layer(kConvolution) {
stride_ = 0;
pads_ = 0;
dilations_ = 0;
implType_ = kDefault;
}
ConvolutionalLayer(size_t step, size_t pads, size_t dilations,
const Tensor& kernel, const Tensor& bias = Tensor(),
ImplType implType = kDefault) {
ImplType implType = kDefault)
: Layer(kConvolution) {
stride_ = step;
pads_ = pads;
dilations_ = dilations;
Expand Down
6 changes: 3 additions & 3 deletions include/layers/DropOutLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class DropOutLayer : public Layer {
double drop_rate_;

public:
DropOutLayer() = default;
DropOutLayer(double drop_rate) { drop_rate_ = drop_rate; }
static std::string get_name() { return "DropOut layer"; }
DropOutLayer(double drop_rate = 0.0) : Layer(kDropout) {
drop_rate_ = drop_rate;
}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
#ifdef ENABLE_STATISTIC_WEIGHTS
Expand Down
8 changes: 5 additions & 3 deletions include/layers/EWLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ T relu(const T& value) {

class EWLayer : public Layer {
public:
EWLayer() = default;
EWLayer() : Layer(kElementWise), func_("none"), alpha_(0.0F), beta_(0.0F) {}
EWLayer(std::string function, float alpha = 0.0F, float beta = 0.0F)
: func_(std::move(function)), alpha_(alpha), beta_(beta) {}
: Layer(kElementWise),
func_(std::move(function)),
alpha_(alpha),
beta_(beta) {}

static std::string get_name() { return "Element-wise layer"; }
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
#ifdef ENABLE_STATISTIC_WEIGHTS
Expand Down
5 changes: 2 additions & 3 deletions include/layers/FCLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ class FCLayer : public Layer {
Tensor bias_;

public:
FCLayer() = default;
FCLayer() : Layer(kFullyConnected) {}
FCLayer(Tensor weights, const Tensor& bias)
: weights_(std::move(weights)), bias_(bias) {}
static std::string get_name() { return "Fully-connected layer"; }
: Layer(kFullyConnected), weights_(std::move(weights)), bias_(bias) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
#ifdef ENABLE_STATISTIC_WEIGHTS
Expand Down
6 changes: 3 additions & 3 deletions include/layers/FlattenLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ class FlattenLayer : public Layer {
std::vector<size_t> order_;

public:
FlattenLayer() : order_({0, 1, 2, 3}) {}
FlattenLayer(const std::vector<size_t>& order) : order_(order) {}
static std::string get_name() { return "Flatten layer"; }
FlattenLayer() : Layer(kFlatten), order_({0, 1, 2, 3}) {}
FlattenLayer(const std::vector<size_t>& order)
: Layer(kFlatten), order_(order) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
#ifdef ENABLE_STATISTIC_WEIGHTS
Expand Down
11 changes: 8 additions & 3 deletions include/layers/InputLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ class InputLayer : public Layer {
int std_;

public:
InputLayer() = default;
InputLayer(LayInOut layin, LayInOut layout, int mean = 0, int std = 1) {
type_ = LayerType::kInput;
InputLayer() : Layer(kInput) {
layin_ = kNchw;
layout_ = kNchw;
mean_ = 0;
std_ = 1;
}
InputLayer(LayInOut layin, LayInOut layout, int mean = 0, int std = 1)
: Layer(kInput) {
layin_ = layin;
layout_ = layout;
mean_ = mean;
Expand Down
2 changes: 1 addition & 1 deletion include/layers/Layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ struct PostOperations {
class Layer {
public:
Layer() = default;
Layer(LayerType type) : type_(type) {}
virtual ~Layer() = default;
PostOperations postops;
int getID() const { return id_; }
void setID(int id) { id_ = id; }
LayerType getName() const { return type_; }
void setName(LayerType type) { type_ = type; }
virtual void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) = 0;
#ifdef ENABLE_STATISTIC_WEIGHTS
Expand Down
6 changes: 3 additions & 3 deletions include/layers/OutputLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace it_lab_ai {

class OutputLayer : public Layer {
public:
OutputLayer() = default;
OutputLayer(const std::vector<std::string>& labels) : labels_(labels) {}
static std::string get_name() { return "Output layer"; }
OutputLayer() : Layer(kOutput) {}
OutputLayer(const std::vector<std::string>& labels)
: Layer(kOutput), labels_(labels) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override {
output = input;
Expand Down
6 changes: 3 additions & 3 deletions include/layers/PoolingLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ enum PoolingType : uint8_t { kAverage, kMax };

class PoolingLayer : public Layer {
public:
PoolingLayer() = default;
PoolingLayer() : Layer(kPooling), implType_(kDefault) {}
PoolingLayer(const Shape& pooling_shape, std::string pooling_type = "average",
ImplType implType = kDefault)
: poolingShape_(pooling_shape),
: Layer(kPooling),
poolingShape_(pooling_shape),
poolingType_(std::move(pooling_type)),
implType_(implType) {}
static std::string get_name() { return "Pooling layer"; }
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
#ifdef ENABLE_STATISTIC_WEIGHTS
Expand Down
2 changes: 0 additions & 2 deletions include/layers/ReduceLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ class ReduceLayer : public Layer {
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;

static std::string get_name() { return "ReduceLayer"; }

#ifdef ENABLE_STATISTIC_WEIGHTS
Tensor get_weights() override { return Tensor(); }
#endif
Expand Down
6 changes: 2 additions & 4 deletions include/layers/SplitLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ namespace it_lab_ai {
class SplitLayer : public Layer {
public:
SplitLayer(int axis, std::vector<int> splits)
: axis_(axis), splits_(std::move(splits)) {}
: Layer(kSplit), axis_(axis), splits_(std::move(splits)) {}

SplitLayer(int axis, int num_outputs)
: axis_(axis), num_outputs_(num_outputs) {}
: Layer(kSplit), axis_(axis), num_outputs_(num_outputs) {}
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;

static std::string get_name() { return "SplitLayer"; }

#ifdef ENABLE_STATISTIC_WEIGHTS
Tensor get_weights() override { return Tensor(); }
#endif
Expand Down
4 changes: 1 addition & 3 deletions include/layers/TransposeLayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace it_lab_ai {
class TransposeLayer : public Layer {
public:
explicit TransposeLayer(std::vector<int64_t> perm = {})
: perm_(std::move(perm)) {}
: Layer(kTranspose), perm_(std::move(perm)) {}

void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override;
Expand All @@ -18,8 +18,6 @@ class TransposeLayer : public Layer {
Tensor get_weights() override { return Tensor(); }
#endif

static std::string get_name() { return "TransposeLayer"; }

private:
std::vector<int64_t> perm_;

Expand Down
Loading
Loading