Python 为什么我的Google Colab会话内存不足?

Python 为什么我的Google Colab会话内存不足?,python,opencv,out-of-memory,google-colaboratory,Python,Opencv,Out Of Memory,Google Colaboratory,我在一个文件夹中有几个图像,我正在尝试将每个图像转换为灰度并保存到另一个文件夹中 由于内存不足,GoogleColab会话不断崩溃,我尝试在每个变量上使用del 这是我的密码 img_array = [] for filename in FileArray: img = cv2.imread('train/train/Img-'+filename) height, width, layers = img.shape size = (width, height)

我在一个文件夹中有几个图像,我正在尝试将每个图像转换为灰度并保存到另一个文件夹中

由于内存不足,GoogleColab会话不断崩溃,我尝试在每个变量上使用del

这是我的密码

img_array = []

for filename in FileArray:
    img = cv2.imread('train/train/Img-'+filename)
    height, width, layers = img.shape
    size = (width, height)
    img_array.append(img)

    image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

    cv2.imwrite('train/gray/Img-'+filename, image)
    del img
    del height
    del width
    del layers
    del size
    del image

即使您正在删除
img
,图像仍保存在
img\u数组
列表中的内存中。如果您在
文件数组中有很多图像,您可以通过将它们都保存在内存中来快速地检查RAM

尝试删除该行:

img_array.append(img)

你的问题是什么?你做过任何分析吗?我的假设是他们需要将所有图像添加到
img\u数组中
,因为它们被用于其他地方,我很好奇是否真的是这样。@AMC是的,这是一个深入学习的项目,这个img_阵列将馈送给DCGAN@James谢谢你为我修复了它,我会尝试以不同的方式存储它们,以避免这个问题。没问题。请记住通过单击答案旁边的小复选标记将问题标记为已回答。您好,我面临着同样的问题,在将列表转换为Np数组后,我在Colab pro上阅读了4352张
(512*512*3)
。你是怎么解决的?删除附加对我来说不起作用,因为我需要为培训存储它们。