从config加载Keras图层

时间:2017-02-28 12:02:11

标签: python keras keras-layer

我正在尝试使用" layer_from_config" Keras utilty用于从先前保存的配置加载图层,如下所述: https://keras.io/layers/about-keras-layers/

对于初学者,我试图在基本模型上使用它

FATAL EXCEPTION: main
              Process: de.sentoon.touchtesterv4, PID: 11743
              java.lang.RuntimeException: Unable to start activity ComponentInfo{de.sentoon.touchtesterv4/de.sentoon.touchtesterv4.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class de.sentoon.touchtesterv4.DrawingView
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2788)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1503)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6209)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class de.sentoon.touchtesterv4.DrawingView
               Caused by: android.view.InflateException: Binary XML file line #20: Error inflating class de.sentoon.touchtesterv4.DrawingView
               Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
                  at java.lang.Class.getConstructor0(Class.java:2204)
                  at java.lang.Class.getConstructor(Class.java:1683)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:618)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:288)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                  at de.sentoon.touchtesterv4.MainActivity.onCreate(MainActivity.java:27)
                  at android.app.Activity.performCreate(Activity.java:6745)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2788)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1503)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6209)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

正如预期的那样,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) 返回一个dict类型的对象,并将其打印为

config

但是,当我运行上面的代码时,我收到以下错误消息

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

那么,我做错了什么?

1 个答案:

答案 0 :(得分:1)

好的,这是一个奇怪的案例,可能来自更新。这是它的工作方式:

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

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

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

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

因此model.get_config()是包含layer_from_config()将接受的字典列表的那个。

不是获取图层列表,而是获取“坏”格式的配置,而是必须获取模型配置,该配置是具有正确格式的图层配置列表。

他们的文档似乎不是最新的。他们应该调整它,或者他们应该调整layer.get_config()的代码。

无论如何,你现在可以使用它:)