简体   繁体   English

从config加载Keras图层

[英]Loading Keras layers from config

I am trying to use the "layer_from_config" Keras utilty to load layers from previously saved configurations, as described in: https://keras.io/layers/about-keras-layers/ 我正在尝试使用“layer_from_config”Keras utilty从以前保存的配置加载图层,如下所述: https ://keras.io/layers/about-keras-layers/

For starters, I'm trying to use it on a basic model 对于初学者,我试图在基本模型上使用它

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)

As expected, config returns a dict type object, and printing it reads 正如所料, config返回一个dict类型的对象,并打印它读取

{'activation': 'relu', 'trainable': True, 'name': 'activation_1'} 

However, when I run the code above I'm getting the following error message 但是,当我运行上面的代码时,我收到以下错误消息

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'

So, what am I doing wrong? 那么,我做错了什么?

Ok this is a weird case, maybe coming from an update. 好的,这是一个奇怪的案例,可能来自更新。 Here is the way it's working : 这是它的工作方式:

if you print(model.layers[1].get_config()) : 如果你print(model.layers[1].get_config())

{'trainable': True, 'name': 'activation_1', 'activation': 'relu'}

if you print(model.get_config()[1]) : 如果你print(model.get_config()[1])

{'config': {'trainable': True, 'name': 'activation_1', 'activation': 'relu'}, 'class_name': 'Activation'}

So the model.get_config() is the one containing a list of dictionnaries that layer_from_config() will accept. 因此, model.get_config()是包含layer_from_config()将接受的字典列表的那个。

Instead of getting the list of layers and then its config which is in a "bad" format, you have to get the model config which is a list of layers config with the right format. 您不必获取图层列表,而是获取“坏”格式的配置,而是必须获取模型配置,该配置是具有正确格式的图层配置列表。

Their doc isn't up to date it seems. 他们的文件似乎不是最新的。 Either they should adapt it, or they should adapt the code of layer.get_config() . 他们应该调整它,或者他们应该调整layer.get_config()的代码。

Anyway, you can use it now :) 无论如何,你现在可以使用:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM