Home > Mobile >  Is it possible to use image_dataset_from_directory() with convolutional autoencoders in Keras?
Is it possible to use image_dataset_from_directory() with convolutional autoencoders in Keras?

Time:11-03

There is a similar question here which asks how to use image_dataset_from_directory() with autoencoder. Question is actually unanswered, because answer suggests using something else.

My question is, is it even possible to use image_dataset_from_directory() as input for convolutional autoencoder in Keras?

CodePudding user response:

It is definitely possible, you just have to adjust your inputs to your model beforehand:

import tensorflow as tf
import pathlib

dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)

batch_size = 32

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(28, 28),
  batch_size=batch_size)

normalization_layer = tf.keras.layers.Rescaling(1./255)

def change_inputs(images, labels):
  x = tf.image.resize(normalization_layer(images),[28, 28], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)
  return x, x

normalized_ds = train_ds.map(change_inputs)

input_img = tf.keras.Input(shape=(28, 28, 3))
x = tf.keras.layers.Flatten()(input_img)
x = tf.keras.layers.Dense(28 * 28 * 3, activation='relu')(x)
output = tf.keras.layers.Reshape(target_shape=(28, 28 ,3))(x)
autoencoder = tf.keras.Model(input_img, output)

autoencoder.compile(optimizer='adam', loss='mse')

history = autoencoder.fit(normalized_ds, epochs=2)
Found 3670 files belonging to 5 classes.
Using 2936 files for training.
Epoch 1/2
92/92 [==============================] - 4s 41ms/step - loss: 0.1538
Epoch 2/2
92/92 [==============================] - 4s 40ms/step - loss: 0.1300

Note that I used a single fully concatenated neural layer as the encoder and as the decoder, but they can easily be replaced by a CNN network. I have also reduced the images to a much smaller size for quick demonstration.

  • Related