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.

and here is my test code for evaluate my model.

微信截图_20200413013018829×579 12.5 KB

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.

微信截图_20200413013654629×510 163 KB

I check every stage output in libtorch and pytorch. I find that in pytorch, after ‘predict = net(test_data)’, the tensor is:

微信截图_20200413013931579×597 15.7 KB

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:

微信截图_20200413014426581×564 76.7 KB

so i go back to check my image input, i also find in pytorch, data input is:

there is another value such as 255.0/128.0 in tensor. But in C++, there are manys of huge values:

image805×593 184 KB

therefore i get a full black image output in C++ while a nice result in python.

I wonder how to do segmentation in libtorch with my case. Thanks!