ZeroDivisionError: float division by zero during net_segment inference patch aggregation

168 views Asked by At

I ran (on Ubuntu 16.04 in a Google Cloud VM Instance):

net_segment inference -c <path-to-config>

for a binary segmentation problem using unet_2d with softmax and a (96,96,1) spatial window. This was after I trained my model for 10 epochs and saved the checkpoint. I'm not sure why it's drawing a zero division error from windows_aggregator_resize.py. What is the cause of this issue and what can I do to fix it?

Here are some inference settings and the corresponding error:

pixdim: (1.0, 1.0, 1.0)

[NETWORK]
batch_size: 1
cutoff: (0.01, 0.99)
name: unet_2d
normalisation: False
volume_padding_size: (96, 96, 0)
reg_type: L2
window_sampling: resize
multimod_foreground_type: and

[INFERENCE]
border = (96,96,0)
inference_iter = -1
output_interp_order = 0
spatial_window_size = (96,96,2)

INFO:niftynet: Accessing /home/xchaosfailx1/niftynet/models/MSD/heart_la_seg/models/model.ckpt-10 ...
INFO:niftynet: Restoring parameters from /home/xchaosfailx1/niftynet/models/MSD/heart_la_seg/models/model.ckpt-10
INFO:niftynet: Cleaning up...
WARNING:niftynet: stopped early, incomplete loops
INFO:niftynet: stopping sampling threads
INFO:niftynet: SegmentationApplication stopped (time in second 17.07).
Traceback (most recent call last):
  File "/home/xchaosfailx1/.local/bin/net_segment", line 11, in <module>
    sys.exit(main())
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/__init__.py", line 139, in main
    app_driver.run_application()
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/engine/application_driver.py", line 275, in run_application
    self._inference_loop(session, loop_status)
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/engine/application_driver.py", line 493, in _inference_loop
    self._loop(iter_generator(itertools.count(), INFER), sess, loop_status)
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/engine/application_driver.py", line 442, in _loop
    iter_msg.current_iter_output[NETWORK_OUTPUT])
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/application/segmentation_application.py", line 390, in interpret_output
    batch_output['window'], batch_output['location'])
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/engine/windows_aggregator_resize.py", line 55, in decode_batch
    self._save_current_image(window[batch_id, ...], resize_to_shape)
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/engine/windows_aggregator_resize.py", line 82, in _save_current_image
    [float(p) / float(d) for p, d in zip(window_shape, image_shape)]
  File "/home/xchaosfailx1/.local/lib/python3.5/site-packages/niftynet/engine/windows_aggregator_resize.py", line 82, in <listcomp>
    [float(p) / float(d) for p, d in zip(window_shape, image_shape)]
ZeroDivisionError: float division by zero

For reproducing the error:

2

There are 2 answers

2
Wenqi Li On

Didn't check the inference data but I think spatial_window_size in [INFERENCE] should be 96, 96, 1 as that's what you set in training.

0
jxc On

The mistake that I made was that I set the border (96,96,0) under [Inference] to the same shape as my spatial window (96,96,1), so when the batch was cropped in decode_batch, the cropped image had an image shape with 0s in it. Hence, when the zoom ratio was calculated in _save_current_image, it led to a ZeroDivsionError. The temporary fix was to remove volume padding and changing the border=(0,0,0).