【问题标题】:ValueError: The input must have 3 channels; got `input_shape=(200, 200, 1)`ValueError:输入必须有3个通道;得到了`input_shape=(200, 200, 1)`
【发布时间】:2021-01-24 07:46:55
【问题描述】:

我正在尝试将迁移学习与 VGG16 一起使用。我正在使用 Keras。但我得到了错误

vgg = vgg16.VGG16(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 1))

有什么问题可以帮忙吗?

注意:IMG_SIZE = 200

错误的痕迹是

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-1b17094c93e2> in <module>
      3 import keras
      4 
----> 5 vgg = vgg16.VGG16(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 1))
      6 
      7 output = vgg.layers[-1].output

c:\users\hiteshsom\documents\deepanshu_q2\env\lib\site-packages\tensorflow\python\keras\applications\vgg16.py in VGG16(include_top, weights, input_tensor, input_shape, pooling, classes, classifier_activation)
    124                      ' as true, `classes` should be 1000')
    125   # Determine proper input shape
--> 126   input_shape = imagenet_utils.obtain_input_shape(
    127       input_shape,
    128       default_size=224,

c:\users\hiteshsom\documents\deepanshu_q2\env\lib\site-packages\tensorflow\python\keras\applications\imagenet_utils.py in obtain_input_shape(input_shape, default_size, min_size, data_format, require_flatten, weights)
    363           raise ValueError('`input_shape` must be a tuple of three integers.')
    364         if input_shape[-1] != 3 and weights == 'imagenet':
--> 365           raise ValueError('The input must have 3 channels; got '
    366                            '`input_shape=' + str(input_shape) + '`')
    367         if ((input_shape[0] is not None and input_shape[0] < min_size) or

ValueError: The input must have 3 channels; got `input_shape=(200, 200, 1)`

【问题讨论】:

  • (200, 200, 1) 与 (200, 200, 3) 不同
  • 但是我的输入图像尺寸是 (200, 200, 1)。我有灰度图像
  • 您使用的是在 ImageNet 上预训练的模型,即颜色数据(3 通道)。您不能将此模型用于 1 通道数据。
  • 这能回答你的问题吗? Neural network doesn't accept grayscale images

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


【解决方案1】:

您不能对单通道图像使用 imagenet 权重。这可能会解决您的问题:

vgg = vgg16.VGG16(include_top=False, weights=None, input_shape=(IMG_SIZE, IMG_SIZE, 1))

【讨论】:

    猜你喜欢
    • 2020-07-05
    • 2018-10-28
    • 1970-01-01
    • 2021-04-08
    • 1970-01-01
    • 2022-12-22
    • 2021-08-14
    • 2022-01-25
    • 1970-01-01
    相关资源
    最近更新 更多