![](/img/trans.png)
[英]MemoryError: Unable to allocate 5.62 GiB for an array with shape (16384, 30720, 3) and data type float32 When training StyleGan2
[英]Memory error while training my model: Unable to allocate 31.9 GiB for an array with shape (3094, 720, 1280, 3) and data type float32
因此,我根据人类的存在为我的图像提供标签为“0”和“1”。 当我传递所有图像并尝试训练我的 model 时。 我收到 memory 错误。
import warnings
warnings.filterwarnings('ignore')
import tensorflow as to
import tensorflow.keras
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ReduceLROnPlateau, CSVLogger, EarlyStopping
from tensorflow.keras.models import Model
from tensorflow.keras.layers import GlobalAveragePooling2D, Dense
from tensorflow.keras.applications.resnet50 import ResNet50
from PIL import Image
import os
import numpy as np
train_x=[]
train_y=[]
for path in os.listdir('C:\\Users\\maini_\\Desktop\\TestAndTrain\\in\\train'):
img = Image.open('C:\\Users\\maini_\\Desktop\\TestAndTrain\\in\\train\\'+path)
train_x.append(np.array(img))
train_y.append(1)
img.close()
for path in os.listdir('C:\\Users\\maini_\\Desktop\\TestAndTrain\\notin\\train'):
img = Image.open('C:\\Users\\maini_\\Desktop\\TestAndTrain\\notin\\train\\'+path)
train_x.append(np.array(img))
train_y.append(0)
img.close()
print("done" )
train_x = np.array(train_x)
train_x = train_x.astype(np.float32)
train_x /= 255.0
train_y = np.array(train_y)
我正在与
您已尝试将3094
张大小为720x1280
的图像作为一个单一批次传递到 model 中,从而产生总计31.9GB的数据。 您的 GPU 过载,无法一次物理存储和处理所有数据,您需要使用批处理。
由于每次尝试处理数据都会遇到麻烦,我建议使用ImageDataGenerator()
和flow_from_directory()
来自动加载图片进行训练。
一种理想的设置方法如下
train_datagen = ImageDataGenerator(
rescale=1./255,
validation_split=0.3) #Splits the data 70/30 for training and validation
train_generator = train_datagen.flow_from_directory(
train_data_dir,
color_mode='grayscale',
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=True,
subset='training')
validation_generator = train_datagen.flow_from_directory(
train_data_dir,
color_mode='grayscale',
target_size=(img_width, img_height),
batch_size=batch_size,
class_mode='categorical',
shuffle=True,
subset='validation')
然后安装 model 你需要调用model.fit_generator()
class
model.fit_generator(train_generator, epochs=epochs, callbacks=callbacks, validation_data=validation_generator)
这是在 Keras 中训练模型时处理大量图像的最佳方法,因为数据是从每个批次的目录生成(或流动)的,而不是在 Python 中手动加载等。 唯一需要注意的是目录设置与您当前拥有的略有不同。 您需要将目录设置更改为
TestAndTrain
-Train
-in
-notin
-Test
-in
-notin
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.