Reputation: 1000
I am trying to use Transfer learning with VGG16. I am using Keras. But I got error on
vgg = vgg16.VGG16(include_top=False, weights='imagenet', input_shape=(IMG_SIZE, IMG_SIZE, 1))
Any help what is wrong ?
Note: IMG_SIZE
= 200
The trace of error is
---------------------------------------------------------------------------
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)`
Upvotes: 5
Views: 17610
Reputation: 536
Supposedly you want to use the VGG16 pre-trained model on ImageNet dataset you should process your input image to be RGB instead of grayscale. Is it possible to provide how you load your images?
For example if you use opencv you should probably do something like this:
rgb_img = cv2.cvtColor(grayscale_image,cv2.COLOR_GRAY2RGB)
If you have your image as a Tensor object then you can do something like:
rgb_tensor = tf.image.grayscale_to_rgb(grayscale_tensor)
or
rgb_tensor = tf.concat([grayscale_tensor, grayscale_tensor, grayscale_tensor], axis=-1)
since, if you have a grayscale image, the rgb equivalent is expressing the grayscale values on each RGB channel.
Upvotes: 0
Reputation: 1
just add before downloading VGG16 pretrained model.
tf.keras.backend.set_image_data_format("channels_last")
Changing channel position from first to last will solve the error.
Upvotes: 0
Reputation: 21
You're trying to use 1 channel image with VGG16 net, whereas you must use 3.
This'll solve your issue:
vgg = vgg16.VGG16(include_top=False, weights=None, input_shape=(IMG_SIZE, IMG_SIZE, 3))
Upvotes: 1
Reputation: 464
You cannot use the imagenet weights whith single channel images. This may solve your issue:
vgg = vgg16.VGG16(include_top=False, weights=None, input_shape=(IMG_SIZE, IMG_SIZE, 1))
Upvotes: 3