O'Reilly logo

Python Deep Learning Cookbook by Indra den Bakker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. First, we need to import all the necessary libraries:
import osfrom os import listdirfrom os.path import joinimport numpy as npimport randomimport matplotlib.pyplot as pltimport torchvisionfrom torchvision import transformsimport torchvision.datasets as datasetsimport torchimport torch.nn as nnimport torch.nn.functional as Fimport torch.optim as optimfrom torch.autograd import Variable
  1. In this recipe, we will be using the CelebA Faces dataset. We will import the data with a custom function in PyTorch:
class data_from_dir(data.Dataset):     def __init__(self, image_dir, transform=None):        super(DatasetFromFolder, self).__init__()        self.image_dir = image_dir        self.image_filenames = [ x for x in listdir(image_dir) if  is_image_file(x)] ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required