summaryrefslogtreecommitdiff
path: root/tools/test_net.cpp
diff options
context:
space:
mode:
authorEvan Shelhamer <shelhamer@imaginarynumber.net>2014-02-24 22:45:29 -0800
committerEvan Shelhamer <shelhamer@imaginarynumber.net>2014-02-26 12:37:44 -0800
commit4f1cdeb4ef35b9f85646a9b624188fd761644f1d (patch)
tree0727acbf45b45c7f3e05120868e62cb3a2faf0c5 /tools/test_net.cpp
parent650b7b16dbbf0ee7b3a7362fce0821315d608d54 (diff)
downloadcaffeonacl-4f1cdeb4ef35b9f85646a9b624188fd761644f1d.tar.gz
caffeonacl-4f1cdeb4ef35b9f85646a9b624188fd761644f1d.tar.bz2
caffeonacl-4f1cdeb4ef35b9f85646a9b624188fd761644f1d.zip
Make tools/ for core binaries, stow scripts/ in tools/extra
Collect core Caffe tools like train_net, device_query, etc. together in tools/ and include helper scripts under tools/extra.
Diffstat (limited to 'tools/test_net.cpp')
-rw-r--r--tools/test_net.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/tools/test_net.cpp b/tools/test_net.cpp
new file mode 100644
index 00000000..5b8305af
--- /dev/null
+++ b/tools/test_net.cpp
@@ -0,0 +1,57 @@
+// 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 <cuda_runtime.h>
+
+#include <cstring>
+#include <cstdlib>
+
+#include "caffe/caffe.hpp"
+
+using namespace caffe;
+
+int main(int argc, char** argv) {
+ if (argc < 4) {
+ LOG(ERROR) << "test_net net_proto pretrained_net_proto iterations [CPU/GPU]";
+ return 0;
+ }
+
+ cudaSetDevice(0);
+ Caffe::set_phase(Caffe::TEST);
+
+ if (argc == 5 && strcmp(argv[4], "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);
+
+ int total_iter = atoi(argv[3]);
+ LOG(ERROR) << "Running " << total_iter << "Iterations.";
+
+ double test_accuracy = 0;
+ vector<Blob<float>*> dummy_blob_input_vec;
+ for (int i = 0; i < total_iter; ++i) {
+ const vector<Blob<float>*>& result =
+ caffe_test_net.Forward(dummy_blob_input_vec);
+ 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;
+
+ return 0;
+}