【问题标题】:Is it possible to extract part of a multi-input model in Keras/TensorFlow?是否可以在 Keras/TensorFlow 中提取部分多输入模型?
【发布时间】:2020-04-15 10:27:27
【问题描述】:

我有这个多输入网络:

def build_model():
  inputRGB = tf.keras.Input(shape=(128,128,3), name='train_ds')
  inputFixed = tf.keras.Input(shape=(128,128,3), name='fixed_ds')
  inputDinamic = tf.keras.Input(shape=(128,128,3), name='dinamic_ds')

    # rete per Immagini RGB
  rgb = models.Sequential()  
  rgb = layers.Conv2D(32, (5, 5), padding='SAME')(inputRGB)
  rgb = layers.PReLU()(rgb)
  rgb = layers.MaxPooling2D((2, 2))(rgb)
  rgb = layers.BatchNormalization()(rgb)
  rgb = layers.Conv2D(64, (3, 3))(rgb)
  rgb = layers.PReLU()(rgb)
  rgb = layers.Conv2D(64, (3, 3))(rgb)
  rgb = layers.PReLU()(rgb)
  rgb = layers.Conv2D(64, (3, 3))(rgb)
  rgb = layers.PReLU()(rgb)
  rgb = layers.Dropout(0.5)(rgb)
  rgb = layers.GlobalAvgPool2D()(rgb)
  rgb = Model(inputs = inputRGB, outputs=rgb)

    # rete per Density Map con "Pallini"
  fixed = models.Sequential()
  fixed = layers.Conv2D(32, (5, 5), padding='SAME')(inputFixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.MaxPooling2D((2, 2))(fixed)
  fixed = layers.BatchNormalization()(fixed)
  fixed = layers.Conv2D(64, (3, 3))(fixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.Conv2D(64, (3, 3))(fixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.Conv2D(64, (3, 3))(fixed)
  fixed = layers.PReLU()(fixed)
  fixed = layers.Dropout(0.5)(fixed)
  fixed = layers.GlobalAvgPool2D()(fixed)
  fixed = Model(inputs = inputFixed, outputs=fixed)

    # rete per Density map per "assembramenti"
  dinamic = models.Sequential()  
  dinamic = layers.Conv2D(32, (5, 5), padding='SAME')(inputDinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.MaxPooling2D((2, 2))(dinamic)
  dinamic = layers.BatchNormalization()(dinamic)
  dinamic = layers.Conv2D(64, (3, 3))(dinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.Conv2D(64, (3, 3))(dinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.Conv2D(64, (3, 3))(dinamic)
  dinamic = layers.PReLU()(dinamic)
  dinamic = layers.Dropout(0.5)(dinamic)
  dinamic = layers.GlobalAvgPool2D()(dinamic)
  dinamic = Model(inputs = inputDinamic, outputs=dinamic)

  concat = layers.concatenate([rgb.output, fixed.output, dinamic.output])  # merge the outputs of the two models
  k = layers.Dense(1)(concat)

  modelFinal = Model(inputs={'train_ds':inputRGB, 'fixed_ds':inputFixed, 'dinamic_ds':inputDinamic}, outputs=[k])

  opt = tf.keras.optimizers.Adam(learning_rate=0.001, amsgrad=False)


  modelFinal.compile(optimizer=opt , loss='mae', metrics=['mae'])
  return modelFinal

我想从最好的模型中提取,我已经使用以下代码行保存并重新加载:

best_model = tf.keras.models.load_model(checkpoint_path + 'regression_count_128.30-1.11.hdf5')

只是前面展示的多输入神经网络的第一部分。具体来说,我想提取以 RGB 图像作为输入的部分,以便仅在 RGB 测试图像上测试模型(使用 3 种不同类型的图像进行训练)。

【问题讨论】:

    标签: python tensorflow keras neural-network tf.keras


    【解决方案1】:

    使用输入层的名称('train_ds')和RGB部分的输出层的名称(你没有命名,但你可以使用best_model.summary()找到它,它以'global_average_pooling2d'开头)像这样构建一个新模型:

    rgb_model = Model(
          best_model.get_layer('train_ds').output,
          best_model.get_layer(name_of_rgb_output_layer).output
    )
    

    旁注:... = model.Sequential() 行是多余的,可以删除,因为您使用的是 keras 的功能 API 来定义您的模型。

    【讨论】:

    • 也可以添加一个形状为 1 的 Dense 层?? (就像之前的多输入神经网络一样)
    • @Marco 当然,只需创建一个Dense 层并将其应用于 rgb 部分的输出,然后创建模型。见this
    • 我忘了问一个基本问题(利用您的可用性),这样创建的新网络无论如何都会保留真实模型的权重?
    • @Marco 当然,权重会被保留(除了新添加的Dense 层,它将具有随机权重)。
    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 2021-04-18
    • 1970-01-01
    • 1970-01-01
    • 2022-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多