I build a U-Net for segmenting medical images in pytorch 1.3. There’s no problems in training and testing stages when using pytorch. Here is my codes for converting ‘model.pth’ to ‘model.pt’, and ‘model.pt’ will apply in libtorch for testing with C++ later.
everything goes fine in pytorch, and i can get the correct result in python code.
However, when i code in C++ with using libtorch, i can’t get the same result in python code. Here is my C++ code for testing.
But in C++, there’re werid values appear after ‘auto result = module.forward(inputs).toTensor();’, these values change into NaN, here is the result output:
I had similar issues before. Turns out I was passing CPU data into loaded model’s forward function. Make sure your inputs are in CUDA.
Edit: Nevermind I just realized you also defined your network in CPU. However most of my nan output cases occured because I gave the wrong input type. It would be good to check the data type and which device it is in both Python and C++.