我正在尝试使用“layer_from_config”Keras 实用程序从以前保存的配置中加载图层,如下所述: https ://keras.io/layers/about-keras-layers/
对于初学者,我正在尝试在基本模型上使用它
import keras
keras.backend.set_image_dim_ordering("th")
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D
from keras.layers import Activation, Dropout, Flatten, Dense
# dimensions of our images.
img_width, img_height = 150, 150
train_data_dir = '//shared_directory/projects/try_CD/data/train/'
validation_data_dir = '//shared_directory/projects/try_CD/data/validation'
nb_train_samples = 2000
nb_validation_samples = 800
nb_epoch = 50 # 50
model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Convolution2D(64, 3, 3))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
from keras.utils.layer_utils import layer_from_config
config = model.layers[1].get_config()
layer = layer_from_config(config)
正如预期的那样,config
返回一个 dict 类型对象,并打印它读取
{'activation': 'relu', 'trainable': True, 'name': 'activation_1'}
但是,当我运行上面的代码时,我收到以下错误消息
Traceback (most recent call last):
File "keras_CvD.py", line 91, in <module>
layer = layer_from_config(config)
File "/usr/local/lib/python2.7/dist-packages/keras/utils/layer_utils.py", line 26, in layer_from_config
class_name = config['class_name']
KeyError: 'class_name'
那么,我做错了什么?