-
Notifications
You must be signed in to change notification settings - Fork 2
/
Net.cpp
531 lines (466 loc) · 13.6 KB
/
Net.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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
* @Author: Weiliang Chen
* @Date: 2016-09-29 10:51:07
* @Last Modified by: Weiliang Chen
* @Last Modified time: 2016-09-29 10:55:27
*/
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <armadillo>
#include "Net.h"
#include "ConvLayer.h"
#include "Blob.h"
#include "common.h"
#include "Layer.h"
#include "Utils.h"
using std::ifstream;
using std::stringstream;
using std::string;
using std::vector;
using std::cout;
using std::endl;
namespace fn {
template <typename Dtype>
Net<Dtype>::Net(std::string name) {
name_ = name;
}
template <typename Dtype>
Net<Dtype>::~Net()
{
}
template<typename Dtype>
bool Net<Dtype>::init(std::string model_dir)
{
//Load mean image.
mean_ = arma::Cube<Dtype>(IMAGE_SIZE_HEIGHT, IMAGE_SIZE_WIDTH, IMAGE_CHANNELS);
if (!load(model_dir+"/mean.txt", IMAGE_SIZE_HEIGHT, IMAGE_SIZE_WIDTH, IMAGE_CHANNELS, mean_)) {
return false;
}
//conv1
Blob<Dtype> w1(4,4,1,32);
arma::Col<Dtype> b1(32);
if (!load(model_dir+"/w1.txt", 4, 4, 1, 32, w1)) {
return false;
}
if (!load(model_dir+"/b1.txt", 32, b1)) {
return false;
}
conv1_.LayerSetUp(w1, b1, 1, 1, 0, 0);
//conv1_1
Blob<Dtype> w1_1(3, 3, 32, 32);
arma::Col<Dtype> b1_1(32);
if (!load(model_dir + "/w1_1.txt", 3, 3, 32, 32, w1_1)) {
return false;
}
if (!load(model_dir + "/b1_1.txt", 32, b1_1)) {
return false;
}
conv1_1_.LayerSetUp(w1_1, b1_1, 1, 1, 1, 1);
//pool1
pool1_.LayerSetUp(2, 2, 2, 2, 0, 0);
//conv2_1
Blob<Dtype> w2_1(3, 3, 32, 32);
arma::Col<Dtype>b2_1(32);
if (!load(model_dir + "/w2_1.txt", 3, 3, 32, 32, w2_1)) {
return false;
}
if (!load(model_dir + "/b2_1.txt", 32, b2_1)) {
return false;
}
conv2_1_.LayerSetUp(w2_1, b2_1, 1, 1, 1, 1);
//conv2_2
Blob<Dtype> w2_2(3, 3, 32, 32);
arma::Col<Dtype>b2_2(32);
if (!load(model_dir + "/w2_2.txt", 3, 3, 32, 32, w2_2)) {
return false;
}
if (!load(model_dir + "/b2_2.txt", 32, b2_2)) {
return false;
}
conv2_2_.LayerSetUp(w2_2, b2_2, 1, 1, 1, 1);
//conv2
Blob<Dtype> w2(3,3,32,64);
arma::Col<Dtype>b2(64);
if (!load(model_dir+"/w2.txt", 3, 3, 32, 64, w2)) {
return false;
}
if (!load(model_dir+"/b2.txt", 64, b2)) {
return false;
}
conv2_.LayerSetUp(w2, b2, 1, 1, 0, 0);
//pool2
pool2_.LayerSetUp(2, 2, 1, 1, 0, 0);
//conv3_1
Blob<Dtype> w3_1(3, 3, 64, 64);
arma::Col<Dtype>b3_1(64);
if (!load(model_dir + "/w3_1.txt", 3, 3, 64, 64, w3_1)) {
return false;
}
if (!load(model_dir + "/b3_1.txt", 64, b3_1)) {
return false;
}
conv3_1_.LayerSetUp(w3_1, b3_1, 1, 1, 1, 1);
//conv3_2
Blob<Dtype> w3_2(3, 3, 64, 64);
arma::Col<Dtype>b3_2(64);
if (!load(model_dir + "/w3_2.txt", 3, 3, 64, 64, w3_2)) {
return false;
}
if (!load(model_dir + "/b3_2.txt", 64, b3_2)) {
return false;
}
conv3_2_.LayerSetUp(w3_2, b3_2, 1, 1, 1, 1);
//conv3_3
Blob<Dtype> w3_3(3, 3, 64, 64);
arma::Col<Dtype>b3_3(64);
if (!load(model_dir + "/w3_3.txt", 3, 3, 64, 64, w3_3)) {
return false;
}
if (!load(model_dir + "/b3_3.txt", 64, b3_3)) {
return false;
}
conv3_3_.LayerSetUp(w3_3, b3_3, 1, 1, 1, 1);
//conv3_4
Blob<Dtype> w3_4(3, 3, 64, 64);
arma::Col<Dtype>b3_4(64);
if (!load(model_dir + "/w3_4.txt", 3, 3, 64, 64, w3_4)) {
return false;
}
if (!load(model_dir + "/b3_4.txt", 64, b3_4)) {
return false;
}
conv3_4_.LayerSetUp(w3_4, b3_4, 1, 1, 1, 1);
//conv3
Blob<Dtype> w3(3, 3, 64, 96);
arma::Col<Dtype>b3(96);
if (!load(model_dir+"/w3.txt", 3, 3, 64, 96, w3)) {
return false;
}
if (!load(model_dir+"/b3.txt", 96, b3)) {
}
conv3_.LayerSetUp(w3, b3, 1, 1, 0, 0);
//pool3
pool3_.LayerSetUp(2, 2, 2, 2, 0, 0);
//conv4
Blob<Dtype> w4(2, 2, 96, 128);
arma::Col<Dtype>b4(128);
if (!load(model_dir+"/w4.txt", 2, 2, 96, 128, w4)) {
return false;
}
if (!load(model_dir+"/b4.txt", 128, b4)) {
return false;
}
conv4_.LayerSetUp(w4, b4, 1, 1, 0, 0);
//fc160_1
arma::Mat<Dtype>fc160_w1(9504,160);
arma::Col<Dtype>fc160_b1(160);
if (!load(model_dir+"/fc160_b1.txt", 160, fc160_b1)) {
return false;
}
if (!load(model_dir+"/fc160_w1.txt", 9504, 160, fc160_w1)) {
return false;
}
fc160_1_.LayerSetUp(fc160_w1, fc160_b1);
//fc160_2
arma::Mat<Dtype>fc160_w2(10240, 160);
arma::Col<Dtype>fc160_b2(160);
if (!load(model_dir+"/fc160_b2.txt", 160, fc160_b2)) {
return false;
}
if (!load(model_dir+"/fc160_w2.txt", 10240, 160, fc160_w2)) {
return false;
}
fc160_2_.LayerSetUp(fc160_w2, fc160_b2);
return true;
}
template<typename Dtype>
void Net<Dtype>::forward(const cv::Mat & input, Dtype *feat160)
{
cv::Mat input_gray;
if (!input.data) {
cout <<"No image data." << endl;
return;
}
if (abs(input.rows*1.0 / input.cols - IMAGE_SIZE_HEIGHT*1.0 / IMAGE_SIZE_WIDTH) > 0.01) {
cout << "Warning:The length-width ratio of the input image is not 55/47.Result may be inaccurate." << endl;
}
if (input.channels() != 1) {
cv::cvtColor(input, input_gray, CV_BGR2GRAY);
}
else {
input_gray = input;
}
/*******************************Data Transform******************************************/
cv::Mat image;
cv::resize(input_gray, image, cv::Size(IMAGE_SIZE_WIDTH, IMAGE_SIZE_HEIGHT));
arma::Cube<Dtype> conv1_input = arma::Cube<Dtype>(image.rows, image.cols, image.channels());
cvMat2Cube(image, conv1_input);
//Subtract the mean image.
conv1_input = conv1_input - mean_;
cube2cvMat(conv1_input, image);
image.convertTo(image, CV_8UC3);
cv::Mat im_mean;
cube2cvMat(mean_, im_mean);
im_mean.convertTo(im_mean, CV_8UC3);
/****************************************************************************************/
//conv1
vector<int> shape;
shape.resize(3);
conv1_.CalShape(conv1_input, shape);
arma::Cube<Dtype > conv1_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv1_.Forward(conv1_input, conv1_output);
//relu1
relu1_.Forward(conv1_output, conv1_output);
// printCube(conv1_output, "conv1_output.txt");
//conv1_1
conv1_1_.CalShape(conv1_output, shape);
arma::Cube<Dtype> conv1_1_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv1_1_.Forward(conv1_output, conv1_1_output);
//relu1_1
relu1_1_.Forward(conv1_1_output, conv1_1_output);
// printCube(conv1_1_output, "conv1_1_output.txt");
//pool1
pool1_.CalShape(conv1_1_output, shape);
arma::Cube<Dtype > pool1_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
pool1_.Forward(conv1_1_output, pool1_output);
// printCube(pool1_output, "pool1_output.txt");
//conv2_1
conv2_1_.CalShape(pool1_output, shape);
arma::Cube<Dtype> conv2_1_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv2_1_.Forward(pool1_output, conv2_1_output);
// printCube(conv2_1_output, "conv2_1_output1.txt");
//relu2_1
relu2_1_.Forward(conv2_1_output, conv2_1_output);
// printCube(conv2_1_output, "conv2_1_output.txt");
//conv2_2
conv2_2_.CalShape(conv2_1_output, shape);
arma::Cube<Dtype> conv2_2_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv2_2_.Forward(conv2_1_output, conv2_2_output);
//relu2_2
relu2_2_.Forward(conv2_2_output, conv2_2_output);
//res2_2
conv2_2_output = conv2_2_output + pool1_output;
//conv2
conv2_.CalShape(conv2_2_output, shape);
arma::Cube<Dtype> conv2_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv2_.Forward(conv2_2_output, conv2_output);
//relu2
relu2_.Forward(conv2_output, conv2_output);
// printCube(conv2_output, "conv2_output.txt");
//pool2
pool2_.CalShape(conv2_output, shape);
arma::Cube<Dtype> pool2_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
pool2_.Forward(conv2_output, pool2_output);
// printCube(pool2_output, "pool2_output.txt");
//conv3_1
conv3_1_.CalShape(pool2_output, shape);
arma::Cube<Dtype> conv3_1_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv3_1_.Forward(pool2_output, conv3_1_output);
//relu3_1
relu3_1_.Forward(conv3_1_output, conv3_1_output);
//conv3_2
conv3_2_.CalShape(conv3_1_output, shape);
arma::Cube<Dtype> conv3_2_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv3_2_.Forward(conv3_1_output, conv3_2_output);
//relu3_2
relu3_2_.Forward(conv3_2_output, conv3_2_output);
//res3_2
conv3_2_output = conv3_2_output + pool2_output;
//conv3_3
conv3_3_.CalShape(conv3_2_output, shape);
arma::Cube<Dtype> conv3_3_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv3_3_.Forward(conv3_2_output, conv3_3_output);
//relu3_3
relu3_3_.Forward(conv3_3_output, conv3_3_output);
//conv3_4
conv3_4_.CalShape(conv3_3_output, shape);
arma::Cube<Dtype> conv3_4_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv3_4_.Forward(conv3_3_output, conv3_4_output);
//relu3_4
relu3_4_.Forward(conv3_4_output, conv3_4_output);
//res3_4
conv3_4_output = conv3_4_output + conv3_2_output;
//conv3
conv3_.CalShape(conv3_4_output, shape);
arma::Cube<Dtype> conv3_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv3_.Forward(conv3_4_output, conv3_output);
//relu3
relu3_.Forward(conv3_output, conv3_output);
//pool3
pool3_.CalShape(conv3_output, shape);
arma::Cube<Dtype> pool3_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
pool3_.Forward(conv3_output, pool3_output);
//conv4
conv4_.CalShape(pool3_output, shape);
arma::Cube<Dtype> conv4_output = arma::Cube<Dtype>(shape[0], shape[1], shape[2]);
conv4_.Forward(pool3_output, conv4_output);
//relu4
relu4_.Forward(conv4_output, conv4_output);
//fc160_1
arma::Col<Dtype> fc160_1_output = arma::Col<Dtype>(160);
fc160_1_.Forward(pool3_output, fc160_1_output);
//fc160_2
arma::Col<Dtype> fc160_2_output = arma::Col<Dtype>(160);
fc160_2_.Forward(conv4_output, fc160_2_output);
//fc160
float sum = 0;
for (int i = 0; i < fc160_1_output.n_elem; ++i) {
feat160[i] = fc160_1_output.at(i) + fc160_2_output.at(i);
sum += feat160[i];
}
}
template<typename Dtype>
void Net<Dtype>::forward(const cv::Mat &image, std::vector<Dtype> &feat, bool flip){
cv::Mat im_flip;
int length;
Dtype *pf = NULL;
if(flip){
length = 320;
cv::flip(image, im_flip, 1);
pf = (Dtype *)malloc(sizeof(Dtype)*length);
forward(image, pf);
forward(im_flip, pf + 160);
}
else{
length = 160;
pf = (Dtype *)malloc(sizeof(Dtype)*length);
forward(im_flip, pf);
}
//Normalization
Dtype sum = 0;
for (int i = 0; i <length; ++i) {
sum += pf[i]*pf[i];
}
sum = sqrt(sum);
feat.clear();
for (int i = 0; i < length; ++i) {
feat.push_back(pf[i] / sum) ;
}
free(pf);
}
template<typename Dtype>
void Net<Dtype>::forward(const cv::Mat & image, Dtype * feat, bool flip)
{
cv::Mat im_flip;
int length;
if (flip) {
cv::flip(image, im_flip, 1);
forward(image, feat);
forward(im_flip, feat + 160);
length = 320;
}
else {
forward(im_flip, feat);
length = 160;
}
//Normalization
Dtype sum = 0;
for (int i = 0; i <length; ++i) {
sum += feat[i]*feat[i];
}
sum = sqrt(sum);
for (int i = 0; i < length; ++i) {
feat[i] = feat[i] / sum;
}
}
//
template<typename Dtype>
bool Net<Dtype>::load(std::string filename,
const int height, const int width, const int channels, const int number,
Blob<Dtype>& weights)
{
ifstream in(filename);
if (!in.is_open()) {
cout << "Cannot open " <<filename<<"."<< endl;
return false;
}
stringstream ss;
vector<arma::Cube<Dtype>> *filters=weights.data_vec();
filters->clear();
for (int n = 0; n < number; ++n) {
arma::Cube<Dtype> filter = arma::Cube<Dtype>(height, width, channels);
Dtype *data = filter.memptr();
const int channel_size = height*width;
for (int c = channels; c --; data+=channel_size) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
char buffer[256];
in >> buffer;
Dtype value = 0;
value=atof(buffer);
*(data+x*height+y) = value;
}
}
}
filters->push_back(filter);
}
return true;
}
template<typename Dtype>
bool Net<Dtype>::load(std::string filename, const int height, const int width, const int channels, arma::Cube<Dtype>& mean)
{
ifstream in(filename);
if (!in.is_open()) {
cout << "Cannot open " << filename << "." << endl;
return false;
}
Dtype *data = mean.memptr();
Dtype value;
const int channel_size = mean.n_elem_slice;
for (int c = channels; c--; data += channel_size) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
char buffer[256];
in >> buffer;
value = atof(buffer);
*(data + x*height + y) = value;
}
}
int k = 0;
}
return true;
}
template<typename Dtype>
bool Net<Dtype>::load(std::string filename, const int length, arma::Col<Dtype>& bias)
{
ifstream in(filename);
if (!in.is_open()) {
cout << "Cannot open " << filename << "." << endl;
return false;
}
bias = arma::Col<Dtype>(length);
Dtype *data = bias.memptr();
for (int i = 0; i < length; ++i) {
char buffer[256];
in >> buffer;
Dtype value;
value = atof(buffer);
*(data++) = value;
}
return true;
}
template<typename Dtype>
bool Net<Dtype>::load(std::string filename, const int height, const int width, arma::Mat<Dtype>& weights)
{
ifstream in(filename);
if (!in.is_open()) {
cout << "Cannot open " << filename << "." << endl;
return false;
}
Dtype *data = weights.memptr();
Dtype value;
for (int w = 0; w < width; ++w) {
for (int h = 0; h < height; ++h) {
char buffer[256];
in >> buffer;
value = atof(buffer);
*(data + w*height + h) = value;
}
}
return true;
}
//Explicit instantiation
INSTANTIATE_CLASS(Net);
} // namespace fn