transforms中的相关变换

Image.open()输出PIL格式

	例如:		
from PIL import Image
path = "hymenoptera_data/train/bees/16838648_415acd9e3f.jpg"
img_PIL = Image.open(path)
print(img_PIL)
#输出:<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=500x450 at 0x1B7F0FD3C50>

Totensor()输出tensor格式(Convert a PIL Image or numpy.ndarray to tensor.)

from PIL import Image
from torchvision import transforms
path = "hymenoptera_data/train/bees/16838648_415acd9e3f.jpg"
img_PIL = Image.open(path)
img_ToTensor = transforms.ToTensor()
img_transTensor = img_ToTensor(img_PIL)
print(img_transTensor)
#输出:tensor([[[0.0980, 0.0863, 0.0902,  ..., 0.0314, 0.0314, 0.0431],
         [0.0784, 0.0863, 0.0863,  ..., 0.0235, 0.0196, 0.0196],
         [0.0510, 0.0784, 0.0863,  ..., 0.0431, 0.0353, 0.0392],
         ...,

cv.imread()输出narrays格式

import cv2.cv2
path = "hymenoptera_data/train/bees/39672681_1302d204d1.jpg"
img_narrays = cv2.cv2.imread(path)
print(type(img_narrays))
#输出:<class 'numpy.ndarray'>

SummaryWriter类,通过tensorboard将文件保存在要创建的“log”中。

from torch.utils.tensorboard import SummaryWriter

wirter = SummaryWriter("logs")
wirter.add_image("test",img_tensor) ##torch.Tensor, numpy.array, or string/blobname 这几种类型的参数都可以
wirter.close()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值