首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有办法将keras mobilenetv2模型划分为子模型吗?

有办法将keras mobilenetv2模型划分为子模型吗?
EN

Stack Overflow用户
提问于 2020-03-17 19:56:02
回答 1查看 955关注 0票数 1

我试图将mobilenetv2模型分为两部分。

我首先想要运行模型的第一部分,保存输出,然后出于某些原因将其提供给第二个模型。我尝试过代码找到了这里,但是我得到了以下错误:

代码语言:javascript
运行
复制
ValueError: A merge layer should be called on a list of inputs.

我认为这是因为模型不是顺序的。有人能帮忙吗?

EN

回答 1

Stack Overflow用户

发布于 2020-03-18 15:34:50

正如我在评论中提到的,mobile_net_v2中的一些层期望有多个输入,这些输入是以前一些层的输出。因此,将它们添加到顺序模型中会导致错误。我有个解决办法给你。使用mobile_net_v2实现(我自己的)在链接中,我能够创建您想要的模型:

代码语言:javascript
运行
复制
import tensorflow as tf
from tensorflow.keras import layers, Model, Sequential


def conv_block(input_tensor, c, s, t, expand=True):
    """
    Convolutional Block for mobile net v2
    Args:
        input_tensor (keras tensor): input tensor
        c (int): output channels
        s (int): stride size of first layer in the series
        t (int): expansion factor
        expand (bool): expand filters or not?

    Returns: keras tensor
    """
    first_conv_channels = input_tensor.get_shape()[-1]
    if expand:
        x = layers.Conv2D(
            first_conv_channels*t,
            1,
            1,
            padding='same',
            use_bias=False
        )(input_tensor)
        x = layers.BatchNormalization()(x)
        x = layers.ReLU(6.0)(x)
    else:
        x = input_tensor

    x = layers.DepthwiseConv2D(
        3,
        s,
        'same',
        1,
        use_bias=False
    )(x)
    x = layers.BatchNormalization()(x)
    x = layers.ReLU(6.0)(x)

    x = layers.Conv2D(
        c,
        1,
        1,
        padding='same',
        use_bias=False
    )(x)
    x = layers.BatchNormalization()(x)

    if input_tensor.get_shape() == x.get_shape() and s == 1:
        return x+input_tensor

    return x


def splitted_model(input_shape=(224,224,3)):

    input = layers.Input(shape=input_shape)

    x = layers.Conv2D(
        32,
        3,
        2,
        padding='same',
        use_bias=False
    )(input)
    x = layers.BatchNormalization()(x)
    x = layers.ReLU(6.0)(x)

    x = conv_block(x, 16, 1, 1, expand=False)
    x = conv_block(x, 24, 2, 6)
    x = conv_block(x, 24, 1, 6)

    x = conv_block(x, 32, 2, 6)
    x = conv_block(x, 32, 1, 6)
    x = conv_block(x, 32, 1, 6)

    x = conv_block(x, 64, 2, 6)
    x = conv_block(x, 64, 1, 6)
    x = conv_block(x, 64, 1, 6)
    x = conv_block(x, 64, 1, 6)

    model_f = Model(inputs=input, outputs=x)

    input_2 = layers.Input(shape=(x.shape[1:]))
    x = conv_block(input_2, 96, 1, 6)
    x = conv_block(x, 96, 1, 6)
    x = conv_block(x, 96, 1, 6)

    x = conv_block(x, 160, 2, 6)
    x = conv_block(x, 160, 1, 6)
    x = conv_block(x, 160, 1, 6)

    x = conv_block(x, 320, 1, 6)

    x = layers.Conv2D(
        1280,
        1,
        1,
        padding='same',
        use_bias=False
    )(x)
    x = layers.BatchNormalization()(x)
    x = layers.ReLU(6.0)(x)

    x = layers.GlobalAveragePooling2D()(x)


    model_h = Model(inputs=input_2, outputs=x)

    return model_f, model_h

您可以这样创建您的两个模型:

代码语言:javascript
运行
复制
IMG_SIZE = 160
IMG_SHAPE = (IMG_SIZE, IMG_SIZE, 3)
model_f, model_h = splitted_model(input_shape=IMG_SHAPE)

注意,权重是随机初始化的。如果希望在imagenet上培训来自mobilenet_v2的权重,可以运行以下代码来复制权重:

代码语言:javascript
运行
复制
mobile_net = tf.keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                           include_top=False,
                                           weights='imagenet')
layer_f_counter = 0
layer_h_counter = 0
for i in range(len(mobile_net.layers)):
  if layer_f_counter<len(model_f.layers):
    if len(mobile_net.layers[i].get_weights()) > 0:
      if len(model_f.layers[layer_f_counter].get_weights()) > 0:
        print(mobile_net.layers[i].name,'here', model_f.layers[layer_f_counter].name, layer_f_counter)
        model_f.layers[layer_f_counter].set_weights(mobile_net.layers[i].get_weights())
      layer_f_counter += 1
      print(layer_f_counter)
    else:
      if len(model_f.layers[layer_f_counter].get_weights()) > 0:
        continue
      else:
        layer_f_counter+=1

  else:
    if layer_h_counter<len(model_h.layers):
      if len(mobile_net.layers[i].get_weights()) > 0:
        if len(model_h.layers[layer_h_counter].get_weights()) > 0:
          print(mobile_net.layers[i].name,'here', model_h.layers[layer_h_counter].name, layer_h_counter)
          model_h.layers[layer_h_counter].set_weights(mobile_net.layers[i].get_weights())
        layer_h_counter += 1
        print(layer_h_counter)
      else:
        if len(model_h.layers[layer_h_counter].get_weights()) > 0:
          continue
        else:
          layer_h_counter+=1

它迭代从Keras加载的mobilenet_v2层,将第一部分的权重复制到model_f,其余部分复制到model_h。您可以通过从mobile_net中打印出一些随机层权重来检查权重是否正确复制,以及新模型如下:

代码语言:javascript
运行
复制
print(model_f.layers[1].get_weights()) # printing weights of first conv layer in model_f
print(mobile_net.get_layer('Conv1').get_weights()) # printing weights of fist conv layer in mobile_net

也适用于model_h:

代码语言:javascript
运行
复制
print(model_h.layers[-4].get_weights()) # printing weights of last conv layer in model_h
print(mobile_net.get_layer('Conv_1').get_weights()) # printing weights of last conv layer in mobile_net

注意,我随机选择了将moile_net分离为model_f和model_h的块,您可以编辑它以更改要拆分的位置。希望能帮上忙。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60729336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档