Skip to content
Advertisement

How to train a Keras autoencoder with custom dataset?

I am reading this tutorial in order to create my own autoencoder based on Keras. I followed the tutorial step by step, the only difference is that I want to train the model using my own images data set. So I changed/added the following code:

JavaScript

My images are normal .jpg files in RGB format. However, as soon as training starts the fit() method throws the following exception:

JavaScript

Any ideas what I am missing here?

Advertisement

Answer

Use class_mode=”input” at the flow_from_directory so returned Y will be same as X

https://github.com/tensorflow/tensorflow/blob/v2.4.1/tensorflow/python/keras/preprocessing/image.py#L867-L958

class_mode: One of “categorical”, “binary”, “sparse”, “input”, or None. Default: “categorical”. Determines the type of label arrays that are returned: – “categorical” will be 2D one-hot encoded labels, – “binary” will be 1D binary labels, “sparse” will be 1D integer labels, – “input” will be images identical to input images (mainly used to work with autoencoders). – If None, no labels are returned (the generator will only yield batches of image data, which is useful to use with model.predict()). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.

Code should end up like:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement