使用Keras在Google colab上运行3D CNN的问题

时间:2020-06-26 05:40:23

标签: python tensorflow keras google-colaboratory

我正在尝试使用Google colab上的keras库训练包含3D转换图层的模型。我遇到此错误:

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)

1 个答案:

答案 0 :(得分:2)

尝试添加此代码

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