How to save torchscript model using cpp?

I know detail about how to save torchscript model using python, and load it with c++ from the doc: link.

But how to save torchscript using cpp?I should train my model using c++, but I don’t find any detail about it in document.


You can refer to the repository below to do what you want.

  1. First create a C ++ model with the same structure as the Python model
  2. Save your Python model as a JIT script.
  3. Instead of loading the model into a JIT script, you use torch :: load to load it.

Created by referring torchvision.

Hi, Thanks for your reply.
But I should train model with C++ and save the model as JIT script. The reason why I should save model as JIT script is that the inference code can’t access to model structure(the model defination class). Thus, I should find a way to save model as JIT script using C++.
However, the code you provide above should pass Model instance as parameter, so It doesn’t meet my need.

I don’t know
Is it to avoid exposing the model structure in inference?

It is one aspect. On the other hand, the Script Model has high-performance.

Hi, have you solved it? I have the same problem, and I try to trian the script model in C++, but it not works, could you please give me some help while you have train it successfully?thanks

1 Like

Hi, I found that both torchscript and torch::nn::Module model file can be loaded with torch::load(model, "");. It isn’t nesscery to save torchscript model in cpp while you load torchscript model to nn::Module and train it.