在Keras中保存模型的正确方法 [英] Proper Way to Save Model in Keras
问题描述
我一直在尝试在研究中实施转移学习。我决定使用 keras.applications
中提供的VGG16。
I have been trying to implement transfer learning in my research. I have decided to go with VGG16 as provided in keras.applications
.
我按以下方式加载模型并冻结其权重:
I load the model and freeze its weights as follows:
vgg16 = VGG16(weights='imagenet', include_top=False,input_shape=(img_rows, img_cols, 3), pooling = None)
for layer in vgg16.layers:
layer.trainable = False
我然后添加顶层进行分类:
I then add top layers for classification:
model = Sequential()
model.add(vgg16)
model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dense(1024, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))
我编译并训练数据模型以首先加热顶层:
I compile and train the model on my data to warm up the top layers first:
EPOCHS = 4000
history = model.fit_generator(datagen_train.flow(X_train, y_train, batch_size=100),
validation_data = datagen_val.flow(X_val, y_val, batch_size=100),
epochs = EPOCHS,
steps_per_epoch = np.ceil(len(X_train) / 100),
validation_steps = np.ceil(len(X_val) / 100),
callbacks=[es, mc]
)
我使用常用的Keras命令保存模型: save_model
。
I save the model using the usual Keras command: save_model
.
我的下一个目标是解冻VGG16的某些顶层并再次训练模型(也称为微调)。但是,在使用 load_model
加载模型后,我发现该模型看起来像未经训练的模型,我在保存测试数据集之前对其进行了测试,并且性能高达70%范围。加载相同的模型后,考虑到我有五个类标签,我发现测试数据集的性能大约为20%,这几乎低于机会。
My next goal is unfreeze some of the top layers of VGG16 and train the model again (a.k.a fine-tune). However, upon loading the model with load_model
, I find that the model looks like untrained.I tested it before saving on the test data set and the performance was high in the 70% range. After loading the same model, I find that the performance on the test data set is around 20%, which is almost below chance, considering I have five class labels.
我的 save_model
和 load_model
之间发生了什么命令?
What has happened in between my save_model
and load_model
commands?
推荐答案
Keras支持一个更简单的界面,可将模型权重和模型体系结构一起保存到单个H5文件中。
Keras supports a simpler interface to save both the model weights and model architecture together into a single H5 file.
使用save.model方法保存模型包括我们需要了解的有关模型的所有信息,包括:
Saving the model with save.model way includes everything we need to know about the model, including:
- 模型权重。
- 模型体系结构。
- 模型编译详细信息(损失和指标)。
- 模型优化器状态。
- Model weights.
- Model architecture.
- Model compilation details (loss and metrics).
- Model optimizer state.
然后可以通过调用load_model()函数并传递文件名来加载保存的模型。函数将返回具有相同架构和权重的模型。
Saved model can then be loaded by calling the load_model() function and passing the filename. The function returns the model with the same architecture and weights.
示例:我运行了一个简单的模型,并使用model.save和用keras的load_model加载了。您可以从此处下载数据集。
Example: I have run a simple model and saved using model.save and the did the load with load_model of keras. You can download the dataset from here.
建立并保存模型:
# MLP for Pima Indians Dataset saved to single file
import numpy as np
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
# load pima indians dataset
dataset = np.loadtxt("/content/pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# define model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# compile model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# Model Summary
model.summary()
# Fit the model
model.fit(X, Y, epochs=150, batch_size=10, verbose=0)
# evaluate the model
scores = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
# save model and architecture to single file
model.save("model.h5")
print("Saved model to disk")
输出-
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_7 (Dense) (None, 12) 108
_________________________________________________________________
dense_8 (Dense) (None, 8) 104
_________________________________________________________________
dense_9 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
acc: 77.08%
Saved model to disk
加载模型并评估以验证:
# load and evaluate a saved model
from numpy import loadtxt
from keras.models import load_model
# load model
model = load_model('model.h5')
# summarize model.
model.summary()
# load dataset
dataset = loadtxt("pima-indians-diabetes.csv", delimiter=",")
# split into input (X) and output (Y) variables
X = dataset[:,0:8]
Y = dataset[:,8]
# evaluate the model
score = model.evaluate(X, Y, verbose=0)
print("%s: %.2f%%" % (model.metrics_names[1], score[1]*100))
输出-
Model: "sequential_3"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_7 (Dense) (None, 12) 108
_________________________________________________________________
dense_8 (Dense) (None, 8) 104
_________________________________________________________________
dense_9 (Dense) (None, 1) 9
=================================================================
Total params: 221
Trainable params: 221
Non-trainable params: 0
_________________________________________________________________
acc: 77.08%
这篇关于在Keras中保存模型的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!