/
7_cifar_resnet50_da_bn.cpp
187 lines (141 loc) · 4.24 KB
/
7_cifar_resnet50_da_bn.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
/*
* EDDL Library - European Distributed Deep Learning Library.
* Version: 1.1
* copyright (c) 2022, Universitat Politècnica de València (UPV), PRHLT Research Centre
* Date: March 2022
* Author: PRHLT Research Centre, UPV, (rparedes@prhlt.upv.es), (jon@prhlt.upv.es)
* All rights reserved
*/
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include "eddl/apis/eddl.h"
using namespace eddl;
//////////////////////////////////
// cifar_resnet50_da_bn.cpp:
// Resnet50 with
// BatchNorm
// Data Augmentation
// Using fit for training
//////////////////////////////////
layer BN(layer l)
{
return BatchNormalization(l);
//return l;
}
layer BG(layer l) {
//return GaussianNoise(BN(l),0.3);
return BN(l);
}
layer ResBlock(layer l, int filters,int half, int expand=0) {
layer in=l;
l=ReLu(BG(Conv(l,filters,{1,1},{1,1},"same",false)));
if (half)
l=ReLu(BG(Conv(l,filters,{3,3},{2,2},"same",false)));
else
l=ReLu(BG(Conv(l,filters,{3,3},{1,1},"same",false)));
l=BG(Conv(l,4*filters,{1,1},{1,1},"same",false));
if (half)
return ReLu(Add(BG(Conv(in,4*filters,{1,1},{2,2},"same",false)),l));
else
if (expand) return ReLu(Add(BG(Conv(in,4*filters,{1,1},{1,1},"same",false)),l));
else return ReLu(Add(in,l));
}
int main(int argc, char **argv){
bool testing = false;
bool use_cpu = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "--testing") == 0) testing = true;
else if (strcmp(argv[i], "--cpu") == 0) use_cpu = true;
}
// download CIFAR data
download_cifar10();
// Settings
int epochs = testing ? 2 : 5;
int batch_size =16;
int num_classes = 10;
// network
layer in=Input({3,32,32});
layer l=in;
// Data augmentation
l = RandomCropScale(l, {0.8f, 1.0f});
l = RandomHorizontalFlip(l);
// Resnet-50
l=ReLu(BG(Conv(l,64,{3,3},{1,1},"same",false))); //{1,1}
//l=MaxPool(l,{3,3},{1,1},"same");
// Add explicit padding to avoid the asymmetric padding in the Conv layers
l = Pad(l, {0, 1, 1, 0});
for(int i=0;i<3;i++)
l=ResBlock(l, 64, 0, i==0); // not half but expand the first
for(int i=0;i<4;i++)
l=ResBlock(l, 128,i==0);
for(int i=0;i<6;i++)
l=ResBlock(l, 256,i==0);
for(int i=0;i<3;i++)
l=ResBlock(l,512,i==0);
l=MaxPool(l,{4,4}); // should be avgpool
l=Reshape(l,{-1});
layer out= Softmax(Dense(l, num_classes));
// net define input and output layers list
model net=Model({in},{out});
compserv cs = nullptr;
if (use_cpu) {
cs = CS_CPU();
} else {
cs = CS_GPU({1}); // one GPU
// cs = CS_GPU({1,1},100); // two GPU with weight sync every 100 batches
// cs = CS_CPU();
// cs = CS_FPGA({1});
}
// Build model
build(net,
adam(0.001), // Optimizer
{"softmax_cross_entropy"}, // Losses
{"categorical_accuracy"}, // Metrics
cs);
// plot the model
plot(net,"model.pdf","TB"); // TB --> Top-Bottom mode for dot (graphviz)
// get some info from the network
summary(net);
// Load and preprocess training data
Tensor* x_train = Tensor::load("cifar_trX.bin");
Tensor* y_train = Tensor::load("cifar_trY.bin");
x_train->div_(255.0f);
// Load and preprocess test data
Tensor* x_test = Tensor::load("cifar_tsX.bin");
Tensor* y_test = Tensor::load("cifar_tsY.bin");
x_test->div_(255.0f);
if (testing) {
std::string _range_ = "0:" + std::to_string(2 * batch_size);
Tensor* x_mini_train = x_train->select({_range_, ":"});
Tensor* y_mini_train = y_train->select({_range_, ":"});
Tensor* x_mini_test = x_test->select({_range_, ":"});
Tensor* y_mini_test = y_test->select({_range_, ":"});
delete x_train;
delete y_train;
delete x_test;
delete y_test;
x_train = x_mini_train;
y_train = y_mini_train;
x_test = x_mini_test;
y_test = y_mini_test;
}
float lr=0.001;
for(int j=0;j<3;j++) {
lr/=10.0;
setlr(net,{lr,0.9});
for(int i=0;i<epochs;i++) {
// training, list of input and output tensors, batch, epochs
fit(net,{x_train},{y_train},batch_size, 1);
// Evaluate test
std::cout << "Evaluate test:" << std::endl;
evaluate(net,{x_test},{y_test});
}
}
delete x_train;
delete y_train;
delete x_test;
delete y_test;
delete net;
return EXIT_SUCCESS;
}