当前位置:网站首页>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()
边栏推荐
- Small application project works WeChat integral mall small program of graduation design (4) the opening report of finished product
- Nacos配置中心
- spicy (two) unit hooks
- Rpc 和 gRpc 简介汇总
- 小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
- Nacos service registration and discovery
- ospf 综合实验(重发布,特殊区域)
- Advanced Microservices Cloud Alibaba
- 小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书
- 【转】Swift 中的面向协议编程:引言
猜你喜欢

Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!

小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能

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

第51篇-知乎请求头参数分析【2022-07-28】

Forum management system

路由过滤器

Nacos namespace

ospf 导图

vscode 调试和远程

CMake的安装和测试
随机推荐
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (6) Question Opening Reply PPT
Gateway 路由网关
SDL player in action
Process priority nice
cv2.polylines
Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!
Nacos cluster partition
Forum management system
The curl command to get the network IP
What is the difference between mission, vision and values?
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
Flutter record learning different animation (2)
小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
Anti-shake and throttling
RRU、BBU、AAU
Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Work (2) Mini Program Function
Microservice CAP Principles
Redis server启动后会做哪些操作?
骁龙7系芯片表现如何?Reno8 Pro佐证新一代神U