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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.