python - 代码重写 - MemoryError

标签 python csv numpy

我正在编写一个 python 脚本来读取两个 csv 文件。代码片段如下。如果文件包含少量记录(8,000 条),则代码可以完美运行,但是如果文件包含大量记录(120,000 条),我会在线上遇到 MemoryError (X_train = X_train.astype('float32'))。

img_lst_train = []
label_lst_train = []

img_lst_test = []
label_lst_test = []

print ('Reading training file')

with open('train.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        img = cv2.imread(row[0])
        img_lst_train.append(img) 
        label_lst_train.append(row[1])

print ('Reading testing file')

with open('val.csv') as csvfile:
    readCSV = csv.reader(csvfile, delimiter=',')
    for row in readCSV:
        img = cv2.imread(row[0])
        img_lst_test.append(img) 
        label_lst_test.append(row[1])



img_lst_train = np.array(img_lst_train)
label_lst_train = np.array(label_lst_train)
img_lst_test = np.array(img_lst_test)
label_lst_test = np.array(label_lst_test)


X_train = img_lst_train
y_train = label_lst_train
X_test  = img_lst_test
y_test  = label_lst_test

# Convert class vectors to binary class matrices.
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)


X_train = X_train.astype('float32')
X_test = X_test.astype('float32')

train.csv和val.csv的结构

path to image file, label
path to image file, label
path to image file, label
.........................

如何重写上面的代码以避免 MemoryError

最佳答案

Numpy 的 astype函数支持参数copy,如果设置为 false,将在初始数组上工作而不是生成副本。在代码中:

X_train = X_train.astype('float32', copy=False)
X_test = X_test.astype('float32', copy=False)

如果您在某些时候仍然耗尽内存,您还可以按顺序而不是同时读取训练集、验证集和测试集。一旦转换为浮点型,数组占用的空间就会减少,这可能会产生影响。

关于python - 代码重写 - MemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48683211/

相关文章:

python - 如何像在 MATLAB 中那样将 eig 与 nobalance 选项一起使用?

python - 分词时结合单数和复数、动词和副词的 nltk 频率

python - 将 csv 文件读取到 pandas 失败

python - 在Python中删除EXIF数据?

python - 为 Python 安装 cx_Oracle

mysql - 加载csv文件时如何将负值插入为0并保留正值

linux - 将 ctrl 分隔文件转换为 csv

python - 使用Python删除不包含正则表达式匹配的单元格/行

python - 切片 2d numpy 数组

python - 绘制折线图时出错