Problem with Tensorflow Dataset in Autoencoder for images on hard drive

Question:

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
drive.mount('/content/gdrive')

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 = data_iterator.next()

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

encoder = Sequential()
encoder.add(Flatten(input_shape=[171,256]))
encoder.add(Dense(400,activation='relu'))
encoder.add(Dense(200,activation='relu'))
encoder.add(Dense(100,activation='relu'))
encoder.add(Dense(50,activation='relu'))
encoder.add(Dense(25,activation='relu'))

decoder = Sequential()
decoder.add(Dense(50,input_shape=[25],activation='relu'))
decoder.add(Dense(100,activation='relu'))
decoder.add(Dense(200,activation='relu'))
decoder.add(Dense(400,activation='relu'))
decoder.add(Dense(171*256,activation='sigmoid'))

decoder.add(Reshape([171,256]))
autoencoder = Sequential([encoder,decoder])
autoencoder.compile(loss='binary_crossentropy',optimizer=SGD(lr=1.5),metrics=['accuracy'])
autoencoder.fit(batch,batch,epochs=5)

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 autoencoder.fit(batch,batch,epochs=5)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py 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/training.py", line 1021, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", 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>]```

Asked By: Joseph

||

Answers:

You can try:

...
autoencoder.fit(batch[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 = data.map(lambda x, y: (x, x))
autoencoder.fit(data, 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