title: Tensorflow ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 1)
body: <p>The input layer takes the shape of (128,128,1) i.e., single channel images:</p>
  <pre><code>inputs = keras.Input(shape=(128,128,1))
  </code></pre>
  <p>The model is fed from the generators for train and test sets as follows:</p>
  <pre><code>ds_train = tf.data.Dataset.list_files(str(PATH))
  ds_test = tf.data.Dataset.list_files(str(PATH.parent/ 'test'/'*.png'))
  
  ds_train = (ds_train.map(load_augment,num_parallel_calls=tf.data.AUTOTUNE)
              .shuffle(578)
              .batch(batch_size))
  
  ds_test = ds_test.shuffle(144).batch(batch_size)
  </code></pre>
  <p>The mapping function <code>load_augment()</code> is given below:</p>
  <pre><code>@tf.function()
  def load_augment(image_file)
    image1, image2 = load(image_file) # loads using tf.io and returns two images
    image1, image2 = resize(image1, image2, 128, 128) # resizes pairs of images
    if tf.random.uniform(()) &gt; 0.5:
       image1 = tf.image.flip_left_right(image1) #flips the images
       image2 = tf.image.flip_left_right(image2)
    image1, image2 = resize(image1, image2, img_size[0]+20, img_size[1]+20)
    image1, image2 = random_crop(image1, image2) # random-crops to get a pair of images of 128x128
    return image2, image2
  </code></pre>
  <p>The model is compiled and trained as</p>
  <pre><code>model.compile(optimizer=optimizer, loss=&quot;sparse_categorical_crossentropy&quot;)
  
  callbacks = [
      keras.callbacks.ModelCheckpoint(&quot;a_name.h5&quot;)
  ]
  
  epochs = 200
  model.fit(ds_train, epochs=epochs, 
            validation_data=ds_test, 
            callbacks=callbacks) # ds_test is the test dataset shown above
  </code></pre>
  <p>This throws the error <strong>after running for the first epoch</strong>:</p>
  <blockquote>
  <p>ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 1)</p>
  </blockquote>
  <p>I will appreciate if anyone can help.</p>
  <p><strong>More information:</strong></p>
  <p>The the functions <code>resize()</code> and <code>random_crop()</code> writes tensors as they use tf.image tools.</p>
  <p>I checked the shapes of the tensors in the generator as follows:</p>
  <pre><code>ds = ds_train.take(1)
  iter_ds = iter(ds)
  ds1 = next(iter_ds)
  ds1[0].shape # returns TensorShape([4, 128, 128, 1])
  ds1[1].shape # returns TensorShape([4, 128, 128, 1])
  </code></pre>