Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImportError: cannot import name 'nnUNetPredictor' #4

Closed
mathieuboudreau opened this issue Oct 25, 2023 · 10 comments
Closed

ImportError: cannot import name 'nnUNetPredictor' #4

mathieuboudreau opened this issue Oct 25, 2023 · 10 comments

Comments

@mathieuboudreau
Copy link
Member

OS: MacOS
venv tool: conda

I've followed all the installation instructions up to testing the segmentation command (installing both models), and when I run that command I get the following output (for both SEM and TEM images, and both UM and BF models):

(nnunet) mathieuboudreau@Mathieus-MacBook-Pro nn-axondeepseg % python nn_axondeepseg.py --seg-type UM --path-out . --path-dataset sem.png  
Traceback (most recent call last):
  File "nn_axondeepseg.py", line 25, in <module>
    from nnunetv2.inference.predict_from_raw_data import nnUNetPredictor
ImportError: cannot import name 'nnUNetPredictor' from 'nnunetv2.inference.predict_from_raw_data' (/Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages/nnunetv2/inference/predict_from_raw_data.py)

Is this missing package not in the requirements.txt file? Or (more likely in my opinion) because the requirements don't have any versions in requirements.txt, and the latest nnunet software does not have that feature (or is renamed)?

@mathieuboudreau
Copy link
Member Author

Here's the list of packages that were installed in my venv:

(nnunet) mathieuboudreau@Mathieus-MacBook-Pro nn-axondeepseg % pip freeze
acvl-utils==0.2
batchgenerators==0.25
certifi==2023.7.22
charset-normalizer==3.3.1
connected-components-3d==3.12.3
contourpy==1.1.1
cycler==0.12.1
dicom2nifti==2.4.8
dynamic-network-architectures==0.2
filelock==3.12.4
fonttools==4.43.1
fsspec==2023.10.0
future==0.18.3
graphviz==0.20.1
idna==3.4
imagecodecs==2023.3.16
imageio==2.31.6
importlib-resources==6.1.0
Jinja2==3.1.2
joblib==1.3.2
kiwisolver==1.4.5
lazy_loader==0.3
linecache2==1.0.0
loguru==0.7.2
MarkupSafe==2.1.3
matplotlib==3.7.3
MedPy==0.4.0
mpmath==1.3.0
networkx==3.1
nibabel==5.1.0
nnunetv2==2.0
numpy==1.24.4
opencv-python==4.8.1.78
packaging==23.2
pandas==2.0.3
Pillow==10.0.1
pydicom==2.4.3
pyparsing==3.1.1
python-dateutil==2.8.2
python-gdcm==3.0.22
pytz==2023.3.post1
PyWavelets==1.4.1
PyYAML==6.0.1
requests==2.31.0
scikit-image==0.21.0
scikit-learn==1.3.2
scipy==1.10.1
seaborn==0.13.0
SimpleITK==2.3.0
six==1.16.0
sympy==1.12
threadpoolctl==3.2.0
tifffile==2023.7.10
torch==2.1.0
tqdm==4.66.1
traceback2==1.4.0
typing_extensions==4.8.0
tzdata==2023.3
unittest2==1.1.0
urllib3==2.0.7
yacs==0.1.8
zipp==3.17.0
(nnunet) mathieuboudreau@Mathieus-MacBook-Pro nn-axondeepseg % 

@hermancollin
Copy link
Member

hermancollin commented Oct 26, 2023

Ahh, yes we need to pin the nnunet version. The problem is that nnUNetPredictor is a refactored version of the older nnunet inference function and it was only added very recently. I have version 2.2, but the minor was only released a month ago. Here is an issue from the ivadomed utilities repo mentionning this: ivadomed/utilities#38

The requirements.txt file should instead look like this:

nnunetv2>=2.2

@mathieuboudreau
Copy link
Member Author

mathieuboudreau commented Oct 26, 2023

Gotcha! I just tested and got this message:

(nnunet) mathieuboudreau@Mathieus-MacBook-Pro nn-axondeepseg % pip install -r requirements.txt

ERROR: Ignored the following versions that require a different python version: 2.1 Requires-Python >=3.9; 2.1.2 Requires-Python >=3.9; 2.2 Requires-Python >=3.9
ERROR: Could not find a version that satisfies the requirement nnunetv2>=2.2 (from versions: 2.0)
ERROR: No matching distribution found for nnunetv2>=2.2

Seems like I installed python with 3.8, but we'll also need to inform the users that they'll need a venv with python >=3.9 as well

That's why the unrestircted req file didn't install the latest version

@hermancollin
Copy link
Member

Oh well, I forgot about that. As per https://pip.pypa.io/en/stable/reference/requirements-file-format/, we can specify this in the requirements.txt file using something like this:

python_version >= "2.9"

@mathieuboudreau
Copy link
Member Author

Oh well, I forgot about that. As per https://pip.pypa.io/en/stable/reference/requirements-file-format/, we can specify this in the requirements.txt file using something like this:

python_version >= "2.9"

I didn't know that - neat!

@mathieuboudreau
Copy link
Member Author

Ahh, but if you do that, it'll just not install the package at all actually:

(nnunet) mathieuboudreau@Mathieus-MacBook-Pro nn-axondeepseg % pip install -r requirements.txt

Ignoring nnunetv2: markers 'python_version >= "3.9"' don't match your environment
Requirement already satisfied: torch in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from -r requirements.txt (line 2)) (2.1.0)
Requirement already satisfied: opencv-python in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from -r requirements.txt (line 3)) (4.8.1.78)
Requirement already satisfied: loguru in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from -r requirements.txt (line 4)) (0.7.2)
Requirement already satisfied: filelock in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from torch->-r requirements.txt (line 2)) (3.12.4)
Requirement already satisfied: typing-extensions in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from torch->-r requirements.txt (line 2)) (4.8.0)
Requirement already satisfied: sympy in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from torch->-r requirements.txt (line 2)) (1.12)
Requirement already satisfied: networkx in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from torch->-r requirements.txt (line 2)) (3.1)
Requirement already satisfied: jinja2 in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from torch->-r requirements.txt (line 2)) (3.1.2)
Requirement already satisfied: fsspec in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from torch->-r requirements.txt (line 2)) (2023.10.0)
Requirement already satisfied: numpy>=1.17.0 in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from opencv-python->-r requirements.txt (line 3)) (1.24.4)
Requirement already satisfied: MarkupSafe>=2.0 in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from jinja2->torch->-r requirements.txt (line 2)) (2.1.3)
Requirement already satisfied: mpmath>=0.19 in /Users/mathieuboudreau/opt/anaconda3/envs/nnunet/lib/python3.8/site-packages (from sympy->torch->-r requirements.txt (line 2)) (1.3.0)
(nnunet) mathieuboudreau@Mathieus-MacBook-Pro nn-axondeepseg % 

So we should stick with only setting nnunetv2>=2.2 then

@hermancollin
Copy link
Member

Ah damn. I'll add a note in the README that the user needs python >=3.9 then.

@mathieuboudreau
Copy link
Member Author

Besides, that, I installed it with the new nnunetv2 condition and this error doesn't occur anymore, but now I'm encountering #5

So I'll make a PR to close this one and we can continue discussing in the next issue

@mathieuboudreau
Copy link
Member Author

nm - I see you have https://github.com/axondeepseg/nn-axondeepseg/tree/ac/pin_nnunetv2 this branch already, so I'll let you do it!

@mathieuboudreau
Copy link
Member Author

Closed with #6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants