python - 在 keras 上使用 multiple_gpu_model - 导致资源耗尽

标签 python tensorflow keras gpu

我通过以下方式构建了我的网络:

# Build U-Net model
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = Lambda(lambda x: x / 255) (inputs)
width = 64
c1 = Conv2D(width, (3, 3), activation='relu', padding='same') (s)
c1 = Conv2D(width, (3, 3), activation='relu', padding='same') (c1)
p1 = MaxPooling2D((2, 2)) (c1)

c2 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (p1)
c2 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (c2)
p2 = MaxPooling2D((2, 2)) (c2)

c3 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (p2)
c3 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (c3)
p3 = MaxPooling2D((2, 2)) (c3)

c4 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (p3)
c4 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (c4)
p4 = MaxPooling2D(pool_size=(2, 2)) (c4)

c5 = Conv2D(width*16, (3, 3), activation='relu', padding='same') (p4)
c5 = Conv2D(width*16, (3, 3), activation='relu', padding='same') (c5)

u6 = Conv2DTranspose(width*8, (2, 2), strides=(2, 2), padding='same') (c5)
u6 = concatenate([u6, c4])
c6 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (u6)
c6 = Conv2D(width*8, (3, 3), activation='relu', padding='same') (c6)

u7 = Conv2DTranspose(width*4, (2, 2), strides=(2, 2), padding='same') (c6)
u7 = concatenate([u7, c3])
c7 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (u7)
c7 = Conv2D(width*4, (3, 3), activation='relu', padding='same') (c7)

u8 = Conv2DTranspose(width*2, (2, 2), strides=(2, 2), padding='same') (c7)
u8 = concatenate([u8, c2])
c8 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (u8)
c8 = Conv2D(width*2, (3, 3), activation='relu', padding='same') (c8)

u9 = Conv2DTranspose(width, (2, 2), strides=(2, 2), padding='same') (c8)
u9 = concatenate([u9, c1], axis=3)
c9 = Conv2D(width, (3, 3), activation='relu', padding='same') (u9)
c9 = Conv2D(width, (3, 3), activation='relu', padding='same') (c9)

outputs = Conv2D(1, (1, 1), activation='sigmoid') (c9)
with tf.device('/cpu:0'):
    model = Model(inputs=[inputs], outputs=[outputs])

sgd = optimizers.SGD(lr=0.03, decay=1e-6, momentum=0.9, nesterov=True)
parallel_model = multi_gpu_model(model, gpus=8)
parallel_model.compile(optimizer=sgd, loss='binary_crossentropy', metrics=[mean_iou])
model.summary()

请注意,我正在按照 keras documentation 的建议在 CPU 上实例化基本模型。 。然后,我使用以下几行运行网络:

# Fit model
earlystopper = EarlyStopping(patience=20, verbose=1)
checkpointer = ModelCheckpoint('test.h5', verbose=1, save_best_only=True)
results = parallel_model.fit(X_train, Y_train, validation_split=0.05, batch_size = 256, verbose=1, epochs=100, 
                    callbacks=[earlystopper, checkpointer])

但是,尽管我使用的是 multiple_gpu_model,我的代码仍然会导致以下错误:

OOM when allocating tensor with shape[32,128,256,256] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc

这表明网络正在尝试在单个 GPU(而不是 8 个)上运行 256 的批处理大小。我是否没有正确实现此操作?我需要像示例中那样使用 Xception 吗?

最佳答案

张量的第一个暗淡是batch_size,所以在你的情况下一切都很好。您已将batch_size指定为256并且使用8个GPU。所以你得到的batch_size是32,如错误中所述。 该错误还表明您的模型仍然太大,batch_size 为 32,GPU 无法处理。

关于python - 在 keras 上使用 multiple_gpu_model - 导致资源耗尽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50033178/

相关文章:

python-2.7 - 如何使用tensorflow对图像进行实时分类?

python - Keras LSTM 准确率太高

python - 获取可能路径的 MxN 网格(矩阵)问题

python - Tensorflow flatten vs numpy flatten 函数对机器学习训练的影响

python - 我如何做一个模仿 'keepdims' 的 einsum?

opencv - Opencv输出中的输出重叠

nlp - Keras SimpleRNN 输入形状和掩蔽

python - Keras 中的 S 形层

python - Numpy:添加行和列

python - 在 python 中使用 selenium 选择复选框