Skip to main content

GANs training on CelebA report

I used GANs to make fake faces, trained on the celebA dataset. My goal is to make them as conviencing as possible.
Last updated: May 6, 2022
CelebA dataset avialable here.
The images are cropped to 112x128 pixels, the idea is the keep the original portrait proportion with numbers easily divisible for convolutive layers.


First sweep

Configuration

The first sweep was a random search over (about) 20 runs, with the following configuration

parameters_dic = {
    'epochs':{'value':5},
    'batch_size':{'values':[64,128]},
    'latent_dim':{'values':[50,100,200]},
    'gen_struc':{'values':[0,1,2]},
    'disc_struc':{'values':[0,1,2]},
}

With 'gen_struc' and 'dic_struc' being 3 different configurations of the generatorand the discriminator, they are as follow


def define_discriminator(structure,in_shape=INPUT_SHAPE) :
  model = Sequential()
  if structure == 0 :
    model.add(Conv2D(128, (3,3), strides=(2,2), padding = 'same', input_shape=in_shape))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(128, (3,3), strides=(2,2), padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Flatten())
    model.add(Dropout(0.4))
    model.add(Dense(1,activation="sigmoid"))

  elif structure == 1 :
    model.add(Conv2D(256, (3,3), strides=(2,2), padding = 'same', input_shape=in_shape))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(256, (3,3), strides=(2,2), padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Flatten())
    model.add(Dropout(0.4))
    model.add(Dense(1,activation="sigmoid"))

  elif structure == 2 :
    model.add(Conv2D(128, (3,3), strides=(2,2), padding = 'same', input_shape=in_shape))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(128, (3,3), strides=(2,2), padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Conv2D(128, (3,3), strides=(2,2), padding='same'))
    model.add(LeakyReLU(alpha=0.2))
    model.add(Flatten())
    model.add(Dropout(0.4))
    model.add(Dense(1,activation="sigmoid"))

  opt = Adam(learning_rate=0.0002, beta_1=0.5)
  model.compile(loss="binary_crossentropy", optimizer=opt, metrics=["accuracy"])
  return model

And

def define_generator(structure,latent_dim):    #latent_dim is the dimension of the latent vector (e.g., 100)
	model = Sequential()
	
	if structure == 0 :
		n_nodes = 64 * 28 * 32  
		model.add(Dense(n_nodes, input_dim=latent_dim)) #Dense layer so we can work with 1D latent vector
		model.add(LeakyReLU(alpha=0.2))
		model.add(Reshape((28, 32, 64)))  
		model.add(Conv2DTranspose(64, (4,4), strides=(2,2), padding='same'))
		model.add(LeakyReLU(alpha=0.2))
		model.add(Conv2DTranspose(64, (4,4), strides=(2,2), padding='same'))
		model.add(LeakyReLU(alpha=0.2))
	
elif structure == 1 : 
		n_nodes = 512 * 28 * 32  
		model.add(Dense(n_nodes, input_dim=latent_dim)) #Dense layer so we can work with 1D latent vector
		model.add(LeakyReLU(alpha=0.2))
		model.add(Reshape((28, 32, 512)))  
		model.add(Conv2DTranspose(256, (4,4), strides=(2,2), padding='same'))
		model.add(LeakyReLU(alpha=0.2))
		model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
		model.add(LeakyReLU(alpha=0.2))

	elif structure == 2 : 
		n_nodes = 128 * 14 * 16  
		model.add(Dense(n_nodes, input_dim=latent_dim)) #Dense layer so we can work with 1D latent vector
		model.add(LeakyReLU(alpha=0.2))
		model.add(Reshape((14,16, 128)))  
		model.add(Conv2DTranspose(128, (4,4), strides=(2,2), padding='same'))
		model.add(LeakyReLU(alpha=0.2))
		model.add(Conv2DTranspose(64, (4,4), strides=(2,2), padding='same'))
		model.add(LeakyReLU(alpha=0.2))
		model.add(Conv2DTranspose(64, (4,4), strides=(2,2), padding='same'))
		model.add(LeakyReLU(alpha=0.2))
	# generate
	model.add(Conv2D(3, (8,8), activation='tanh', padding='same'))
	return model


Measures

My biggest difficulty in working with GANs is a the lack of easy way to measure how good a GAN is doing, this is why the sweep fonction of wandb is very useful, because it allows visual evaluation of GANs.To evaluate the GANs, I choose to select 5 vectors of latent points at the beginning of each run. At the end of each epoch, I generate 5 images with the generator and the 5 latent points. By doing that, I keep the same vectors between epochs, so we can see the evolution of the GAN over the same "face".Although I could have chose to use the same latent vectors for every GANs, I wanted to try runs with different latent dimensions making it impossible to use the same 5 vectors in every run. I intend to do that when possible, but it wasn't the goal at this time.
I also measured the average training time over an epoch for each run and the 3 losses (generator, discriminator real, discriminator fake) that I put in a table at the end of each epoch.
Note : I struggled to log the losses, because I wanted to have log for each bach, but also for each epoch, and the wandb.log seems to only allow logging at one level of loop at the time. So I put everything in a table and logged the loss at the end of the epoch, but by doing so, it seems that the losses weren't logged properly and I couldn't access the smoothing function of wandb to make for clearer losses. Note 2 : Because of a coding mistake that I made, the first 10 runs have an average time of 0, thankfuly, I've learned that wandb keeps track of the duration of the last epoch in the summary, which allowed me to have an idea of the duration of the runs.

Durations

The goal of the sweep was not to make conveincing pictures, but rather to determine which structure is the more efficient, meaning provides the highest quality pictures with the lowest amount of training time.

We can see on the both graph that the discriminator sctructure is the one impacting the training time the most. At least, with the 3 structure I've chosen for the discriminator, the structure 1 is the only one having a significant impact on the duration of the epoch. Wereas the structures I've choosen for the generator don't seem to impact the training time so much.




41 selected runs


Samples

To measure the performances of the run, I will give each run a point if, on the 5th epoch, 3 out of 5 pictures have the following :


  • Clear eyes
  • Clear mouth
  • Distinction between face and background
  • Realistic face tone

22 selected runs