4

So I have a pretrained pytorch model, I have saved both the model and the parameters just in case, but I need to use this model in C or C++ code, anybody know how I can do that?

Thanks a lot.

Rael
  • 149
  • 2
  • 10

2 Answers2

0

Try pytorch 1.0, which include a c++ library. The model could be exported to torch script format and be loaded in C++, check here https://pytorch.org/tutorials/advanced/cpp_export.html

  • but according to what i have see this option is not really covered i mean their is no tuto that show how to do so for many model except this one u shared here :!. – DINA TAKLIT Feb 27 '19 at 06:29
0

If you are using Ubuntu, try pytorch package in PPA of ppa:nnstreamer/ppa. In this Ubuntu pytorch package, there are header files and pkgconfig (.pc) file that helps building C/C++ programs using pytorch. If you want more simple ways to invoke pytorch/caffe2 models from C/C++ program, you may use C/API of nnstreamer or approach nnstreamer w/ GStreamer API as well.

To install nnstreamer or C-friendly pytorch (or tensorflow), use PPA by:

$ sudo add-apt-repository ppa:nnstreamer/ppa
$ sudo apt-get update
$ suto apt-get install ${whateveryouwant}

Candidates for ${whateveryouwant}: nnstreamer, nnstreamer-dev, nnstreamer-pytorch

If you want to access pytorch directly from your C code without using nnstreamer, you may refer to pytorch-wrapper of nnstreamer at: https://github.com/nnsuite/nnstreamer/blob/master/ext/nnstreamer/tensor_filter/tensor_filter_pytorch_core.cc

MZX
  • 66
  • 3