質問編集履歴
2
追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,10 +1,8 @@
|
|
1
|
-
|
1
|
+
C++で書かれているcaffe(2でないほう)のコードがあります。
|
2
|
-
|
2
|
+
|
3
|
-
これをpython2.7に直したい
|
3
|
+
これをpython2.7に直したいのです。
|
4
|
-
|
5
|
-
|
4
|
+
|
6
|
-
|
7
|
-
C++のコードを書きました
|
5
|
+
まずC++のコードを書きました
|
8
6
|
|
9
7
|
```cpp
|
10
8
|
|
1
全面修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
pythonのcaffeコードを直せない。
|
test
CHANGED
@@ -1,41 +1,13 @@
|
|
1
|
-
C++で書かれているcaffe(2でないほう)のコードがあります。
|
1
|
+
”C++で書かれているcaffe(2でないほう)のコードがあります。
|
2
|
-
|
2
|
+
|
3
|
-
これをpython2.7に直した
|
3
|
+
これをpython2.7に直したい”
|
4
|
+
|
4
|
-
|
5
|
+
ということです。
|
6
|
+
|
5
|
-
C++のコード
|
7
|
+
C++のコードを書きました
|
6
8
|
|
7
9
|
```cpp
|
8
10
|
|
9
|
-
/*
|
10
|
-
|
11
|
-
*
|
12
|
-
|
13
|
-
* Copyright (c) 2015, Facebook, Inc. All rights reserved.
|
14
|
-
|
15
|
-
*
|
16
|
-
|
17
|
-
* Licensed under the Creative Commons Attribution-NonCommercial 3.0
|
18
|
-
|
19
|
-
* License (the "License"). You may obtain a copy of the License at
|
20
|
-
|
21
|
-
* https://creativecommons.org/licenses/by-nc/3.0/.
|
22
|
-
|
23
|
-
* Unless required by applicable law or agreed to in writing, software
|
24
|
-
|
25
|
-
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
26
|
-
|
27
|
-
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
28
|
-
|
29
|
-
* License for the specific language governing permissions and limitations
|
30
|
-
|
31
|
-
* under the License.
|
32
|
-
|
33
|
-
*
|
34
|
-
|
35
|
-
*
|
36
|
-
|
37
|
-
*/
|
38
|
-
|
39
11
|
|
40
12
|
|
41
13
|
#include <stdio.h> // for snprintf
|
@@ -70,307 +42,185 @@
|
|
70
42
|
|
71
43
|
|
72
44
|
|
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
|
-
|
45
|
+
int main(int argc, char** argv) {
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
char * MODEL_FILE = "/home/kokawa2003/work/ww2/C3D-v1.0/python/prototxt/c3d_sport1m_feature_extractor_file.prototxt";
|
50
|
+
|
51
|
+
char * PRETRAINED = "/home/kokawa2003/work/ww2/C3D-v1.0/python/conv3d_deepnetA_sport1m_iter_1900000";
|
52
|
+
|
53
|
+
char * LAYER = "fc6-1";
|
54
|
+
|
55
|
+
int INDEX = 0;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
Caffe::set_phase(Caffe::TEST);
|
66
|
+
|
67
|
+
shared_ptr<Net<Dtype> > feature_extraction_net(
|
68
|
+
|
69
|
+
new Net<Dtype>(string(MODEL_FILE)));
|
70
|
+
|
71
|
+
feature_extraction_net->CopyTrainedLayersFrom(string(PRETRAINED));
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
Caffe::set_mode(Caffe::GPU);
|
76
|
+
|
77
|
+
Caffe::SetDevice(0);
|
78
|
+
|
79
|
+
vector<Blob<float>*> input_vec;
|
80
|
+
|
81
|
+
feature_extraction_net->Forward(input_vec);
|
82
|
+
|
83
|
+
const shared_ptr<Blob<Dtype> > feature_blob = feature_extraction_net
|
84
|
+
|
85
|
+
->blob_by_name(LAYER);
|
86
|
+
|
87
|
+
int num_features = feature_blob->num();
|
88
|
+
|
89
|
+
int ch=feature_blob->channels();
|
90
|
+
|
91
|
+
int len=feature_blob->length() ;
|
92
|
+
|
93
|
+
int h=feature_blob->height();
|
94
|
+
|
95
|
+
int w=feature_blob->width() ;
|
96
|
+
|
97
|
+
int c=feature_blob->count();
|
98
|
+
|
99
|
+
LOG(ERROR) <<ch;
|
100
|
+
|
101
|
+
LOG(ERROR) <<num_features;
|
102
|
+
|
103
|
+
LOG(ERROR) <<h;
|
104
|
+
|
105
|
+
LOG(ERROR) <<w;
|
106
|
+
|
107
|
+
LOG(ERROR) <<c;
|
108
|
+
|
109
|
+
Dtype *mc=feature_blob->mutable_cpu_data();
|
110
|
+
|
111
|
+
for(int i=0;i<c;i++){
|
112
|
+
|
113
|
+
LOG(ERROR)<<mc[i];
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
return 0;
|
136
118
|
|
137
119
|
}
|
138
120
|
|
139
121
|
|
140
122
|
|
141
|
-
|
142
|
-
|
143
|
-
template<typename Dtype>
|
144
|
-
|
145
|
-
int feature_extraction_pipeline(int argc, char** argv);
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
int main(int argc, char** argv) {
|
150
|
-
|
151
|
-
return feature_extraction_pipeline<float>(argc, argv);
|
152
|
-
|
153
|
-
|
123
|
+
```
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
124
|
+
|
158
|
-
|
159
|
-
int feature_extraction_pipeline(int argc, char** argv) {
|
160
|
-
|
161
|
-
LOG(ERROR) << "SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS";
|
162
|
-
|
163
|
-
const int num_required_args = 7;
|
164
|
-
|
165
|
-
if (argc < num_required_args) {
|
166
|
-
|
167
|
-
LOG(ERROR) <<
|
168
|
-
|
169
|
-
"\nUsage: extract_image_features.bin <feature_extractor_prototxt_file>"
|
170
|
-
|
171
|
-
" <c3d_pre_trained_model> <gpu_id> <mini_batch_size> <number_of_mini_batches>"
|
172
|
-
|
173
|
-
" <output_prefix_file> feature_name1> [<feature_name2>, ..]\n";
|
174
|
-
|
175
|
-
|
125
|
+
このコードなのですが
|
176
|
-
|
126
|
+
|
177
|
-
|
127
|
+
最初に
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
128
|
+
|
182
|
-
|
183
|
-
char* pretrained_model = argv[2];
|
184
|
-
|
185
|
-
int device_id = atoi(argv[3]);
|
186
|
-
|
187
|
-
uint batch_size = atoi(argv[4]);
|
188
|
-
|
189
|
-
uint num_mini_batches = atoi(argv[5]);
|
190
|
-
|
191
|
-
char* fn_feat = argv[6];
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
129
|
+
Caffe::set_phase(Caffe::TEST);
|
196
|
-
|
130
|
+
|
197
|
-
|
131
|
+
から始まりますが、これが
|
198
|
-
|
199
|
-
Caffe::set_mode(Caffe::GPU);
|
200
|
-
|
201
|
-
Caffe::SetDevice(device_id);
|
202
|
-
|
203
|
-
LOG(ERROR) << "Using GPU #" << device_id;
|
204
|
-
|
205
|
-
}
|
206
|
-
|
207
|
-
else{
|
208
|
-
|
209
|
-
Caffe::set_mode(Caffe::CPU);
|
210
|
-
|
211
|
-
LOG(ERROR) << "Using CPU";
|
212
|
-
|
213
|
-
}
|
214
|
-
|
215
|
-
|
216
132
|
|
217
133
|
shared_ptr<Net<Dtype> > feature_extraction_net(
|
218
134
|
|
219
|
-
new Net<Dtype>(string(
|
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
|
-
//Dtype* feature_blob_data;
|
308
|
-
|
309
|
-
for (int n = 0; n < num_features; ++n) {
|
310
|
-
|
311
|
-
LOG(ERROR) << "KKKKKKKKKKKKKKKKK"<<list_prefix.size()<<":"<<n ;
|
312
|
-
|
313
|
-
if (list_prefix.size()>n){
|
314
|
-
|
315
|
-
string fn_feat = list_prefix[n] + string(".") + strext;
|
316
|
-
|
317
|
-
LOG(ERROR) << "UUUUUUUUUUUUUUUUU"<<fn_feat <<":"<<n;
|
318
|
-
|
319
|
-
mysave_blob_to_binary(feature_blob.get(), fn_feat, n);
|
320
|
-
|
321
|
-
}
|
322
|
-
|
323
|
-
}
|
324
|
-
|
325
|
-
}
|
326
|
-
|
327
|
-
image_index += list_prefix.size();
|
328
|
-
|
329
|
-
if (batch_index % 100 == 0) {
|
330
|
-
|
331
|
-
LOG(ERROR)<< "Extracted features of " << image_index <<
|
332
|
-
|
333
|
-
" images.";
|
334
|
-
|
335
|
-
}
|
336
|
-
|
337
|
-
}
|
338
|
-
|
339
|
-
LOG(ERROR)<< "Successfully extracted " << image_index << " features!";
|
340
|
-
|
341
|
-
infile.close();
|
342
|
-
|
343
|
-
return 0;
|
344
|
-
|
345
|
-
}
|
135
|
+
new Net<Dtype>(string(MODEL_FILE)));
|
136
|
+
|
137
|
+
の下に来るとどうも、最後の出力が毎回変わるのがわかりました。それでこのコードなのです。
|
138
|
+
|
139
|
+
これと同じことをpythonでしたかったので
|
140
|
+
|
141
|
+
このコードをかきました。
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
```python
|
146
|
+
|
147
|
+
import sys, os, os.path, numpy, caffe
|
148
|
+
|
149
|
+
import inspect
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
MODEL_FILE = '/home/kokawa2003/work/ww2/C3D-v1.0/python/prototxt/c3d_sport1m_feature_extractor_file.prototxt'
|
154
|
+
|
155
|
+
PRETRAINED = '/home/kokawa2003/work/ww2/C3D-v1.0/python/conv3d_deepnetA_sport1m_iter_1900000'
|
156
|
+
|
157
|
+
LAYER = 'fc6-1'
|
158
|
+
|
159
|
+
INDEX = 0
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
res=inspect.getmembers(caffe.Net);
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
#net = caffe.Classifier(MODEL_FILE)
|
172
|
+
|
173
|
+
net = caffe.Net(MODEL_FILE,PRETRAINED)
|
174
|
+
|
175
|
+
net.set_phase_test()
|
176
|
+
|
177
|
+
net.set_mode_gpu()
|
178
|
+
|
179
|
+
net.set_device(0)
|
180
|
+
|
181
|
+
net = caffe.Net(MODEL_FILE,PRETRAINED)
|
182
|
+
|
183
|
+
net.forward()
|
184
|
+
|
185
|
+
blob=net.blobs['fc6-1']
|
186
|
+
|
187
|
+
#features = blob.data.copy()
|
188
|
+
|
189
|
+
#funcs=inspect.getmembers(blob)
|
190
|
+
|
191
|
+
#net.blobs['fc6-1'].cpu_data()
|
192
|
+
|
193
|
+
#features.cpu_data()
|
194
|
+
|
195
|
+
print(blob.channels)
|
196
|
+
|
197
|
+
print(blob.num)
|
198
|
+
|
199
|
+
print(blob.height)
|
200
|
+
|
201
|
+
print(blob.width)
|
202
|
+
|
203
|
+
print(blob.count)
|
204
|
+
|
205
|
+
#print(len(blob.data))
|
206
|
+
|
207
|
+
#print(len(blob.data[0]))
|
208
|
+
|
209
|
+
for onedata1 in blob.data:
|
210
|
+
|
211
|
+
for one2 in onedata1:
|
212
|
+
|
213
|
+
print(one2)
|
214
|
+
|
215
|
+
#print(blob.data[0]);
|
216
|
+
|
217
|
+
|
346
218
|
|
347
219
|
```
|
348
220
|
|
349
|
-
|
221
|
+
これで同等な出力が出るのは見ましたがコードがちょっとアレです。
|
350
|
-
|
222
|
+
|
351
|
-
|
223
|
+
とくに
|
352
|
-
|
353
|
-
import sys, os, os.path, numpy, caffe
|
354
|
-
|
355
|
-
import inspect
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
MODEL_FILE = '/home/kokawa2003/work/ww2/C3D-v1.0/python/prototxt/c3d_sport1m_feature_extractor_file.prototxt'
|
360
|
-
|
361
|
-
PRETRAINED = '/home/kokawa2003/work/ww2/C3D-v1.0/python/conv3d_deepnetA_sport1m_iter_1900000'
|
362
|
-
|
363
|
-
LAYER = 'fc6-1'
|
364
|
-
|
365
|
-
INDEX = 0
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
|
374
224
|
|
375
225
|
net = caffe.Net(MODEL_FILE,PRETRAINED)
|
376
226
|
|
@@ -378,96 +228,20 @@
|
|
378
228
|
|
379
229
|
net.set_mode_gpu()
|
380
230
|
|
381
|
-
net.
|
231
|
+
net.set_device(0)
|
382
|
-
|
383
|
-
|
232
|
+
|
384
|
-
|
385
|
-
|
233
|
+
net = caffe.Net(MODEL_FILE,PRETRAINED)
|
386
|
-
|
234
|
+
|
387
|
-
|
235
|
+
のあたりが....
|
388
|
-
|
236
|
+
|
389
|
-
|
237
|
+
netを二回読んでいるのは読む前にnet.set_phase_test()
|
390
|
-
|
391
|
-
|
238
|
+
|
392
|
-
|
393
|
-
print(net.blobs['fc6-1'].channels)
|
394
|
-
|
395
|
-
print(net.blobs['fc6-1'].num)
|
396
|
-
|
397
|
-
#net.blobs['fc6-1'].length
|
398
|
-
|
399
|
-
print(net.blobs['fc6-1'].height)
|
400
|
-
|
401
|
-
print(net.blobs['fc6-1'].width)
|
402
|
-
|
403
|
-
print(net.blobs['fc6-1'].count)
|
404
|
-
|
405
|
-
#net.blobs['fc6-1'].mutable_cpu_data
|
406
|
-
|
407
|
-
print(len(features))
|
408
|
-
|
409
|
-
print(features[0])
|
410
|
-
|
411
|
-
print(funcs)
|
412
|
-
|
413
|
-
|
414
|
-
|
415
|
-
|
416
|
-
|
417
|
-
```
|
418
|
-
|
419
|
-
途中出力しているblogのデータを見ると同じなので、ここまではあっていると思っていますが、
|
420
|
-
|
421
|
-
最後のC++の方にある
|
422
|
-
|
423
|
-
buff = blob->mutable_cpu_data() + blob->offset(num_index);
|
424
|
-
|
425
|
-
の演算をpythonの方でやるやり方がわかりません
|
426
|
-
|
427
|
-
理由は
|
428
|
-
|
429
|
-
blob->mutable_cpu_data()
|
430
|
-
|
431
|
-
blob->offset(num_index)
|
432
|
-
|
433
|
-
|
239
|
+
をしたかったがやり方がわからないからです。
|
434
|
-
|
435
|
-
|
240
|
+
|
436
|
-
|
437
|
-
最後にログを載せます。
|
438
|
-
|
439
|
-
prototxtも要望あれば載せます
|
440
|
-
|
441
|
-
```c++
|
442
|
-
|
443
|
-
|
241
|
+
2回目のnet = caffe.Net(MODEL_FILE,PRETRAINED)を取ると出力が毎回変わります。
|
444
|
-
|
445
|
-
|
242
|
+
|
446
|
-
|
447
|
-
....
|
448
|
-
|
449
|
-
I1004 16:56:16.851805 30246 net.cpp:176] Network initialization done.
|
450
|
-
|
451
|
-
I1004 16:56:16.851807 30246 net.cpp:177] Memory required for Data 5653289408
|
452
|
-
|
453
|
-
E1004 16:56:17.099912 30246 extract_image_features_test.cpp:115] Extracting features for 1 batches
|
454
|
-
|
455
|
-
E1004 16:56:17.980628 30246 extract_image_features_test.cpp:147] TTTTTTTTTTTTTTTTTTTTT50:1:4096:1:1:1:2048000x55d761bc0040
|
456
|
-
|
457
|
-
E1004 16:56:17.980654 30246 extract_image_features_test.cpp:152] KKKKKKKKKKKKKKKKK1:0
|
458
|
-
|
459
|
-
E1004 16:56:17.980656 30246 extract_image_features_test.cpp:155] UUUUUUUUUUUUUUUUU./temp2/001665.fc6-1:0
|
460
|
-
|
461
|
-
E1004 16:56:17.980712 30246 extract_image_features_test.cpp:48] AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
462
|
-
|
463
|
-
....
|
464
|
-
|
465
|
-
E1004 16:56:17.980847 30246 extract_image_features_test.cpp:162] Extracted features of 1 images.
|
466
|
-
|
467
|
-
E1004 16:56:17.980849 30246 extract_image_features_test.cpp:166] Successfully extracted 1 features!
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
```
|
472
|
-
|
473
|
-
|
243
|
+
本当はどう書くのがいいのでしようか?
|
244
|
+
|
245
|
+
動いているからいいとも思うが、偶然かもしれない。
|
246
|
+
|
247
|
+
ちょっとアブノーマルだと思うので正しい書き方を知りたいです。
|