keras 模型微调与迁移训练

1.只加载部分权重

assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
f = h5py.File(weights_path)
for k in range(f.attrs['nb_layers']):
    if k >= len(model.layers):
        # we don't look at the last (fully-connected) layers in the savefile
        break
    g = f['layer_{}'.format(k)]
    weights = [g['param_{}'.format(p)] for p in range(g.attrs['nb_params'])]
    model.layers[k].set_weights(weights)
f.close()
print('Model loaded.')

2.某些权重设置为不训练

for layer in model.layers[:25]:
    layer.trainable = False

猜你喜欢

转载自blog.csdn.net/u011489887/article/details/85265448