Caffe2 - C++ API
A deep learning, cross platform ML framework
gl_model_test.h
1 #include "caffe2/mobile/contrib/arm-compute/core/context.h"
2 #include "caffe2/mobile/contrib/arm-compute/test/gl_operator_test.h"
3 #include "caffe2/mobile/contrib/arm-compute/core/rewrite_net.h"
4 #include <gtest/gtest.h>
5 
6 #include "caffe2/core/operator.h"
7 #include "caffe2/core/workspace.h"
8 #include <unordered_set>
9 
10 CAFFE2_DEFINE_int(warmup, 10, "The number of iterations to warm up.");
11 CAFFE2_DEFINE_int(iter, 100, "The number of iterations to run.");
12 CAFFE2_DEFINE_bool(
13  run_individual,
14  false,
15  "Whether to benchmark individual operators.");
16 
17 
18 constexpr float tol = 0.03;
19 namespace caffe2 {
20  void benchmarkModel(std::string init_net_pb, std::string predict_net_pb, std::string input_name, std::vector<int> input_dims, std::string net_name="benchmark_net", std::unordered_set<std::string> cpu_ops = std::unordered_set<std::string>({})) {
21  unique_ptr<caffe2::Workspace> ws(new caffe2::Workspace());
22  NetDef init_net_def;
23  CAFFE_ENFORCE(ReadProtoFromFile(init_net_pb, &init_net_def));
24  CAFFE_ENFORCE(ws->RunNetOnce(init_net_def));
25  NetDef predict_net_def, predict_net_def_gpu;
26  CAFFE_ENFORCE(ReadProtoFromFile(predict_net_pb, &predict_net_def));
27  PopulateCPUBlob(ws.get(), true, input_name, input_dims);
28 
29  tryConvertToOpenGL(predict_net_def, &predict_net_def_gpu, false, cpu_ops);
30  // change the name of last op
31  auto index = predict_net_def_gpu.op().size() - 1;
32  auto last_blob = predict_net_def_gpu.op()[index].output()[0];
33  auto op = predict_net_def_gpu.mutable_op(index);
34  auto output = op->mutable_output(0);
35  *output = last_blob + "_gpu";
36 
37  for (auto i = 0; i < predict_net_def_gpu.external_output_size(); ++i) {
38  auto out = predict_net_def_gpu.mutable_external_output(i);
39  if (*out == last_blob) {
40  *out = last_blob + "_gpu";
41  }
42  }
43 
44  compareNetResult4D(*ws, predict_net_def, predict_net_def_gpu, last_blob, last_blob + "_gpu");
45 
46  NetBase* net = ws->CreateNet(predict_net_def);
47  LOG(INFO) << "[C2DEBUG] Benchmarking OpenGL Net";
48  net->TEST_Benchmark(caffe2::FLAGS_warmup, caffe2::FLAGS_iter, caffe2::FLAGS_run_individual);
49  // Test CPU
50  for (auto i = 0; i < predict_net_def.op().size(); ++i) {
51  auto op = predict_net_def.mutable_op(i);
52  if (std::find(cpu_ops.begin(), cpu_ops.end(), op->type()) == cpu_ops.end()) {
53  op->mutable_device_option()->set_device_type(CPU);
54  }
55  }
56  predict_net_def.set_type("simple");
57  predict_net_def.set_name("cpu_net");
58  net = ws->CreateNet(predict_net_def);
59  LOG(INFO) << "[C2DEBUG] Benchmarking CPU Net";
60  net->TEST_Benchmark(caffe2::FLAGS_warmup, caffe2::FLAGS_iter, caffe2::FLAGS_run_individual);
61 
62  }
63 } // namespace caffe2
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
Definition: workspace.h:47
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...