0
在转移学习中,只能在训练期间保存完全连接的图层,并在以后加载以便继续训练?如果可能的话,该怎么做?Keras vgg转移学习保存/加载自定义完全连接图层
在转移学习中,只能在训练期间保存完全连接的图层,并在以后加载以便继续训练?如果可能的话,该怎么做?Keras vgg转移学习保存/加载自定义完全连接图层
这可能不是最好的解决方案,但我认为这可以解决您的问题。
import pickle
#set a name for all fully connected layers.
model.add(Dense(...,name='fc1'))
model.add(Dense(...,name='fc2'))
model.add(Dense(...,name='fc3'))
layers_to_save = ['fc1','fc2','fc3'] # add here any layer you want to save
# Save weights to a dictionary
weights_dict = dict([(layer.name, layer.get_weights()) for layer in model.layers if layer.name in layers_to_save])
with open('filename.pickle', 'wb') as handle:
pickle.dump(weights_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
# Load weights
with open('filename.pickle', 'rb') as handle:
weights_dict = pickle.load(handle)
for name in layers_to_save:
model.get_layer(name).set_weights(weights_dict[name])
您还可以检查此keras blog post下半年的另一种方法。
谢谢。我看到负载顶部模型,但我没有看到如何保存顶部模型。这是我的问题。如果使用'from keras.callbacks import ModelCheckpoint ckp_callback = ModelCheckpoint(model_file,monitor =“val_loss”,save_best_only = True,save_weights_only = True,mode ='min')',所有权重包括vgg都会被保存,对吗?如何保存顶级模型? – user6101147
对不起,我编辑过我的帖子。在那篇博文中,首先,您需要保存瓶颈功能(最后一个卷积层的输出),然后用它们训练一个FC模型。在某些情况下,这可能是个好主意。 – Matin