2 #include "caffe2/core/init.h" 3 #include "caffe2/core/operator.h" 4 #include "caffe2/core/tensor.h" 5 #include "caffe2/core/timer.h" 6 #include "caffe2/utils/proto_utils.h" 8 #define TEST_REAL_DATA 0 14 #define POPULATE_DATA(_n, _s, _l) \ 16 Blob* _blob = ws.CreateBlob((_n)); \ 17 auto* _tensor = BlobGetMutableTensor(_blob, CPU); \ 18 _tensor->Resize((_s)); \ 19 memcpy(_tensor->mutable_data<float>(), data_##_l, _tensor->nbytes()); \ 23 #define POPULATE_DATA(_n, _s, _l) \ 25 Blob* _blob = ws.CreateBlob((_n)); \ 26 auto* _tensor = BlobGetMutableTensor(_blob, CPU); \ 27 _tensor->Resize((_s)); \ 28 memset(_tensor->mutable_data<float>(), 1, _tensor->nbytes()); \ 39 void AddConstInput(
const vector<int64_t>& shape,
44 CPUContext context(option);
45 Blob* blob = ws->CreateBlob(name);
46 auto* tensor = BlobGetMutableTensor(blob, CPU);
47 tensor->Resize(shape);
48 math::Set<float, CPUContext>(tensor->size(), value,
49 tensor->mutable_data<
float>(),
53 void AddNoiseInput(
const vector<int64_t>& shape,
57 CPUContext context(option);
58 Blob* blob = ws->CreateBlob(name);
59 auto* tensor = BlobGetMutableTensor(blob, CPU);
60 tensor->Resize(shape);
62 math::RandGaussian<float, CPUContext>(
65 tensor->mutable_data<
float>(),
70 float snpe_run(
int iters, Workspace& ws) {
75 POPULATE_DATA(
"X_snpe", (caffe2::vector<int64_t>{H, W, C}), hwc);
78 def.set_name(
"snpe_test");
80 def.add_input(
"X_snpe");
81 def.add_output(
"snpeout");
82 std::ostringstream model_buffer;
83 std::ifstream file(
"/data/local/tmp/squeeze_net.dlc", std::ios::in|std::ios::binary);
84 CAFFE_ENFORCE(file.is_open(),
"Couldn't open test model.");
85 model_buffer << file.rdbuf();
86 CAFFE_ENFORCE(model_buffer.str().length() > 0,
"Couldn't load model into string.");
87 def.add_arg()->CopyFrom(MakeArgument(
"model_buffer", model_buffer.str()));
89 unique_ptr<OperatorBase> op(CreateOperator(def, &ws));
93 for (
auto i = 0; i < iters; ++i) {
96 return timer.MicroSeconds();
99 float caffe2_run(
int iters, Workspace& ws) {
108 ReadProtoFromBinaryFile(
"/data/local/tmp/squeeze_init_net.pb", &init_net);
109 ReadProtoFromBinaryFile(
"/data/local/tmp/squeeze_predict_net.pb", &predict_net);
110 ws.RunNetOnce(init_net);
111 POPULATE_DATA(
"data", (caffe2::vector<int64_t>{N, C, H, W}), chw);
112 predict_net.set_name(
"SqueezeNet");
113 ws.CreateNet(predict_net);
118 for (
auto i = 0; i < iters; ++i) {
119 ws.RunNet(
"SqueezeNet");
121 float us = timer.MicroSeconds();
123 OperatorDef copy_def;
124 copy_def.set_type(
"Copy");
125 copy_def.set_name(
"Copy");
126 copy_def.add_input(
"softmaxout");
127 copy_def.add_output(
"caffe2out");
128 unique_ptr<OperatorBase> copy_op(CreateOperator(copy_def, &ws));
135 int main(
int argc,
char** argv) {
140 std::cout <<
"Testing caffe2...";
141 float t_caffe2 = caffe2::caffe2_run(iters, ws);
142 std::cout <<
"done!\nTesting snpe...";
143 float t_snpe = caffe2::snpe_run(iters, ws);
144 std::cout <<
"done!\n";
151 CAFFE_ENFORCE(snpe_tensor.size() == caffe2_tensor.size(),
"Outputs are not the same!\n");
153 float total_diff = 0;
154 float KL_divergence = 0;
155 float JS_divergence = 0;
159 for (
auto i = 0; i < snpe_tensor.size(); ++i) {
160 auto Q = caffe2_tensor.data<
float>()[i];
161 auto P = snpe_tensor.data<
float>()[i];
166 auto diff = fabs(P - Q);
167 auto avg = P + Q / 2;
169 KL_divergence += P * log(P / Q);
170 JS_divergence += 0.5 * P * log(P / Q) + 0.5 * Q * log(Q / P);
173 if (diff / avg > 0.10 && avg > 0.01) {
174 std::cout <<
"Diff: " << diff <<
" (" << P <<
" vs " << Q <<
")\n";
178 float avg_diff = total_diff;
179 printf(
"Average difference is %f%%\n", avg_diff * 100);
180 printf(
"JS Divergence is %f\n", JS_divergence);
181 printf(
"KL Divergence is %f\n", KL_divergence);
182 printf(
"Predicted %d with %f%% confidence\n", max_index, max * 100);
184 printf (
"Caffe2: %f microseconds.\n", t_caffe2);
185 printf (
"SNPE: %f microseconds.\n", t_snpe);
186 printf (
"SNPE impl %fx faster\n", t_caffe2/t_snpe);
Blob is a general container that hosts a typed pointer.
Workspace is a class that holds all the related objects created during runtime: (1) all blobs...
const Blob * GetBlob(const string &name) const
Gets the blob with the given name as a const pointer.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
bool GlobalInit(int *pargc, char ***pargv)
Initialize the global environment of caffe2.
const T & Get() const
Gets the const reference of the stored object.