Error loading the model torchscript

username: triilman25
cooked: <p>I don’t know what happen with <code>torch::jit::script::Module</code> I can’t load my torchscript model.</p>
<p>libtorch version that I used : 2.5.10 and 2.3.1<br>
cmake : +2.30<br>
IDE : clion</p>
<pre data-code-wrap="cpp"><code class="lang-cpp">#include &lt;iostream&gt;
#include "raylib.h"
#include "torch/torch.h"
#include "torch/script.h"
#include "c10/core/DynamicCast.h"
#include &lt;opencv2/opencv.hpp&gt;
#include &lt;memory&gt;
#include &lt;utility&gt;
// #include &lt;assert.h&gt;

//PREPROCESSOR
#define SUPPER_COMMENT 1
// TIP To &lt;b&gt;Run&lt;/b&gt; code, press &lt;shortcut actionId="Run"/&gt; or
// click the &lt;icon src="AllIcons.Actions.Execute"/&gt; icon in the gutter.
namespace trm { //trm is "transform"
    class ToTensor {
    public:
        ToTensor(int img_width,int img_height):
        width_(img_width),
        height_(img_height)
        {}
        at::Tensor operator()(const cv::Mat&amp; img) const {

            //resize gambar
            cv::Mat resized_img;
            cv::resize(img,resized_img, cv::Size(width_, height_));

            //konversi dari BGR ke RGB
            cv::Mat rgb_img;
            cv::cvtColor(resized_img, rgb_img, cv::COLOR_BGR2RGB);

            //konversi ke tensor dengan range [0,1]
            auto tensor_img = torch::from_blob(
                rgb_img.data,
                {rgb_img.rows, rgb_img.cols, 3},
                torch::kByte
                ).permute({2,0,1}).to(torch::kFloat32).div(255.0);

            tensor_img = tensor_img.unsqueeze(0);
            std::cout &lt;&lt; "tensor shape: " &lt;&lt; tensor_img.sizes() &lt;&lt; std::endl;
            return tensor_img;
        }

        static std::vector&lt;torch::jit::IValue&gt; toInput(torch::Tensor tensor_image) {
            // Create a vector of inputs.
            return std::vector&lt;torch::jit::IValue&gt;{std::move(tensor_image)};
        }
    private:
        int width_;
        int height_;
    };

//Z-Transform bellow
    class Normalize {
    public:
        Normalize(const std::vector&lt;float&gt; &amp;mean, const std::vector&lt;float&gt; &amp;std)
        : mean_(at::tensor(mean).view({3,1,1})),
        std_(at::tensor(std).view({3,1,1}))
        {

        }

        torch::Tensor operator()(const at::Tensor&amp; img_tensor) const {
            return (img_tensor - mean_) / std_;
        }

    private:
        at::Tensor mean_;
        at::Tensor std_;
    };
}

#if SUPPER_COMMENT
std::string get_image_type(const cv::Mat&amp; img, bool more_info=true)
{
    std::string r;
    int type = img.type();
    uchar depth = type &amp; CV_MAT_DEPTH_MASK;
    uchar chans = 1 + (type &gt;&gt; CV_CN_SHIFT);

    switch (depth) {
        case CV_8U:  r = "8U"; break;
        case CV_8S:  r = "8S"; break;
        case CV_16U: r = "16U"; break;
        case CV_16S: r = "16S"; break;
        case CV_32S: r = "32S"; break;
        case CV_32F: r = "32F"; break;
        case CV_64F: r = "64F"; break;
        default:     r = "User"; break;
    }

    r += "C";
    r += (chans + '0');

    if (more_info)
        std::cout &lt;&lt; "depth: " &lt;&lt; img.depth() &lt;&lt; " channels: " &lt;&lt; img.channels() &lt;&lt; std::endl;

    return r;
}

void show_image(cv::Mat&amp; img, std::string title)
{
    std::string image_type = get_image_type(img);
    cv::namedWindow(title + " type:" + image_type, cv::WINDOW_NORMAL); // Create a window for display.
    cv::imshow(title, img);
    cv::waitKey(0);
}

#endif

int main(int argc, char *argv[]) {
// argc starts from 1..n, argv's index starts from 0..n
    // if (argc &lt;= 2) {
    //     std::cerr &lt;&lt; "Usage: " &lt;&lt; argv[0] &lt;&lt; " &lt;path-to-exported-script-module&gt;\n";
    //     return -1;
    // }

    // Z-transform mean and standard deviation value
    const std::vector&lt;float&gt; mean = {0.485, 0.456, 0.406};
    const std::vector&lt;float&gt; std = {0.229, 0.224, 0.225};

    //get image
    std::string msg = "sample image";
    std::string img_path = R"(D:\project-inf\derm-iterface\images\tincorp12.jpg)";
    cv::Mat image = cv::imread(img_path);
#if SUPPER_COMMENT
    show_image(image, msg);
#endif
    if (image.empty()) {
        std::cerr &lt;&lt; "Failed to load image: " &lt;&lt; argv[2] &lt;&lt; "\n";
    }

    //ToTensor--&gt;unsqueeze --&gt; Z-Transform --&gt; toInput
    trm::ToTensor to_tensor(224,224);
    at::Tensor tensor_img = to_tensor(image);
    std::cout &lt;&lt; tensor_img.sizes() &lt;&lt; std::endl;

    // // Z-transform
    trm::Normalize norm(mean, std);
    at::Tensor normalized_tensor_img = norm(tensor_img);

    // // toInput
    std::vector&lt;torch::jit::IValue&gt; inputs = trm::ToTensor::toInput(normalized_tensor_img);
    // std::cout &lt;&lt;  &lt;&lt; std::endl;
//get model

try {
        // Deserialize the ScriptModule from a file using torch::jit::load().
    std::string model_name = R"(D:\project-inf\derm-iterface\models\nonmixup-modelTineaNCPU-75acc-19122024_09-53Hm.pt)";
    torch::jit::script::Module moduleV2 = torch::jit::load(model_name);
    std::cout &lt;&lt; "module can load " &lt;&lt; std::endl;
        //logit
    const at::Tensor output = moduleV2.forward(inputs).toTensor();

    std::cout &lt;&lt; "output shape: " &lt;&lt; output.sizes() &lt;&lt; std::endl;
    std::cout &lt;&lt; "output: " &lt;&lt; output[0] &lt;&lt; std::endl;
    std::cout &lt;&lt; output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) &lt;&lt; '\n';
}
catch (const c10::Error&amp; e) {
    std::cerr &lt;&lt; "error loading the model\n"&lt;&lt;e.msg();
    return -1;
}

std::cout &lt;&lt; "ok\n";
std::system("pause");
//testing part
    // auto tensor = torch::tensor(mean);
    // auto reshaped_tensor = tensor.view({3,1,1});
    // std::cout &lt;&lt; reshaped_tensor &lt;&lt; "\n";

return EXIT_SUCCESS;
}

// TIP See CLion help at &lt;a
// href="https://www.jetbrains.com/help/clion/"&gt;jetbrains.com/help/clion/&lt;/a&gt;.
//  Also, you can try interactive lessons for CLion by selecting
//  'Help | Learn IDE Features' from the main menu.
</code></pre>
<p>does error correlated with this warning<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/a/ca52992e7b710729c0553ae89817754ed96768d4.png" data-download-href="https://discuss.pytorch.org/uploads/default/ca52992e7b710729c0553ae89817754ed96768d4" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/c/a/ca52992e7b710729c0553ae89817754ed96768d4.png" alt="image" data-base62-sha1="sRPpK4NL18pW6hn5yKBCOMksLpG" width="690" height="185" data-dominant-color="F0DBD9"><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">image</span><span class="informations">872×235 13.2 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>