Python 在Keras中保存模型的正确方法
我一直试图在研究中实施迁移学习。我决定使用Python 在Keras中保存模型的正确方法,python,tensorflow,keras,conv-neural-network,transfer-learning,Python,Tensorflow,Keras,Conv Neural Network,Transfer Learning,我一直试图在研究中实施迁移学习。我决定使用keras.applications中提供的VGG16 我加载模型并冻结其权重,如下所示: vgg16=vgg16(weights='imagenet',include_top=False,input_shape=(img_行,img_列,3),pooling=None) 对于vgg16.layers中的层: layer.trainable=错误 然后添加顶层进行分类: model = Sequential() model.add(vgg16) mod
keras.applications
中提供的VGG16
我加载模型并冻结其权重,如下所示:
vgg16=vgg16(weights='imagenet',include_top=False,input_shape=(img_行,img_列,3),pooling=None)
对于vgg16.layers中的层:
layer.trainable=错误
然后添加顶层进行分类:
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'))
我根据我的数据编译并训练模型,以便首先预热顶层:
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\u model
我的下一个目标是解冻VGG16的一些顶层,并再次训练模型(也称为微调)。但是,在使用load\u model
加载模型时,我发现该模型看起来像未经培训的。我在保存测试数据集之前对其进行了测试,性能在70%范围内很高。加载相同的模型后,我发现测试数据集的性能大约为20%,这几乎低于概率,因为我有五个类标签
在我的
save\u model
和load\u model
命令之间发生了什么 Keras支持一个更简单的界面,将模型权重和模型架构一起保存到单个H5文件中
使用save.model方式保存模型包括我们需要了解的关于模型的所有信息,包括:
# 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")
# 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%
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%
加载模型并评估以验证:
# 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")
# 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%
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%
试试
model.save()
和load\u model()
。这正是我使用的。我已经找到了问题的原因。保存模型并将其加载回进行微调后,我使用fit方法开始对其进行训练。结果证明,优化算法很快就把它带向了错误的方向,尽管我已经将相对较低的学习率设置为0.0001。我通过进一步降低学习速度来修复它。加载后不需要编译。它将使用正确的优化器状态自动编译。