ANTsX / ANTsPy

A fast medical imaging analysis library in Python with algorithms for registration, segmentation, and more.
https://antspyx.readthedocs.io
Apache License 2.0
597 stars 160 forks source link

Potential memory leak #117

Closed jankrepl closed 4 years ago

jankrepl commented 4 years ago

Describe the bug When computing image similarities the memory is not cleared after computation.

To Reproduce Run the below minimal example and have htop running at the same time.

import ants
import numpy as np

img = np.random.random((400, 400))
img_ants = ants.image_clone(ants.from_numpy(img), pixeltype='float')

for i in range(50000000000):
    ants.image_similarity(img_ants, img_ants, metric_type='Correlation')

Expected behavior After each computation of the similarity metric the memory should be cleared. However, the higher the iteration the more memory is taken (eventually leading to out of memory errors).

Desktop

stnava commented 4 years ago

ok - I verified this. also noting:

anyway - thanks for this - would appreciate any help with further investigation.

stnava commented 4 years ago

Fixed.

hassaanmahmood commented 3 years ago

I am running code to create atlas, I got full disk error. Is there any solution to this?

 `         for i in range(10):
       Nlin_reg_avg=0

for affines in SUBJECTS[:200]:

    moving_img = ants.image_read( affines )  
    mytx = ants.registration(fixed=fixed_affine_atlas , moving=moving_img, type_of_transform='SyNOnly', grad_step=0.25 ,      reg_iterations=(30,50,20 ),flow_sigma=9,total_sigma=0.2)
    warped_moving = mytx['warpedmovout']
    Nlin_reg_avg=Nlin_reg_avg+(warped_moving.numpy())

Nlin_reg_avg_mean=Nlin_reg_avg/200
Nlin_reg_avg_mean_norm=Nlin_reg_avg_mean/np.max(Nlin_reg_avg_mean)
img = nib.Nifti1Image(Nlin_reg_avg_mean_norm, ref.affine)
nib.save(img, '/Nlin_reg_avg_mean_norm_'+str(i)+'.nii' )
fixed_affine_atlas = ants.from_numpy(Nlin_reg_avg_mean_norm, spacing=fixed_norm_atlas.spacing,
                              origin=fixed_norm_atlas.origin, direction=fixed_norm_atlas.direction)

` My hard disk got full, on investigation I find /tmp folder of 200G with files like these as below: e.g /tmp/tmp1y8z1uo80GenericAffine.mat /tmp/tmp_s4mi6ya1InverseWarp.nii.gz /tmp/tmpeyew06150GenericAffine.mat /tmp/tmpr2o5pl5q0GenericAffine.mat /tmp/tmpuzkont371InverseWarp.nii.gz /tmp/tmp0p486fke1InverseWarp.nii.gz /tmp/tmpwegb539c0GenericAffine.mat /tmp/tmpdctyw2ph1Warp.nii.gz /tmp/tmp7pzvk9g01InverseWarp.nii.gz /tmp/tmpu49tvvao1Warp.nii.gz /tmp/tmphs2lqlh_0GenericAffine.mat /tmp/tmpsu8n32ml1Warp.nii.gz /tmp/tmpzna7lwiy0GenericAffine.mat /tmp/tmptkfaptjx0GenericAffine.mat /tmp/tmphsql8njm1InverseWarp.nii.gz /tmp/tmpxxp5jxid1Warp.nii.gz /tmp/tmp423nwa4l1InverseWarp.nii.gz /tmp/tmp7c30zdvb1InverseWarp.nii.gz /tmp/.Test-unix /tmp/tmp5iyh4mce0GenericAffine.mat /tmp/.font-unix /tmp/tmp7zr5nmc91Warp.nii.gz /tmp/tmpscurinxj0GenericAffine.mat /tmp/tmp0p486fke1Warp.nii.gz /tmp/tmpl22reg7q1InverseWarp.nii.gz /tmp/tmp6olffh461InverseWarp.nii.gz /tmp/tmpqip8xo3b1InverseWarp.nii.gz /tmp/tmppcgfw04u.py /tmp/tmp2303poln0GenericAffine.mat

ntustison commented 3 years ago

Use the outprefix option in ants.registration to save to a known directory and delete the files when no longer needed.

Jose-Verdu-Diaz commented 3 weeks ago

Fixed.

@stnava I'm having a similar issue when performing image registration on batches of images. Memory usage builds up until running out. Could you expand on how this was fixed, or what was the actual problem?