-
Notifications
You must be signed in to change notification settings - Fork 0
/
googlenet.cpp
188 lines (158 loc) · 7.58 KB
/
googlenet.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
/*
* 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 <string>
#include <vector>
#include <map>
#include "mxnet-cpp/MxNetCpp.h"
using namespace mxnet::cpp;
Symbol ConvFactory(Symbol data, int num_filter,
Shape kernel,
Shape stride = Shape(1, 1),
Shape pad = Shape(0, 0),
const std::string & name = "",
const std::string & suffix = "") {
Symbol conv_w("conv_" + name + suffix + "_w"), conv_b("conv_" + name + suffix + "_b");
Symbol conv = Convolution("conv_" + name + suffix, data,
conv_w, conv_b, kernel,
num_filter, stride, Shape(1, 1), pad);
return Activation("relu_" + name + suffix, conv, "relu");
}
Symbol InceptionFactory(Symbol data, int num_1x1, int num_3x3red,
int num_3x3, int num_d5x5red, int num_d5x5,
PoolingPoolType pool, int proj, const std::string & name) {
Symbol c1x1 = ConvFactory(data, num_1x1, Shape(1, 1),
Shape(1, 1), Shape(0, 0), name + "_1x1");
Symbol c3x3r = ConvFactory(data, num_3x3red, Shape(1, 1),
Shape(1, 1), Shape(0, 0), name + "_3x3", "_reduce");
Symbol c3x3 = ConvFactory(c3x3r, num_3x3, Shape(3, 3),
Shape(1, 1), Shape(1, 1), name + "_3x3");
Symbol cd5x5r = ConvFactory(data, num_d5x5red, Shape(1, 1),
Shape(1, 1), Shape(0, 0), name + "_5x5", "_reduce");
Symbol cd5x5 = ConvFactory(cd5x5r, num_d5x5, Shape(5, 5),
Shape(1, 1), Shape(2, 2), name + "_5x5");
Symbol pooling = Pooling(name + "_pool", data, Shape(3, 3), pool,
false, false, PoolingPoolingConvention::kValid,
Shape(1, 1), Shape(1, 1));
Symbol cproj = ConvFactory(pooling, proj, Shape(1, 1),
Shape(1, 1), Shape(0, 0), name + "_proj");
std::vector<Symbol> lst;
lst.push_back(c1x1);
lst.push_back(c3x3);
lst.push_back(cd5x5);
lst.push_back(cproj);
return Concat("ch_concat_" + name + "_chconcat", lst, lst.size());
}
Symbol GoogleNetSymbol(int num_classes) {
// data and label
Symbol data = Symbol::Variable("data");
Symbol data_label = Symbol::Variable("data_label");
Symbol conv1 = ConvFactory(data, 64, Shape(7, 7), Shape(2, 2), Shape(3, 3), "conv1");
Symbol pool1 = Pooling("pool1", conv1, Shape(3, 3), PoolingPoolType::kMax,
false, false, PoolingPoolingConvention::kValid, Shape(2, 2));
Symbol conv2 = ConvFactory(pool1, 64, Shape(1, 1), Shape(1, 1),
Shape(0, 0), "conv2");
Symbol conv3 = ConvFactory(conv2, 192, Shape(3, 3), Shape(1, 1), Shape(1, 1), "conv3");
Symbol pool3 = Pooling("pool3", conv3, Shape(3, 3), PoolingPoolType::kMax,
false, false, PoolingPoolingConvention::kValid, Shape(2, 2));
Symbol in3a = InceptionFactory(pool3, 64, 96, 128, 16, 32, PoolingPoolType::kMax, 32, "in3a");
Symbol in3b = InceptionFactory(in3a, 128, 128, 192, 32, 96, PoolingPoolType::kMax, 64, "in3b");
Symbol pool4 = Pooling("pool4", in3b, Shape(3, 3), PoolingPoolType::kMax,
false, false, PoolingPoolingConvention::kValid, Shape(2, 2));
Symbol in4a = InceptionFactory(pool4, 192, 96, 208, 16, 48, PoolingPoolType::kMax, 64, "in4a");
Symbol in4b = InceptionFactory(in4a, 160, 112, 224, 24, 64, PoolingPoolType::kMax, 64, "in4b");
Symbol in4c = InceptionFactory(in4b, 128, 128, 256, 24, 64, PoolingPoolType::kMax, 64, "in4c");
Symbol in4d = InceptionFactory(in4c, 112, 144, 288, 32, 64, PoolingPoolType::kMax, 64, "in4d");
Symbol in4e = InceptionFactory(in4d, 256, 160, 320, 32, 128, PoolingPoolType::kMax, 128, "in4e");
Symbol pool5 = Pooling("pool5", in4e, Shape(3, 3), PoolingPoolType::kMax,
false, false, PoolingPoolingConvention::kValid, Shape(2, 2));
Symbol in5a = InceptionFactory(pool5, 256, 160, 320, 32, 128, PoolingPoolType::kMax, 128, "in5a");
Symbol in5b = InceptionFactory(in5a, 384, 192, 384, 48, 128, PoolingPoolType::kMax, 128, "in5b");
Symbol pool6 = Pooling("pool6", in5b, Shape(7, 7), PoolingPoolType::kAvg,
false, false, PoolingPoolingConvention::kValid, Shape(1, 1));
Symbol flatten = Flatten("flatten", pool6);
Symbol fc1_w("fc1_w"), fc1_b("fc1_b");
Symbol fc1 = FullyConnected("fc1", flatten, fc1_w, fc1_b, num_classes);
return SoftmaxOutput("softmax", fc1, data_label);
}
int main(int argc, char const *argv[]) {
int batch_size = 50;
int max_epoch = 100;
float learning_rate = 1e-4;
float weight_decay = 1e-4;
auto googlenet = GoogleNetSymbol(10);
std::map<std::string, NDArray> args_map;
std::map<std::string, NDArray> aux_map;
args_map["data"] = NDArray(Shape(batch_size, 3, 256, 256), Context::gpu());
args_map["data_label"] = NDArray(Shape(batch_size), Context::gpu());
googlenet.InferArgsMap(Context::gpu(), &args_map, args_map);
auto train_iter = MXDataIter("ImageRecordIter")
.SetParam("path_imglist", "./train.lst")
.SetParam("path_imgrec", "./train.rec")
.SetParam("data_shape", Shape(3, 256, 256))
.SetParam("batch_size", batch_size)
.SetParam("shuffle", 1)
.CreateDataIter();
auto val_iter = MXDataIter("ImageRecordIter")
.SetParam("path_imglist", "./val.lst")
.SetParam("path_imgrec", "./_val.rec")
.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);
auto *exec = googlenet.SimpleBind(Context::gpu(), args_map);
auto arg_names = googlenet.ListArguments();
for (int iter = 0; iter < max_epoch; ++iter) {
LG << "Epoch: " << iter;
train_iter.Reset();
while (train_iter.Next()) {
auto data_batch = train_iter.GetDataBatch();
data_batch.data.CopyTo(&args_map["data"]);
data_batch.label.CopyTo(&args_map["data_label"]);
NDArray::WaitAll();
exec->Forward(true);
exec->Backward();
for (size_t i = 0; i < arg_names.size(); ++i) {
if (arg_names[i] == "data" || arg_names[i] == "data_label") continue;
opt->Update(i, exec->arg_arrays[i], exec->grad_arrays[i]);
}
}
Accuracy acu;
val_iter.Reset();
while (val_iter.Next()) {
auto data_batch = val_iter.GetDataBatch();
data_batch.data.CopyTo(&args_map["data"]);
data_batch.label.CopyTo(&args_map["data_label"]);
NDArray::WaitAll();
exec->Forward(false);
NDArray::WaitAll();
acu.Update(data_batch.label, exec->outputs[0]);
}
LG << "Accuracy: " << acu.Get();
}
delete exec;
MXNotifyShutdown();
return 0;
}