【问题标题】:Origin of the numpy and pickle new memory error?numpy 和 pickle 新内存错误的起源?
【发布时间】:2021-03-03 23:31:48
【问题描述】:

当我想创建用于深度学习的训练图像集时,我遇到了一个问题。图像是 299x299 灰度 .tif 文件。每个类别有近 3000 张图像。我曾经在大约 6.5 Go 的 pickle 文件中编译这些数据。然后我设法毫无问题地加载它。

但是,我最近想再次使用这个训练集,当我运行与以前完全相同的代码时,我遇到了这个内存错误:

import numpy as np
import os
import cv2
from tqdm import tqdm
import random
import pickle

DATADIR = "path/to/folder"
CATEGORIES = ["A", "B","C","D"]

training_data = []
IMG_SIZE_H = 299
IMG_SIZE_W = 299

def create_training_data():
    for category in CATEGORIES:
        
        path = os.path.join(DATADIR, category)
        class_num = CATEGORIES.index(category)
        

        list_dir = os.listdir(path)

        list_dir[:] = [item for item in list_dir if '.tif' in item]

        for img in tqdm(list_dir):
            try:
                img_array = cv2.imread(os.path.join(path, img), cv2.IMREAD_GRAYSCALE)
                new_array = cv2.resize(img_array, (IMG_SIZE_H, IMG_SIZE_W))
                training_data.append([new_array, class_num])
            except Exception as e:
                pass
            
create_training_data()

random.shuffle(training_data)

X = []
y = []

for features, label in training_data:
    X.append(features)
    y.append(label)
    
X = np.array(X).reshape(-1, IMG_SIZE_H, IMG_SIZE_W, 1)

X = X.reshape(X.shape[0], 299, 299, 1).astype('float32')

pickle_out = open("X_train.pickle", "wb")
pickle.dump(X, pickle_out, protocol=4)
pickle_out.close()

pickle_out = open("y_train.pickle", "wb")
pickle.dump(y, pickle_out)
pickle_out.close()

MemoryError Traceback(最近调用 最后)在 ----> 1 X = X.reshape(X.shape[0], 299, 299, 1).astype('float32')

MemoryError: Unable to allocate 6.06 GiB for an array with shape (18204, 299, 299, 1) 和数据类型 float32

此外,我保留了旧的 pickle 文件(大小为 6.5 Go),当我尝试将它加载到与以前完全相同的 CNN 中时,我也遇到了内存错误:

X_train = pickle.load(open('X_train.pickle', 'rb'))

MemoryError Traceback(最近调用 最后)在 ----> 1 X_train = pickle.load(open('X_train.pickle', 'rb'))

这很奇怪,因为我没有更改任何东西,相同的计算机,相同的 python 版本(3.7),相同的图像,相同的代码,相同的 pickle 和 numpy 版本(仅更新了 tensorflow,但我没有看到任何链接) .

我感觉我的电脑本身的内存有问题,但我之前的代码一直使用同一台电脑。

如果您有任何可以解释或解决此问题的线索,请告诉我!

【问题讨论】:

    标签: python numpy tensorflow memory pickle


    【解决方案1】:

    MemoryError 是内存不足异常。

    MemoryError: 无法为形状为 (18204, 299, 299, 1) 且数据类型为 float32 的数组分配 6.06 GiB

    你能检查一下你有>7 gb 的内存可用吗?

    【讨论】:

    • 嗨@JunkyByte!谢谢您的回答。正如您所建议的,ram 非常低(出于未知原因)。重新启动计算机后,代码运行正常(永远记住调试的基础知识^^)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多