【问题标题】:How to replace the input channel shape from (224, 224, 3) to (224, 224, 1) in VGG16?如何将 VGG16 中的输入通道形状从 (224, 224, 3) 替换为 (224, 224, 1)?
【发布时间】:2019-09-12 20:53:53
【问题描述】:

我正在使用 VGG16 进行迁移学习。我的图像是灰度的。因此,我需要将 Vgg16 的输入通道形状从 (224, 224, 3) 更改为 (224, 224, 1)。我尝试了以下代码并得到了错误:

TypeError: build() takes from 1 to 2 positional arguments but 4 were given

谁能帮助我我哪里做错了?

vgg16_model= load_model('Fetched_VGG.h5')
vgg16_model.summary()

# transform the model to Sequential
model= Sequential()
for layer in vgg16_model.layers[1:-1]:
    model.add(layer)

# Freezing the layers (Oppose weights to be updated)
for layer in model.layers:
    layer.trainable = False

model.build(224,224,1)
model.add(Dense(2, activation='softmax', name='predictions'))

【问题讨论】:

  • 问题与deeplearning4j 无关 - 请不要向无关标签发送垃圾邮件(已删除)。

标签: tensorflow machine-learning keras deep-learning vgg-net


【解决方案1】:

你不能,即使你去掉输入层,这个模型有一个已经编译的图,你的第一个 conv 层需要一个具有 3 个通道的输入。我不认为有一个简单的解决方法可以让它接受 1 个频道(如果有的话)。

您需要在三维中重复您的数据,并在所有 3 个波段中拥有相同的灰度图像,而不是 RGB,这样就可以了。

如果您的图像的形状为:(224,224,1):

import numpy as np
gray_image_3band = np.repeat(gray_img, repeats = 3, axis = -1)

如果您的图像的形状为:(224,224)

gray_image_3band = np.repeat(gray_img[..., np.newaxis], repeats = 3, axis = -1)

您不再需要以这种方式调用 model.build(),保留输入层。但如果你想调用它,你需要像这样将形状作为元组传递:

model.build( (224, 224, 1) ) # this is correct, notice the parentheses

【讨论】:

  • 使用ImageDataGenerator().flow_from_directory批量加载图片时如何在三维重复数据?例如,我像这样加载数据:test_batches = ImageDataGenerator().flow_from_directory(test_path, target_size=(224, 224), classes=['Benign', 'Malignant'], batch_size=7, color_mode="grayscale")。在这种情况下,数据以灰度加载到批次中。当我调查''test_batches.image_shape''时加载的图像形状是(224,224,1)。
  • 像这样遍历你的生成器:for x_batch, y_batch in your_generator, and in the for loop apply the np.repeat to your images,如果你不想在索引过程中混淆自己或使用地图功能,只需使用 np.zeros() 制作空占位符,例如,批量处理图像并放入占位符。虽然这对于训练来说非常不够,但您可以在训练之前将图像更改为适当的大小并将它们保存在它们的目录中,然后在它们上使用生成器。
  • 我使用 matlab 图形工具检查了我的图像。例如,对于图像,通道显示为 (0.422,0.422,0.422)。因此,图像是一个 3 通道灰度图像。在那种情况下,我不需要在训练之前更改图像,对吗?我只需要在批量加载图像时消除参数 color_mode="grayscale" ,不是吗?我以这种方法批量加载图像,输入形状大小为 (224,224,3)。
  • 是的,如果它已经扩展到 3 维图像,您不需要做任何额外的处理,只需将它们视为普通 RGB 图像并将它们提供给模型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2019-11-21
  • 2020-01-11
  • 2021-02-24
  • 1970-01-01
  • 2022-06-15
相关资源
最近更新 更多