From 6089b209d10bbeb5f80f5bca569c6f2cecb11904 Mon Sep 17 00:00:00 2001 From: Kevin Darby Date: Fri, 28 Dec 2018 23:09:30 -0500 Subject: [PATCH] we want caffe2 in c++ not libtorch --- src/sdk/examples/ai_order_plugin_example.cpp | 42 +++++++++++++++----- src/sdk/examples/build_examples.sh | 3 +- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/sdk/examples/ai_order_plugin_example.cpp b/src/sdk/examples/ai_order_plugin_example.cpp index a8f257de3..be57e3441 100644 --- a/src/sdk/examples/ai_order_plugin_example.cpp +++ b/src/sdk/examples/ai_order_plugin_example.cpp @@ -1,6 +1,11 @@ //the pytorch guys were kind enough to #define LOG... -#include "torch/script.h" +//#include "torch/script.h" +//https://github.com/leonardvandriel/caffe2_cpp_tutorial/blob/master/src/caffe2/binaries/pretrained.cc +#include "caffe2/core/init.h" +#include "caffe2/core/net.h" +#include "caffe2/utils/proto_utils.h" #undef LOG +using namespace caffe2; #include "spark/lib/logging/logging.h" #include "spark/lib/orderman/orders/DynamicOrder.h" @@ -79,17 +84,34 @@ void AILeanOrder::on_init_new( const PreActivationOrderServices &services) { { - // Deserialize the ScriptModule from a file using torch::jit::load(). - std::shared_ptr module = torch::jit::load(""); - bts_assert(module != nullptr); - - std::vector inputs; - inputs.push_back(torch::ones({1, 3, 224, 224})); + std::vector data; + //caffe2::GlobalInit(&argc, &argv); + caffe2::GlobalInit(); + std::vector dims({1, /*image.channels(), image.rows, image.cols*/ 2, 4, 4}); + //TensorCPU tensor(dims, data, NULL); + Tensor tensor(dims, DeviceType::CPU); - // Execute the model and turn its output into a tensor. - at::Tensor output = module->forward(inputs).toTensor(); + // Load Squeezenet model + NetDef init_net, predict_net; + + // >>> with open(path_to_INIT_NET) as f: + CAFFE_ENFORCE(ReadProtoFromFile("", &init_net)); + + // >>> with open(path_to_PREDICT_NET) as f: + CAFFE_ENFORCE(ReadProtoFromFile("", &predict_net)); + + // >>> p = workspace.Predictor(init_net, predict_net) + Workspace workspace("tmp"); + CAFFE_ENFORCE(workspace.RunNetOnce(init_net)); + auto input = workspace.CreateBlob("data")->GetMutable(); + input->ResizeLike(tensor); + input->ShareData(tensor); + CAFFE_ENFORCE(workspace.RunNetOnce(predict_net)); + + // >>> results = p.run([img]) + auto &output_name = predict_net.external_output(0); + auto output = workspace.GetBlob(output_name)->Get(); - std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n'; } me->def = def; diff --git a/src/sdk/examples/build_examples.sh b/src/sdk/examples/build_examples.sh index 7067e14a9..0a3ca08b9 100755 --- a/src/sdk/examples/build_examples.sh +++ b/src/sdk/examples/build_examples.sh @@ -32,7 +32,7 @@ g++ -isystem ../externals/include \ -L ../lib \ -l spark \ -L ../torch/lib \ - -l torch + -l caffe2 else g++ -isystem ../externals/include \ @@ -49,3 +49,4 @@ g++ -isystem ../externals/include \ -l spark fi +