├── GIF └── FaceGan.gif ├── data ├── real_faces.jpg ├── face_image_data │ └── download.txt └── preprocessed_data │ └── download.txt ├── generated_faces └── fake_faces.jpg ├── models └── FaceGAN_EPOCH_150 │ └── download_pretrained_model.txt ├── preprocess_data.py └── README.md /GIF/FaceGan.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrithickcodes/Face_generation_using_DCGAN/HEAD/GIF/FaceGan.gif -------------------------------------------------------------------------------- /data/real_faces.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrithickcodes/Face_generation_using_DCGAN/HEAD/data/real_faces.jpg -------------------------------------------------------------------------------- /generated_faces/fake_faces.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hrithickcodes/Face_generation_using_DCGAN/HEAD/generated_faces/fake_faces.jpg -------------------------------------------------------------------------------- /data/face_image_data/download.txt: -------------------------------------------------------------------------------- 1 | Unzip the data and then extract it. 2 | Link to the dataset: https://www.kaggle.com/jessicali9530/celeba-dataset 3 | -------------------------------------------------------------------------------- /models/FaceGAN_EPOCH_150/download_pretrained_model.txt: -------------------------------------------------------------------------------- 1 | Download the model from the below link. 2 | Link: https://drive.google.com/drive/folders/1-j_WE-uUgYek9sy1-j9BIFIOdrV33kmU?usp=sharing 3 | -------------------------------------------------------------------------------- /data/preprocessed_data/download.txt: -------------------------------------------------------------------------------- 1 | Download the already preprocessed dataset from the below link. 2 | Link: https://drive.google.com/file/d/1lfA2wB0IUcAwTaGA_IT0IgUQQTBmYyhQ/view?usp=sharing 3 | -------------------------------------------------------------------------------- /preprocess_data.py: -------------------------------------------------------------------------------- 1 | """ 2 | To preprocess the celerity faces dataset 3 | """ 4 | import numpy as np 5 | import cv2 6 | import os 7 | from tqdm import tqdm 8 | import random 9 | 10 | 11 | def make_npy_file(datapath, savepath, image_shape, face_limit = 60_000): 12 | # faces will get appended in the below list 13 | face_images = [] 14 | # to get a progress bar 15 | face_image_files = os.listdir(datapath) 16 | random.shuffle(face_image_files) 17 | 18 | pbar = tqdm(total = face_limit) 19 | count = 0 20 | for faces in face_image_files: 21 | face_path = os.path.join(datapath, faces) 22 | pbar.update(n = 1) 23 | face = cv2.imread(face_path) 24 | face = cv2.cvtColor(face, cv2.COLOR_BGR2RGB) 25 | resizedRGBface = cv2.resize(face, image_shape, interpolation = cv2.INTER_AREA) 26 | face_images.append(resizedRGBface) 27 | count += 1 28 | if count == face_limit: 29 | break 30 | 31 | pbar.close() 32 | face_images = np.array(face_images) 33 | # saving the npy file 34 | saver_path = os.path.join(savePath, "celebrity_faces.npy") 35 | np.save(saver_path, face_images) 36 | print(f"File saved at : {savepath}") 37 | 38 | # Paths 39 | dataPath = os.path.join(os.getcwd(), "data/face_image_data/img_align_celeba") 40 | savePath = os.path.join(os.getcwd(), "data/preprocessed_data") 41 | 42 | make_npy_file(datapath = dataPath, 43 | savepath = savePath, 44 | image_shape = (64,64), 45 | face_limit= 60_000) 46 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Face Generation using DCGAN 2 | 3 | Generative modeling is an unsupervised learning task in machine learning that involves automatically discovering and learning the patterns in input data in such a way that the model can be used to generate new examples which are not in the original dataset, If you are new to GAN you can read my [blog](https://medium.com/analytics-vidhya/understanding-gans-deriving-the-adversarial-loss-from-scratch-ccd8b683d7e2) on GAN. DCGAN, or Deep Convolutional GAN, is such a network architechture that is capable of generating RGB images of size 64x64. The idea of Deep Convolutional generative adversarial network was proposed in this [paper](https://arxiv.org/abs/1511.06434v1). 4 | 5 | ## DCGAN Architecture 6 | The network architecture is shown in the below figure. 7 | 8 | 9 | 10 | The Network hyperparameters are, 11 | 12 | Face_images_for_training = 60000 13 | IMAGE_HEIGHT = 64 14 | IMAGE_WIDTH = 64 15 | IMAGE_CHANNEL = 3 16 | NOISE_SIZE = 100 17 | DISCRIMINATOR_LEARNING_RATE = 0.0002 18 | GENERATOR_LEARNING_RATE = 0.0002 19 | BATCH_SIZE = 128 20 | EPOCHS = 150 21 | BETA1 = 0.5 22 | WEIGHT_INIT_STDDEV = 0.02 23 | EPSILON = 0.00001 24 | 25 | 26 | ## Results 27 | 28 | This project uses DCGAN to generate fake face images of celebrities, the model was trained on the [CelebFaces Attributes dataset](https://www.kaggle.com/jessicali9530/celeba-dataset). The below GIF shows how the generator learns to generate a fake face over time. 29 | 30 | 31 | 32 | The model was trained for 150 epochs and then it was able to generate decent fake face images, some of them are given below. 33 | 34 | 35 | 36 | 37 | --------------------------------------------------------------------------------