forked from szagoruyko/loadcaffe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadcaffe.cpp
646 lines (602 loc) · 21.6 KB
/
loadcaffe.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
//
// main.cpp
// loadcaffe
//
// Created by Sergey Zagoruyko on 28/11/14.
// Copyright (c) 2014 Sergey Zagoruyko. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <TH/TH.h>
#include <locale>
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include "build/caffe.pb.h"
using google::protobuf::io::FileInputStream;
using google::protobuf::io::FileOutputStream;
using google::protobuf::io::ZeroCopyInputStream;
using google::protobuf::io::CodedInputStream;
using google::protobuf::Message;
extern "C" {
void loadBinary(void** handle, const char* prototxt_name, const char* binary_name);
void convertProtoToLua(void** handle, const char* lua_name, const char* cuda_package);
void convertProtoToLuaV1(const caffe::NetParameter &netparam, const char* lua_name, const char* cuda_package);
void convertProtoToLuaV2(const caffe::NetParameter &netparam, const char* lua_name, const char* cuda_package);
void loadModule(const void** handle, const char* name, THFloatTensor* weight, THFloatTensor* bias);
void loadModuleV2(const caffe::NetParameter* netparam, const char* name, THFloatTensor* weight, THFloatTensor* bias);
void loadModuleV1(const caffe::NetParameter* netparam, const char* name, THFloatTensor* weight, THFloatTensor* bias);
void destroyBinary(void** handle);
}
bool ReadProtoFromTextFile(const char* filename, Message* proto) {
int fd = open(filename, O_RDONLY);
if(fd < 0)
return false;
FileInputStream* input = new FileInputStream(fd);
bool success = google::protobuf::TextFormat::Parse(input, proto);
delete input;
close(fd);
return success;
}
bool ReadProtoFromBinaryFile(const char* filename, Message* proto) {
int fd = open(filename, O_RDONLY);
if(fd < 0)
return false;
ZeroCopyInputStream* raw_input = new FileInputStream(fd);
CodedInputStream* coded_input = new CodedInputStream(raw_input);
coded_input->SetTotalBytesLimit(1073741824, 536870912);
bool success = proto->ParseFromCodedStream(coded_input);
delete coded_input;
delete raw_input;
close(fd);
return success;
}
enum PACKAGE_TYPE {
CCN2, NN, CUDNN
};
void convertProtoToLua(void** handle, const char* lua_name, const char* cuda_package)
{
std::locale::global(std::locale());
const caffe::NetParameter netparam = *(const caffe::NetParameter*)handle[1];
if (netparam.layers_size() > 0)
convertProtoToLuaV1(netparam, lua_name, cuda_package);
else
convertProtoToLuaV2(netparam, lua_name, cuda_package);
}
void convertProtoToLuaV1(const caffe::NetParameter &netparam, const char* lua_name, const char* cuda_package)
{
PACKAGE_TYPE cuda_package_type = CCN2;
if(std::string(cuda_package) == "ccn2")
cuda_package_type = CCN2;
else if(std::string(cuda_package) == "nn")
cuda_package_type = NN;
else if(std::string(cuda_package) == "cudnn")
cuda_package_type = CUDNN;
std::ofstream ofs (lua_name);
ofs << "require '" << cuda_package << "'\n";
ofs << "local model = {}\n";
if(std::string(cuda_package)=="ccn2")
ofs<< "table.insert(model, {'torch_transpose_dwhb', nn.Transpose({1,4},{1,3},{1,2})})\n";
int num_output = netparam.input_dim_size();
for (int i=0; i<netparam.layers_size(); ++i)
{
std::vector<std::pair<std::string, std::string>> lines;
auto& layer = netparam.layers(i);
switch(layer.type())
{
case caffe::V1LayerParameter::CONVOLUTION:
{
auto ¶m = layer.convolution_param();
int groups = param.group() == 0 ? 1 : param.group();
int nInputPlane = layer.blobs(0).channels()*groups;
int nOutputPlane = layer.blobs(0).num();
//int nOutputPlane = param.num_output();
num_output = nOutputPlane;
int kW = param.kernel_w();
int kH = param.kernel_h();
int dW = param.stride_w();
int dH = param.stride_h();
if(kW==0 || kH==0)
{
kW = param.kernel_size();
kH = kW;
}
if(dW==0 || dH==0)
{
dW = param.stride();
dH = dW;
}
int pad_w = param.pad_w();
int pad_h = param.pad_h();
if(pad_w==0 || pad_h==0)
{
pad_w = param.pad();
pad_h = pad_w;
}
if(cuda_package_type == CCN2)
{
if(kW != kH || dW != dH || pad_w != pad_h)
{
std::cout << "ccn2 only supports square images!\n";
break;
}
char buf[1024];
sprintf(buf, "ccn2.SpatialConvolution(%d, %d, %d, %d, %d, %d)",
nInputPlane, nOutputPlane, kW, dW, pad_w, groups);
lines.emplace_back(layer.name(), buf);
}
else if(cuda_package_type == NN)
{
if(groups != 1)
{
std::cout << "nn supports no groups!\n";
break;
}
char buf[1024];
sprintf(buf, "nn.SpatialConvolutionMM(%d, %d, %d, %d, %d, %d, %d, %d)",
nInputPlane, nOutputPlane, kW, kH, dW, dH, pad_w, pad_h);
lines.emplace_back(layer.name(), buf);
}
else
{
char buf[1024];
sprintf(buf, "cudnn.SpatialConvolution(%d, %d, %d, %d, %d, %d, %d, %d, %d)",
nInputPlane, nOutputPlane, kW, kH, dW, dH, pad_w, pad_h, groups);
lines.emplace_back(layer.name(), buf);
}
break;
}
case caffe::V1LayerParameter::POOLING:
{
auto ¶m = layer.pooling_param();
int kW = param.kernel_w();
int kH = param.kernel_h();
int dW = param.stride_w();
int dH = param.stride_h();
int padW = param.pad_w();
int padH = param.pad_h();
if(kW==0 || kH==0)
{
kW = param.kernel_size();
kH = kW;
}
if(dW==0 || dH==0)
{
dW = param.stride();
dH = dW;
}
if(padW==0 && padH==0)
{
padW = param.pad();
padH = padW;
}
char buf[1024];
switch(cuda_package_type)
{
case CCN2: // ceil mode by default
if(param.pool() == caffe::PoolingParameter::MAX)
sprintf(buf, "ccn2.SpatialMaxPooling(%d, %d)", kW, dW);
else if(param.pool() == caffe::PoolingParameter::AVE)
sprintf(buf, "ccn2.SpatialAvgPooling(%d, %d)", kW, dW);
else if(param.pool() == caffe::PoolingParameter::STOCHASTIC)
THError("Stochastic pooling is not implemented in DHWB format");
break;
case CUDNN:
if(param.pool() == caffe::PoolingParameter::MAX)
sprintf(buf, "cudnn.SpatialMaxPooling(%d, %d, %d, %d, %d, %d):ceil()", kW, kH, dW, dH, padW, padH);
else if(param.pool() == caffe::PoolingParameter::AVE)
sprintf(buf, "cudnn.SpatialAveragePooling(%d, %d, %d, %d, %d, %d):ceil()", kW, kH, dW, dH, padW, padH);
else if(param.pool() == caffe::PoolingParameter::STOCHASTIC)
sprintf(buf, "inn.SpatialStochasticPooling(%d, %d, %d, %d)", kW, kH, dW, dH);
break;
case NN:
if(param.pool() == caffe::PoolingParameter::MAX)
sprintf(buf, "nn.SpatialMaxPooling(%d, %d, %d, %d, %d, %d):ceil()", kW, kH, dW, dH, padW, padH);
else if(param.pool() == caffe::PoolingParameter::AVE)
sprintf(buf, "inn.SpatialAveragePooling(%d, %d, %d, %d)", kW, kH, dW, dH); // padding is not supported yet
else if(param.pool() == caffe::PoolingParameter::STOCHASTIC)
sprintf(buf, "inn.SpatialStochasticPooling(%d, %d, %d, %d)", kW, kH, dW, dH);
break;
}
lines.emplace_back(layer.name(), buf);
break;
}
case caffe::V1LayerParameter::RELU:
{
switch(cuda_package_type)
{
case CUDNN:
lines.emplace_back(layer.name(), "cudnn.ReLU(true)");
break;
default:
lines.emplace_back(layer.name(), "nn.ReLU(true)");
break;
}
break;
}
case caffe::V1LayerParameter::TANH:
{
switch(cuda_package_type)
{
case CUDNN:
lines.emplace_back(layer.name(), "cudnn.Tanh(true)");
break;
default:
lines.emplace_back(layer.name(), "nn.Tanh()");
break;
}
break;
}
case caffe::V1LayerParameter::SIGMOID:
{
switch(cuda_package_type)
{
case CUDNN:
lines.emplace_back(layer.name(), "cudnn.Sigmoid(true)");
break;
default:
lines.emplace_back(layer.name(), "nn.Sigmoid()");
break;
}
break;
}
case caffe::V1LayerParameter::LRN:
{
auto ¶m = layer.lrn_param();
int local_size = param.local_size();
float alpha = param.alpha();
float beta = param.beta();
float k = param.k();
char buf[1024];
if(std::string(cuda_package) == "ccn2")
sprintf(buf, "ccn2.SpatialCrossResponseNormalization(%d, %.6f, %.4f, %f)", local_size, alpha, beta, k);
else
sprintf(buf, "inn.SpatialCrossResponseNormalization(%d, %.6f, %.4f, %f)", local_size, alpha, beta, k);
lines.emplace_back(layer.name(), buf);
break;
}
case caffe::V1LayerParameter::INNER_PRODUCT:
{
auto ¶m = layer.inner_product_param();
int nInputPlane = layer.blobs(0).width();
int nOutputPlane = param.num_output();
char buf[1024];
sprintf(buf, "nn.Linear(%d, %d)", nInputPlane, nOutputPlane);
if(num_output != nInputPlane)
{
if(std::string(cuda_package) == "ccn2")
lines.emplace_back("torch_transpose_bdwh", "nn.Transpose({4,1},{4,2},{4,3})");
lines.emplace_back("torch_view", "nn.View(-1):setNumInputDims(3)");
}
lines.emplace_back(layer.name(), buf);
num_output = nOutputPlane;
break;
}
case caffe::V1LayerParameter::DROPOUT:
{
char buf[1024];
sprintf(buf, "nn.Dropout(%f)", layer.dropout_param().dropout_ratio());
lines.emplace_back(layer.name(), buf);
break;
}
case caffe::V1LayerParameter::SOFTMAX_LOSS:
{
lines.emplace_back(layer.name(), "nn.SoftMax()");
break;
}
case caffe::V1LayerParameter::SOFTMAX:
{
lines.emplace_back(layer.name(), "nn.SoftMax()");
break;
}
default:
{
std::cout << "MODULE " << layer.name() << " UNDEFINED\n";
break;
}
}
if(!lines.empty())
for(auto& it: lines)
ofs << "table.insert(model, {'" << it.first << "', " << it.second << "})\n";
else
{
ofs << "-- warning: module '" << layer.name() << " [type " << layer.type() << "]" << "' not found\n";
std::cout << "warning: module '" << layer.name() << " [type " << layer.type() << "]" << "' not found\n";
}
}
ofs << "return model";
}
void convertProtoToLuaV2(const caffe::NetParameter &netparam, const char* lua_name, const char* cuda_package)
{
PACKAGE_TYPE cuda_package_type = CCN2;
if(std::string(cuda_package) == "ccn2")
cuda_package_type = CCN2;
else if(std::string(cuda_package) == "nn")
cuda_package_type = NN;
else if(std::string(cuda_package) == "cudnn")
cuda_package_type = CUDNN;
std::ofstream ofs (lua_name);
ofs << "require '" << cuda_package << "'\n";
ofs << "local model = {}\n";
if(std::string(cuda_package)=="ccn2")
ofs<< "table.insert(model, {'torch_transpose_dwhb', nn.Transpose({1,4},{1,3},{1,2})})\n";
int num_output = netparam.input_shape_size() * 4;
for (int i=0; i<netparam.layer_size(); ++i)
{
std::vector<std::pair<std::string, std::string>> lines;
auto& layer = netparam.layer(i);
if(layer.type() == "Convolution")
{
auto ¶m = layer.convolution_param();
int groups = param.group() == 0 ? 1 : param.group();
int nInputPlane = layer.blobs(0).shape().dim(1)*groups;
int nOutputPlane = layer.blobs(0).shape().dim(0);
//int nOutputPlane = param.num_output();
num_output = nOutputPlane;
int kW = param.kernel_w();
int kH = param.kernel_h();
int dW = param.stride_w();
int dH = param.stride_h();
if(kW==0 || kH==0)
{
kW = param.kernel_size();
kH = kW;
}
if(dW==0 || dH==0)
{
dW = param.stride();
dH = dW;
}
int pad_w = param.pad_w();
int pad_h = param.pad_h();
if(pad_w==0 || pad_h==0)
{
pad_w = param.pad();
pad_h = pad_w;
}
if(cuda_package_type == CCN2)
{
if(kW != kH || dW != dH || pad_w != pad_h)
{
std::cout << "ccn2 only supports square images!\n";
break;
}
char buf[1024];
sprintf(buf, "ccn2.SpatialConvolution(%d, %d, %d, %d, %d, %d)",
nInputPlane, nOutputPlane, kW, dW, pad_w, groups);
lines.emplace_back(layer.name(), buf);
}
else if(cuda_package_type == NN)
{
if(groups != 1)
{
std::cout << "nn supports no groups!\n";
break;
}
char buf[1024];
sprintf(buf, "nn.SpatialConvolutionMM(%d, %d, %d, %d, %d, %d, %d, %d)",
nInputPlane, nOutputPlane, kW, kH, dW, dH, pad_w, pad_h);
lines.emplace_back(layer.name(), buf);
}
else
{
char buf[1024];
sprintf(buf, "cudnn.SpatialConvolution(%d, %d, %d, %d, %d, %d, %d, %d, %d)",
nInputPlane, nOutputPlane, kW, kH, dW, dH, pad_w, pad_h, groups);
lines.emplace_back(layer.name(), buf);
}
}
if(layer.type() == "Pooling")
{
auto ¶m = layer.pooling_param();
std::string ptype = param.pool() == caffe::PoolingParameter::MAX ? "Max" : "Avg";
int kW = param.kernel_w();
int kH = param.kernel_h();
int dW = param.stride_w();
int dH = param.stride_h();
int padW = param.pad_w();
int padH = param.pad_h();
if(kW==0 || kH==0)
{
kW = param.kernel_size();
kH = kW;
}
if(dW==0 || dH==0)
{
dW = param.stride();
dH = dW;
}
if(padW==0 && padH==0)
{
padW = param.pad();
padH = padW;
}
char buf[1024];
switch(cuda_package_type)
{
case CCN2: // ceil mode by default
if(param.pool() == caffe::PoolingParameter::MAX)
sprintf(buf, "ccn2.SpatialMaxPooling(%d, %d)", kW, dW);
else if(param.pool() == caffe::PoolingParameter::AVE)
sprintf(buf, "ccn2.SpatialAvgPooling(%d, %d)", kW, dW);
else if(param.pool() == caffe::PoolingParameter::STOCHASTIC)
THError("Stochastic pooling is not implemented in DHWB format");
break;
case CUDNN:
if(param.pool() == caffe::PoolingParameter::MAX)
sprintf(buf, "cudnn.SpatialMaxPooling(%d, %d, %d, %d, %d, %d):ceil()", kW, kH, dW, dH, padW, padH);
else if(param.pool() == caffe::PoolingParameter::AVE)
sprintf(buf, "cudnn.SpatialAveragePooling(%d, %d, %d, %d, %d, %d):ceil()", kW, kH, dW, dH, padW, padH);
else if(param.pool() == caffe::PoolingParameter::STOCHASTIC)
sprintf(buf, "inn.SpatialStochasticPooling(%d, %d, %d, %d)", kW, kH, dW, dH);
break;
case NN:
if(param.pool() == caffe::PoolingParameter::MAX)
sprintf(buf, "nn.SpatialMaxPooling(%d, %d, %d, %d, %d, %d):ceil()", kW, kH, dW, dH, padW, padH);
else if(param.pool() == caffe::PoolingParameter::AVE)
sprintf(buf, "inn.SpatialAveragePooling(%d, %d, %d, %d)", kW, kH, dW, dH); // padding is not supported yet
else if(param.pool() == caffe::PoolingParameter::STOCHASTIC)
sprintf(buf, "inn.SpatialStochasticPooling(%d, %d, %d, %d)", kW, kH, dW, dH);
break;
}
lines.emplace_back(layer.name(), buf);
}
if(layer.type() == "ReLU")
{
if(cuda_package_type == CUDNN)
lines.emplace_back(layer.name(), "cudnn.ReLU(true)");
else
lines.emplace_back(layer.name(), "nn.ReLU(true)");
}
if(layer.type() == "Sigmoid")
{
if(cuda_package_type == CUDNN)
lines.emplace_back(layer.name(), "cudnn.Sigmoid(true)");
else
lines.emplace_back(layer.name(), "nn.Sigmoid()");
}
if(layer.type() == "Tanh")
{
if(cuda_package_type == CUDNN)
lines.emplace_back(layer.name(), "cudnn.Tanh(true)");
else
lines.emplace_back(layer.name(), "nn.Tanh()");
}
if(layer.type() == "LRN")
{
auto ¶m = layer.lrn_param();
int local_size = param.local_size();
float alpha = param.alpha();
float beta = param.beta();
float k = param.k();
char buf[1024];
if(std::string(cuda_package) == "ccn2")
sprintf(buf, "ccn2.SpatialCrossResponseNormalization(%d, %.6f, %.4f, %f)", local_size, alpha, beta, k);
else
sprintf(buf, "inn.SpatialCrossResponseNormalization(%d, %.6f, %.4f, %f)", local_size, alpha, beta, k);
lines.emplace_back(layer.name(), buf);
}
if(layer.type() == "InnerProduct")
{
auto ¶m = layer.inner_product_param();
int nInputPlane = layer.blobs(0).shape().dim(1);
int nOutputPlane = param.num_output();
char buf[1024];
sprintf(buf, "nn.Linear(%d, %d)", nInputPlane, nOutputPlane);
if(num_output != nInputPlane)
{
if(std::string(cuda_package) == "ccn2")
lines.emplace_back("torch_transpose_bdwh", "nn.Transpose({4,1},{4,2},{4,3})");
lines.emplace_back("torch_view", "nn.View(-1):setNumInputDims(3)");
}
lines.emplace_back(layer.name(), buf);
num_output = nOutputPlane;
}
if(layer.type() == "Dropout")
{
char buf[1024];
sprintf(buf, "nn.Dropout(%f)", layer.dropout_param().dropout_ratio());
lines.emplace_back(layer.name(), buf);
}
if(layer.type()=="SoftmaxWithLoss")
{
lines.emplace_back(layer.name(), "nn.SoftMax()");
}
if(layer.type()=="Softmax")
{
lines.emplace_back(layer.name(), "nn.SoftMax()");
}
if(!lines.empty())
for(auto& it: lines)
ofs << "table.insert(model, {'" << it.first << "', " << it.second << "})\n";
else
{
ofs << "-- warning: module '" << layer.name() << " [type " << layer.type() << "]" << "' not found\n";
std::cout << "warning: module '" << layer.name() << " [type " << layer.type() << "]" << "' not found\n";
}
}
ofs << "return model";
}
void loadBinary(void** handle, const char* prototxt_name, const char* binary_name)
{
caffe::NetParameter* netparam = new caffe::NetParameter();
ReadProtoFromTextFile(prototxt_name, netparam);
bool success = ReadProtoFromBinaryFile(binary_name, netparam);
if(success)
{
std::cout << "Successfully loaded " << binary_name << std::endl;
handle[1] = netparam;
}
else
std::cout << "Couldn't load " << binary_name << std::endl;
}
void destroyBinary(void** handle)
{
const caffe::NetParameter* netparam2 = (const caffe::NetParameter*)handle[1];
delete netparam2;
}
void loadModule(const void** handle, const char* name, THFloatTensor* weight, THFloatTensor* bias)
{
if(handle == NULL)
{
std::cout << "network not loaded!\n";
return;
}
const caffe::NetParameter* netparam = (const caffe::NetParameter*)handle[1];
if (netparam->layers_size() > 0)
loadModuleV1(netparam, name, weight, bias);
else
loadModuleV2(netparam, name, weight, bias);
}
void loadModuleV1(const caffe::NetParameter* netparam, const char* name, THFloatTensor* weight, THFloatTensor* bias)
{
int n = netparam->layers_size();
for(int i=0; i<n; ++i)
{
auto &layer = netparam->layers(i);
if(std::string(name) == layer.name())
{
int nInputPlane = layer.blobs(0).channels();
int nOutputPlane = layer.blobs(0).num();
int kW = layer.blobs(0).width();
int kH = layer.blobs(0).height();
printf("%s: %d %d %d %d\n", name, nOutputPlane, nInputPlane, kW, kH);
THFloatTensor_resize4d(weight, nOutputPlane, nInputPlane, kW, kH);
memcpy(THFloatTensor_data(weight), layer.blobs(0).data().data(), sizeof(float)*nOutputPlane*nInputPlane*kW*kH);
THFloatTensor_resize1d(bias, layer.blobs(1).data_size());
memcpy(THFloatTensor_data(bias), layer.blobs(1).data().data(), sizeof(float)*layer.blobs(1).data_size());
}
}
}
void loadModuleV2(const caffe::NetParameter* netparam, const char* name, THFloatTensor* weight, THFloatTensor* bias)
{
int n = netparam->layer_size();
for(int i=0; i<n; ++i)
{
auto &layer = netparam->layer(i);
if(std::string(name) == layer.name())
{
int nInputPlane = layer.blobs(0).shape().dim(1);
int nOutputPlane = layer.blobs(0).shape().dim(0);
int kW = layer.blobs(0).shape().dim(3);
int kH = layer.blobs(0).shape().dim(2);
if(layer.type() == "InnerProduct")
{
printf("%s: %d %d %d %d\n", name, 1, 1, nInputPlane, nOutputPlane);
THFloatTensor_resize4d(weight, 1, 1, nInputPlane, nOutputPlane);
memcpy(THFloatTensor_data(weight), layer.blobs(0).data().data(), sizeof(float)*nOutputPlane*nInputPlane);
}
else
{
printf("%s: %d %d %d %d\n", name, nOutputPlane, nInputPlane, kW, kH);
THFloatTensor_resize4d(weight, nOutputPlane, nInputPlane, kW, kH);
memcpy(THFloatTensor_data(weight), layer.blobs(0).data().data(), sizeof(float)*nOutputPlane*nInputPlane*kW*kH);
}
THFloatTensor_resize1d(bias, layer.blobs(1).data_size());
memcpy(THFloatTensor_data(bias), layer.blobs(1).data().data(), sizeof(float)*layer.blobs(1).data_size());
}
}
}