-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInputLayer.hpp
More file actions
191 lines (185 loc) · 6.51 KB
/
Copy pathInputLayer.hpp
File metadata and controls
191 lines (185 loc) · 6.51 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
#pragma once
#include <algorithm>
#include <cmath>
#include "layers/Layer.hpp"
namespace it_lab_ai {
enum LayInOut : uint8_t {
kNchw, // 0
kNhwc // 1
};
class InputLayer : public Layer {
private:
LayInOut layin_;
LayInOut layout_;
int mean_;
int std_;
public:
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;
std_ = std;
} // layout = kNchw(0), kNhwc(1)
#ifdef ENABLE_STATISTIC_WEIGHTS
Tensor get_weights() override {
std::vector<int> v = {0};
Tensor a = make_tensor(v);
return a;
}
#endif
void run(const std::vector<Tensor>& input,
std::vector<Tensor>& output) override {
if (input.size() != 1) {
throw std::runtime_error("InputLayer: Input tensors not 1");
}
switch (input[0].get_type()) {
case Type::kInt: {
std::vector<int> in = *input[0].as<int>();
if (input[0].get_shape().dims() != 4) {
throw std::out_of_range(
"The size of the shape does not match what is needed for the "
"input layer");
}
for (int& re : in) {
re = (re - mean_) / std_;
}
Shape sh(input[0].get_shape());
if (layin_ == kNchw && layout_ == kNhwc) {
int n = static_cast<int>(sh[0]);
int c = static_cast<int>(sh[1]);
int h = static_cast<int>(sh[2]);
int w = static_cast<int>(sh[3]);
if (n < 1 || c < 1 || h < 1 || w < 1) {
throw std::out_of_range("One of the sizes <= 0");
}
std::vector<int> res(n * h * w * c);
for (int n1 = 0; n1 < n; ++n1) {
for (int c1 = 0; c1 < c; ++c1) {
for (int h1 = 0; h1 < h; ++h1) {
for (int w1 = 0; w1 < w; ++w1) {
int nchw_index = n1 * c * h * w + c1 * h * w + h1 * w + w1;
int nhwc_index = n1 * h * w * c + h1 * w * c + w1 * c + c1;
res[nhwc_index] = in[nchw_index];
}
}
}
}
Shape sh1({static_cast<unsigned long long>(n),
static_cast<unsigned long long>(h),
static_cast<unsigned long long>(w),
static_cast<unsigned long long>(c)});
output[0] = make_tensor<int>(res, sh1);
break;
}
if (layin_ == kNhwc && layout_ == kNchw) {
int n = static_cast<int>(sh[0]);
int c = static_cast<int>(sh[3]);
int h = static_cast<int>(sh[1]);
int w = static_cast<int>(sh[2]);
if (n < 1 || c < 1 || h < 1 || w < 1) {
throw std::out_of_range("One of the sizes <= 0");
}
std::vector<int> res(n * h * w * c);
for (int n1 = 0; n1 < n; ++n1) {
for (int c1 = 0; c1 < c; ++c1) {
for (int h1 = 0; h1 < h; ++h1) {
for (int w1 = 0; w1 < w; ++w1) {
int nhwc_index = n1 * h * w * c + h1 * w * c + w1 * c + c1;
int nchw_index = n1 * c * h * w + c1 * h * w + h1 * w + w1;
res[nchw_index] = in[nhwc_index];
}
}
}
}
Shape sh1({static_cast<unsigned long long>(n),
static_cast<unsigned long long>(c),
static_cast<unsigned long long>(h),
static_cast<unsigned long long>(w)});
output[0] = make_tensor<int>(res, sh1);
break;
}
output[0] = make_tensor<int>(in, sh);
break;
}
case Type::kFloat: {
std::vector<float> in = *input[0].as<float>();
if (input[0].get_shape().dims() != 4) {
throw std::out_of_range(
"The size of the shape does not match what is needed for the "
"input layer");
}
for (float& re : in) {
re = static_cast<float>((re - mean_) / std_);
}
Shape sh(input[0].get_shape());
if (layin_ == kNchw && layout_ == kNhwc) {
int n = static_cast<int>(sh[0]);
int c = static_cast<int>(sh[1]);
int h = static_cast<int>(sh[2]);
int w = static_cast<int>(sh[3]);
if (n < 1 || c < 1 || h < 1 || w < 1) {
throw std::out_of_range("One of the sizes <= 0");
}
std::vector<float> res(n * h * w * c);
for (int n1 = 0; n1 < n; ++n1) {
for (int c1 = 0; c1 < c; ++c1) {
for (int h1 = 0; h1 < h; ++h1) {
for (int w1 = 0; w1 < w; ++w1) {
int nchw_index = n1 * c * h * w + c1 * h * w + h1 * w + w1;
int nhwc_index = n1 * h * w * c + h1 * w * c + w1 * c + c1;
res[nhwc_index] = in[nchw_index];
}
}
}
}
Shape sh1({static_cast<unsigned long long>(n),
static_cast<unsigned long long>(h),
static_cast<unsigned long long>(w),
static_cast<unsigned long long>(c)});
output[0] = make_tensor<float>(res, sh1);
break;
}
if (layin_ == kNhwc && layout_ == kNchw) {
int n = static_cast<int>(sh[0]);
int c = static_cast<int>(sh[3]);
int h = static_cast<int>(sh[1]);
int w = static_cast<int>(sh[2]);
if (n < 1 || c < 1 || h < 1 || w < 1) {
throw std::out_of_range("One of the sizes <= 0");
}
std::vector<float> res(n * h * w * c);
for (int n1 = 0; n1 < n; ++n1) {
for (int c1 = 0; c1 < c; ++c1) {
for (int h1 = 0; h1 < h; ++h1) {
for (int w1 = 0; w1 < w; ++w1) {
int nhwc_index = n1 * h * w * c + h1 * w * c + w1 * c + c1;
int nchw_index = n1 * c * h * w + c1 * h * w + h1 * w + w1;
res[nchw_index] = in[nhwc_index];
}
}
}
}
Shape sh1({static_cast<unsigned long long>(n),
static_cast<unsigned long long>(c),
static_cast<unsigned long long>(h),
static_cast<unsigned long long>(w)});
output[0] = make_tensor<float>(res, sh1);
break;
}
output[0] = make_tensor<float>(in, sh);
break;
}
default: {
throw std::runtime_error("No such type");
}
}
}
};
} // namespace it_lab_ai