09.05.2023 Views

pdfcoffee

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Generative Adversarial Networks

CycleGAN in TensorFlow 2.0

In the last section of this chapter we will implement a CycleGAN in TensorFlow

2.0. The CycleGAN requires a special dataset, a paired dataset, from one domain

of images to another domain. So, besides the necessary modules, we will use

tensorflow_datasets as well:

import tensorflow as tf

from tensorflow.keras import Model

from tensorflow.keras.losses import mean_squared_error, mean_absolute_

error

import os

import time

import matplotlib.pyplot as plt

import numpy as np

import tensorflow_datasets as tfds

TensorFlow's Dataset API contains a list of datasets. It has many paired datasets for

CycleGANs, such as horse to zebra, apples to oranges, and so on. You can access the

complete list here: https://www.tensorflow.org/datasets/catalog/cycle_gan.

For our code we will be using summer2winter_yosemite, which contains images

of Yosemite (USA) in summer (Dataset A) and winter (Dataset B). We will train the

CycleGAN to convert an input image of summer to winter and vice versa. Let us

load the data and get train and test images:

dataset, metadata = tfds.load('cycle_gan/summer2winter_yosemite',

with_info=True, as_supervised=True)

train_A, train_B = dataset['trainA'], dataset['trainB']

test_A, test_B = dataset['testA'], dataset['testB']

We need to set some hyperparameters:

BUFFER_SIZE = 1000

BATCH_SIZE = 1

IMG_WIDTH = 256

IMG_HEIGHT = 256

EPOCHS = 50

[ 218 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!