当前位置:网站首页>Pytorch framework learning record 3 - the use of Transform
Pytorch framework learning record 3 - the use of Transform
2022-07-30 03:57:00 【Yuzu Roo】
Pytorch框架学习记录3——Transform的使用
1. 导入
from torchvision import transforms
2.ToTensor的用法
作用:将PIL图像或者numpy转换成tensor格式

from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image
img_path = "C:\\Users\\hp\\PycharmProjects\pythonProject\\Pytorch_Learning\\flower_data\\train\daisy\\5547758_eea9edfd54_n.jpg"
img = Image.open(img_path)
tensor = transforms.ToTensor()
tensor_img = tensor(img)
writer = SummaryWriter("logs")
writer.add_image("tensor_img", tensor_img, 1)
writer.close()

3. Normalize的用法
用均值和标准差归一化张量图像,参数:mean, std;The input image type is tensor,输出类型为tensor
o u t p u t [ c h a n n e l ] = ( i n p u t [ c h a n n e l ] − m e a n [ c h a n n e l ] ) / s t d [ c h a n n e l ] output[channel] = (input[channel] - mean[channel]) / std[channel] output[channel]=(input[channel]−mean[channel])/std[channel]
# Normalize
transform_normal = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
img_normal = transform_normal(img_tensor)
print(type(img_normal))
writer.add_image("Normalize", img_normal, 1)

4. RandomCrop 的用法
随机裁剪,参数sizeIndicates the size of the randomly cropped image,The input image type is PIL类型,The type of the output is alsoPIL类型.
The example below is a random crop of an image10次,The size of the image after each crop is 32*32
# RandomCrop
transform_randomCrop = transforms.RandomCrop((32, 32))
for i in range(0, 10):
img_randomCrop = transform_randomCrop(img_PIL)
# print(type(img_randomCrop))
img_randomCrop = transform_tensor(img_randomCrop)
writer.add_image("RandomCrop", img_randomCrop, i)

5. Resize的用法
将输入的PILCrop the image to the specified size,输入参数为size(int类型或者turple类型),输出为PIL图像
# Resize
transform_resize = transforms.Resize((128, 128))
img_resize = transform_resize(img_PIL)
img_resize = transform_tensor(img_resize)
writer.add_image("Resize", img_resize, 1)

6. Compose的用法
This method brings all the methods together,按照顺序依次执行,用法示例如下:
# Compose
transform_compose = transforms.Compose([transform_resize, transform_tensor])
img_resize_2 = transform_compose(img_PIL)
writer.add_image("Compose", img_resize_2, 1)
writer.close()
7. 所有代码
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
from torchvision.transforms import transforms
img_path = "C:\\Users\\hp\\PycharmProjects\pythonProject\\Pytorch_Learning\\flower_data\\train\daisy\\5547758_eea9edfd54_n.jpg"
writer = SummaryWriter("logs")
img_PIL = Image.open(img_path)
# ToTensor
transform_tensor = transforms.ToTensor()
img_tensor = transform_tensor(img_PIL)
# Normalize
transform_normal = transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
img_normal = transform_normal(img_tensor)
print(type(img_normal))
writer.add_image("Normalize", img_normal, 1)
# RandomCrop
transform_randomCrop = transforms.RandomCrop((32, 32))
for i in range(0, 10):
img_randomCrop = transform_randomCrop(img_PIL)
# print(type(img_randomCrop))
img_randomCrop = transform_tensor(img_randomCrop)
writer.add_image("RandomCrop", img_randomCrop, i)
# Resize
transform_resize = transforms.Resize((128, 128))
img_resize = transform_resize(img_PIL)
img_resize = transform_tensor(img_resize)
writer.add_image("Resize", img_resize, 1)
# Compose
transform_compose = transforms.Compose([transform_resize, transform_tensor])
img_resize_2 = transform_compose(img_PIL)
writer.add_image("Compose", img_resize_2, 1)
writer.close()
边栏推荐
- Basic introduction to protect the network operations
- 防抖与节流
- Boutique: Taobao/Tmall Get Order Details API for Purchased Products
- Taobao/Tmall get Taobao store details API
- flutter 记录学习不一样的动画(一)
- 小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
- Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (1) Development Overview
- 运行时间监控:如何确保网络设备运行时间
- 论坛管理系统
- 小程序毕设作品之微信二手交易小程序毕业设计成品(1)开发概要
猜你喜欢

Nacos cluster partition

spicy(一)基本定义

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Products (6) Question Opening and Defense PPT

小程序毕设作品之微信二手交易小程序毕业设计成品(8)毕业设计论文模板

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Product (2) Mini Program Function

写给技术人的管理入门知识1:什么是管理

小程序毕设作品之微信二手交易小程序毕业设计成品(1)开发概要

骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U

vscode 调试和远程

Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (7) Interim Inspection Report
随机推荐
STM32 SPI+WM8978 voice loopback
Pytorch框架学习记录6——torch.nn.Module和torch.nn.functional.conv2d的使用
小程序毕设作品之微信积分商城小程序毕业设计成品(7)中期检查报告
List获取差集产生的问题
Resampling a uniformly sampled signal
OpenFeign实现降级
对均匀采样信号进行重采样
发给你的好友,让 TA 请你吃炸鸡!
TCP congestion control technology and acceleration principle of BBR
Gateway routing gateway
智能答题功能,CRMEB知识付费系统必须有!
Monitor page deployment
小程序毕设作品之微信积分商城小程序毕业设计成品(4)开题报告
(redistribute, special comprehensive experiment ospf area)
The difference between BGP room and ordinary room in Beijing
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
Mini Program Graduation Works WeChat Points Mall Mini Program Graduation Design Finished Work (5) Task Book
高并发框架 Disruptor
小程序毕设作品之微信积分商城小程序毕业设计成品(2)小程序功能
小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板