BBillot/SynthSeg

InvalidArgumentError: 4D Input Expected with SynthSeg_predict.py on MacOS M2

Closed this issue · 7 comments

I encountered an InvalidArgumentError while running SynthSeg_predict.py for segmenting an MRI volume on a Mac with the M2 chip. The NifTi file has the properties: Image Dimensions (X, Y, Z): 768 x 768 x 149. The error suggests an issue with the input being "4-dimensional [1,256,256,160,24]".

For installation, I followed the SynthSeg guide for Conda, Python 3.8 but opted for a custom TensorFlow installation (tensorflow-macos and tensorflow-metal), using pip install tensorflow-macos and pip install tensorflow-metal, to ensure ARM compatibility with the M2 chip. My TensorFlow version is 2.13.0.

Ran SynthSeg_predict.py with the command:
python ./scripts/commands/SynthSeg_predict.py --i /path/to/input.nii.gz --o /path/to/output.nii.gz --robust

Encountered the following error:

InvalidArgumentError: input must be 4-dimensional[1,256,256,160,24]

Could the custom TensorFlow installations (tensorflow-macos and tensorflow-metal) be causing compatibility issues with SynthSeg? Are there specific versions or configurations of TensorFlow and Keras that are recommended for MacOS M2 chip compatibility? I can provide more logs if needed.

Thanks!

Are you sure your image is not multi-channel ie: 768 x 768 x 149 x 24 ?
Also could you put the whole traceback of the error ?
Thanks

I have double-checked the image, and it is not multi-channel. The image dimensions are 768 x 768 x 149 with a single channel. Here are the details of the NIfTI file:

File Size: 38.4 Mb
Image Dimensions (X, Y, Z): 768 x 768 x 149
Series Length: 1
Voxel Dimensions (X, Y, Z): 0.299 x 0.299 x 1 Millimeters
Size of Series Point: 0.031 Seconds

Traceback of the error:

the following problem occurred with image path/to/input.nii.gz :
Traceback (most recent call last):
  File "path/to/SynthSeg/predict_synthseg.py", line 182, in predict
    post_patch_segmentation = net.predict(image)
  ...
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node 'unet2/unet_bn_down_0/FusedBatchNormV3' defined at (most recent call last):
    File "path/to/scripts/commands/SynthSeg_predict.py", line 113, in <module>
      predict(path_images=args['i'],
    ...
    File "path/to/keras/src/layers/normalization/batch_normalization.py", line 979, in _fused_batch_norm_inference
      return tf.compat.v1.nn.fused_batch_norm(
Node: 'unet2/unet_bn_down_0/FusedBatchNormV3'
2 root error(s) found.
  (0) INVALID_ARGUMENT:  input must be 4-dimensional[1,256,256,160,24]
     [[{{node unet2/unet_bn_down_0/FusedBatchNormV3}}]]
     [[unet2/unet2_conv_uparm_5_0/BiasAdd/_400]]
  (1) INVALID_ARGUMENT:  input must be 4-dimensional[1,256,256,160,24]
     [[{{node unet2/unet_bn_down_0/FusedBatchNormV3}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_predict_function_6169]

resuming program execution

segmentation saved in: path/to/output.nii.gz

Okay, so based on the error, your input is indeed uni-modal, no problem with that.
The error happens for the last network of the "robust" pipeline, which is very weird...
Do you have the same problem if you don't use the "--robust" flag in your command line ?

It's just weird that what causes the problem is a simple batch normalisation layer... So maybe you're right, it could be an incompatibility issue between your tensorflow and keras, especially since you installed tensorflow with pip and keras with conda... Doesn't TF-metal and tf-macos exist in conda ? Or maybe you could try to downgrade your tf 2.13 to something closer to tf 2.2 ?

Hi again,

After lots of testing and troubleshooting, I finally got it working! Turns out that the issue was related to Tensorflow-metal and GPU usage. I managed to run SynthSeg just fine on different versions of Tensorflow-macos, including the oldest one I could get working (2.7.0) and the latest (2.13.0), but only by explicitly running it on the CPU. Whenever I attempted to run it on the GPU with Tensorflow-metal, it produced the same error I previously mentioned, regardless of whether or not I used the --robust flag. Thought this might be worth mentioning since it seems like the GPU support with Tensorflow-metal is the tricky part. If you need more details on this, just let me know.

Cheers!

I'm glad you got it working!
And thanks a lot for the details, I'll be sure to reference this in case it happens to other users. :)

Hello Noah,

May I get to know which version of keras did you use?

Hi,

I currently use tensorflow-macos == 2.13.0 and keras == 2.13.1