質問編集履歴

2

追記

2021/10/06 04:10

投稿

kokawa2003
kokawa2003

スコア217

test CHANGED
File without changes
test CHANGED
@@ -1,10 +1,8 @@
1
- C++で書かれているcaffe(2でないほう)のコードがあります。
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

全面修正

2021/10/06 04:10

投稿

kokawa2003
kokawa2003

スコア217

test CHANGED
@@ -1 +1 @@
1
- C++ で書かれているcaffeコードをpythonにした
1
+ pythonのcaffeコードを直せな
test CHANGED
@@ -1,41 +1,13 @@
1
- C++で書かれているcaffe(2でないほう)のコードがあります。
1
+ C++で書かれているcaffe(2でないほう)のコードがあります。
2
-
2
+
3
- これをpython2.7に直したく思ました。caffeが古くてpython3が使えなかったためです。
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
- using namespace caffe; // NOLINT(build/namespaces)
74
-
75
-
76
-
77
-
78
-
79
- bool mysave_blob_to_binary(Blob<float>* blob, const string fn_blob, int num_index){
80
-
81
- FILE *f;
82
-
83
- float *buff;
84
-
85
- int n, c, l, w, h;
86
-
87
- f = fopen(fn_blob.c_str(), "wb");
88
-
89
- if (f==NULL)
90
-
91
- return false;
92
-
93
-
94
-
95
- if (num_index<0){
96
-
97
- n = blob->num();
98
-
99
- buff = blob->mutable_cpu_data();
100
-
101
- }else{
102
-
103
- LOG(ERROR) << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
104
-
105
- n = 1;
106
-
107
- buff = blob->mutable_cpu_data() + blob->offset(num_index);
108
-
109
- }
110
-
111
- c = blob->channels();
112
-
113
- l = blob->length();
114
-
115
- h = blob->height();
116
-
117
- w = blob->width();
118
-
119
-
120
-
121
- fwrite(&n, sizeof(int), 1, f);
122
-
123
- fwrite(&c, sizeof(int), 1, f);
124
-
125
- fwrite(&l, sizeof(int), 1, f);
126
-
127
- fwrite(&h, sizeof(int), 1, f);
128
-
129
- fwrite(&w, sizeof(int), 1, f);
130
-
131
- fwrite(buff, sizeof(float), n * c * l * h * w, f);
132
-
133
- fclose(f);
134
-
135
- return true;
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
- template<typename Dtype>
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
- return 1;
125
+ このコードなのですが
176
-
126
+
177
- }
127
+ 最初に
178
-
179
-
180
-
181
- char* net_proto = argv[1];
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
- Caffe::set_phase(Caffe::TEST);
129
+ Caffe::set_phase(Caffe::TEST);
196
-
130
+
197
- if (device_id>=0){
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(net_proto)));
220
-
221
- feature_extraction_net->CopyTrainedLayersFrom(string(pretrained_model));
222
-
223
-
224
-
225
- for (int i=7; i<argc; i++){
226
-
227
- CHECK(feature_extraction_net->has_blob(string(argv[i])))
228
-
229
- << "Unknown feature blob name " << string(argv[i])
230
-
231
- << " in the network " << string(net_proto);
232
-
233
- }
234
-
235
-
236
-
237
- LOG(ERROR)<< "Extracting features for " << num_mini_batches << " batches";
238
-
239
- std::ifstream infile(fn_feat);
240
-
241
- string feat_prefix;
242
-
243
- std::vector<string> list_prefix;
244
-
245
- int c = 0;
246
-
247
-
248
-
249
- vector<Blob<float>*> input_vec;
250
-
251
- int image_index = 0;
252
-
253
-
254
-
255
- for (int batch_index = 0; batch_index < num_mini_batches; ++batch_index) {
256
-
257
- feature_extraction_net->Forward(input_vec);
258
-
259
- list_prefix.clear();
260
-
261
- for (int n=0; n<batch_size; n++){
262
-
263
- if (infile >> feat_prefix)
264
-
265
- list_prefix.push_back(feat_prefix);
266
-
267
- else
268
-
269
- break;
270
-
271
- }
272
-
273
-
274
-
275
- if (list_prefix.empty())
276
-
277
- break;
278
-
279
- for (int k=7; k<argc; k++){
280
-
281
- string strext=string(argv[k]);
282
-
283
- const shared_ptr<Blob<Dtype> > feature_blob = feature_extraction_net
284
-
285
- ->blob_by_name(strext);
286
-
287
- int num_features = feature_blob->num();
288
-
289
- int ch=feature_blob->channels();
290
-
291
- int len=feature_blob->length() ;
292
-
293
- int h=feature_blob->height();
294
-
295
- int w=feature_blob->width() ;
296
-
297
- int c=feature_blob->count();
298
-
299
- Dtype *mc=feature_blob->mutable_cpu_data();
300
-
301
- LOG(ERROR) << "TTTTTTTTTTTTTTTTTTTTT"<<num_features<<":"<<list_prefix.size()<<":"<<ch<<":"<<len<<":"<<h<<":"<<w<<":"<<c<<feature_blob;
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
- で、これをPythonに直すめこのコードを書きました
221
+ これで同等な出力が出るのは見ましコードがちょっとアレです。
350
-
222
+
351
- ```python
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.forward()
231
+ net.set_device(0)
382
-
383
- blob=net.blobs['fc6-1']
232
+
384
-
385
- features = net.blobs['fc6-1'].data.copy()
233
+ net = caffe.Net(MODEL_FILE,PRETRAINED)
386
-
234
+
387
- funcs=inspect.getmembers(blob)
235
+ のあたりが....
388
-
236
+
389
- #net.blobs['fc6-1'].cpu_data()
237
+ netを二回読んでいるのは読む前にnet.set_phase_test()
390
-
391
- #features.cpu_data()
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
- にあるpythonの関数見つけないからです。
239
+ をしかったやり方がわからないからです。
434
-
435
- blob->offset(num_index)は多分blob[num_index]だろーとは考えていますが>mutable_cpu_dataは全く分かりません。
240
+
436
-
437
- 最後にログを載せます。
438
-
439
- prototxtも要望あれば載せます
440
-
441
- ```c++
442
-
443
- WARNING: Logging before InitGoogleLogging() is written to STDERR
241
+ 2回目のnet = caffe.Net(MODEL_FILE,PRETRAINED)を取ると出力が毎回変わります。
444
-
445
- E1004 16:56:15.849175 30246 extract_image_features_test.cpp:77] SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS
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
- pythonも載せたいのですが長すぎでNGで
243
+ 本当はどう書くのがのでしようか?
244
+
245
+ 動いているからいいとも思うが、偶然かもしれない。
246
+
247
+ ちょっとアブノーマルだと思うので正しい書き方を知りたいです。