Error in inference farward when loading and using a trained training model

username: nanamaru
cooked: <p>We plan to use Visual Studio 2019 to load a model that has already been trained in PyTorch with Script conversion into C++ with CUDA 11.3 and LibTorch 1.11 for class classification. Input image is a black and white image, input size is 224x224. Therefore, the tensor size is <code>{1,1,224,224}</code>.</p>
<p>Here is the current code, and as an example, we are currently inputting a sample tensor with no image input.</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;
#include &lt;torch/script.h&gt;
#include &lt;opencv2/opencv.hpp&gt;

int main() {
    // Load the model
    std::string model_path = "model_weights_c++.pt";
    torch::jit::script::Module module;
    try {
        module = torch::jit::load(model_path);
    }
    catch (const c10::Error&amp; e) {
        std::cerr &lt;&lt; "Error loading the model: " &lt;&lt; e.what() &lt;&lt; std::endl;
        return -1;
    }

    // Set the device to GPU if available
    torch::Device device(torch::kCPU);
    if (torch::cuda::is_available()) {
        device = torch::Device(torch::kCUDA); }
    }

    // Create a vector of inputs.
    std::vector&lt;torch::jit::IValue&gt; inputs;
    inputs.push_back(torch::ones({ 1, 1, 224, 224 }));
    module.to(device);

    // Execute the model and turn its output into a tensor.
    std::vector&lt;torch::jit::IValue&gt; outputs;
    at::Tensor output = module.forward(inputs).toTensor().to(device);
    std::cout &lt;&lt; output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) &lt;&lt; '\n';
    return 0;
}
</code></pre>
<p>The build succeeds, but while executing module.forward(), the following program stops the effect with an unhandled exception. [return stack.front()].<br>
What is the reason? Please help.</p>
<pre><code class="lang-auto">at::IValue operator()(
    Stack stack, kwargs&amp; kwargs&amp; kwargs
    const Kwargs&amp; kwargs = Kwargs()) {
    getSchema().checkAndNormalizeInputs(stack, kwargs);
    run(stack);
    return stack.front(); }                  &lt;-------------here

  }
</code></pre>
<p>The call history is shown in the following image.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/1/d1fbb964af2cd00fc8216d38b961bf6d2d9becbf.png" data-download-href="https://discuss.pytorch.org/uploads/default/d1fbb964af2cd00fc8216d38b961bf6d2d9becbf" title="pic"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1fbb964af2cd00fc8216d38b961bf6d2d9becbf_2_690x287.png" alt="pic" data-base62-sha1="tXB6GQ9matQkBGzxzPN6t6lGFj1" width="690" height="287" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1fbb964af2cd00fc8216d38b961bf6d2d9becbf_2_690x287.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1fbb964af2cd00fc8216d38b961bf6d2d9becbf_2_1035x430.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d1fbb964af2cd00fc8216d38b961bf6d2d9becbf_2_1380x574.png 2x" data-dominant-color="282D33"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pic</span><span class="informations">1960×816 89.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>