We can see that the batch looks like below.īelow are the functions to be used for setting the GPU. To check out the batch, we do, show_batch(train_dl) image_size = 64 batch_size = 128 stats = (0.5, 0.5, 0.5), (0.5, 0.5, 0.5) train_ds = ImageFolder(DATA_DIR, transform=T.Compose()) train_dl = DataLoader(train_ds, batch_size, shuffle=True, num_workers=3, pin_memory=True) Creating Helper Functions for our Workįunction for De-normalization: def denorm(img_tensors): return img_tensors * stats + statsįunction for Showing the Images: def show_images(images, nmax=64): fig, ax = plt.subplots(figsize=(8, 8)) ax.set_xticks() ax.set_yticks() ax.imshow(make_grid(denorm(tach()), nrow=8).permute(1, 2, 0))įunction for Showing the Batch: def show_batch(dl, nmax=64): for images, _ in dl: show_images(images, nmax) break We will also create a data loader to load the data in batches. This will ensure that pixel values are in the range (-1, 1), which is more convenient for training the discriminator. We will also resize and crop the images to 64圆4 px, and normalize the pixel values with a mean & standard deviation of 0.5 for each channel. Let’s load this dataset using the ImageFolder class from torchvision. Importing Required Libraries from import DataLoader from torchvision.datasets import ImageFolder import ansforms as T import torch from torchvision.utils import make_grid import matplotlib.pyplot as plt import torch.nn as nn %matplotlib inline from torchvision.utils import save_image from tqdm.notebook import tqdm import torch.nn.functional as F from IPython.display import Image Creating the DataLoader Exploring the Data import os DATA_DIR = ‘./animefacedataset’ print(os.listdir(DATA_DIR)) print(os.listdir(DATA_DIR+’/images’)) The dataset has a single folder called images which contains all 63,000+ images in JPG format. Once you enter those, your dataset will be downloaded in Colab. You can find the API key in the account details of your Kaggle account. You will need to have a Kaggle account for downloading this dataset as the above command will ask for your Kaggle account ID and the API key. import opendatasets as od dataset_url = ‘' od.download(dataset_url) We will download the data with the ‘opendatasets’ library. So, I will show you how it is done step-wise. I have used Google Colab for coding the problem. There are no labels here as this is an unsupervised learning technique. We’ll use the Anime Face Dataset, which consists of over 63,000 cropped anime faces. In this tutorial, we’ll train a GAN to generate images of anime characters’ faces. GANs however, can be notoriously difficult to train, and are extremely sensitive to hyperparameters, activation functions and regularization. This way both the generator and the discriminator get better at doing their jobs. Training happens in tandem: we train the discriminator for a few epochs, then train the generator for a few epochs, and repeat. The generator generates a “fake” sample given a random vector/matrix, and the discriminator attempts to detect whether a given sample is “real” (picked from the training data) or “fake” (generated by the generator). They fall under Generative Modeling which is an unsupervised learning task in ML involved in generating new content. GANs are being used in image generation, voice generation and video generation. This new data is very close in resemblance to the original data used by the networks. In this process, both of the neural networks learn a lot and develop more accurate results which can be used by us to create new, synthetic data. Generative adversarial networks (GANs), in simple terms, are two neural network players competing against each other for the sole purpose of defeating the other. Creating New Anime Faces with Generative Adversarial Networks (GANs) using PyTorch
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |