PyTorch中torchvision介绍

x33g5p2x  于2021-11-11 转载在 其他  
字(3.1k)|赞(0)|评价(0)|浏览(316)

**      TorchVision****包包含流行的数据集、模型架构和用于计算机视觉的图像转换**,它是PyTorch项目的一部分。TorchVison最新发布版本为v0.11.1,发布较频繁,它的license为BSD-3-Clause。它的源码位于:
 https://github.com/pytorch/vision 

**      TorchVision由C++(CUDA)和Python3实现**,依赖Torch、PNG、JPEG,还依赖PIL(Pillow, Python Imaging Library)。推荐使用Anaconda安装 ,安装时注意对Python和Torch有版本要求。对应TorchVison 0.11.1,Torch版本要求为1.10.0,Python要求为[3.6, 3.9]。通过Anaconda安装TorchVison 0.11.1执行如下命令:

conda create -n torchvision_0.11.1 python=3.8
conda activate torchvision_0.11.1
conda install torchvision==0.11.1 -c pytorch

**     **TorchVision也对外提供C++接口,通过CMakeLists.txt生成动态库。

**     **TorchVision功能:

**     **(1).torchvision.datasets包支持下载/加载的数据集有几十种,如CIFAR、COCO、MNIST等,所有的数据集都有相似的API加载方式。每种数据集在datasets包中都对应一个.py文件,如CIFAR对应有cifar.py。

**     **(2).torchvision.io包提供执行IO操作函数,用于读写视频和图像。

**     **(3).torchvision.models包提供各种模型定义,包括图像分类如AlexNet、VGG等;对象检测如Faster R-CNN、Mask R-CNN等;分割、关键点检测等。

**     **(4).torchvision.ops包实现特定于计算机视觉的操作,如RoI(Region of Interest) Align、RoI(Region of Interest) Pool等。

**     **(5).torchvision.transforms包实现图像变换。大多数转换同时接受PIL图像和tensor图像,尽管有些转换仅适用于PIL,有些则仅适用于tensor。接受tensor图像的转换也接受批量的tensor图像。tensor图像是具有(C, H, W)形状的tensor,其中C是通道数,H和W是图像的高度和宽度。批量tensor图像是一个(B, C, H, W)形状的tensor,其中B是一批图像的数量。tensor图像的预期范围由tensor dtype隐式定义。具有float dtype的tensor图像的值应为[0, 1)。具有整数dtype的tensor图像应具有[0, MAX_DTYPE],其中MAX_DTYPE是该dtype中可以表示的最大值。

**     **以下为测试代码:

from torchvision import datasets
from torchvision import io
from torchvision import models
from torchvision import ops
from torchvision import transforms

import torch

# 下载MNIST数据集: torchvision.datasets包
test = datasets.MNIST("../../data", train=False, download=True)
train = datasets.MNIST("../../data", train=True, download=False)
print(f"raw_folder: test: {test.raw_folder}, train: {train.raw_folder}")
print(f"processed_folder: test: {test.processed_folder}, train: {train.processed_folder}")
print(f"extra_repr:\ntest: {test.extra_repr}\ntrain: {train.extra_repr}")
print(f"class to index: {test.class_to_idx}")

# 读写图像: torchvision.io包
tensor = io.read_image("../../data/image/1.jpg")
print("tensor shape:", tensor.shape)
io.write_png(tensor, "../../data/image/result.png")

tensor = io.read_image("../../data/image/lena.png")
print("tensor shape:", tensor.shape)
io.write_jpeg(tensor, "../../data/image/result.jpg")

# 下载pre-trained AlexNet模型: torchvision.models包
net = models.alexnet(pretrained=True)

# 计算机视觉操作: torchvision.ops包
boxes = torch.tensor([[1, 1, 101, 101], [3, 5, 13, 15], [2, 4, 22, 44]])
area = ops.box_area(boxes)
print(f"area: {area}")

index = ops.remove_small_boxes(boxes, min_size=20)
print(f"index: {index}")

# 图像变换: torchvision.transforms包
resize = transforms.Resize(size=[256, 128])
img = resize.forward(tensor)
io.write_jpeg(img, "../../data/image/resize.jpg")

grayscale = transforms.Grayscale()
img2 = grayscale.forward(img)
io.write_jpeg(img2, "../../data/image/gray.jpg")

affine = transforms.RandomAffine(degrees=35)
img3 = affine.forward(tensor)
io.write_jpeg(img3, "../../data/image/affine.jpg")

crop = transforms.CenterCrop(size=[128, 128])
img4 = crop.forward(tensor)
io.write_jpeg(img4, "../../data/image/crop.jpg")

**      GitHub**:https://github.com/fengbingchun/PyTorch_Test

相关文章

微信公众号

最新文章

更多