且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Keras VGG16 微调

更新时间:2023-12-02 22:00:34

我认为 vgg 网络描述的权重不适合您的模型,错误源于此.无论如何,有一种更好的方法可以使用网络本身来做到这一点,如 (https://keras.io/applications/#vgg16).

I think that the weights described by the vgg net do not fit your model and the error stems from this. Anyways there is a way better way to do this using the network itself as described in (https://keras.io/applications/#vgg16).

你可以使用:

base_model = keras.applications.vgg16.VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=None)

实例化一个经过预训练的 vgg 网络.然后你可以冻结层并使用模型类来实例化你自己的模型,如下所示:

to instantiate a vgg net that is pre-trained. Then you can freeze the layers and use the model class to instantiate your own model like this:

x = base_model.output
x = Flatten()(x)
x = Dense(your_classes, activation='softmax')(x) #minor edit
new_model = Model(input=base_model.input, output=x)

要组合底部和顶部网络,您可以使用以下代码段.使用了以下函数(输入层(https://keras.io/getting-启动/功能 API 指南/)/load_model (https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model)和keras的功能API):>

To combine the bottom and the top network you can use the following code snippet. The following functions are used (Input Layer (https://keras.io/getting-started/functional-api-guide/) / load_model (https://keras.io/getting-started/faq/#how-can-i-save-a-keras-model) and the functional API of keras):

final_input = Input(shape=(3, 224, 224))
base_model = vgg...
top_model = load_model(weights_file)

x = base_model(final_input)
result = top_model(x)
final_model = Model(input=final_input, output=result)