Building A CycleGAN Model Using Tensorflow 2
Building A CycleGAN Model Using Tensorflow 2
Search Medium
Save
• With TFRecord, you can combine multiple datasets into one dataset.
For example — in this work, we combine multiple images into one
TFRecord. This combined record is well integrated into the data
loading and preprocessing functionalities of tf.data.Datasets . This is
specially helpful when loading a really big dataset — instead of loading
the entire dataset in the memory, the dataset library can load only the
necessary portion of the TFRecord for processing.
feature = {
‘image’: _bytes_feature(image),
}
def _bytes_feature(value):
“””Returns a bytes_list from a string / byte.”””
if isinstance(value, type(tf.constant(0))):
value = value.numpy()
return
tf.train.Feature(bytes_list=tf.train.BytesList(value=
[value]))
Once we have the TFRecord, how do we know that the record was actually
created correctly? We can count the number of items in each of our
TFRecords (remember — we have multiple images in one TFRecord). We
can also visualize the entire dataset.
FILENAMES = tf.io.gfile.glob('cat*.tfrec')
print(f'TFRecords files: {FILENAMES}')
print(f'Created image samples:
{count_data_items(FILENAMES)}')
display_samples(load_dataset(FILENAMES,
*IMAGE_SIZE).batch(1), 2, 5)
For the end-to-end code on custom dataset creation, please refer here
In our work, we will use FID score. Frechet Inception Distance (FID)
measures the distance between the features of generated image and real
image. The lower the FID, the better. If FID is 0, it means two images are
same
• Use the pretrained Inception V3 model, remove the last layer from it
• Generate feature vectors of the two images (generated and real). The
vector size will be 2,048
If you want to learn more on how to calculate FID score, please refer here
After training the model, we want to save it (assuming we are happy with
the train/validation loss and evaluation). While saving the model, we want
to make sure we save the entire model.
• During inference, you don’t need the model architecture code. This
way, you will have a clean inference logic and faster development
cycle.
• If you want to convert the model for edge devices (TFLite) or want to
run the model in browser with Tensorflow.js, you need to have the
entire model
There are a couple of ways to save the entire model. For Tensorflow 2, I
prefer SavedModel format. You can use Keras’s model.save method or
Tensorflow’s tf.keras.models.save_model method. In the model.save()
Now that we have our model saved, we can write the inference logic and
deploy it for people to use. For this project, I have deployed the model in
Hugging Face Space here. If you visit that link, you will see something like
this —
There, you can upload your cat photo (or any other pet), press the submit
button and wait 10 sec to see the cat art something like below —
Photo to art
You may realize that I need a lot more iterations to improve the quality of
the art. But nonetheless, this is a fun app to play with!
Details on how to deploy a model in Hugging Face Space can be found
here.
References:
1. https://developers.google.com/machine-learning/gan
4. Kaggle
Deep CycleGAN
Learning Example
Tensor Flow https://www.kaggle.com/amyjang/monet-
Generative Adversarial Hugging Face
cyclegan-tutorial
Cats
5. How to calculate FID score https://machinelearningmastery.com/how-
to-implement-the-frechet-inception-distance-fid-from-scratch/