This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathalexnet.cpp
More file actions
327 lines (311 loc) · 12.9 KB
/
alexnet.cpp
File metadata and controls
327 lines (311 loc) · 12.9 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
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
*/
#include <iostream>
#include <map>
#include <string>
#include "mxnet-cpp/MxNetCpp.h"
using namespace std;
using namespace mxnet::cpp;
Symbol AlexnetSymbol(int num_classes) {
auto input_data = Symbol::Variable("data");
auto target_label = Symbol::Variable("label");
/*stage 1*/
auto conv1 = Operator("Convolution")
.SetParam("kernel", Shape(11, 11))
.SetParam("num_filter", 96)
.SetParam("stride", Shape(4, 4))
.SetParam("dilate", Shape(1, 1))
.SetParam("pad", Shape(0, 0))
.SetParam("num_group", 1)
.SetParam("workspace", 512)
.SetParam("no_bias", false)
.SetInput("data", input_data)
.CreateSymbol("conv1");
auto relu1 = Operator("Activation")
.SetParam("act_type", "relu") /*relu,sigmoid,softrelu,tanh */
.SetInput("data", conv1)
.CreateSymbol("relu1");
auto pool1 = Operator("Pooling")
.SetParam("kernel", Shape(3, 3))
.SetParam("pool_type", "max") /*avg,max,sum */
.SetParam("global_pool", false)
.SetParam("stride", Shape(2, 2))
.SetParam("pad", Shape(0, 0))
.SetInput("data", relu1)
.CreateSymbol("pool1");
auto lrn1 = Operator("LRN")
.SetParam("nsize", 5)
.SetParam("alpha", 0.0001)
.SetParam("beta", 0.75)
.SetParam("knorm", 1)
.SetInput("data", pool1)
.CreateSymbol("lrn1");
/*stage 2*/
auto conv2 = Operator("Convolution")
.SetParam("kernel", Shape(5, 5))
.SetParam("num_filter", 256)
.SetParam("stride", Shape(1, 1))
.SetParam("dilate", Shape(1, 1))
.SetParam("pad", Shape(2, 2))
.SetParam("num_group", 1)
.SetParam("workspace", 512)
.SetParam("no_bias", false)
.SetInput("data", lrn1)
.CreateSymbol("conv2");
auto relu2 = Operator("Activation")
.SetParam("act_type", "relu") /*relu,sigmoid,softrelu,tanh */
.SetInput("data", conv2)
.CreateSymbol("relu2");
auto pool2 = Operator("Pooling")
.SetParam("kernel", Shape(3, 3))
.SetParam("pool_type", "max") /*avg,max,sum */
.SetParam("global_pool", false)
.SetParam("stride", Shape(2, 2))
.SetParam("pad", Shape(0, 0))
.SetInput("data", relu2)
.CreateSymbol("pool2");
auto lrn2 = Operator("LRN")
.SetParam("nsize", 5)
.SetParam("alpha", 0.0001)
.SetParam("beta", 0.75)
.SetParam("knorm", 1)
.SetInput("data", pool2)
.CreateSymbol("lrn2");
/*stage 3*/
auto conv3 = Operator("Convolution")
.SetParam("kernel", Shape(3, 3))
.SetParam("num_filter", 384)
.SetParam("stride", Shape(1, 1))
.SetParam("dilate", Shape(1, 1))
.SetParam("pad", Shape(1, 1))
.SetParam("num_group", 1)
.SetParam("workspace", 512)
.SetParam("no_bias", false)
.SetInput("data", lrn2)
.CreateSymbol("conv3");
auto relu3 = Operator("Activation")
.SetParam("act_type", "relu") /*relu,sigmoid,softrelu,tanh */
.SetInput("data", conv3)
.CreateSymbol("relu3");
auto conv4 = Operator("Convolution")
.SetParam("kernel", Shape(3, 3))
.SetParam("num_filter", 384)
.SetParam("stride", Shape(1, 1))
.SetParam("dilate", Shape(1, 1))
.SetParam("pad", Shape(1, 1))
.SetParam("num_group", 1)
.SetParam("workspace", 512)
.SetParam("no_bias", false)
.SetInput("data", relu3)
.CreateSymbol("conv4");
auto relu4 = Operator("Activation")
.SetParam("act_type", "relu") /*relu,sigmoid,softrelu,tanh */
.SetInput("data", conv4)
.CreateSymbol("relu4");
auto conv5 = Operator("Convolution")
.SetParam("kernel", Shape(3, 3))
.SetParam("num_filter", 256)
.SetParam("stride", Shape(1, 1))
.SetParam("dilate", Shape(1, 1))
.SetParam("pad", Shape(1, 1))
.SetParam("num_group", 1)
.SetParam("workspace", 512)
.SetParam("no_bias", false)
.SetInput("data", relu4)
.CreateSymbol("conv5");
auto relu5 = Operator("Activation")
.SetParam("act_type", "relu")
.SetInput("data", conv5)
.CreateSymbol("relu5");
auto pool3 = Operator("Pooling")
.SetParam("kernel", Shape(3, 3))
.SetParam("pool_type", "max")
.SetParam("global_pool", false)
.SetParam("stride", Shape(2, 2))
.SetParam("pad", Shape(0, 0))
.SetInput("data", relu5)
.CreateSymbol("pool3");
/*stage4*/
auto flatten =
Operator("Flatten").SetInput("data", pool3).CreateSymbol("flatten");
auto fc1 = Operator("FullyConnected")
.SetParam("num_hidden", 4096)
.SetParam("no_bias", false)
.SetInput("data", flatten)
.CreateSymbol("fc1");
auto relu6 = Operator("Activation")
.SetParam("act_type", "relu")
.SetInput("data", fc1)
.CreateSymbol("relu6");
auto dropout1 = Operator("Dropout")
.SetParam("p", 0.5)
.SetInput("data", relu6)
.CreateSymbol("dropout1");
/*stage5*/
auto fc2 = Operator("FullyConnected")
.SetParam("num_hidden", 4096)
.SetParam("no_bias", false)
.SetInput("data", dropout1)
.CreateSymbol("fc2");
auto relu7 = Operator("Activation")
.SetParam("act_type", "relu")
.SetInput("data", fc2)
.CreateSymbol("relu7");
auto dropout2 = Operator("Dropout")
.SetParam("p", 0.5)
.SetInput("data", relu7)
.CreateSymbol("dropout2");
/*stage6*/
auto fc3 = Operator("FullyConnected")
.SetParam("num_hidden", num_classes)
.SetParam("no_bias", false)
.SetInput("data", dropout2)
.CreateSymbol("fc3");
auto softmax = Operator("SoftmaxOutput")
.SetParam("grad_scale", 1)
.SetParam("ignore_label", -1)
.SetParam("multi_output", false)
.SetParam("use_ignore", false)
.SetParam("normalization", "null") /*batch,null,valid */
.SetInput("data", fc3)
.SetInput("label", target_label)
.CreateSymbol("softmax");
return softmax;
}
int main(int argc, char const *argv[]) {
/*basic config*/
int batch_size = 256;
int max_epo = 100;
float learning_rate = 1e-4;
float weight_decay = 1e-4;
/*context and net symbol*/
auto ctx = Context::gpu();
auto Net = AlexnetSymbol(10);
/*args_map and aux_map is used for parameters' saving*/
map<string, NDArray> args_map;
map<string, NDArray> aux_map;
/*we should tell mxnet the shape of data and label*/
args_map["data"] = NDArray(Shape(batch_size, 3, 256, 256), ctx);
args_map["label"] = NDArray(Shape(batch_size), ctx);
/*with data and label, executor can be generated automatically*/
auto *exec = Net.SimpleBind(ctx, args_map);
auto arg_names = Net.ListArguments();
aux_map = exec->aux_dict();
args_map = exec->arg_dict();
/*if fine tune from some pre-trained model, we should load the parameters*/
// NDArray::Load("./model/alex_params_3", nullptr, &args_map);
/*else, we should use initializer Xavier to init the params*/
Xavier xavier = Xavier(Xavier::gaussian, Xavier::in, 2.34);
for (auto &arg : args_map) {
/*be careful here, the arg's name must has some specific ends or starts for
* initializer to call*/
xavier(arg.first, &arg.second);
}
/*print out to check the shape of the net*/
for (const auto &s : Net.ListArguments()) {
LG << s;
const auto &k = args_map[s].GetShape();
for (const auto &i : k) {
cout << i << " ";
}
cout << endl;
}
/*these binary files should be generated using im2rc tools, which can be found
* in mxnet/bin*/
auto train_iter = MXDataIter("ImageRecordIter")
.SetParam("path_imglist", "./data/train_rec.lst")
.SetParam("path_imgrec", "./data/train_rec.bin")
.SetParam("data_shape", Shape(3, 256, 256))
.SetParam("batch_size", batch_size)
.SetParam("shuffle", 1)
.CreateDataIter();
auto val_iter = MXDataIter("ImageRecordIter")
.SetParam("path_imglist", "./data/val_rec.lst")
.SetParam("path_imgrec", "./data/val_rec.bin")
.SetParam("data_shape", Shape(3, 256, 256))
.SetParam("batch_size", batch_size)
.CreateDataIter();
Optimizer* opt = OptimizerRegistry::Find("ccsgd");
opt->SetParam("momentum", 0.9)
->SetParam("rescale_grad", 1.0 / batch_size)
->SetParam("clip_gradient", 10)
->SetParam("lr", learning_rate)
->SetParam("wd", weight_decay);
Accuracy acu_train, acu_val;
LogLoss logloss_val;
for (int iter = 0; iter < max_epo; ++iter) {
LG << "Train Epoch: " << iter;
/*reset the metric every epoch*/
acu_train.Reset();
/*reset the data iter every epoch*/
train_iter.Reset();
while (train_iter.Next()) {
auto batch = train_iter.GetDataBatch();
LG << train_iter.GetDataBatch().index.size();
/*use copyto to feed new data and label to the executor*/
batch.data.CopyTo(&args_map["data"]);
batch.label.CopyTo(&args_map["label"]);
exec->Forward(true);
exec->Backward();
for (size_t i = 0; i < arg_names.size(); ++i) {
if (arg_names[i] == "data" || arg_names[i] == "label") continue;
opt->Update(i, exec->arg_arrays[i], exec->grad_arrays[i]);
}
NDArray::WaitAll();
acu_train.Update(batch.label, exec->outputs[0]);
}
LG << "ITER: " << iter << " Train Accuracy: " << acu_train.Get();
LG << "Val Epoch: " << iter;
acu_val.Reset();
val_iter.Reset();
logloss_val.Reset();
while (val_iter.Next()) {
auto batch = val_iter.GetDataBatch();
LG << val_iter.GetDataBatch().index.size();
batch.data.CopyTo(&args_map["data"]);
batch.label.CopyTo(&args_map["label"]);
exec->Forward(false);
NDArray::WaitAll();
acu_val.Update(batch.label, exec->outputs[0]);
logloss_val.Update(batch.label, exec->outputs[0]);
}
LG << "ITER: " << iter << " Val Accuracy: " << acu_val.Get();
LG << "ITER: " << iter << " Val LogLoss: " << logloss_val.Get();
/*save the parameters*/
stringstream ss;
ss << iter;
string iter_str;
ss >> iter_str;
string save_path_param = "./model/alex_param_" + iter_str;
auto save_args = args_map;
/*we do not want to save the data and label*/
save_args.erase(save_args.find("data"));
save_args.erase(save_args.find("label"));
/*the alexnet does not get any aux array, so we do not need to save
* aux_map*/
LG << "ITER: " << iter << " Saving to..." << save_path_param;
NDArray::Save(save_path_param, save_args);
}
/*don't foget to release the executor*/
delete exec;
MXNotifyShutdown();
return 0;
}