we want caffe2 in c++ not libtorch

This commit is contained in:
Kevin Darby committed 2018-12-28 23:09:30 -05:00
1 parent d7a12d0dd9
commit 6089b209d1
2 files changed
+34 -11

No files matched your search

+32 -10
View File
@@ -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<torch::jit::script::Module> module = torch::jit::load("");
bts_assert(module != nullptr);
std::vector<torch::jit::IValue> inputs;
inputs.push_back(torch::ones({1, 3, 224, 224}));
std::vector<float> data;
//caffe2::GlobalInit(&argc, &argv);
caffe2::GlobalInit();
std::vector<int> 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<TensorCPU>();
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<TensorCPU>();
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
}
me->def = def;
+2 -1
View File
@@ -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