MCPcopy Create free account
hub / github.com/BVLC/caffe / test

Function test

tools/caffe.cpp:266–334  ·  view source on GitHub ↗

Test: score a model.

Source from the content-addressed store, hash-verified

264
265// Test: score a model.
266int test() {
267 CHECK_GT(FLAGS_model.size(), 0) << "Need a model definition to score.";
268 CHECK_GT(FLAGS_weights.size(), 0) << "Need model weights to score.";
269 vector<string> stages = get_stages_from_flags();
270
271 // Set device id and mode
272 vector<int> gpus;
273 get_gpus(&gpus);
274 if (gpus.size() != 0) {
275 LOG(INFO) << "Use GPU with device ID " << gpus[0];
276#ifndef CPU_ONLY
277 cudaDeviceProp device_prop;
278 cudaGetDeviceProperties(&device_prop, gpus[0]);
279 LOG(INFO) << "GPU device name: " << device_prop.name;
280#endif
281 Caffe::SetDevice(gpus[0]);
282 Caffe::set_mode(Caffe::GPU);
283 } else {
284 LOG(INFO) << "Use CPU.";
285 Caffe::set_mode(Caffe::CPU);
286 }
287 // Instantiate the caffe net.
288 Net<float> caffe_net(FLAGS_model, caffe::TEST, FLAGS_level, &stages);
289 caffe_net.CopyTrainedLayersFrom(FLAGS_weights);
290 LOG(INFO) << "Running for " << FLAGS_iterations << " iterations.";
291
292 vector<int> test_score_output_id;
293 vector<float> test_score;
294 float loss = 0;
295 for (int i = 0; i < FLAGS_iterations; ++i) {
296 float iter_loss;
297 const vector<Blob<float>*>& result =
298 caffe_net.Forward(&iter_loss);
299 loss += iter_loss;
300 int idx = 0;
301 for (int j = 0; j < result.size(); ++j) {
302 const float* result_vec = result[j]->cpu_data();
303 for (int k = 0; k < result[j]->count(); ++k, ++idx) {
304 const float score = result_vec[k];
305 if (i == 0) {
306 test_score.push_back(score);
307 test_score_output_id.push_back(j);
308 } else {
309 test_score[idx] += score;
310 }
311 const std::string& output_name = caffe_net.blob_names()[
312 caffe_net.output_blob_indices()[j]];
313 LOG(INFO) << "Batch " << i << ", " << output_name << " = " << score;
314 }
315 }
316 }
317 loss /= FLAGS_iterations;
318 LOG(INFO) << "Loss: " << loss;
319 for (int i = 0; i < test_score.size(); ++i) {
320 const std::string& output_name = caffe_net.blob_names()[
321 caffe_net.output_blob_indices()[test_score_output_id[i]]];
322 const float loss_weight = caffe_net.blob_loss_weights()[
323 caffe_net.output_blob_indices()[test_score_output_id[i]]];

Callers

nothing calls this directly

Calls 7

get_stages_from_flagsFunction · 0.85
get_gpusFunction · 0.85
CopyTrainedLayersFromMethod · 0.80
ForwardMethod · 0.80
countMethod · 0.80
sizeMethod · 0.45
cpu_dataMethod · 0.45

Tested by

no test coverage detected