且构网

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

Keras VGG16微调

更新时间:2023-12-02 21:56:22

我认为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- starts/functional-api-guide/)/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)