当前位置: 首页 > 面试题库 >

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

常英纵
2023-03-14
问题内容

给定一个预定义的Keras模型,我试图首先加载预先训练的权重,然后从内部(非最后几个)模型中删除一到三个模型,然后将其替换为另一层。

我似乎在keras.io上找不到任何有关做这种事情或从预定义模型中删除图层的文档。

我使用的模型是一个良好的ole VGG-16网络,该网络通过如下所示的函数实例化:

def model(self, output_shape):

    # Prepare image for input to model
    img_input = Input(shape=self._input_shape)

    # Block 1
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
    x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

    # Block 2
    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
    x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

    # Block 3
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
    x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

    # Block 4
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

    # Block 5
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
    x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)

    # Classification block
    x = Flatten(name='flatten')(x)
    x = Dense(4096, activation='relu', name='fc1')(x)
    x = Dropout(0.5)(x)
    x = Dense(4096, activation='relu', name='fc2')(x)
    x = Dropout(0.5)(x)
    x = Dense(output_shape, activation='softmax', name='predictions')(x)

    inputs = img_input

    # Create model.
    model = Model(inputs, x, name=self._name)

    return model

例如,在将原始权重加载到所有其他层之后,我想在第1块中使用两个Conv层,并仅用一个Conv层替换它们。

有任何想法吗?


问题答案:

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

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

vgg_model = applications.VGG16(include_top=True, weights='imagenet')

# Disassemble layers
layers = [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-trainable

x = new_conv
for i in range(3, len(layers)):
    layers[i].trainable = False
    x = layers[i](x)

# Final touch
result_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,616
Trainable params: 1,792
Non-trainable params: 138,318,824
_________________________________________________________________


 类似资料:
  • 我有一个由Access DB使用ResultSet&填充的JTable。我有一个方法可以正确地从数据库中删除记录,但在刷新表模型的当前视图时遇到了困难。我看过类似的帖子,并尝试使用和,但没有成功。我还注意到其他帖子提到了的使用,因为它有add/remove行方法,但我使用的代码来自我去年使用的Java教科书(教授从未达到这一点,所以我试图自己学习)... 以下是定制JFrame的类: 下面是Abs

  • 插入、更新和删除语句基于以开头的层次结构生成 UpdateBase . 这个 Insert 和 Update 构建基于中介的 ValuesBase . DML基础构造函数 顶级的“插入”、“更新”、“删除”构造函数。 Object Name Description delete(table[, whereclause, bind, returning, ...], **dialect_kw) 构建

  • 我有一个mongoose模式,看起来如下所示: ); 我需要将数据推送到这个架构。 } 此数据推送到数据库后3天必须删除。如何在Node.js中实现?我发现它真的很混乱,试了很多代码。如有任何回答,我们将不胜感激!谢谢 附注。我使用mongoDb图集

  • 问题内容: 我正在尝试转学;为此,我想删除神经网络的最后两层并添加另外两层。这是一个示例代码,它也会输出相同的错误。 我使用删除了该图层,但是当我尝试添加其输出时出现此错误 AttributeError:“模型”对象没有属性“添加” 我知道该错误的最可能原因是不当使用。我应该使用其他什么语法? 编辑: 我试图在keras中删除/添加图层,但不允许在加载外部重物后添加它。 它显示此错误 问题答案:

  • 当我删除所有旧数据时。之后,我想插入新的数据,我插入的第一个新数据以1开头。我怎么能做到。

  • 问题内容: 我与CohortGroup和Employee有很多关系。每当我将Employee插入CohortGroup时,hibernate都会从解析表中删除该组,然后再次插入所有成员以及新成员。为什么不只是添加新的呢? 组中的注释: 员工的另一边 代码嗅探 下面是日志中报告的SQL 这种接缝的效率很低,并且会引起一些问题。如果有人提出要求将雇员添加到组中,则有些人将其改写。 诸如equals和h