pytorch进阶—pytorch生态

目录

1、torchvision

2、Transforms学习

2、1导入图像

2.2 沿中心切割图像

2.3 图像亮度等改变

2.4 RGB三通道转换为灰度图像

2.5 等比缩放图像

2.6 随机裁剪图像区域指定大小

2.7按照概率P 图像水平翻转

2.8 图像水平翻转

2.9随机裁剪指定大小

2.10 图像组合变化(Compose())

3、pytorchVideo

1、torchvision

torchvision会包括我们经常处理图像一些库

(1) torchvision.tramsforms__可以用于图像预处理和数据增强(数据缩放、翻转等)

(2)torchvision.datasets__包括一些常见数据集(Fashion-MNIST、ImageNet、VOC等)

(3)torchvision.models__包括一些官方预训练模型权重(VGG、ResNet、Mobilenetv2等)

(4)torchvision.utils__提供一些可视化方法

(5)torchvision.io__视频、图像文件io操作

2、Transforms学习

2、1导入图像

import torch
import matplotlib.pyplot as plt

from torchvision.models import vgg11
from PIL import Image
import numpy as np
model = vgg11(pretrained=True)
img_path = 'C:/Users/CHDT/Desktop/cat.jpg'
img = Image.open(img_path).resize((224,224))
rgb_img = np.float32(img)/255
plt.imshow(img)

pytorch进阶---pytorch生态

2.2 沿中心切割图像

#给定图片沿中心切割
img_centercrop1 = transforms.CenterCrop((500,500))(img)
print(img_centercrop1.size)
#对图片沿中心缩小切割,超出期望大小的部分剔除
img_centercrop2 = transforms.CenterCrop((224,224))(img)
print(img_centercrop2.size)
plt.subplot(1,3,1),plt.imshow(img),plt.title('Original')
plt.subplot(1,3,2),plt.imshow(img_centercrop1),plt.title('500*500')
plt.subplot(1,3,3),plt.imshow(img_centercrop2),plt.title('224*224')

pytorch进阶---pytorch生态

2.3 图像亮度等改变

#对图像亮度,对比度,饱和度,色调进行改变
img_CJ = transforms.ColorJitter(brightness=1,contrast=0.5,saturation=0.5,hue=0.5)(img)
print(img_CJ.size)
plt.imshow(img_CJ)

pytorch进阶---pytorch生态

2.4 RGB三通道转换为灰度图像

img_grey_c3 = transforms.Grayscale(num_output_channels=3)(img)
img_grey_c1 = transforms.Grayscale(num_output_channels=1)(img)
plt.subplot(1,2,1),plt.imshow(img_grey_c3),plt.title('channels=3')
plt.subplot(1,2,2),plt.imshow(img_grey_c1),plt.title('channels=1')
plt.show()

pytorch进阶---pytorch生态

2.5 等比缩放图像

#等比缩放图像
img_resize = transforms.Resize(224)(img)
print(img_resize.size)
plt.imshow(img_resize)

pytorch进阶---pytorch生态

2.6 随机裁剪图像区域指定大小

#随机裁剪成指定大小
import torch
torch.manual_seed(31)
#随机裁剪
img_randown_crop1 = transforms.RandomCrop(224)(img)
img_randown_crop2 = transforms.RandomCrop(224)(img)
print(img_randown_crop1.size)
plt.subplot(1,2,1),plt.imshow(img_randown_crop1),plt.title('1')
plt.subplot(1,2,2),plt.imshow(img_randown_crop2),plt.title('2')
plt.show()

pytorch进阶---pytorch生态

2.7按照概率P 图像水平翻转

import torch
torch.manual_seed(31)
img1 = transforms.RandomHorizontalFlip(p=0.5)(img)
print(img1.size)
plt.imshow(img1)

pytorch进阶---pytorch生态

2.8 图像水平翻转

import torch
torch.manual_seed(31)
img1 = transforms.RandomVerticalFlip()(img)#水平翻转图像
print(img1.size)
plt.imshow(img1)

pytorch进阶---pytorch生态

2.9随机裁剪指定大小

#随机裁剪指定大小
img2 = transforms.RandomResizedCrop(223,scale=(0.5,0.5),ratio = (0.8,1))(img)
print(img2.size)
plt.imshow(img2)

scale参数表示裁剪区域大小占图像总面积的值

ratio代表随机裁剪宽高比范围

pytorch进阶---pytorch生态

2.10 图像组合变化(Compose())

#对图像组合变化
transformer = transforms.Compose([
    transforms.Resize(256),#将图像转化为256的特征图
    transforms.transforms.RandomResizedCrop((224),scale=(0.5,1.0)),#随即裁剪尺寸大小为224的特征图,scale参数表示裁剪区域占总面积的比例
    transforms.RandomVerticalFlip(),#垂直翻转图像
])
img_transform = transformer(img)
plt.imshow(img_transform)

pytorch进阶---pytorch生态

3、pytorchVideo

pytorchVideo包括加速视频理解研究所需要的可重用、模块化、和高效的组件,

pytorchVideo安装

pip install pytorchvideo

对于pytorchtextNLP学习huggingface工具包,单独更新

参考

https://github.com/datawhalechina

Original: https://blog.csdn.net/m0_64013271/article/details/123721908
Author: data小孙
Title: pytorch进阶—pytorch生态

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/642315/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球