consolidate test into caffe cli
authorEvan Shelhamer <shelhamer@imaginarynumber.net>
Thu, 7 Aug 2014 06:38:17 +0000 (23:38 -0700)
committerEvan Shelhamer <shelhamer@imaginarynumber.net>
Thu, 7 Aug 2014 06:45:35 +0000 (23:45 -0700)
tools/caffe.cpp
tools/test_net.cpp

index 57e7a5e..313803d 100644 (file)
@@ -107,6 +107,40 @@ int train() {
 RegisterBrewFunction(train);
 
 
+// Test: score a model.
+int test() {
+  CHECK_GT(FLAGS_model.size(), 0) << "Need a model definition to score.";
+  CHECK_GT(FLAGS_weights.size(), 0) << "Need model weights to score.";
+
+  // Set device id and mode
+  if (FLAGS_gpu) {
+    LOG(INFO) << "Use GPU with device id " << FLAGS_device_id;
+    Caffe::SetDevice(FLAGS_device_id);
+    Caffe::set_mode(Caffe::GPU);
+  } else {
+    LOG(INFO) << "Use CPU.";
+    Caffe::set_mode(Caffe::CPU);
+  }
+  // Instantiate the caffe net.
+  Caffe::set_phase(Caffe::TEST);
+  Net<float> caffe_net(FLAGS_model);
+  caffe_net.CopyTrainedLayersFrom(FLAGS_weights);
+  LOG(INFO) << "Running for " << FLAGS_iterations << " iterations.";
+
+  double test_score = 0;
+  for (int i = 0; i < FLAGS_iterations; ++i) {
+    const vector<Blob<float>*>& result = caffe_net.ForwardPrefilled();
+    test_score += result[0]->cpu_data()[0];
+    LOG(INFO) << "Batch " << i << ", score: " << result[0]->cpu_data()[0];
+  }
+  test_score /= FLAGS_iterations;
+  LOG(INFO) << "Score: " << test_score;
+
+  return 0;
+}
+RegisterBrewFunction(test);
+
+
 // Time: benchmark the execution time of a model.
 int time() {
   CHECK_GT(FLAGS_model.size(), 0) << "Need a model definition to time.";
@@ -187,6 +221,7 @@ int main(int argc, char** argv) {
       "usage: caffe <command> <args>\n\n"
       "commands:\n"
       "  train           train or finetune a model\n"
+      "  test            score a model\n"
       "  device_query    show GPU diagnostic information\n"
       "  time            benchmark model execution time");
   // Run tool or show usage.
index de97ab8..cebbc4b 100644 (file)
@@ -1,53 +1,7 @@
-// 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 <cstdlib>
-#include <cstring>
-#include <vector>
-
 #include "caffe/caffe.hpp"
 
-using namespace caffe;  // NOLINT(build/namespaces)
-
 int main(int argc, char** argv) {
-  if (argc < 4 || argc > 6) {
-    LOG(ERROR) << "test_net net_proto pretrained_net_proto iterations "
-        << "[CPU/GPU] [Device ID]";
-    return 1;
-  }
-
-  Caffe::set_phase(Caffe::TEST);
-
-  if (argc >= 5 && strcmp(argv[4], "GPU") == 0) {
-    Caffe::set_mode(Caffe::GPU);
-    int device_id = 0;
-    if (argc == 6) {
-      device_id = atoi(argv[5]);
-    }
-    Caffe::SetDevice(device_id);
-    LOG(ERROR) << "Using GPU #" << device_id;
-  } else {
-    LOG(ERROR) << "Using CPU";
-    Caffe::set_mode(Caffe::CPU);
-  }
-
-  Net<float> caffe_test_net(argv[1]);
-  caffe_test_net.CopyTrainedLayersFrom(argv[2]);
-
-  int total_iter = atoi(argv[3]);
-  LOG(ERROR) << "Running " << total_iter << " iterations.";
-
-  double test_accuracy = 0;
-  for (int i = 0; i < total_iter; ++i) {
-    const vector<Blob<float>*>& result = caffe_test_net.ForwardPrefilled();
-    test_accuracy += result[0]->cpu_data()[0];
-    LOG(ERROR) << "Batch " << i << ", accuracy: " << result[0]->cpu_data()[0];
-  }
-  test_accuracy /= total_iter;
-  LOG(ERROR) << "Test accuracy: " << test_accuracy;
-
+  LOG(FATAL) << "Deprecated. Use caffe.bin test --model=... "
+      "--weights=... instead.";
   return 0;
 }