-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_inference.cpp
More file actions
206 lines (199 loc) · 5.76 KB
/
Copy pathtest_inference.cpp
File metadata and controls
206 lines (199 loc) · 5.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <vector>
#include "graph/graph.hpp"
#include "gtest/gtest.h"
#include "layers/ConvLayer.hpp"
#include "layers/EWLayer.hpp"
#include "layers/FCLayer.hpp"
#include "layers/InputLayer.hpp"
#include "layers/OutputLayer.hpp"
#include "layers/PoolingLayer.hpp"
using namespace it_lab_ai;
TEST(bfs, check_result_vec) {
Graph graph(5);
Shape sh1({1, 5, 5, 3});
std::vector<int> vec;
vec.reserve(75);
for (int i = 0; i < 75; ++i) {
vec.push_back(3);
}
Tensor input = make_tensor(vec, sh1);
Tensor output = make_tensor(vec, sh1);
InputLayer a1(kNhwc, kNchw, 1, 2);
InputLayer a3(kNhwc, kNhwc, 1, 1);
a1.setName(kInput);
a3.setName(kInput);
std::vector<int> kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1};
Shape sh2({3, 3});
Tensor kernel = make_tensor(kernelvec, sh2);
ConvolutionalLayer a2(1, 0, 1, kernel);
ConvolutionalLayer a4(1, 0, 1, kernel);
a2.setName(kConvolution);
a4.setName(kConvolution);
graph.setInput(a1, input);
graph.makeConnection(a1, a2);
graph.makeConnection(a1, a3);
graph.makeConnection(a2, a4);
graph.setOutput(a4, output);
graph.inference();
std::vector<int> tmp = *output.as<int>();
std::vector<int> res = {81, 81, 81};
#ifdef ENABLE_STATISTIC_TENSORS
std::vector<Tensor> tensors = graph.getTensors();
for (int i = 0; i < tensors.size(); i++) {
std::vector<int> ten = *tensors[i].as<int>();
for (int j = 0; j < ten.size(); j++) {
std::cout << ten[j] << ' ';
}
std::cout << '\n';
}
#endif
#ifdef ENABLE_STATISTIC_TIME
std::vector<std::string> times = graph.getTimeInfo();
for (int j = 0; j < times.size(); j++) {
std::cout << times[j] << ' ';
}
std::cout << '\n';
#endif
#ifdef ENABLE_STATISTIC_WEIGHTS
std::vector<Tensor> weights = graph.getWEIGHTS();
for (int i = 0; i < weights.size(); i++) {
switch (weights[i].get_type()) {
case Type::kInt: {
std::vector<int> ten = *weights[i].as<int>();
for (int j = 0; j < ten.size(); j++) {
std::cout << ten[j] << ' ';
}
std::cout << '\n';
break;
}
case Type::kFloat: {
std::vector<float> ten = *weights[i].as<float>();
for (int j = 0; j < ten.size(); j++) {
std::cout << ten[j] << ' ';
}
std::cout << '\n';
break;
}
}
}
#endif
ASSERT_EQ(tmp, res);
}
TEST(bfs, check_end_to_end) {
Graph graph(6);
Shape sh1({1, 5, 5, 3});
std::vector<float> vec;
vec.reserve(75);
for (int i = 0; i < 75; ++i) {
vec.push_back(3);
}
Tensor input = make_tensor(vec, sh1);
Tensor output = make_tensor(vec, sh1);
InputLayer a1(kNhwc, kNchw, 1, 2);
std::vector<float> kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1};
Shape sh2({3, 3});
Tensor kernel = make_tensor(kernelvec, sh2);
ConvolutionalLayer a2(1, 0, 1, kernel);
Shape poolshape = {2, 2};
EWLayer a3("linear", 2.0F, 3.0F);
PoolingLayer a4(poolshape, "average");
FCLayer a6;
OutputLayer a5;
graph.setInput(a1, input);
graph.makeConnection(a1, a2);
graph.makeConnection(a2, a3);
graph.makeConnection(a3, a4);
graph.makeConnection(a4, a5);
graph.makeConnection(a5, a6);
graph.setOutput(a5, output);
graph.inference();
#ifdef ENABLE_STATISTIC_WEIGHTS
std::vector<Tensor> weights = graph.getWEIGHTS();
for (int i = 0; i < weights.size(); i++) {
switch (weights[i].get_type()) {
case Type::kInt: {
std::vector<int> ten = *weights[i].as<int>();
for (int j = 0; j < ten.size(); j++) {
std::cout << ten[j] << ' ';
}
std::cout << '\n';
break;
}
case Type::kFloat: {
std::vector<float> ten = *weights[i].as<float>();
for (int j = 0; j < ten.size(); j++) {
std::cout << ten[j] << ' ';
}
std::cout << '\n';
break;
}
}
}
#endif
std::vector<float> tmp = *output.as<float>();
std::vector<float> tmp_output = softmax<float>(*output.as<float>());
std::vector<float> res(3, 21);
ASSERT_EQ(tmp, res);
}
TEST(bfs, check_struct_layer) {
Graph graph(5);
Shape sh1({1, 5, 5, 3});
std::vector<int> vec;
vec.reserve(75);
for (int i = 0; i < 75; ++i) {
vec.push_back(3);
}
Tensor input = make_tensor(vec, sh1);
Tensor output = make_tensor(vec, sh1);
InputLayer a1(kNhwc, kNchw, 1, 2);
a1.setName(kInput);
std::vector<int> kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1};
Shape sh2({3, 3});
Tensor kernel = make_tensor(kernelvec, sh2);
ConvolutionalLayer a2(1, 0, 1, kernel);
ConvolutionalLayer a3(1, 0, 1, kernel);
// EWLayer a4("linear", 2.0F, 3.0F);
// a2.ewops.layers.push_back(&a4);
// a2.ewops.countlayers++;
a2.setName(kConvolution);
a3.setName(kConvolution);
graph.setInput(a1, input);
graph.makeConnection(a1, a2);
graph.makeConnection(a2, a3);
graph.setOutput(a3, output);
graph.inference();
std::vector<int> tmp = *output.as<int>();
std::vector<int> res = {81, 81, 81};
ASSERT_EQ(tmp, res);
}
TEST(bfs, check_struct_layer_added) {
Graph graph(5);
Shape sh1({1, 5, 5, 3});
std::vector<int> vec;
vec.reserve(75);
for (int i = 0; i < 75; ++i) {
vec.push_back(3);
}
Tensor input = make_tensor(vec, sh1);
Tensor output = make_tensor(vec, sh1);
InputLayer a1(kNhwc, kNchw, 1, 2);
a1.setName(kInput);
std::vector<int> kernelvec = {1, 1, 1, 1, 1, 1, 1, 1, 1};
Shape sh2({3, 3});
Tensor kernel = make_tensor(kernelvec, sh2);
ConvolutionalLayer a2(1, 0, 1, kernel);
ConvolutionalLayer a3(1, 0, 1, kernel);
EWLayer a4("linear", 2.0F, 3.0F);
a2.postops.layers.push_back(&a4);
a2.postops.count++;
a2.setName(kConvolution);
a3.setName(kConvolution);
graph.setInput(a1, input);
graph.makeConnection(a1, a2);
graph.makeConnection(a2, a3);
graph.setOutput(a3, output);
graph.inference();
std::vector<int> tmp = *output.as<int>();
std::vector<int> res = {189, 189, 189};
ASSERT_EQ(tmp, res);
}