How to do it...

Run the following steps to train a GAN on the anime faces using PyTorch and to generate new realistic anime face images:

  1. Define the following function to make the PyTorch data loader apply transform to resize the images of a given batch size from a given directory, and then return them using a Python generator:
def get_data_loader(batch_size, image_size, data_dir='anime/'):    image_transforms = transforms.Compose(\                    [transforms.Resize(image_size), \                    transforms.ToTensor(), ])   indices = np.random.choice(63565, 50000) # get 50k random                 samples   data_loader = torch.utils.data.DataLoader( \                    datasets.ImageFolder(data_dir, \                    transform=image_transforms), \                    sampler=SubsetRandomSampler(indices),                                          batch_size=batch_size) return data_loader ...

Get Python Image Processing Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.