-
Notifications
You must be signed in to change notification settings - Fork 3
/
nets.cpp
373 lines (314 loc) · 12.5 KB
/
nets.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//#include "alexnet.h"
#include "nets.hpp"
#include "clfc.hpp"
#include "torch/torch.h"
#include <iostream>
using namespace torch;
typedef unsigned long int uli;
//const alnet_t POOLS_NUM = 3;
/*const alnet_t STRIDE_SIZE = 1;
const alnet_t PADDING_SIZE_1 = 2;
const alnet_t PADDING_SIZE_2 = 1;*/
/*const alnet_t OUR_OUT_NEURONS_CONV_1 = 64;
const alnet_t OUR_OUT_NEURONS_CONV_2 = 128;
const alnet_t OUR_OUT_NEURONS_CONV_3 = 256;
const alnet_t OUR_OUT_NEURONS_CONV_4 = OUT_NEURONS_CONV_3 * 2 / 3;
const alnet_t OUR_OUT_NEURONS_CONV_5 = OUT_NEURONS_CONV_4; */
//const alnet_t OUT_PIC_SIZE = OUT_NEURONS_CONV_5 * PICTURE_SIZE / pow(4, POOLS_NUM); // 256 * 6 * 6
//const alnet_t FC_NEURONS_NUM = 4096;
const std::string FORWARD_ERROR_STR = "Forward failed: ";
Tensor Net::forward(Tensor x)
{
return x;
}
OurNet::OurNet()
{
conv1_1 = register_module("conv1_1", nn::Conv2d(nn::Conv2dOptions(CHANELS_NUM, 64, 3).stride(1).padding(1)));
conv1_2 = register_module("conv1_2", nn::Conv2d(nn::Conv2dOptions(64, 64, 3).stride(1).padding(1)));
conv2_1 = register_module("conv2_1", nn::Conv2d(nn::Conv2dOptions(64, 128, 3).stride(1).padding(1)));
conv2_2 = register_module("conv2_2", nn::Conv2d(nn::Conv2dOptions(128, 128, 3).stride(1).padding(1)));
conv3_1 = register_module("conv3_1", nn::Conv2d(nn::Conv2dOptions(128, 256, 3).stride(1).padding(1)));
conv3_2 = register_module("conv3_2", nn::Conv2d(nn::Conv2dOptions(256, 256, 3).stride(1).padding(1)));
fc1 = register_module("fc1", nn::Linear(256 * 6 * 6, 32));
fc2 = register_module("fc2", nn::Linear(32, NUM_CLASSES));
}
Tensor OurNet::forward(Tensor x)
{
try {
x = x.view({x.size(0), 1, PICTURE_HEIGHT, PICTURE_WIDTH});
x = relu(conv1_1->forward(x));
x = relu(conv1_2->forward(x));
x = nn::BatchNorm2d(64)->forward(x);
x = max_pool2d(x, 2, 2);
x = relu(conv2_1->forward(x));
x = relu(conv2_2->forward(x));
x = nn::BatchNorm2d(128)->forward(x);
x = max_pool2d(x, 2, 2);
x = relu(conv3_1->forward(x));
x = relu(conv3_2->forward(x));
x = nn::BatchNorm2d(256)->forward(x);
x = max_pool2d(x, 2, 2);
x = x.view({-1, 256 * 6 * 6});
x = fc1->forward(x);
x = nn::BatchNorm1d(32)->forward(x);
x = relu(x);
x = dropout(x, 0.2, is_training());
x = fc2->forward(x);
} catch (const c10::ValueError& ve) {
std::cerr << FORWARD_ERROR_STR << ve.what() << std::endl;
std::cerr << ve.msg() << std::endl;
} catch (const c10::IndexError& ie) {
std::cerr << FORWARD_ERROR_STR << ie.what() << std::endl;
std::cerr << ie.msg() << std::endl;
} catch (const c10::EnforceFiniteError& efe) {
std::cerr << FORWARD_ERROR_STR << efe.what() << std::endl;
std::cerr << efe.msg() << std::endl;
} catch (c10::Error oe) {
std::cerr << FORWARD_ERROR_STR << oe.what() << std::endl;
std::cerr << oe.msg() << std::endl;
} catch (...) {
std::cerr << FORWARD_ERROR_STR << std::endl;
}
return x;
}
AlexNet::AlexNet()
{
conv1 = register_module("conv1", nn::Conv2d(nn::Conv2dOptions(CHANELS_NUM, 64, 5).stride(1).padding(2)));
conv2 = register_module("conv2", nn::Conv2d(nn::Conv2dOptions(64, 192, 5).stride(1).padding(2)));
conv3 = register_module("conv3", nn::Conv2d(nn::Conv2dOptions(192, 384, 3).stride(1).padding(1)));
conv4 = register_module("conv4", nn::Conv2d(nn::Conv2dOptions(384, 256, 3).stride(1).padding(1)));
conv5 = register_module("conv5", nn::Conv2d(nn::Conv2dOptions(256, 256, 3).stride(1).padding(1)));
fc1 = register_module("fc1", nn::Linear(256 * 6 * 6, 4096));
fc2 = register_module("fc2", nn::Linear(4096, 4096));
fc3 = register_module("fc3", nn::Linear(4096, NUM_CLASSES));
}
Tensor AlexNet::forward(Tensor x)
{
try {
x = x.view({x.size(0), 1, PICTURE_HEIGHT, PICTURE_WIDTH});
x = relu(conv1->forward(x));
x = max_pool2d(x, 2, 2);
x = relu(conv2->forward(x));
x = max_pool2d(x, 2);
x = relu(conv3->forward(x));
x = relu(conv4->forward(x));
x = relu(conv5->forward(x));
x = max_pool2d(x, 2);
x = x.view({x.size(0), -1});
x = dropout(x, 0.7, is_training());
x = relu(fc1->forward(x));
x = nn::BatchNorm1d(4096)->forward(x);
x = dropout(x, 0.7, is_training());
x = relu(fc2->forward(x));
x = nn::BatchNorm1d(4096)->forward(x);
x = fc3->forward(x);
} catch (const c10::ValueError& ve) {
std::cerr << FORWARD_ERROR_STR << ve.what() << std::endl;
std::cerr << ve.msg() << std::endl;
} catch (const c10::IndexError& ie) {
std::cerr << FORWARD_ERROR_STR << ie.what() << std::endl;
std::cerr << ie.msg() << std::endl;
} catch (const c10::EnforceFiniteError& efe) {
std::cerr << FORWARD_ERROR_STR << efe.what() << std::endl;
std::cerr << efe.msg() << std::endl;
} catch (c10::Error oe) {
std::cerr << FORWARD_ERROR_STR << oe.what() << std::endl;
std::cerr << oe.msg() << std::endl;
} catch (...) {
std::cerr << FORWARD_ERROR_STR << std::endl;
}
return x;
}
SmallNet::SmallNet()
{
conv1 = register_module("conv1", nn::Conv2d(torch::nn::Conv2dOptions(CHANELS_NUM , 64, 5).stride(1).padding(2)));
conv2 = register_module("conv2", nn::Conv2d(torch::nn::Conv2dOptions(64, 128, 3).stride(1).padding(1)));
fc1 = register_module("fc1", nn::Linear(12 * 12 * 128, 256));
fc2 = register_module("fc2", nn::Linear(256, NUM_CLASSES));
}
Tensor SmallNet::forward(Tensor x)
{
try {
x = x.view({x.size(0), 1, PICTURE_HEIGHT, PICTURE_WIDTH});
x = dropout(x, 0.2, is_training());
x = relu(conv1->forward(x));
x = nn::BatchNorm2d(64)->forward(x);
x = max_pool2d(x, 2, 2);
x = torch::dropout(x, 0.5, is_training());
x = torch::relu(conv2->forward(x));
x = torch::nn::BatchNorm2d(128)->forward(x);
x = torch::max_pool2d(x, 2, 2);
x = x.view({x.size(0), -1});
x = dropout(x, 0.5, is_training());
x = relu(fc1->forward(x));
x = nn::BatchNorm1d(256)->forward(x);
x = dropout(x, 0.2, is_training());
x = fc2->forward(x);
} catch (const c10::ValueError& ve) {
std::cerr << FORWARD_ERROR_STR << ve.what() << std::endl;
std::cerr << ve.msg() << std::endl;
} catch (const c10::IndexError& ie) {
std::cerr << FORWARD_ERROR_STR << ie.what() << std::endl;
std::cerr << ie.msg() << std::endl;
} catch (const c10::EnforceFiniteError& efe) {
std::cerr << FORWARD_ERROR_STR << efe.what() << std::endl;
std::cerr << efe.msg() << std::endl;
} catch (c10::Error oe) {
std::cerr << FORWARD_ERROR_STR << oe.what() << std::endl;
std::cerr << oe.msg() << std::endl;
} catch (...) {
std::cerr << FORWARD_ERROR_STR << std::endl;
}
return x;
}
VGG16::VGG16()
{
conv1 = register_module("conv1", nn::Conv2d(nn::Conv2dOptions(CHANELS_NUM, 64, 3).stride(1).padding(1)));
conv2 = register_module("conv2", nn::Conv2d(nn::Conv2dOptions(64, 64, 3).stride(1).padding(1)));
conv3 = register_module("conv3", nn::Conv2d(nn::Conv2dOptions(64, 128, 3).stride(1).padding(1)));
conv4 = register_module("conv4", nn::Conv2d(nn::Conv2dOptions(128, 128, 3).stride(1).padding(1)));
conv5 = register_module("conv5", nn::Conv2d(nn::Conv2dOptions(128, 256, 3).stride(1).padding(1)));
conv6 = register_module("conv6", nn::Conv2d(nn::Conv2dOptions(256, 256, 3).stride(1).padding(1)));
conv7 = register_module("conv7", nn::Conv2d(nn::Conv2dOptions(256, 512, 3).stride(1).padding(1)));
conv8 = register_module("conv8", nn::Conv2d(nn::Conv2dOptions(512, 512, 3).stride(1).padding(1)));
fc1 = register_module("fc1", nn::Linear(512 * 3 * 3, 4096));
fc2 = register_module("fc2", nn::Linear(4096, 4096));
fc3 = register_module("fc3", nn::Linear(4096, NUM_CLASSES));
}
Tensor VGG16::forward(Tensor x)
{
try {
x = x.view({x.size(0), 1, PICTURE_HEIGHT, PICTURE_WIDTH});
x = conv1->forward(x);
x = conv2->forward(x);
x = max_pool2d(x, 2, 2);
x = conv3->forward(x);
x = conv4->forward(x);
x = max_pool2d(x, 2, 2);
x = conv5->forward(x);
x = conv6->forward(x);
x = conv6->forward(x);
x = max_pool2d(x, 2, 2);
x = conv7->forward(x);
x = conv8->forward(x);
x = conv8->forward(x);
x = max_pool2d(x, 2, 2);
x = x.view({x.size(0), -1});
x = fc1->forward(x);
x = fc2->forward(x);
x = fc3->forward(x);
} catch (const c10::ValueError& ve) {
std::cerr << FORWARD_ERROR_STR << ve.what() << std::endl;
std::cerr << ve.msg() << std::endl;
} catch (const c10::IndexError& ie) {
std::cerr << FORWARD_ERROR_STR << ie.what() << std::endl;
std::cerr << ie.msg() << std::endl;
} catch (const c10::EnforceFiniteError& efe) {
std::cerr << FORWARD_ERROR_STR << efe.what() << std::endl;
std::cerr << efe.msg() << std::endl;
} catch (c10::Error oe) {
std::cerr << FORWARD_ERROR_STR << oe.what() << std::endl;
std::cerr << oe.msg() << std::endl;
} catch (...) {
std::cerr << FORWARD_ERROR_STR << std::endl;
}
return x;
}
nn::Conv2d conv3x3(alnet_t in_planes, alnet_t out_planes, alnet_t stride = 1)
{
//nn::Conv2d conv = nn::Module::register_module("conv3x3", nn::Conv2d(nn::Conv2dOptions(in_planes, out_planes, 3).stride(stride).padding(1)));
return nn::Conv2d(nn::Conv2dOptions(in_planes, out_planes, 3).stride(stride).padding(1));
}
BasicBlock::BasicBlock(alnet_t inplanes, alnet_t planes, alnet_t str, nn::Sequential down)
{
conv1 = conv3x3(inplanes, planes, str);
conv2 = conv3x3(planes, planes);
expansion = 1;
this->planes = planes;
downsample = down;
stride = str;
}
Tensor BasicBlock::forward(Tensor x)
{
try {
Tensor residual;
if (downsample)
residual = downsample->forward(x);
else
residual = x;
x = relu(conv1->forward(x));
x = nn::BatchNorm2d(planes)->forward(x);
x = conv2->forward(x);
x = nn::BatchNorm2d(planes)->forward(x);
x += residual;
x = relu(x);
} catch (const c10::ValueError& ve) {
std::cerr << FORWARD_ERROR_STR << ve.what() << std::endl;
std::cerr << ve.msg() << std::endl;
} catch (const c10::IndexError& ie) {
std::cerr << FORWARD_ERROR_STR << ie.what() << std::endl;
std::cerr << ie.msg() << std::endl;
} catch (const c10::EnforceFiniteError& efe) {
std::cerr << FORWARD_ERROR_STR << efe.what() << std::endl;
std::cerr << efe.msg() << std::endl;
} catch (c10::Error oe) {
std::cerr << FORWARD_ERROR_STR << oe.what() << std::endl;
std::cerr << oe.msg() << std::endl;
} catch (...) {
std::cerr << FORWARD_ERROR_STR << std::endl;
}
return x;
}
ResNet18::ResNet18()
{
inplanes = 64;
convv1 = nn::Conv2d(nn::Conv2dOptions(CHANELS_NUM, 64, 2).stride(2).padding(3));
layer1 = make_layer(64, 1);
layer2 = make_layer(64, 2);
layer3 = make_layer(64, 2);
layer4 = make_layer(64, 2);
fc = register_module("fc", nn::Linear(64, NUM_CLASSES));
}
nn::Sequential ResNet18::make_layer(alnet_t planes, alnet_t stride)
{
nn::Sequential downsample = nullptr;
if (stride != 1 || inplanes != planes)
downsample = nn::Sequential(
nn::Conv2d(nn::Conv2dOptions(inplanes, planes, 1).stride(stride).padding(0)),
nn::BatchNorm2d(planes)
);
return nn::Sequential(BasicBlock(inplanes, planes, stride, downsample),
BasicBlock(inplanes, planes, 1, nullptr),
BasicBlock(inplanes, planes, 1, nullptr));
}
Tensor ResNet18::forward(Tensor x)
{
try {
x = x.view({x.size(0), 1, PICTURE_HEIGHT, PICTURE_WIDTH});
x = relu(convv1->forward(x));
x = nn::BatchNorm2d(64)->forward(x);
x = max_pool2d(x, 2, 2);
x = layer1->forward(x);
x = layer2->forward(x);
x = layer3->forward(x);
x = layer4->forward(x);
x = avg_pool2d(x, 2, 1);
x = x.view({x.size(0), -1});
x = fc->forward(x);
} catch (const c10::ValueError& ve) {
std::cerr << FORWARD_ERROR_STR << ve.what() << std::endl;
std::cerr << ve.msg() << std::endl;
} catch (const c10::IndexError& ie) {
std::cerr << FORWARD_ERROR_STR << ie.what() << std::endl;
std::cerr << ie.msg() << std::endl;
} catch (const c10::EnforceFiniteError& efe) {
std::cerr << FORWARD_ERROR_STR << efe.what() << std::endl;
std::cerr << efe.msg() << std::endl;
} catch (c10::Error oe) {
std::cerr << FORWARD_ERROR_STR << oe.what() << std::endl;
std::cerr << oe.msg() << std::endl;
} catch (...) {
std::cerr << FORWARD_ERROR_STR << std::endl;
}
return x;
}