torchvision是独立于pytorch的关于图像操作的一些方便工具库。
torchvision的详细介绍在:https://pypi.org/project/torchvision/
torchvision主要包括一下几个包:
Anaconda:
conda install torchvision -c pytorch
pip:
pip install torchvision
由于此包是配合pytorch的对于图像处理来说必不可少的,
对于以后要用的torch来说一站式的anaconda是首选,毕竟人生苦短。
(anaconda + vscode +pytorch 非常好用) 值得推荐!
包括以下数据集:
数据集有 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)
在构造上每个数据集的API有一些轻微的差异,但是都包含以下参数:
每个数据集都有类似参数,所以很容易通过一个掌握其他全部。
dset.MNIST(root, train=True, transform=None, target_transform=None, download=False)
root:数据的目录,里边有 processed/training.pt 和processed/test.pt 的内容
train: True -使用训练集, False -使用测试集.
transform: 给输入图像施加变换
target_transform:给目标值(类别标签)施加的变换
download: 是否下载mnist数据集
This requires the COCO API to be installed
dset.CocoCaptions(root="dir where images are", annFile="json annotation file", [transform, target_transform])
Example:
import torchvision.datasets as dset
import torchvision.transforms as transforms
cap = 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 sample
print("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']
dset.CocoDetection(root="dir where images are", annFile="json annotation file", [transform, target_transform])
dset.LSUN(db_path, classes='train', [transform, target_transform])
dset.CIFAR10(root, train=True, transform=None, target_transform=None, download=False)
dset.CIFAR100(root, train=True, transform=None, target_transform=None, download=False)
dset.STL10(root, split='train', transform=None, target_transform=None, download=False)
root : root directory of dataset where there is folder stl10_binary
split : 'train' = Training set, 'test' = Test set, 'unlabeled' = Unlabeled set,
'train+unlabeled' = Training + Unlabeled set (missing label marked as -1)
download : True = downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, does not do anything.
dset.SVHN(root, split='train', transform=None, target_transform=None, download=False)
root : root directory of dataset where there is folder SVHN
split : 'train' = Training set, 'test' = Test set, 'extra' = Extra training set
download : True = downloads the dataset from the internet and
puts it in root directory. If dataset is already downloaded, does not do anything.
一个通用的数据加载器,图像应该按照以下方式放置:
root/dog/xxx.png
root/dog/xxy.png
root/dog/xxz.png
root/cat/123.png
root/cat/nsdf3.png
root/cat/asd932_.png
dset.ImageFolder(root="root folder path", [transform, target_transform])
ImageFolder有以下成员:
This is simply implemented with an ImageFolder dataset.
The data is preprocessed as described here
Learning Local Image Descriptors Data http://phototour.cs.washington.edu/patches/default.htm
import torchvision.datasets as dset
import torchvision.transforms as transforms
dataset = dset.PhotoTour(root = 'dir where images are',
name = 'name of the dataset to load',
transform=transforms.ToTensor())
print('Loaded PhotoTour: {} with {} images.'
.format(dataset.name, len(dataset.data)))
models 子包含了以下的模型框架:
这里对于每种模型里可能包含很多子模型,比如Resnet就有 34,51,101,152不同层数。
这些成熟的模型的意义就是你可以在torchvision的安装路径下找到 可以通过命令 print(torchvision.models.__file__) #'d:\\Anaconda3\\lib\\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 包括形似(3*H*W)的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>
变换(Transforms)是常用的图像变换。可以通过 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 ]),
])
缩放输入的 PIL.Image到给定的“尺寸”。 ‘尺寸’ 指的是较短边的尺寸.
例如,如果 height > width, 那么图像将被缩放为 (size * height / width, size) - size: 图像较短边的尺寸- interpolation: Default: PIL.Image.BILINEAR
从中间裁剪一个 PIL.Image 到给定尺寸. 尺寸可以是一个元组 (target_height, target_width) 或一个整数,整数将被认为是正方形的尺寸 (size, size)
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) If padding is non-zero, then the image is first zero-padded on each side with padding pixels.
随机进行PIL.Image图像的水平翻转,概率是0.5.
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 ratio
This is popularly used to train the Inception networks - size: size of the smaller edge - interpolation: Default: PIL.Image.BILINEAR
Pads the given image on each side with padding number of pixels, and the padding pixels are filled with pixel value fill. If a 5x5image is padded with padding=1 then it becomes 7x7
Given mean: (R, G, B) and std: (R, G, B), will normalize each channel of the torch.*Tensor, i.e. channel = (channel - mean) / std
Given a Python lambda, applies it to the input img and returns it. For example:
transforms.Lambda(lambda x: x.add(10))
Given a 4D mini-batch Tensor of shape (B x C x H x W), or a list of images all of the same size, makes a grid of images
normalize=True will shift the image to the range (0, 1), by subtracting the minimum and dividing by the maximum pixel value.
if range=(min, max) where min and max are numbers, then these numbers are used to normalize the image.
scale_each=True will scale each image in the batch of images separately rather than computing the (min, max) over all images.
Example usage is given in this notebook <https://gist.github.com/anonymous/bf16430f7750c023141c562f3e9f2a91>
Saves a given Tensor into an image file.
If given a mini-batch tensor, will save the tensor as a grid of images.
All options after filename are passed through to make_grid. Refer to it’s documentation for more details
用以输出图像的拼接,很方便。
没想到这篇文章阅读量这么大,考虑跟新下。
图像引擎:由于需要读取处理图片所以需要相关的图像库。现在torchvision可以支持多个图像读取库,可以切换。
使用的函数是:
torchvision.``get_image_backend
() #获取图像存取引擎
torchvision.``set_image_backend
(backend) #改变图像读取引擎
#backend (string) –图像引擎的名字:是 {‘PIL’, ‘accimage’}其中之一。 accimage
包使用的是因特尔(Intel) IPP 库。它的速度快于PIL,但是并不支持很多的图像操作。
由于这个是后边的,普通用处不大,知道即可。
一分钱也是真爱
分类: pytorch
手机扫一扫
移动阅读更方便
你可能感兴趣的文章