栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Keras模型中删除然后插入新的中间层

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Keras模型中删除然后插入新的中间层

假设您有一个模型

vgg16_model
,可以通过上面的函数或通过初始化
keras.applications.VGG16(weights='imagenet')
。现在,您需要在中间插入一个新层,以节省其他层的权重。

想法是将整个网络分解为单独的层,然后将其重新组装。这是专门用于您的任务的代码:

vgg_model = applications.VGG16(include_top=True, weights='imagenet')# Disassemble layerslayers = [l for l in vgg_model.layers]# Defining new convolutional layer.# important: the number of filters should be the same!# Note: the receiptive field of two 3x3 convolutions is 5x5.new_conv = Conv2D(filters=64,        kernel_size=(5, 5),       name='new_conv',       padding='same')(layers[0].output)# Now stack everything back# Note: If you are going to fine tune the model, do not forget to#       mark other layers as un-trainablex = new_convfor i in range(3, len(layers)):    layers[i].trainable = False    x = layers[i](x)# Final touchresult_model = Model(input=layer[0].input, output=x)result_model.summary()

上面代码的输出是:

_________________________________________________________________Layer (type)      Output Shape   Param #   =================================================================input_50 (InputLayer)        (None, 224, 224, 3)       0         _________________________________________________________________new_conv (Conv2D) (None, 224, 224, 64)      1792      _________________________________________________________________block1_pool (MaxPooling2D)   (None, 112, 112, 64)      0         _________________________________________________________________block2_conv1 (Conv2D)        (None, 112, 112, 128)     73856     _________________________________________________________________block2_conv2 (Conv2D)        (None, 112, 112, 128)     147584    _________________________________________________________________block2_pool (MaxPooling2D)   (None, 56, 56, 128)       0         _________________________________________________________________block3_conv1 (Conv2D)        (None, 56, 56, 256)       295168    _________________________________________________________________block3_conv2 (Conv2D)        (None, 56, 56, 256)       590080    _________________________________________________________________block3_conv3 (Conv2D)        (None, 56, 56, 256)       590080    _________________________________________________________________block3_pool (MaxPooling2D)   (None, 28, 28, 256)       0         _________________________________________________________________block4_conv1 (Conv2D)        (None, 28, 28, 512)       1180160   _________________________________________________________________block4_conv2 (Conv2D)        (None, 28, 28, 512)       2359808   _________________________________________________________________block4_conv3 (Conv2D)        (None, 28, 28, 512)       2359808   _________________________________________________________________block4_pool (MaxPooling2D)   (None, 14, 14, 512)       0         _________________________________________________________________block5_conv1 (Conv2D)        (None, 14, 14, 512)       2359808   _________________________________________________________________block5_conv2 (Conv2D)        (None, 14, 14, 512)       2359808   _________________________________________________________________block5_conv3 (Conv2D)        (None, 14, 14, 512)       2359808   _________________________________________________________________block5_pool (MaxPooling2D)   (None, 7, 7, 512)         0         _________________________________________________________________flatten (Flatten) (None, 25088)  0         _________________________________________________________________fc1 (Dense)       (None, 4096)   102764544 _________________________________________________________________fc2 (Dense)       (None, 4096)   16781312  _________________________________________________________________predictions (Dense)          (None, 1000)   4097000   =================================================================Total params: 138,320,616Trainable params: 1,792Non-trainable params: 138,318,824_________________________________________________________________


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624260.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号