Laod a model to c++

jonatanbe8: <p>I’m working on integrating a PyTorch model into a C++ application using LibTorch. I have a class PositionEvaluation_Board_Game that inherits from torch::nn::Module. I’ve defined the constructor and forward methods, and I’ve overloaded the function call operator to use this class as a model.</p>
<p>I’m trying to add a method that loads a model from a file path to initialize an object of this class, similar to how I’ve done it in Python. However, I’m encountering issues with loading the model and assigning it to the current instance of the class.</p>
<p>Here’s the method I’m trying to implement in C++:</p>
<p>void PositionEvaluation_Board_Game::load_model(const std::string&amp; model_path) {<br>
if (!std::ifstream(model_path).good()) {<br>
throw std::runtime_error("Model file does not exist: " + model_path);<br>
}<br>
try {<br>
auto module = torch::jit::load(model_path);<br>
*this = std::move(module);<br>
std::cout &lt;&lt; "Model loaded successfully from: " &lt;&lt; model_path &lt;&lt; std::endl;<br>
} catch (const c10::Error&amp; e) {<br>
std::cerr &lt;&lt; "Failed to load model: " &lt;&lt; e.what() &lt;&lt; std::endl;<br>
throw;<br>
}<br>
}<br>
<strong>Issues:</strong></p>
<ol>
<li>
<p><strong>Compilation Error</strong>: I receive errors indicating that *this = module; is not valid because there is no overloaded assignment operator that can assign a torch::jit::Module to PositionEvaluation_Board_Game.</p>
</li>
<li>
<p><strong>Conceptual Error</strong>: I’m unclear on the correct way to load and use a trained PyTorch model in C++. Specifically, I’m confused about how to integrate the loaded torch::jit::Module with my custom module class that extends torch::nn::Module.</p>
</li>
</ol>
<p><strong>Questions:</strong></p>
<ol>
<li>
<p>How do I correctly load a TorchScript model in C++ and assign it to an instance of a class derived from torch::nn::Module?</p>
</li>
<li>
<p>Is there a straightforward way to use this loaded model for inference in C++?</p>
</li>
</ol>
<p>(this is how i impemented this in python)</p>
<pre><code>def load_model(self, model_path):
    """
    Loads the model weights from a given file path.

    Args:
        model_path (str): Path to the PyTorch model file.
    """
    if os.path.exists(model_path):
        self.model_state = torch.load(model_path,weights_only=True)
        print("Model loaded successfully from:", model_path)
        # Assuming 'self.model' is the attribute where the actual PyTorch model is stored
        # If your model architecture is defined within this class, apply the state like below
        # self.model.load_state_dict(self.model_state)
    else:
        raise FileNotFoundError("Computer's model does not exist at the specified path.")
</code></pre>
<p>Thank you!!</p>

Christopher_Manderfe: <p>Hi Jonatan,<br>
actually it is not possible to generate a torch::nn::Module from a torch::jit::script::Module what you get from torch::jit::load(). The better way here could be to use the script::Module as a member of your class.<br>
For inference you need to convert your tensor to a vector of IValue:<br>
torch::jit::script::Module my_module = torch::jit::load(…);<br>
torch::Tensor input_data = …<br>
std::vector<a>torch::jit::IValue</a> v_input;<br>
v_input.push_back(input_data);<br>
torch::Tensor output = my_model(v_input).to_tensor();</p>
<p>Best regards<br>
Chris</p>

jonatanbe8: <p>Thank you very much!!!<br>
It worked</p>