您的当前位置:首页正文

Tensorforms的使用(一):ToTensor()的使用

2024-11-25 来源:个人技术集锦
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
from PIL import Image

# 打开图片
img_path = "D:\\PycharmProjects\\learn_torch\\dataset\\train\\ants\\0013035.jpg"
img = Image.open(img_path)

writer = SummaryWriter("../../../logs")
# 转换图
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)

writer.add_image("Tensor_img", tensor_img, 1)
writer.close()

1.引入包

from torchvision import transforms

2.转换:

实例化

tensor_trans = transforms.ToTensor()

使用这个类的方法:

tensor_img = tensor_trans(img)

3.添加

writer.add_image("Tensor_img", tensor_img, 1)
显示全文