繁体   English   中英

调整 Mnist 数据集图像大小时的 MemoryError

[英]MemoryError When Resize Mnist data set images

我是深度学习的新手。我正在尝试将 mnist 图像从 28 * 28 更改为 224 * 224。

所以我决定使用调整大小的方法。 导入 MNIST 数据集后,我尝试调整它的大小:

(X_train, y_train), (X_test, y_test) = mnist.load_data()

x_train_small = tf.image.resize(X_train, (224,224)).numpy() 

但我得到了这个错误

MemoryError: Unable to allocate 11.2 GiB for an array with shape (60000, 224, 224, 1) and data type float32

我的电脑很旧,我只有16gig内存。 如何调整 Mnist 数据集的大小?

考虑使用tf.data.Dataset并动态调整图像大小,因为批次通过:

import tensorflow as tf

(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()

resize = lambda x, y: (tf.image.resize(tf.expand_dims(x, -1), (224, 224)), y)

train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train)).map(resize)

for image, label in train_ds.take(5):
    print(image.shape)
(224, 224, 1)
(224, 224, 1)
(224, 224, 1)
(224, 224, 1)
(224, 224, 1)

您可以将此数据集直接传递给model.fit(train_ds)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM