PyTorch中torchvision介绍

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

**      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执行如下命令:

  1. conda create -n torchvision_0.11.1 python=3.8
  2. conda activate torchvision_0.11.1
  3. 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中可以表示的最大值。

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

  1. from torchvision import datasets
  2. from torchvision import io
  3. from torchvision import models
  4. from torchvision import ops
  5. from torchvision import transforms
  6. import torch
  7. # 下载MNIST数据集: torchvision.datasets包
  8. test = datasets.MNIST("../../data", train=False, download=True)
  9. train = datasets.MNIST("../../data", train=True, download=False)
  10. print(f"raw_folder: test: {test.raw_folder}, train: {train.raw_folder}")
  11. print(f"processed_folder: test: {test.processed_folder}, train: {train.processed_folder}")
  12. print(f"extra_repr:\ntest: {test.extra_repr}\ntrain: {train.extra_repr}")
  13. print(f"class to index: {test.class_to_idx}")
  14. # 读写图像: torchvision.io包
  15. tensor = io.read_image("../../data/image/1.jpg")
  16. print("tensor shape:", tensor.shape)
  17. io.write_png(tensor, "../../data/image/result.png")
  18. tensor = io.read_image("../../data/image/lena.png")
  19. print("tensor shape:", tensor.shape)
  20. io.write_jpeg(tensor, "../../data/image/result.jpg")
  21. # 下载pre-trained AlexNet模型: torchvision.models包
  22. net = models.alexnet(pretrained=True)
  23. # 计算机视觉操作: torchvision.ops包
  24. boxes = torch.tensor([[1, 1, 101, 101], [3, 5, 13, 15], [2, 4, 22, 44]])
  25. area = ops.box_area(boxes)
  26. print(f"area: {area}")
  27. index = ops.remove_small_boxes(boxes, min_size=20)
  28. print(f"index: {index}")
  29. # 图像变换: torchvision.transforms包
  30. resize = transforms.Resize(size=[256, 128])
  31. img = resize.forward(tensor)
  32. io.write_jpeg(img, "../../data/image/resize.jpg")
  33. grayscale = transforms.Grayscale()
  34. img2 = grayscale.forward(img)
  35. io.write_jpeg(img2, "../../data/image/gray.jpg")
  36. affine = transforms.RandomAffine(degrees=35)
  37. img3 = affine.forward(tensor)
  38. io.write_jpeg(img3, "../../data/image/affine.jpg")
  39. crop = transforms.CenterCrop(size=[128, 128])
  40. img4 = crop.forward(tensor)
  41. io.write_jpeg(img4, "../../data/image/crop.jpg")

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

相关文章