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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
|
// Copyright 2014 BVLC and contributors.
#include <map>
#include <set>
#include <string>
#include <vector>
#include "caffe/common.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/proto/deprecated/caffe_v0_to_v1_bridge.pb.h"
#include "caffe/layer.hpp"
#include "caffe/net.hpp"
#include "caffe/util/io.hpp"
#include "caffe/util/insert_splits.hpp"
#include "caffe/util/upgrade_proto.hpp"
using std::pair;
using std::map;
using std::set;
const int kNetParameterVersionNumber = 1;
namespace caffe {
template <typename Dtype>
Net<Dtype>::Net(const NetParameter& param) {
Init(param);
}
template <typename Dtype>
Net<Dtype>::Net(const string& param_file) {
NetParameter param;
ReadParamsFromTextFile(param_file, ¶m);
Init(param);
}
template <typename Dtype>
void Net<Dtype>::Init(const NetParameter& in_param) {
// Create a copy of in_param with splits added where necessary.
NetParameter param;
InsertSplits(in_param, ¶m);
// Basically, build all the layers and set up its connections.
name_ = param.name();
map<string, int> blob_name_to_idx;
set<string> available_blobs;
int num_layers = param.layers_size();
CHECK_EQ(param.input_size() * 4, param.input_dim_size())
<< "Incorrect bottom blob dimension specifications.";
size_t memory_used = 0;
// set the input blobs
for (int i = 0; i < param.input_size(); ++i) {
const string& blob_name = param.input(i);
shared_ptr<Blob<Dtype> > blob_pointer(
new Blob<Dtype>(param.input_dim(i * 4),
param.input_dim(i * 4 + 1),
param.input_dim(i * 4 + 2),
param.input_dim(i * 4 + 3)));
blobs_.push_back(blob_pointer);
blob_names_.push_back(blob_name);
blob_need_backward_.push_back(param.force_backward());
net_input_blob_indices_.push_back(i);
net_input_blobs_.push_back(blob_pointer.get());
blob_name_to_idx[blob_name] = i;
available_blobs.insert(blob_name);
memory_used += blob_pointer->count();
}
DLOG(INFO) << "Memory required for Data" << memory_used*sizeof(Dtype);
// For each layer, set up their input and output
bottom_vecs_.resize(param.layers_size());
top_vecs_.resize(param.layers_size());
bottom_id_vecs_.resize(param.layers_size());
top_id_vecs_.resize(param.layers_size());
for (int i = 0; i < param.layers_size(); ++i) {
bool in_place = false;
const LayerParameter& layer_param = param.layers(i);
layers_.push_back(shared_ptr<Layer<Dtype> >(GetLayer<Dtype>(layer_param)));
layer_names_.push_back(layer_param.name());
LOG(INFO) << "Creating Layer " << layer_param.name();
bool need_backward = param.force_backward();
// Figure out this layer's input and output
for (int j = 0; j < layer_param.bottom_size(); ++j) {
const string& blob_name = layer_param.bottom(j);
const int blob_id = blob_name_to_idx[blob_name];
if (available_blobs.find(blob_name) == available_blobs.end()) {
LOG(FATAL) << "Unknown blob input " << blob_name <<
" to layer" << j;
}
LOG(INFO) << layer_param.name() << " <- " << blob_name;
bottom_vecs_[i].push_back(
blobs_[blob_id].get());
bottom_id_vecs_[i].push_back(blob_id);
// If a blob needs backward, this layer should provide it.
need_backward |= blob_need_backward_[blob_id];
available_blobs.erase(blob_name);
}
for (int j = 0; j < layer_param.top_size(); ++j) {
const string& blob_name = layer_param.top(j);
// Check if we are doing in-place computation
if (layer_param.bottom_size() > j &&
blob_name == layer_param.bottom(j)) {
// In-place computation
LOG(INFO) << layer_param.name() << " -> " << blob_name << " (in-place)";
in_place = true;
available_blobs.insert(blob_name);
top_vecs_[i].push_back(
blobs_[blob_name_to_idx[blob_name]].get());
top_id_vecs_[i].push_back(blob_name_to_idx[blob_name]);
} else if (blob_name_to_idx.find(blob_name) != blob_name_to_idx.end()) {
// If we are not doing in-place computation but has duplicated blobs,
// raise an error.
LOG(FATAL) << "Duplicate blobs produced by multiple sources.";
} else {
// Normal output.
LOG(INFO) << layer_param.name() << " -> " << blob_name;
shared_ptr<Blob<Dtype> > blob_pointer(new Blob<Dtype>());
blobs_.push_back(blob_pointer);
blob_names_.push_back(blob_name);
blob_need_backward_.push_back(param.force_backward());
blob_name_to_idx[blob_name] = blob_names_.size() - 1;
available_blobs.insert(blob_name);
top_vecs_[i].push_back(blobs_[blob_names_.size() - 1].get());
top_id_vecs_[i].push_back(blob_names_.size() - 1);
}
}
// After this layer is connected, set it up.
// LOG(INFO) << "Setting up " << layer_names_[i];
layers_[i]->SetUp(bottom_vecs_[i], &top_vecs_[i]);
for (int topid = 0; topid < top_vecs_[i].size(); ++topid) {
LOG(INFO) << "Top shape: " << top_vecs_[i][topid]->num() << " "
<< top_vecs_[i][topid]->channels() << " "
<< top_vecs_[i][topid]->height() << " "
<< top_vecs_[i][topid]->width() << " ("
<< top_vecs_[i][topid]->count() << ")";
if (!in_place)
memory_used += top_vecs_[i][topid]->count();
}
DLOG(INFO) << "Memory required for Data " << memory_used*sizeof(Dtype);
int blobs_lr_size = layers_[i]->layer_param().blobs_lr_size();
CHECK(blobs_lr_size == layers_[i]->blobs().size() || blobs_lr_size == 0)
<< "Incorrect blobs lr size: should be either 0 or the same as "
"the number of the layer's parameter blobs.";
if (blobs_lr_size) {
// Check if this layer needs backward operation itself
for (int j = 0; j < blobs_lr_size; ++j) {
need_backward |= (layers_[i]->layer_param().blobs_lr(j) > 0);
}
} else if (layers_[i]->blobs().size()) {
// catch: if a layer param does not specify blobs_lr, we should assume the
// learning rate to be 1. Thus we will need to perform backward.
need_backward = true;
}
// Finally, set the backward flag
layer_need_backward_.push_back(need_backward);
if (need_backward) {
LOG(INFO) << layer_names_[i] << " needs backward computation.";
for (int j = 0; j < top_id_vecs_[i].size(); ++j) {
blob_need_backward_[top_id_vecs_[i][j]] = true;
}
} else {
LOG(INFO) << layer_names_[i] << " does not need backward computation.";
}
}
// In the end, all remaining blobs are considered output blobs.
for (set<string>::iterator it = available_blobs.begin();
it != available_blobs.end(); ++it) {
LOG(INFO) << "This network produces output " << *it;
net_output_blobs_.push_back(blobs_[blob_name_to_idx[*it]].get());
}
for (size_t i = 0; i < blob_names_.size(); ++i) {
blob_names_index_[blob_names_[i]] = i;
}
for (size_t i = 0; i < layer_names_.size(); ++i) {
layer_names_index_[layer_names_[i]] = i;
}
GetLearningRateAndWeightDecay();
LOG(INFO) << "Network initialization done.";
LOG(INFO) << "Memory required for Data " << memory_used*sizeof(Dtype);
}
template <typename Dtype>
void Net<Dtype>::GetLearningRateAndWeightDecay() {
LOG(INFO) << "Collecting Learning Rate and Weight Decay.";
for (int i = 0; i < layers_.size(); ++i) {
vector<shared_ptr<Blob<Dtype> > >& layer_blobs = layers_[i]->blobs();
for (int j = 0; j < layer_blobs.size(); ++j) {
params_.push_back(layer_blobs[j]);
}
// push the learning rate mutlipliers
if (layers_[i]->layer_param().blobs_lr_size()) {
CHECK_EQ(layers_[i]->layer_param().blobs_lr_size(), layer_blobs.size());
for (int j = 0; j < layer_blobs.size(); ++j) {
float local_lr = layers_[i]->layer_param().blobs_lr(j);
CHECK_GE(local_lr, 0.);
params_lr_.push_back(local_lr);
}
} else {
for (int j = 0; j < layer_blobs.size(); ++j) {
params_lr_.push_back(1.);
}
}
// push the weight decay multipliers
if (layers_[i]->layer_param().weight_decay_size()) {
CHECK_EQ(layers_[i]->layer_param().weight_decay_size(),
layer_blobs.size());
for (int j = 0; j < layer_blobs.size(); ++j) {
float local_decay = layers_[i]->layer_param().weight_decay(j);
CHECK_GE(local_decay, 0.);
params_weight_decay_.push_back(local_decay);
}
} else {
for (int j = 0; j < layer_blobs.size(); ++j) {
params_weight_decay_.push_back(1.);
}
}
}
}
template <typename Dtype>
const vector<Blob<Dtype>*>& Net<Dtype>::ForwardPrefilled(Dtype* loss) {
if (loss != NULL) {
*loss = Dtype(0.);
}
for (int i = 0; i < layers_.size(); ++i) {
// LOG(ERROR) << "Forwarding " << layer_names_[i];
Dtype layer_loss = layers_[i]->Forward(bottom_vecs_[i], &top_vecs_[i]);
if (loss != NULL) {
*loss += layer_loss;
}
}
return net_output_blobs_;
}
template <typename Dtype>
const vector<Blob<Dtype>*>& Net<Dtype>::Forward(
const vector<Blob<Dtype>*> & bottom, Dtype* loss) {
// Copy bottom to internal bottom
for (int i = 0; i < bottom.size(); ++i) {
net_input_blobs_[i]->CopyFrom(*bottom[i]);
}
return ForwardPrefilled(loss);
}
template <typename Dtype>
string Net<Dtype>::Forward(const string& input_blob_protos, Dtype* loss) {
BlobProtoVector blob_proto_vec;
if (net_input_blobs_.size()) {
blob_proto_vec.ParseFromString(input_blob_protos);
CHECK_EQ(blob_proto_vec.blobs_size(), net_input_blobs_.size())
<< "Incorrect input size.";
for (int i = 0; i < blob_proto_vec.blobs_size(); ++i) {
net_input_blobs_[i]->FromProto(blob_proto_vec.blobs(i));
}
}
ForwardPrefilled(loss);
blob_proto_vec.Clear();
for (int i = 0; i < net_output_blobs_.size(); ++i) {
net_output_blobs_[i]->ToProto(blob_proto_vec.add_blobs());
}
string output;
blob_proto_vec.SerializeToString(&output);
return output;
}
template <typename Dtype>
void Net<Dtype>::Backward() {
for (int i = layers_.size() - 1; i >= 0; --i) {
if (layer_need_backward_[i]) {
layers_[i]->Backward(top_vecs_[i], true, &bottom_vecs_[i]);
}
}
}
template <typename Dtype>
void Net<Dtype>::CopyTrainedLayersFrom(const NetParameter& param) {
int num_source_layers = param.layers_size();
for (int i = 0; i < num_source_layers; ++i) {
const LayerParameter& source_layer = param.layers(i);
const string& source_layer_name = source_layer.name();
int target_layer_id = 0;
while (target_layer_id != layer_names_.size() &&
layer_names_[target_layer_id] != source_layer_name) {
++target_layer_id;
}
if (target_layer_id == layer_names_.size()) {
DLOG(INFO) << "Ignoring source layer " << source_layer_name;
continue;
}
DLOG(INFO) << "Copying source layer " << source_layer_name;
vector<shared_ptr<Blob<Dtype> > >& target_blobs =
layers_[target_layer_id]->blobs();
CHECK_EQ(target_blobs.size(), source_layer.blobs_size())
<< "Incompatible number of blobs for layer " << source_layer_name;
for (int j = 0; j < target_blobs.size(); ++j) {
CHECK_EQ(target_blobs[j]->num(), source_layer.blobs(j).num());
CHECK_EQ(target_blobs[j]->channels(), source_layer.blobs(j).channels());
CHECK_EQ(target_blobs[j]->height(), source_layer.blobs(j).height());
CHECK_EQ(target_blobs[j]->width(), source_layer.blobs(j).width());
target_blobs[j]->FromProto(source_layer.blobs(j));
}
}
}
template <typename Dtype>
void Net<Dtype>::CopyTrainedLayersFrom(const string trained_filename) {
NetParameter param;
ReadParamsFromBinaryFile(trained_filename, ¶m);
CopyTrainedLayersFrom(param);
}
template <typename Dtype>
void Net<Dtype>::ReadParamsFromTextFile(const string& param_file,
NetParameter* param) {
if (!ReadProtoFromTextFile(param_file, param)) {
// Failed to parse file as NetParameter; try to parse as a V0NetParameter
// instead.
V0NetParameter v0_param;
CHECK(ReadProtoFromTextFile(param_file, &v0_param))
<< "Failed to parse NetParameter file: " << param_file;
LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file;
LOG(ERROR) << "Note that future Caffe releases will not support "
<< "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade "
<< "this and any other network proto files to the new format.";
if (!UpgradeV0Net(v0_param, param)) {
LOG(ERROR) << "Warning: had one or more problems upgrading "
<< "V0NetParameter to NetParameter (see above); continuing anyway.";
}
}
CHECK_EQ(param->version(), kNetParameterVersionNumber);
}
template <typename Dtype>
void Net<Dtype>::ReadParamsFromBinaryFile(const string& param_file,
NetParameter* param) {
if (!ReadProtoFromBinaryFile(param_file, param)) {
// Failed to parse file as NetParameter; try to parse as a V0NetParameter
// instead.
V0NetParameter v0_param;
CHECK(ReadProtoFromBinaryFile(param_file, &v0_param))
<< "Failed to parse NetParameter file: " << param_file;
LOG(ERROR) << "Parsed file as V0NetParameter: " << param_file;
LOG(ERROR) << "Note that future Caffe releases will not support "
<< "V0NetParameter; use ./build/tools/upgrade_net_proto.bin to upgrade "
<< "this and any other network proto files to the new format.";
if (!UpgradeV0Net(v0_param, param)) {
LOG(ERROR) << "Warning: had one or more problems upgrading "
<< "V0NetParameter to NetParameter (see above); continuing anyway.";
}
}
CHECK_EQ(param->version(), kNetParameterVersionNumber);
}
template <typename Dtype>
void Net<Dtype>::ToProto(NetParameter* param, bool write_diff) {
param->Clear();
param->set_name(name_);
// Add bottom and top
for (int i = 0; i < net_input_blob_indices_.size(); ++i) {
param->add_input(blob_names_[net_input_blob_indices_[i]]);
}
DLOG(INFO) << "Serializing " << layers_.size() << " layers";
for (int i = 0; i < layers_.size(); ++i) {
LayerParameter* layer_param = param->add_layers();
for (int j = 0; j < bottom_id_vecs_[i].size(); ++j) {
layer_param->add_bottom(blob_names_[bottom_id_vecs_[i][j]]);
}
for (int j = 0; j < top_id_vecs_[i].size(); ++j) {
layer_param->add_top(blob_names_[top_id_vecs_[i][j]]);
}
layers_[i]->ToProto(layer_param, write_diff);
}
}
template <typename Dtype>
void Net<Dtype>::Update() {
for (int i = 0; i < params_.size(); ++i) {
params_[i]->Update();
}
}
template <typename Dtype>
bool Net<Dtype>::has_blob(const string& blob_name) {
return blob_names_index_.find(blob_name) != blob_names_index_.end();
}
template <typename Dtype>
const shared_ptr<Blob<Dtype> > Net<Dtype>::blob_by_name(
const string& blob_name) {
shared_ptr<Blob<Dtype> > blob_ptr;
if (has_blob(blob_name)) {
blob_ptr = blobs_[blob_names_index_[blob_name]];
} else {
blob_ptr.reset((Blob<Dtype>*)(NULL));
LOG(WARNING) << "Unknown blob name " << blob_name;
}
return blob_ptr;
}
template <typename Dtype>
bool Net<Dtype>::has_layer(const string& layer_name) {
return layer_names_index_.find(layer_name) != layer_names_index_.end();
}
template <typename Dtype>
const shared_ptr<Layer<Dtype> > Net<Dtype>::layer_by_name(
const string& layer_name) {
shared_ptr<Layer<Dtype> > layer_ptr;
if (has_layer(layer_name)) {
layer_ptr = layers_[layer_names_index_[layer_name]];
} else {
layer_ptr.reset((Layer<Dtype>*)(NULL));
LOG(WARNING) << "Unknown layer name " << layer_name;
}
return layer_ptr;
}
INSTANTIATE_CLASS(Net);
} // namespace caffe
|