PyTorch中torchvision介绍
? ? ? 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 transformsimport 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
總結
以上是生活随笔為你收集整理的PyTorch中torchvision介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python3中Pillow(PIL)介
- 下一篇: TorchVision中使用Faster