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