Can't import torch from python script

Hallo. I am having an issues when trying to run a python script from Labels. In particular, when I try to import torch I get a “ImportError: DLL load failed” error (this happens both in Windows and Ubuntu, on python 3.5, 3.6, 3.7). The other installed modules can be correctly loaded, the error only occurs with torch/torchvision. If I launch python from a terminal and import torch everything works without error, the error only occurs when I try to import it either from the ImFusion Suite or Labels. Is there a way to solve this issue?

Also, is it possible to import a pretrained pytorch model without launching the python script? I mean, something similar to “MachineLearning/Apply PixelWise Model”, where I can use torch models instead of caffe ones?

Dear Maria,

Since we ship our own Torch binaries with our software, there might be some conflicts with yours when importing torch.
Can you try to “import torch” before “import imfusion” in your Python script?

Regarding your second question: the algorithm ApplyPixelwiseModel already supports PyTorch models, you just have to specify it in the configuration file
For instance,

type	neuralnetwork
version 2
engine	torch
fully_convolutional	1

The model needs to be traced before being saved, for instance:

dummy_input = torch.rand(1, channels, slices, height, width)
traced_model = torch.jit.trace(self.model, dummy_input)…)

Hi Raphael,
thank you very much for your reply. As for your question, I have this issue only when working with the imFusion embedded python console. That is, if I launch my python script from the terminal (or from pyCharm) I don’t have any problem with torch and imfusion, regardless the order I use to import them.
But if I launch the embedded python console from within the ImFusion Suite and I try to import torch, there I get the error. The same thing happens when I try to launch a python script from Labels.

Thank you again for your help!

Hi Raphael,
It seems I found a hack to get it working…
The problem is related to the DLLs. If I substitute all the “torch related” DLLs in “C:\Program Files\ImFusion\ImFusion Suite\Suite” with the DLLs I find in my python torch package, I can use python with no problem. However, at that point I cannot use any of your plugins anymore. The same happens whenever I want to use the C++ torch library in a custom designed plugin. Unless I replace the DLLs, it won’t work.

I guess if we use the exact same torch version as you, then we should not encounter this problems and we could be able to use both your DL algorithms and out python scripts…

Thanks again,

Hi Maria,

Thanks for your debugging efforts!
Ok so this is what I expected, a conflicting version of DLLs.
I believe we use Torch 1.2 right now. Is downgrading your Python version of torch a possibility, or do you need a specific version?
By the way, we are in the process of upgrading our Torch plugin to 1.4 with CUDA 10.0 so this should be available soon.