-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathfeat_net_raw.cpp
122 lines (104 loc) · 3.58 KB
/
feat_net_raw.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
// Copyright 2013 Yangqing Jia
//
// This is a simple script that allows one to quickly test a network whose
// structure is specified by text format protocol buffers, and whose parameter
// are loaded from a pre-trained network.
// Usage:
// test_net net_proto pretrained_net_proto iterations [CPU/GPU]
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <string>
#include "caffe/caffe.hpp"
using namespace caffe; // NOLINT(build/namespaces)
template <typename Dtype>
static void save_blob(const string& fn, Blob<Dtype> *b){
LOG(INFO) << "Saving " << fn;
FILE *f = fopen(fn.c_str(), "wb");
CHECK(f != NULL);
fwrite(b->cpu_data(), sizeof(Dtype), b->count(), f);
fclose(f);
}
int main(int argc, char** argv) {
if (argc < 5) {
LOG(ERROR) << "test_net net_proto pretrained_net_proto iterations inputbin output_dir"
<< " [CPU/GPU]";
return 0;
}
Caffe::set_phase(Caffe::TEST);
if (argc == 7 && strcmp(argv[6], "GPU") == 0) {
LOG(ERROR) << "Using GPU";
Caffe::set_mode(Caffe::GPU);
} else {
LOG(ERROR) << "Using CPU";
Caffe::set_mode(Caffe::CPU);
}
NetParameter test_net_param;
ReadProtoFromTextFile(argv[1], &test_net_param);
Net<float> caffe_test_net(test_net_param);
NetParameter trained_net_param;
ReadProtoFromBinaryFile(argv[2], &trained_net_param);
caffe_test_net.CopyTrainedLayersFrom(trained_net_param);
#if 0
SolverState state;
std::string state_file = std::string(argv[2]) + ".solverstate";
ReadProtoFromBinaryFile(state_file, &state);
#endif
int total_iter = atoi(argv[3]);
LOG(ERROR) << "Running " << total_iter << " Iterations.";
double test_accuracy = 0;
vector<Blob<float>*> dummy_blob_input_vec;
//save layer
char output_dir[1024];
int feature_layer_idx = -1;
int data_layer_idx = -1;
for(int i=0;i<caffe_test_net.layer_names().size();i++)
if(caffe_test_net.layer_names()[i] == "relu5"){
feature_layer_idx = i;
break;
}
for(int i=0;i<caffe_test_net.layer_names().size();i++)
if(caffe_test_net.layer_names()[i] == "data"){
data_layer_idx = i;
break;
}
CHECK_NE(feature_layer_idx, -1);
CHECK_NE(data_layer_idx, -1);
LOG(INFO) << "Data layer: " << data_layer_idx;
LOG(INFO) << "Feature layer: " << feature_layer_idx;
Blob<float>* output = caffe_test_net.top_vecs()[feature_layer_idx][0],
*data_blob = caffe_test_net.top_vecs()[data_layer_idx][0];
RawImageLayer<float> *data_layer = dynamic_cast<RawImageLayer<float>* >(caffe_test_net.layers()[data_layer_idx].get());
CHECK(data_layer != 0);
LOG(INFO) << "OUTPUT BLOB dim: " << output->num() << ' '
<< output->channels() << ' '
<< output->width() << ' '
<< output->height();
FILE *finput = fopen(argv[5], "rb");
CHECK(finput != NULL);
const int ih = data_blob->height(), iw = data_blob->width(), ic = data_blob->channels();
double buf[ih*iw*ic];
for (int i = 0; i < total_iter; ++i) {
float *d = data_blob->mutable_cpu_data();
size_t len = ih * iw * ic;
for(int j = 0; j < data_blob->num(); j++){
size_t nread = fread(buf, sizeof(double), len, finput);
CHECK_EQ(nread, len);
for(int k=0;k<len;k++){
d[k] = buf[k];
}
d += len;
}
const vector<Blob<float>*>& result =
caffe_test_net.Forward(dummy_blob_input_vec);
sprintf(output_dir, "%s/feat_%05d", argv[4], i);
save_blob(output_dir, output);
//test_accuracy += result[0]->cpu_data()[0];
//LOG(ERROR) << "Batch " << i << ", accuracy: " << result[0]->cpu_data()[0];
}
fclose(finput);
//test_accuracy /= total_iter;
//LOG(ERROR) << "Test accuracy:" << test_accuracy;
return 0;
}