ResourceExhaustedError:OOM当分配张有形[32,32,239,239]和类型浮

0

的问题

我试图重新CNN图像识别模型从 这个纸(模式1) 使用不同的图像。 然而,配合的模型返回我一个ResourceExhaustedError在第一时代。 批的大小是已经大大小小所以我猜的问题是与我的模式定义我复制的文件。 任何建议改变与该模型将可以理解的。 谢谢你!

#Load dataset
BATCH_SIZE = 32
IMG_SIZE = (244,244)
train_set = tf.keras.preprocessing.image_dataset_from_directory(
    main_dir, 
    shuffle = True,
    image_size = IMG_SIZE,
    batch_size = BATCH_SIZE)
val_set = tf.keras.preprocessing.image_dataset_from_directory(
    main_dir, 
    shuffle = True, 
    image_size = IMG_SIZE,
    batch_size = BATCH_SIZE)
class_names = train_set.class_names
print(class_names)

#Augment data by flipping image and random rotation
data_augmentation = tf.keras.Sequential([
    tf.keras.layers.experimental.preprocessing.RandomFlip('horizontal'),
    tf.keras.layers.experimental.preprocessing.RandomRotation(0.2),
])

#Model definition 
model = Sequential([
    data_augmentation,
    tf.keras.layers.experimental.preprocessing.Rescaling(1./255),
    Conv2D(filters=64,kernel_size=(4,4), activation='relu'),
    Conv2D(filters=32,kernel_size=(3,3), activation='relu'),
    AveragePooling2D(pool_size=(4,4)),

    Conv2D(filters=32,kernel_size=(3,3), activation='relu'),
    Conv2D(filters=32,kernel_size=(3,3), activation='relu'),
    Conv2D(filters=32,kernel_size=(3,3), activation='relu'),
    AveragePooling2D(pool_size=(2,2)),
    Flatten(),
    
    Dense(256, activation='relu'),
    Dense(256, activation='relu'),
    Dense(128, activation='relu'),
    Dense(128, activation='relu'),
    Dense(128, activation='tanh'),
    Dense(1, activation='softmax')

])

model.compile(optimizer='RMSprop',
              loss=keras.losses.CategoricalCrossentropy(from_logits=True),
              metrics=[keras.metrics.CategoricalAccuracy()])

history = model.fit(train_set,validation_data=val_set, epochs=150)

错误之后拟模型:

ResourceExhaustedError:  OOM when allocating tensor with shape[32,32,239,239] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
     [[node gradient_tape/sequential_1/average_pooling2d/AvgPoolGrad (defined at <ipython-input-10-ef749d320491>:1) ]]

选-smi

+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.91.03    Driver Version: 460.91.03    CUDA Version: 11.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|                               |                      |               MIG M. |
|===============================+======================+======================|
|   0  GeForce 940MX       Off  | 00000000:01:00.0 Off |                  N/A |
| N/A   46C    P0    N/A /  N/A |   1938MiB /  2004MiB |      2%      Default |
|                               |                      |                  N/A |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                                  |
|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |
|        ID   ID                                                   Usage      |
|=============================================================================|
|    0   N/A  N/A       959      G   /usr/lib/xorg/Xorg                 97MiB |
|    0   N/A  N/A      1270      G   /usr/bin/gnome-shell               25MiB |
|    0   N/A  N/A      4635      G   /usr/lib/firefox/firefox          212MiB |
|    0   N/A  N/A      5843      C   /usr/bin/python3                 1595MiB |
+-----------------------------------------------------------------------------+

1

最好的答案

0

错误的是由你GPU运行的存储器。 这可能是因为1)你加载过多的数据每个时代,甚至对于你的GPU,或2)如果发生有足够的存,另一个处理保留的某些存储器之间的时代. 这可能发生,因为tensorflow储备金100%的存在运行时间。 你可以限制金额的存保留只是什么是必要的。

import tensorflow as tf
for gpu in tf.config.list_physical_devices("GPU")
    tf.config.experimental.set_memory_growth(gpu, True)

编辑:看你的卡,我相当肯定你没有足够的存于你的批大小和投入的大小(所问题1中)。 你应该下你的批的大小。

2021-11-23 17:57:42

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................