Keras VGG16 fine tuning

前端 未结 3 803
时光说笑
时光说笑 2021-01-18 01:19

There is an example of VGG16 fine-tuning on keras blog, but I can\'t reproduce it.

More precisely, here is code used to init VGG16 without top layer and to freeze a

3条回答
  •  野的像风
    2021-01-18 02:13

    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).

    You can just use:

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

    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)
    

    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)
    

提交回复
热议问题