The same model works differently in Python and libtorch(C++),I don‘t know why)

Hello, the classifier trained by Resnet50 works well in python. The same test data has poor classification accuracy in C++(libtorch). I don’t know why and this is my main code.
//read image
srcImage = cv::imread(strFile, cv::ImreadModes::IMREAD_COLOR);
cv::cvtColor(srcImage, traImage, cv::COLOR_BGR2RGB);
cv::resize(traImage, resImage, cv::Size(224, 224),0, 0, CV_INTER_LINEAR);
resImage.convertTo(detImage, CV_32F, 1.0 / 255, 0);

   // Image conversion 
    std::vector<int64_t> sizes = {1, detImage.rows, detImage.cols, 3};
    at::Tensor tensorImage = torch::from_blob(detImage.data, at::IntList(sizes), at::kByte);
    tensorImage = tensorImage.to(at::kFloat);
    tensorImage = tensorImage.permute({0, 3, 1, 2});
    tensorImage = tensorImage.to(torch::kCUDA);
    std::vector<torch::jit::IValue> inputs;
    inputs.emplace_back(tensorImage);
    
    //calculate
    at::Tensor result = ptModule->forward(inputs).toTensor();
    auto max_result = result.max(1,true);

The Imagenet-trained models expect inputs that are standardized (to mean=0, std=1 per color over all of imagenet training).
You didn’t show the Python inference code, but that would look like it is missing in the C++ code.
(Generally, I’d recommend to check you’re using the same inputs when you wonder why the outputs are different.)

Best regards

Thomas

Thank you. I’ll check the input data is same or not.