votes up 5

The channel dimension of the inputs should be defined. Found `None`.

Package:
keras
github stars 52268
Exception Class:
ValueError

Raise code

return -1 - self.rank
    else:
      return -1

  def _get_input_channel(self, input_shape):
    channel_axis = self._get_channel_axis()
    if input_shape.dims[channel_axis].value is None:
      raise ValueError('The channel dimension of the inputs '
                       'should be defined. Found `None`.')
    return int(input_shape[channel_axis])

  def _get_padding_op(self):
    if self.padding == 'causal':
      op_padding = 'valid'
    else:
      
🙏 Scream for help to Ukraine
Today, 2nd July 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 0 votes down

When initializing a Conv2DTranspose the channel dimension of the input_shape shouldn't be none.

for example for a 128x128 RGB pictures the input shape should be;

input_shape=(128, 128, 3)

Here the last element of the the tuple i.e 3 is indicating that the data(image) has 3 channels.

But if the value of this element is set to None, the given error is raised.

i.e.

input_shape=(128128,None)

How to reproduce the error:

pipenv install tensorflow

import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2DTranspose(1, 
                                          (1,1), 
                                          strides=(2,2), 
                                          input_shape=(22,None)) # this causes the error
                                          )
model.summary()

The error (output):

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-22-dee6fd4b4f35> in <module>()
      1 import tensorflow as tf
      2 model = tf.keras.Sequential()
----> 3 model.add(tf.keras.layers.Conv2DTranspose(1, (1,1), strides=(2,2), input_shape=(2, 2,None)))
      4 model.summary()


7 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/convolutional.py in build(self, input_shape)
   1239     channel_axis = self._get_channel_axis()
   1240     if input_shape.dims[channel_axis].value is None:
-> 1241       raise ValueError('The channel dimension of the inputs '
   1242                        'should be defined. Found `None`.')
   1243     input_dim = int(input_shape[channel_axis])


ValueError: The channel dimension of the inputs should be defined. Found `None`.

How to fix:

The input shape parameter should be given a valid value.

import tensorflow as tf
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2DTranspose(1, 
                                          (1,1), 
                                          strides=(2,2), 
                                          input_shape=(22,3)) # fixed
                                          )
model.summary()

Expected output:

Model: "sequential_14"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_transpose_4 (Conv2DTr (None, 4, 4, 1)           4         
=================================================================
Total params: 4
Trainable params: 4
Non-trainable params: 0
_________________________________________________________________

Jun 23, 2021 kellemnegasi answer
kellemnegasi 30.0k

Add a possible fix

Please authorize to post fix