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

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

Created by referring torchvision.

  
      github.com
  
  
    pytorch/vision/blob/cd1748443afb1ccd168a3b3da29317ca5d4cd253/test/test_models.cpp#L10 11

#include <torch/script.h>
#include <torch/torch.h>
#include <iostream>


#include "../torchvision/csrc/models/models.h"


using namespace vision::models;


template <typename Model>
torch::Tensor forward_model(const std::string& input_path, torch::Tensor x) {
  Model network;
  torch::load(network, input_path);
  network->eval();
  return network->forward(x);
}


torch::Tensor forward_alexnet(const std::string& input_path, torch::Tensor x) {
  return forward_model<AlexNet>(input_path, x);
}





  
  
    
    
  
  



  
      
      GitHub
  
  
    

kerry-Cho/transfer-learning-Libtorch 7

LibTorch Example of transfer-learning. Contribute to kerry-Cho/transfer-learning-Libtorch development by creating an account on GitHub.