将权重加载到从Keras 1模型保存的Keras 2模型中

时间:2019-05-14 18:15:07

标签: python keras

我有一个权重文件mnist.h5,它是用Keras 1和save_weights()保存到MNIST数据集的。

我有下面的代码可以加载mnist.h5权重文件并评估模型的准确性。当与Keras 1.2.2一起运行时,它输出:

Loaded model test accuracy: 99.08%

但是,在Keras 2.2.4上运行时,代码输出:

Loaded model test accuracy: 12.78%

Keras 2.0发行说明建议,可以将用Keras 1保存的重量文件加载到Keras 2模型中。

  

许多图层的保存权重格式已更改。但是,重量   用Keras 1保存的文件仍然可以在Keras 2模型中加载。

https://blog.keras.io/introducing-keras-2.html

在使用Keras 2运行时,我尝试了完全相同的代码(上面链接中的文档还指出Keras 2支持Keras 1 API,例如Convolution2D),并且还要进行以下更改:

Convolution2D(nb_filters, nb_conv, nb_conv)

使用Keras 2 API:

Conv2D(nb_filters, (nb_conv, nb_conv))

但是问题仍然存在:在Keras 2.2.4中,model.evaluate的分类得分仍然约为12%。

果然,当我添加时:

for layer in model.layers:
    weights = layer.get_weights()
    print(weights)

它表明在Keras 1.2.2和2.2.4上运行时,打印的权重数组的形状和值是不同的。

这是使用Keras 1.2.2和2.2.4运行代码的原始输出:https://gist.github.com/robstewart57/fe85603cb5cfc7380d917f215a1467fd

mnist.h5文件在这里:https://www.dropbox.com/s/d5728ah8o41tpom/mnist.h5?dl=0

上面的URL中似乎不支持Keras 2.0的发行说明。

  1. model.evaluate返回不同的输出分数是否令人惊讶?

  2. 如何更改代码,以便Keras 1.2.2和2.2.4都输出与Keras 1.2.2输出相同的〜99%分数?

代码如下:

import os
from keras import *
from keras.layers import *
from keras.models import *
from keras.utils import np_utils
from keras.datasets import mnist as mm

#network/data constants
nb_classes = 10
img_channels = 1
img_rows, img_cols = 28, 28
nb_filters = 32
nb_pool = 2
nb_conv = 3

# get mnist data
(X_train, Y_train), (X_test, Y_test) = mm.load_data()
X_train = X_train.reshape(X_train.shape[0], img_channels, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], img_channels, img_rows, img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
Y_train = np_utils.to_categorical(Y_train, nb_classes)
Y_test = np_utils.to_categorical(Y_test, nb_classes)

model = Sequential()
model.add(Conv2D(nb_filters, (nb_conv, nb_conv),
                            padding='valid',
                            input_shape=(1, img_rows, img_cols)))

# Keras 1.2.2
# model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
#                             border_mode='valid',
#                             input_shape=(1, img_rows, img_cols)))
model.add(Activation('relu'))
model.add(Conv2D(nb_filters, (nb_conv, nb_conv)))
# Keras 1.2.2
# model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))

model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='adadelta',
              metrics=['accuracy'])

# print architecture
model.summary()

# load weights form file
model.load_weights('mnist.h5')
print("Model loaded! Testing.........")

for layer in model.layers:
    weights = layer.get_weights()
    print(weights)

# test
score = model.evaluate(X_test, Y_test, verbose=0)
print("Loaded model test accuracy: " + str(score[1]*100) + "%")

0 个答案:

没有答案