Package: torch-vision
torch-visionThis repository consists of:vision.datasets: Data loaders for popular vision
datasetsvision.models: Definitions for popular model
architectures, such as AlexNet, VGG, and ResNet and pre-trained
models.vision.transforms: Common image transformations
such as random crop, rotations etc.vision.utils: Useful stuff such as saving tensor (3 x H
x W) as image to disk, given a mini-batch creating a grid of images,
etc.InstallationBinaries:condainstalltorchvision-chttps://conda.anaconda.org/t/6N-MsQ4WZ7jo/soumithFrom Source:pipinstall-rrequirements.txtpipinstall.DatasetsThe following dataset loaders are available:COCO (Captioning and Detection)LSUN ClassificationImageFolderImagenet-12CIFAR10 and CIFAR100Datasets have the API: -__getitem__-__len__They all subclass
fromtorch.utils.data.DatasetHence, they can all be multi-threaded
(python multiprocessing) using standard torch.utils.data.DataLoader.For example:torch.utils.data.DataLoader(coco_cap, batch_size=args.batchSize, shuffle=True, num_workers=args.nThreads)In the constructor, each dataset has a slightly different API as needed,
but they all take the keyword args:transform- a function that takes in an image and returns a
transformed versioncommon stuff likeToTensor,RandomCrop, etc. These can be
composed together withtransforms.Compose(see transforms section
below)target_transform- a function that takes in the target and
transforms it. For example, take in the caption string and return a
tensor of word indices.COCOThis requires theCOCO API to be
installedCaptions:dset.CocoCaptions(root="dirwhere images are",annFile="jsonannotation file", [transform, target_transform])Example:importtorchvision.datasetsasdsetimporttorchvision.transformsastransformscap=dset.CocoCaptions(root='dir where images are',annFile='json annotation file',transform=transforms.ToTensor())print('Number of samples: ',len(cap))img,target=cap[3]# load 4th sampleprint("Image Size: ",img.size())print(target)Output:Number of samples: 82783
Image Size: (3L, 427L, 640L)
[u'A plane emitting smoke stream flying over a mountain.',
u'A plane darts across a bright blue sky behind a mountain covered in snow',
u'A plane leaves a contrail above the snowy mountain top.',
u'A mountain that has a plane flying overheard in the distance.',
u'A mountain view with a plume of smoke in the background']Detection:dset.CocoDetection(root="dirwhere images are",annFile="jsonannotation file", [transform, target_transform])LSUNdset.LSUN(db_path,classes='train',[transform, target_transform])db_path = root directory for the database filesclasses =‘train’ - all categories, training set‘val’ - all categories, validation set‘test’ - all categories, test set[‘bedroom_train’, ‘church_train’, …] : a list of categories to
loadCIFARdset.CIFAR10(root, train=True, transform=None, target_transform=None, download=False)dset.CIFAR100(root, train=True, transform=None, target_transform=None, download=False)root: root directory of dataset where there is foldercifar-10-batches-pytrain:True= Training set,False= Test setdownload:True= downloads the dataset from the internet and
puts it in root directory. If dataset already downloaded, does not do
anything.ImageFolderA generic data loader where the images are arranged in this way:root/dog/xxx.png
root/dog/xxy.png
root/dog/xxz.png

root/cat/123.png
root/cat/nsdf3.png
root/cat/asd932_.pngdset.ImageFolder(root="rootfolder path", [transform, target_transform])It has the members:self.classes- The class names as a listself.class_to_idx- Corresponding class indicesself.imgs- The list of (image path, class-index) tuplesImagenet-12This is simply implemented with an ImageFolder dataset.The data is preprocessedas described
hereHere is an
example.ModelsThe models subpackage contains definitions for the following model
architectures:AlexNet: AlexNet variant from
the “One weird trick” paper.VGG: VGG-11, VGG-13, VGG-16,
VGG-19 (with and without batch normalization)ResNet: ResNet-18, ResNet-34,
ResNet-50, ResNet-101, ResNet-152You can construct a model with random weights by calling its
constructor:importtorchvision.modelsasmodelsresnet18=models.resnet18()alexnet=models.alexnet()We provide pre-trained models for the ResNet variants and AlexNet, using
the PyTorchmodel zoo.
These can be constructed by passingpretrained=True:python  import torchvision.models as models  resnet18 = models.resnet18(pretrained=True)  alexnet = models.alexnet(pretrained=True)TransformsTransforms are common image transforms. They can be chained together
usingtransforms.Composetransforms.ComposeOne can compose several transforms together. For example.transform=transforms.Compose([transforms.RandomSizedCrop(224),transforms.RandomHorizontalFlip(),transforms.ToTensor(),transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225]),])Transforms on PIL.ImageScale(size, interpolation=Image.BILINEAR)Rescales the input PIL.Image to the given ‘size’. ‘size’ will be the
size of the smaller edge.For example, if height > width, then image will be rescaled to (size *
height / width, size) - size: size of the smaller edge - interpolation:
Default: PIL.Image.BILINEARCenterCrop(size)- center-crops the image to the given sizeCrops the given PIL.Image at the center to have a region of the given
size. size can be a tuple (target_height, target_width) or an integer,
in which case the target will be of a square shape (size, size)RandomCrop(size, padding=0)Crops the given PIL.Image at a random location to have a region of the
given size. size can be a tuple (target_height, target_width) or an
integer, in which case the target will be of a square shape (size, size)
Ifpaddingis non-zero, then the image is first zero-padded on each
side withpaddingpixels.RandomHorizontalFlip()Randomly horizontally flips the given PIL.Image with a probability of
0.5RandomSizedCrop(size, interpolation=Image.BILINEAR)Random crop the given PIL.Image to a random size of (0.08 to 1.0) of the
original size and and a random aspect ratio of 3/4 to 4/3 of the
original aspect ratioThis is popularly used to train the Inception networks - size: size of
the smaller edge - interpolation: Default: PIL.Image.BILINEARPad(padding, fill=0)Pads the given image on each side withpaddingnumber of pixels, and
the padding pixels are filled with pixel valuefill. If a5x5image is padded withpadding=1then it becomes7x7Transforms on torch.*TensorNormalize(mean, std)Given mean: (R, G, B) and std: (R, G, B), will normalize each channel of
the torch.*Tensor, i.e. channel = (channel - mean) / stdConversion TransformsToTensor()- Converts a PIL.Image (RGB) or numpy.ndarray (H x W x
C) in the range [0, 255] to a torch.FloatTensor of shape (C x H x W)
in the range [0.0, 1.0]ToPILImage()- Converts a torch.*Tensor of range [0, 1] and
shape C x H x W or numpy ndarray of dtype=uint8, range[0, 255] and
shape H x W x C to a PIL.Image of range [0, 255]Generic TransofrmsLambda(lambda)Given a Python lambda, applies it to the inputimgand returns it.
For example:transforms.Lambda(lambdax:x.add(10))Utilsmake_grid(tensor, nrow=8, padding=2)Given a 4D mini-batch Tensor of shape (B x C x H x W), makes a grid of
imagessave_image(tensor, filename, nrow=8, padding=2)Saves a given Tensor into an image file.If given a mini-batch tensor, will save the tensor as a grid of images.