torchvision库
torchvision是独立于pytorch的关于图像操作的一些方便工具库。
torchvision的详细介绍在:https://pypi.org/project/torchvision/
torchvision主要包括一下几个包:
vision.datasets : 几个常用视觉数据集,可以下载和加载,这里主要的高级用法就是可以看源码如何自己写自己的Dataset的子类
vision.models : 流行的模型,例如 AlexNet, VGG, ResNet 和 Densenet 以及 与训练好的参数。
vision.transforms : 常用的图像操作,例如:随机切割,旋转,数据类型转换,图像到tensor ,numpy 数组到tensor , tensor 到 图像等。
vision.utils : 用于把形似 (3 x H x W) 的张量保存到硬盘中,给一个mini-batch的图像可以产生一个图像格网。
torchvision.datasets数据集
数据集 torchvision.datasets
包括以下数据集:
MNIST
Fashion-MNIST
KMNIST
EMNIST
FakeData
COCO
Captions
Detection
LSUN
ImageFolder
DatasetFolder
Imagenet-12
CIFAR
STL10
SVHN
PhotoTour
SBU
Flickr
VOC
Cityscapes
SBD
数据集有 API: - __getitem__ -和__len__ 他们都是 torch.utils.data.Dataset的子类。这样我们在实现我们自己的Dataset数据集的时候至少要实现上边两个方法. 因此, 他们可以使用torch.utils.data.DataLoader里的多线程处理 (python multiprocessing) 。
例如:
torch.utils.data.DataLoader(coco_cap, batch_size=args.batchSize, shuffle=True, num_workers=args.nThreads)
- transform - a function that takes in an image and returns a transformed version
- common stuff like ToTensor, RandomCrop, etc. These can be composed together with transforms.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. 输入为 target, 返回转换后的 target
在构造上每个数据集的API有一些轻微的差异,但是都包含以下参数:
transform - 接受一个图像返回变换后的图像的函数
常用的操作如 ToTensor, RandomCrop等. 他们可以通过transforms.Compose被组合在一起。 (见以下transforms 章节)
target_transform - 一个对目标值进行变换的函数。例如,输入一个图片描述,返回一个编码后的张量(a tensor of word indices)。
模型部分torchvision.models
models 子包含了以下的模型框架:
AlexNet
VGG
ResNet
Inception v3
GoogLeNet
这里对于每种模型里可能包含很多子模型,比如Resnet就有 34,51,101,152不同层数。
这些成熟的模型的意义就是你可以在torchvision的安装路径下找到 可以通过命令 print(torchvision.models.file) 我这边输出为:
‘/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/torchvision/models/init.py’
随机初始化参数
你可以用随机参数初始化一个模型:
import torchvision.models as models
resnet18 = models.resnet18()
alexnet = models.alexnet()
vgg16 = models.vgg16()
squeezenet = models.squeezenet1_0()
使用预训练的模型进行参数初始化.
我们提供了预训练的ResNet的模型参数,以及 SqueezeNet 1.0 and 1.1, and AlexNet, 使用 PyTorch model zoo. 可以在构造函数里添加 pretrained=True:
import torchvision.models as models
resnet18 = models.resnet18(pretrained=True)
alexnet = models.alexnet(pretrained=True)
squeezenet = models.squeezenet1_0(pretrained=True)
所有的预训练模型期待输入同样标准化的数据,例如mini-baches 包括形似(3HW)的3通道的RGB图像,H,W最少是224。
图像的范围必须在[0,1]之间,然后使用 mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225] 进行标准化。
相关的例子在: the imagenet example here https://github.com/pytorch/examples/blob/42e5b996718797e45c46a25c55b031e6768f8440/imagenet/main.py#L89-L101
变换torchvision.transforms
变换(Transforms)是常用的图像变换。可以通过 transforms.Compose进行连续操作:
transforms.Compose
你可以组合几个变换在一起,例如:
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 ]),
])
更多的变换,详细参考博文:Pytorch:transforms的二十二个方法