1 // Copyright 2013 Yangqing Jia
3 #include <cuda_runtime.h>
5 #include <google/protobuf/text_format.h>
10 #include "caffe/blob.hpp"
11 #include "caffe/common.hpp"
12 #include "caffe/net.hpp"
13 #include "caffe/filler.hpp"
14 #include "caffe/proto/caffe.pb.h"
15 #include "caffe/util/io.hpp"
16 #include "caffe/optimization/solver.hpp"
18 using namespace caffe;
20 int main(int argc, char** argv) {
22 Caffe::set_mode(Caffe::GPU);
23 Caffe::set_phase(Caffe::TRAIN);
26 NetParameter net_param;
27 ReadProtoFromTextFile(argv[1],
29 vector<Blob<float>*> bottom_vec;
30 Net<float> caffe_net(net_param, bottom_vec);
32 // Run the network without training.
33 LOG(ERROR) << "Performing Forward";
34 caffe_net.Forward(bottom_vec);
35 LOG(ERROR) << "Performing Backward";
36 LOG(ERROR) << "Initial loss: " << caffe_net.Backward();
38 const vector<shared_ptr<Layer<float> > >& layers = caffe_net.layers();
39 vector<vector<Blob<float>*> >& bottom_vecs = caffe_net.bottom_vecs();
40 vector<vector<Blob<float>*> >& top_vecs = caffe_net.top_vecs();
41 LOG(ERROR) << "*** Benchmark begins ***";
42 for (int i = 0; i < layers.size(); ++i) {
43 const string& layername = layers[i]->layer_param().name();
44 clock_t start = clock();
45 for (int j = 0; j < repeat; ++j) {
46 layers[i]->Forward(bottom_vecs[i], &top_vecs[i]);
48 LOG(ERROR) << layername << "\tforward: "
49 << float(clock() - start) / CLOCKS_PER_SEC << " seconds.";
51 for (int i = layers.size() - 1; i >= 0; --i) {
52 const string& layername = layers[i]->layer_param().name();
53 clock_t start = clock();
54 for (int j = 0; j < repeat; ++j) {
55 layers[i]->Backward(top_vecs[i], true, &bottom_vecs[i]);
57 LOG(ERROR) << layername << "\tbackward: "
58 << float(clock() - start) / CLOCKS_PER_SEC << " seconds.";
60 LOG(ERROR) << "*** Benchmark ends ***";