如何更改通道数以微调 Keras 中的 VGG16 网络

How to change number of channels to fine tune VGG16 net in Keras

我想使用我自己的灰度图像微调 VGG16 模型。我知道我可以通过以下方式完善 tune/add 我自己的顶层:

base_model = keras.applications.vgg16.VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(im_height,im_width,channels))

但根据 documentation,仅当频道 = 3 时。 我曾想过简单地在我的图像中添加两个冗余通道,但这似乎是一种浪费 computation/could 使分类变得更糟。我也可以在三个通道中复制相同的图像,但我同样不确定它会如何执行。

VGG keras模型使用函数:keras.applications.imagenet_utils._obtain_input_shape。 此函数是为 ImageNet 数据量身定制的,因此它强制输入通道为 3。一种可能的解决方法是复制 VGG16 模块并替换行:

input_shape = _obtain_input_shape(input_shape, default_size=224, min_size=48, data_format=K.image_data_format(), include_top=include_top)

与:

input_shape = (im_height, im_width, 1)

附带说明一下,您将无法加载 ImageNet 权重,因为您的输入 space 已更改并且第一层卷积将不匹配。

Keras 预训练模型已经在彩色图像上进行了训练,如果你想发挥它们的全部能力,你应该使用彩色图像进行微调。但是,如果您有灰度图像,您仍然可以通过在三个通道上重复灰度图像来使用这些预训练模型。但显然,它不会像使用彩色图像作为输入那样好。