Torch import time

Hi,
In a new virtual environment, I installed itk with pypi and I tested the import time:

ITK is fast to import. But now, I installed torch with pypi:

The import time is huge now, due to torch. I do not need torch type for itk Image. I do not see anything in the code https://github.com/thewtex/ITK/blob/master/Wrapping/Generators/Python/itk/support/types.py#L34-L40 to avoid the import torch. Do you know a workaround please?

Tested with macOs, itk==5.3.0 and torch==2.1.1

Hi @tbaudier ,

Welcome to the ITK community ! :sun_with_face:

With ITK 5.4, we are support Python 3.8+ → we could transition the package detection to something like:

from importlib.metadata import metadata

[...]
try:
    metadata('torch')
    _HAVE_TORCH = True
except importlib.metadata.PackageNotFoundError:
    pass

if you have a chance to test, a patch would be appreciated.

Hi @matt.mccormick , thank you for your answer.

I did the PR here:

It works well because:
without torch in the environment (with the python itk wrapping now)

with torch in the environment (with the python itk wrapping now)

And with the modification:

2 Likes