ANTsX / ANTsPyNet

Pre-trained models and utilities for deep learning on medical images in Python
https://antspynet.readthedocs.io
Apache License 2.0
201 stars 29 forks source link

errors in some models #98

Closed znee closed 7 months ago

znee commented 8 months ago

Hi, I am using antspy and now antspynet. It is very useful and many thanks for the work.

Meanwhile, I tested a brainage model on my MacBook, but it gives me an error like this.

----> 1 age = antspynet.brain_age(t1, number_of_simulations=3, sd_affine=0.01, verbose=True) 2 print(age)

File /opt/homebrew/Caskroom/miniconda/base/envs/pyjinhee/lib/python3.11/site-packages/antspynet/utilities/brain_age.py:101, in brain_age(t1, do_preprocessing, number_of_simulations, sd_affine, antsxnet_cache_directory, verbose) 94 ################################ 95 # 96 # Load model and weights 97 # 98 ################################ 100 model_weights_file_name = get_pretrained_network("brainAgeDeepBrainNet", antsxnet_cache_directory=antsxnet_cache_directory) --> 101 model = keras.models.load_model(model_weights_file_name) 103 # The paper only specifies that 80 slices are used for prediction. I just picked 104 # a reasonable range spanning the center of the brain 106 which_slices = list(range(45, 125))

File /opt/homebrew/Caskroom/miniconda/base/envs/pyjinhee/lib/python3.11/site-packages/keras/src/saving/saving_api.py:183, in load_model(filepath, custom_objects, compile, safe_mode) 176 return saving_lib.load_model( 177 filepath, 178 custom_objects=custom_objects, 179 compile=compile, 180 safe_mode=safe_mode, 181 ) ... 361 ) 363 for k, name in enumerate(layer_names): 364 g = f[name]

ValueError: Layer count mismatch when loading weights from file. Model expected 1 layers, found 3 saved layers.

I searched google and it might be an issue related to the tensorflow/keras version, but I am not sure of it.

I followed some examples, and found mostly works well, but some give me errors like this or similar.

Thank you again!

ntustison commented 8 months ago

Are you running this example?

znee commented 8 months ago

yes I am. I ran several examples on that page.

ntustison commented 8 months ago

I'm able to run it without issue (macbook pro, python 3.11, tf 2.11). Try clearing out your current antsxnet data (rm ~/.keras/ANTsXNet/*) and running it again.

znee commented 8 months ago

I tried it, but it gives me the same error. Meanwhile, I checked the version of packages.

antspynet 0.2.3 antspyx 0.4.2 keras 3.0.5 tensorflow 2.16.1 tf-keras 2.16.0

Currently, brain extraction, hippocampus segmentation works without error. Results are good.

ntustison commented 8 months ago

First thing to try is downgrade tensorflow. We don't track with the latest version because we've had problems before.

znee commented 8 months ago

Okay, I created conda env of python 3.9, because of tf 2.11, and tf 2.11 was installed. but In this case, import antspynet gives me error.

----> 2 import antspynet

File /opt/homebrew/Caskroom/miniconda/base/envs/pyjinhee2/lib/python3.9/site-packages/antspynet/init.py:4 2 version='0.2.3' ----> 4 from .architectures import 5 from .utilities import

File /opt/homebrew/Caskroom/miniconda/base/envs/pyjinhee2/lib/python3.9/site-packages/antspynet/architectures/init.py:8 6 from .create_densenet_model import create_densenet_model_2d, create_densenet_model_3d 7 from .create_resnet_model import create_resnet_model_2d, create_resnet_model_3d ----> 8 from .create_simple_classification_with_spatial_transformer_network_model import create_simple_classification_with_spatial_transformer_network_model_2d, create_simple_classification_with_spatial_transformer_network_model_3d 10 from .create_deep_back_projection_network_model import create_deep_back_projection_network_model_2d, create_deep_back_projection_network_model_3d 11 from .create_deep_denoise_super_resolution_model import create_deep_denoise_super_resolution_model_2d, create_deep_denoise_super_resolution_model_3d

File /opt/homebrew/Caskroom/miniconda/base/envs/pyjinhee2/lib/python3.9/site-packages/antspynet/architectures/create_simple_classification_with_spatial_transformer_network_model.py:7 2 from tensorflow.keras.models import Model 3 from tensorflow.keras.layers import (Input, Dense, Activation, Flatten, 4 Conv2D, MaxPooling2D, 5 Conv3D, MaxPooling3D) ... 67 tf.config.experimental.tensor_float_32_execution_enabled()): 68 # Must import here, because symbols get pruned to all. 69 import warnings

ImportError: This version of TensorFlow Probability requires TensorFlow version >= 2.16; Detected an installation of version 2.11.0. Please upgrade TensorFlow to proceed.

Before this, I create python 3.11 env but I failed to install tf2.11 using pip or conda install.

ncullen93 commented 8 months ago

I had a similar import issue and it’s because of Keras 3. You need to have Keras 2.

znee commented 8 months ago

Great to hear it. Can you specify the version of Keras and tf ?

ncullen93 commented 8 months ago

Anything before keras 3 should work. You can try this requirements file:

https://github.com/cookpa/ANTsXContainers/blob/master/docker/ANTsPyNet/requirements.txt

znee commented 8 months ago

Thank you, @ncullen93 @ntustison .

I created a conda env as follows, and it works fine for BrainAge and others. (MacOS 14.4)

conda create -n antsnet python=3.11 conda activate antsnet

pip install tf-keras==2.15.1 pip install tensorflow-probability==0.22.0

pip install antspyx pip install antspynet

cookpa commented 7 months ago

Hopefully #104 fixes this by requiring keras 2.X. I also have tensorflow 2.15.1 working on Mac, but I'm not sure it works on all platforms so I set the max to 2.13 for now.