【问题标题】:GradCAM with Transfer-Learning and augmentation layers at imputGrad CAM 在输入端具有迁移学习和增强层
【发布时间】:2022-03-28 20:20:27
【问题描述】:

我正在尝试将 GradCAM 实施到迁移学习模型中。出于这个原因,我需要基础模型的最后一个卷积层的额外输出。 我的模型由预处理/增强层、预训练的 MobileNet 和自定义头部组成。当 MobileNet 实现一个功能层时,我总是得到一个断开连接的图形错误。而且由于一开始的增强层,我没有像其他解决方案提出的那样将 MobileNet 实现为单层。非常感谢您的帮助!

# transfer-learning model

base_model = MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')
          

inputs = Input(shape=(224, 224, 3))
augmented = RandomFlip("horizontal")(inputs)
augmented = RandomRotation(0.1)(augmented)
augmented = RandomZoom(height_factor=(0.0, 0.3), width_factor=(0.0, 0.3), 
                                 fill_mode='constant')(augmented)
mobilenet = base_model(augmented)
pooling = GlobalAveragePooling2D()(mobilenet)
dropout = Dropout(0.5)(pooling)
outputs = Dense(len(classes), activation="softmax")(dropout)
model = Model(inputs=inputs, outputs=outputs)
model.summary()

这是我的 GradCAM 模型:

gradModel = Model(inputs=[model.inputs],
                  outputs=[model.get_layer('mobilenetv2_1.00_224').get_layer('Conv_1').output,
                  model.output])

【问题讨论】:

  • 您是否尝试将 [model.get_layer('mobilenetv2_1.00_224').input] 作为输入传递给 gradcam 而不是 [model.input]?我对 ResNet 使用了类似的方法,效果很好。
  • 是的,我试过了。如果我只使用 MobileNet 中的“Conv_1”作为输出,这可以正常工作。但是,一旦我将 model.output 添加为第二个输出,我就会再次收到图表断开错误。
  • @dergisler 你有办法吗?

标签: python tensorflow transfer-learning


【解决方案1】:

我遇到了类似的问题,最终在数据集级别而不是在模型层中实现了增强。

  train_ds = tf.keras.utils.image_dataset_from_directory(
      train_dir,
      validation_split=0.3,
      label_mode='categorical',
      subset="training",
      seed=s,
      color_mode="rgb",
      image_size=image_size,
      batch_size=batch_size,
  )

 data_augmentation = tf.keras.Sequential([
    tf.keras.layers.RandomFlip("horizontal"),
    tf.keras.layers.RandomRotation(0.1),
    tf.keras.layers.RandomZoom(height_factor=(0.0, 0.3), width_factor=(0.0, 0.3), fill_mode='constant')
  ])
  
  train_ds = train_ds.map(
      lambda x, y: (data_augmentation(x, training=True), y)
  )
 

然后我会将这些数据输入到模型中,它会产生预期的效果。

model.fit(train_ds, EPOCHS)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2017-09-14
    • 2020-05-13
    相关资源
    最近更新 更多