使用 Keras 在 Google colab 上运行 3D CNN 的问题
Posted
技术标签:
【中文标题】使用 Keras 在 Google colab 上运行 3D CNN 的问题【英文标题】:Problem with running 3D CNNs on Google colab using Keras 【发布时间】:2020-10-16 16:43:45 【问题描述】:我正在尝试使用 google colab 上的 keras 库来训练一个包含 3D Conv 层的模型。我遇到了这个错误:
AttributeError Traceback (most recent call last)
<ipython-input-5-c6ef25f2bc4a> in <module>()
8
9 model = Sequential()
---> 10 model.add(Conv3D(16,kernel_size=(3,5,3),padding='same', activation='relu', kernel_initializer='he_normal', input_shape=(20,25,3,1),data_format='channels_first'))
11 model.add(Conv3D(32,kernel_size=(3,3,3),padding='same', activation='relu', kernel_initializer='he_normal',data_format='channels_first'))
12 model.add(Dropout(0.5))
/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py in _get_available_gpus()
504 _LOCAL_DEVICES = [x.name for x in devices]
505 else:
--> 506 _LOCAL_DEVICES = tf.config.experimental_list_devices()
507 return [x for x in _LOCAL_DEVICES if 'device:gpu' in x.lower()]
508
AttributeError: module 'tensorflow._api.v2.config' has no attribute 'experimental_list_devices'
当我尝试 2D 转换层时,不会发生此问题。仅适用于 3D 转换层。我还应该提到,这段代码在我的本地机器上运行得很好。
整个代码块是
import keras
from keras.models import Sequential, Model
from keras.layers import Dense, Flatten, Conv3D, MaxPooling3D, Dropout, BatchNormalization, Input
from keras.utils import to_categorical
from keras import regularizers
model = Sequential()
model.add(Conv3D(16,kernel_size=(3,5,3),padding='same', activation='relu', kernel_initializer='he_normal', input_shape=(20,25,3,1),data_format='channels_first'))
model.add(Conv3D(32,kernel_size=(3,3,3),padding='same', activation='relu', kernel_initializer='he_normal',data_format='channels_first'))
model.add(Dropout(0.5))
#model.add(MaxPooling3D(pool_size=(2, 2,2)))
model.add(Conv3D(64,kernel_size=(3,5,3),padding='same', activation='relu', kernel_initializer='he_normal',data_format='channels_first'))
model.add(MaxPooling3D(pool_size=(2, 2,2)))
model.add(Dropout(0.5))
model.add(Conv3D(128,kernel_size=(3,5,3),padding='same', activation='relu', kernel_initializer='he_normal',data_format='channels_first'))
#model.add(MaxPooling3D(pool_size=(2, 2,2)))
#model.add(Conv3D(64,kernel_size=(3,3,3),padding='same', activation='relu', kernel_initializer='he_normal',data_format='channels_last'))
#model.add(Conv3D(128,kernel_size=(3,3,3),padding='same', activation='relu', kernel_initializer='he_normal',data_format='channels_last'))
model.add(BatchNormalization(center=True, scale=True))
model.add(Flatten())
model.add(Dropout(0.5))
#model.add(Dense(10000, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(5000, activation='relu', kernel_initializer='he_normal'))
model.add(Dropout(0.5))
model.add(Dense(300, activation='relu', kernel_initializer='he_normal'))
model.add(Dense(20, activation='softmax'))
from keras.callbacks import ReduceLROnPlateau
model.compile(loss='categorical_crossentropy',
optimizer=keras.optimizers.Adam(lr=0.001),
metrics=['accuracy'])
model.summary()
reduce_lr = ReduceLROnPlateau(monitor='val_accuracy', factor=0.8,mode = 'max',patience=5, min_lr=0.0001)
# Fit data to model
history = model.fit(X_train, y_train,
callbacks =[reduce_lr],
batch_size=128,
epochs=300,
verbose=1,
validation_split=0.2)
score, acc = model.evaluate(X_test, y_test,
batch_size=128)
print('Test score:', score)
print('Test accuracy:', acc)
【问题讨论】:
在本地机器和 Google 协作中检查您的 Keras 版本。很可能存在某种不匹配 【参考方案1】:尝试添加此代码
import tensorflow as tf
import keras.backend.tensorflow_backend as tfback
print("tf.__version__ is", tf.__version__)
print("tf.keras.__version__ is:", tf.keras.__version__)
def _get_available_gpus():
"""Get a list of available gpu devices (formatted as strings).
# Returns
A list of available GPU devices.
"""
#global _LOCAL_DEVICES
if tfback._LOCAL_DEVICES is None:
devices = tf.config.list_logical_devices()
tfback._LOCAL_DEVICES = [x.name for x in devices]
return [x for x in tfback._LOCAL_DEVICES if 'device:gpu' in x.lower()]
tfback._get_available_gpus = _get_available_gpus
您还应该查看此以获得进一步的解决方案:https://github.com/keras-team/keras/issues/13684
【讨论】:
以上是关于使用 Keras 在 Google colab 上运行 3D CNN 的问题的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Google colab 中更改 Keras/tensorflow 版本?
使用大数据集在 Google Colab TPU 上训练 seq2seq 模型 - Keras
我在 google colab 上训练了一个 keras 模型。现在无法在我的系统上本地加载它。