Problem with Tensorflow Dataset in Autoencoder for images on hard drive


I’m confused on the dataset construction in Tensorflow and can’t get my autoencoder to fit my data. I keep getting errors and am hoping someone can look at this and see where I’m going wrong. I tried fitting just the data and not the batch iterator and got the same error. I even tried constructing my own dataset as a numpy array but I don’t understand completely what it’s looking for. So this is where I’m at currently:

import os
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from matplotlib.image import imread
import matplotlib.image as mpimg
import cv2
# Technically not necessary in newest versions of jupyter
%matplotlib inline

from google.colab import drive

my_data_dir = '/content/gdrive/MyDrive/Skyrmion Vision/testFiles/train/'
images = os.listdir(my_data_dir)

data = tf.keras.utils.image_dataset_from_directory('/content/gdrive/MyDrive/Skyrmion Vision/testFiles/train/',batch_size=1,image_size=(171,256))

data_iterator = data.as_numpy_iterator()
batch =

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense,Flatten,Reshape
from tensorflow.keras.optimizers import SGD

encoder = Sequential()

decoder = Sequential()

autoencoder = Sequential([encoder,decoder])

This gives me an error which isn’t that clear to me what needs to be fixed. Clearly there is a shape error?

Epoch 1/5
ValueError                                Traceback (most recent call last)
<ipython-input-67-aa659ef4ed20> in <module>
----> 1,batch,epochs=5)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ in autograph_handler(*args, **kwargs)
   1145           except Exception as e:  # pylint:disable=broad-except
   1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
   1148             else:
   1149               raise

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1010, in step_function  **
        outputs =, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 859, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/", line 200, in assert_input_compatibility
        raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'

    ValueError: Layer "sequential_2" expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 171, 256, 3) dtype=float32>, <tf.Tensor 'IteratorGetNext:1' shape=(None,) dtype=int32>]```


You can try:

...[0],batch[0], epochs=5)

since your batch of data is a tuple of images and labels and you are actually just interested in the images. Otherwise just filter out the labels and feed your dataset to your model:

data = x, y: (x, x)), epochs=5)

Oh and your two last layers should be:

decoder.add(Dense(171*256 * 3,activation='sigmoid'))

decoder.add(Reshape([171,256, 3]))

since you are working with 3-channel images. Your encoder also needs the input_shape:

encoder.add(Flatten(input_shape=[171,256, 3]))

See also the SO thread Is it possible to use image_dataset_from_directory() with convolutional autoencoders in Keras? for more information.

Answered By – AloneTogether

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published