[英]MemoryError: Unable to allocate 3.33 GiB for an array with shape (15500, 2, 240, 240, 1) and data type int16
To run the code I'm using PyCharm latest version on a Windows 7 64bit with 16Gb of RAM and...要运行代码,我在 Windows 7 64bit 上使用 PyCharm 最新版本,具有 16Gb 的 RAM 和...
Python version: 3.7.7 (default, May 6 2020, 11:45:54) [MSC v.1916 64 bit (AMD64)]
I'm trying to load a lot of NIFTI images using SimplyITK and Numpy from the BraTS 2019 dataset .我正在尝试使用来自BraTS 2019 数据集的 SimplyITK 和 Numpy 加载大量 NIFTI 图像。
This is the code I use to load the images into a numpy array.这是我用来将图像加载到 numpy 数组中的代码。
import SimpleITK as sitk
def read_nifti_images(images_full_path):
"""
Read nifti files from a gziped file.
Read nifti files from a gziped file using SimpleITK library.
Parameters:
images_full_path (string): Full path to gziped file including file name.
Returns:
SimpleITK.SimpleITK.Image, numpy array: images read as image, images read as numpy array
"""
# Reads images using SimpleITK.
images = sitk.ReadImage(images_full_path)
# Get a numpy array from a SimpleITK Image.
images_array = sitk.GetArrayFromImage(images)
# More info about SimpleITK images: http://simpleitk.github.io/SimpleITK-Notebooks/01_Image_Basics.html
return images, images_array
This code works fine with smallest dataset.此代码适用于最小的数据集。 I'm trying to load 518 nii.gz files with 155 images each file.
我正在尝试加载 518 个 nii.gz 文件,每个文件包含 155 个图像。
When I run the code, there are 4GiB of RAM used, and when it gets to 8GiB, it throws the exception.当我运行代码时,使用了 4GiB 的 RAM,当它达到 8GiB 时,它会抛出异常。
Is there any way to load all the images in memory?有没有办法加载 memory 中的所有图像? Maybe there is a memory usage limitation in Windows and/or in PyCharm.
在 Windows 和/或 PyCharm 中可能存在 memory 使用限制。
You have two copies of images in memory, a SimpleITK version and a numpy version.您在 memory 中有两个图像副本,一个 SimpleITK 版本和一个 numpy 版本。 So when you hit 8 gig of images, you've really got 16 gig in memory, hence your crash.
因此,当您点击 8 gig 的图像时,您在 memory 中确实有 16 gig,因此您的崩溃。
You can try using sitk.GetArrayViewFromImage.您可以尝试使用 sitk.GetArrayViewFromImage。 That does not make a whole new copy of the image when converting from SimpleITK to numpy.
当从 SimpleITK 转换为 numpy 时,这不会生成图像的全新副本。 It creates a numpy data structure that points to the same pixel buffer as the SimpleITK image.
它创建了一个 numpy 数据结构,该结构指向与 SimpleITK 图像相同的像素缓冲区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.