进行迁移学习时,如何在vgg16内添加customm层? [英] How to add customm layers inside vgg16 when doing transfer learning?

查看:490
本文介绍了进行迁移学习时,如何在vgg16内添加customm层?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过vgg16使用转移学习.我的主要概念是训练vgg16的前几层,并添加我自己的层,后言添加vgg16中的其余层,并在末尾添加我自己的输出层.为此,我遵循以下顺序:(1)加载图层和freez图层,(2)添加我的图层,(3)加载其余的图层(输出图层除外)[这是我遇到以下错误的地方]和freez该层,(4)添加输出层.我的方法可以吗?如果没有,那么我在哪里做错了?这是错误:

I am trying to use transfer learning using vgg16. My main concept is to train the first few layers of vgg16, and add my own layer, afterwords add the rest of the layers from vgg16, and add my own output layer to the end. To do this I follow this sequence: (1) load layers and freez layers, (2) add my layers, (3) load the rest of layers (except the output layer) [THIS IS WHERE I ENCOUNTER THE FOLLOWING ERROR] and freez the layer, (4) add output layer. Is my approach ok? If not, then where I am doing wrong? Here's the error:

ValueError:输入0与图层block3_conv1不兼容:预期输入形状的轴-1的值为128,但形状为(无,64、56、64)

ValueError: Input 0 is incompatible with layer block3_conv1: expected axis -1 of input shape to have value 128 but got shape (None, 64, 56, 64)

完整的代码在这里,可以更好地理解:

The full code is here for better understanding:

    vgg16_model= load_model('Fetched_VGG.h5')
    vgg16_model.summary()

    model= Sequential()

    #add vgg layer (inputLayer, block1, block2)
    for layer in vgg16_model.layers[0:6]:
        model.add(layer)

    #frees
    # Freezing the layers (Oppose weights to be updated)
    for layer in model.layers:
        layer.trainable = False

    #add custom
    model.add(Conv2D(64, (3, 3), activation='relu', padding='same', name='block66_conv1_m') )
    model.add( Conv2D(64, (3, 3), activation='relu', padding='same', name='block66_conv2_m') )
    model.add( Conv2D(64, (3, 3), activation='relu', padding='same', name='block66_conv3_m') )
    model.add( MaxPooling2D((2, 2), strides=(2, 2), name='block66_pool_m'))

    # add vgg layer (block 3 to last layer (except the output dense layer))
    for layer in vgg16_model.layers[7:-1]:
        model.add(layer)

    # Freezing the layers (Oppose weights to be updated)
    for layer in model.layers:
        layer.trainable = False

    # add out out layer
    model.add(Dense(2, activation='softmax', name='predictions'))
    model.summary()

推荐答案

由于VGG16第7层需要128个过滤器,因此您需要将其与最终的Conv2D匹配

As VGG16 layer 7 is expecting 128 filters you'll need to match this with your final Conv2D

model.add( Conv2D(128, (3, 3), activation='relu', padding='same', name='block66_conv3_m') )

如果尺寸匹配,则您应该能够构建模型,但不清楚要实现的目标.您将其添加到VGG16模型中间的方法将意味着所有下游层都需要重新培训

If the dimensions match you should be able to build your model but it's not clear what you're trying to achieve. Your approach of adding to the middle of the VGG16 model will mean that all the downstream layers will need to be retrained

这篇关于进行迁移学习时,如何在vgg16内添加customm层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆