0

我正在使用以下代码在 Python 中以 NiFTI 格式加载我的文件。

import nibabel as nib 

img_arr = []
for i in range(len(datadir)):
    img = nib.load(datadir[i])
    img_data = img.get_fdata()
    img_arr.append(img_data)
    img.uncache()

少量图像工作正常,但如果我想加载更多图像,我会收到以下错误:

OSError                                   Traceback (most recent call last)
<ipython-input-55-f982811019c9> in <module>()
     10     #img = nilearn.image.smooth_img(datadir[i],fwhm = 3) #Smoothing filter for preprocessing (necessary?)
     11     img = nib.load(datadir[i])
---> 12     img_data = img.get_fdata()
     13     img_arr.append(img_data)
     14     img.uncache()

~\AppData\Roaming\Python\Python36\site-packages\nibabel\dataobj_images.py in get_fdata(self, caching, dtype)
    346             if self._fdata_cache.dtype.type == dtype.type:
    347                 return self._fdata_cache
--> 348         data = np.asanyarray(self._dataobj).astype(dtype, copy=False)
    349         if caching == 'fill':
    350             self._fdata_cache = data

~\AppData\Roaming\Python\Python36\site-packages\numpy\core\_asarray.py in asanyarray(a, dtype, order)
    136 
    137     """
--> 138     return array(a, dtype, copy=False, order=order, subok=True)
    139 
    140 

~\AppData\Roaming\Python\Python36\site-packages\nibabel\arrayproxy.py in __array__(self)
    353     def __array__(self):
    354         # Read array and scale
--> 355         raw_data = self.get_unscaled()
    356         return apply_read_scaling(raw_data, self._slope, self._inter)
    357 

~\AppData\Roaming\Python\Python36\site-packages\nibabel\arrayproxy.py in get_unscaled(self)
    348                                        offset=self._offset,
    349                                        order=self.order,
--> 350                                        mmap=self._mmap)
    351         return raw_data
    352 

~\AppData\Roaming\Python\Python36\site-packages\nibabel\volumeutils.py in array_from_file(shape, in_dtype, infile, offset, order, mmap)
    507                              shape=shape,
    508                              order=order,
--> 509                              offset=offset)
    510             # The error raised by memmap, for different file types, has
    511             # changed in different incarnations of the numpy routine

~\AppData\Roaming\Python\Python36\site-packages\numpy\core\memmap.py in __new__(subtype, filename, dtype, mode, offset, shape, order)
    262             bytes -= start
    263             array_offset = offset - start
--> 264             mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start)
    265 
    266             self = ndarray.__new__(subtype, shape, dtype=descr, buffer=mm,

OSError: [WinError 8] Not enough storage is available to process this command

我认为 img.uncache() 会从内存中删除图像,因此它不会占用太多存储空间,但仍然可以使用图像数组。将这一位添加到代码中并没有改变任何东西。

有谁知道我能帮上忙吗?我正在使用的计算机有 24 核 2.6 GHz CPU,超过 52 GB 内存,工作目录有超过 1.7 TB 的可用存储空间。我正在尝试从 ADNI 数据库加载大约 1500 张 MRI 图像。

任何建议都非常感谢。

4

2 回答 2

0

此错误不是因为1.7TB 硬盘驱动器已满而引起的,而是因为您的内存不足,即RAM了解这两件事的不同之处将很重要

uncache()不会完全从内存中删除项目,如此处所述,但该链接还包含更多内存节省提示。

如果你想完全从内存中删除一个对象,你可以使用垃圾收集器接口,像这样:

import nibabel as nib 
import gc

img_arr = []
for i in range(len(datadir)):
    img = nib.load(datadir[i])
    img_data = img.get_fdata()
    img_arr.append(img_data)
    img.uncache()
    # Delete the img object and free the memory
    del img
    gc.collect()

这应该有助于减少您正在使用的内存量。

于 2019-12-19T15:46:40.083 回答
0

如何解决“没有足够的可用存储空间..”?

  • 尝试执行以下步骤:

    1. 同时按下键盘上的 Windows + R 键,然后在“运行”窗口中键入 Regedit.exe,然后单击“确定”。

    2. 然后展开 HKEY_LOCAL_MACHINE、SYSTEM、CurrentControlSet、services、LanmanServer、Parameters。

    3. 找到 IRPStackSize (如果找到跳到步骤 5),如果它不存在,则右键单击右侧 Window 并选择 New > Dword Value (32)

    4. 现在在名称下键入 IRPStackSize,然后按 Enter。

    5. 右键单击 IRPStackSize 并单击 Modify,然后设置任何高于 15 但低于 50 的值并单击 OK

    6. 重新启动系统并尝试重复发生错误时所做的相同操作。

  • 或者 :

    1. 将以下注册表项 HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management\LargeSystemCache 设置为值“1”

    2. 将以下注册表 HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\Size 设置为值“3”

在“nibabel”中节省内存的另一种方法:

除了 uncache() 方法之外,还有其他方法可以节省内存,您可以使用:

于 2019-12-19T16:07:34.280 回答