I don’t know what happen with torch::jit::script::Module
I can’t load my torchscript model.
libtorch version that I used : 2.5.10 and 2.3.1
cmake : +2.30
IDE : clion
#include <iostream>
#include "raylib.h"
#include "torch/torch.h"
#include "torch/script.h"
#include "c10/core/DynamicCast.h"
#include <opencv2/opencv.hpp>
#include <memory>
#include <utility>
// #include <assert.h>
//PREPROCESSOR
#define SUPPER_COMMENT 1
// TIP To <b>Run</b> code, press <shortcut actionId="Run"/> or
// click the <icon src="AllIcons.Actions.Execute"/> 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& 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 << "tensor shape: " << tensor_img.sizes() << std::endl;
return tensor_img;
}
static std::vector<torch::jit::IValue> toInput(torch::Tensor tensor_image) {
// Create a vector of inputs.
return std::vector<torch::jit::IValue>{std::move(tensor_image)};
}
private:
int width_;
int height_;
};
//Z-Transform bellow
class Normalize {
public:
Normalize(const std::vector<float> &mean, const std::vector<float> &std)
: mean_(at::tensor(mean).view({3,1,1})),
std_(at::tensor(std).view({3,1,1}))
{
}
torch::Tensor operator()(const at::Tensor& 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& img, bool more_info=true)
{
std::string r;
int type = img.type();
uchar depth = type & CV_MAT_DEPTH_MASK;
uchar chans = 1 + (type >> 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 << "depth: " << img.depth() << " channels: " << img.channels() << std::endl;
return r;
}
void show_image(cv::Mat& 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 <= 2) {
// std::cerr << "Usage: " << argv[0] << " <path-to-exported-script-module>\n";
// return -1;
// }
// Z-transform mean and standard deviation value
const std::vector<float> mean = {0.485, 0.456, 0.406};
const std::vector<float> 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 << "Failed to load image: " << argv[2] << "\n";
}
//ToTensor-->unsqueeze --> Z-Transform --> toInput
trm::ToTensor to_tensor(224,224);
at::Tensor tensor_img = to_tensor(image);
std::cout << tensor_img.sizes() << std::endl;
// // Z-transform
trm::Normalize norm(mean, std);
at::Tensor normalized_tensor_img = norm(tensor_img);
// // toInput
std::vector<torch::jit::IValue> inputs = trm::ToTensor::toInput(normalized_tensor_img);
// std::cout << << 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 << "module can load " << std::endl;
//logit
const at::Tensor output = moduleV2.forward(inputs).toTensor();
std::cout << "output shape: " << output.sizes() << std::endl;
std::cout << "output: " << output[0] << std::endl;
std::cout << output.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << '\n';
}
catch (const c10::Error& e) {
std::cerr << "error loading the model\n"<<e.msg();
return -1;
}
std::cout << "ok\n";
std::system("pause");
//testing part
// auto tensor = torch::tensor(mean);
// auto reshaped_tensor = tensor.view({3,1,1});
// std::cout << reshaped_tensor << "\n";
return EXIT_SUCCESS;
}
// TIP See CLion help at <a
// href="https://www.jetbrains.com/help/clion/">jetbrains.com/help/clion/</a>.
// Also, you can try interactive lessons for CLion by selecting
// 'Help | Learn IDE Features' from the main menu.
does error correlated with this warning