How to use Tensorflow's cycleGAN on my own dataset

Viewed 466

I am trying to use the code there: https://www.tensorflow.org/tutorials/generative/cyclegan on my own data.

In this tutorial a Dataset object is created with this code:

dataset, metadata = tfds.load('cycle_gan/horse2zebra',
                          with_info=True, as_supervised=True)

train_horses, train_zebras = dataset['trainA'], dataset['trainB']
test_horses, test_zebras = dataset['testA'], dataset['testB']

I cannot figure out how to create a similar object from my own data which are in folder like:

data/trainA
data/trainB
data/testA
data/testB

Is there a method that would looks like the flow_from_directory method in order to load my data with the same format as tfds.load(...)

Something that would looks like

train_horses = foo("data/trainA")
1 Answers

you can create a dataset from a directory with an ImageDataGenerator and even perform preprocessing steps such a scaling, rotating and many more. Then use flow_from_directory() and pass the returned DirectoryIterator object to your model.fit()

Example:

train_gen = tf.keras.preprocessing.image.ImageDataGenerator(
    rescale = 1./255,
    horizontal_flip=True,
    zoom_range=0.4,
    rotation_range = 35,
    fill_mode="reflect"
    )

DIR_TRAIN = "data/trainA"
train_generator = train_gen.flow_from_directory(DIR_TRAIN)

test_gen = tf.keras.preprocessing.image.ImageDataGenerator(
    rescale = 1./255,
    )
DIR_TEST = "data/testA"
train_generator = train_gen.flow_from_directory(DIR_TEST)

model.fit(train_gen,
    steps_per_epoch=20,
    epochs=100,
    validation_data=test_gen,
    validation_steps=100
    )

In your case you would need to perform this procedure for both training and test sets.

Related