1. ToTensor定义
导入:from torchvision import transforms
通过transforms.ToTensor解决两个问题(PIL image/numpy.ndarray 转化为 tensor )
ToTensor()返回一个ToTensor的对象(创建具体的工具),传入pic就会返回一个Tensor类型的图片(使用工具)
- ToTensor使用(结合tensorboard)
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_path = 'dataset/train/ants/6743948_2b8c096dda.jpg'
img = Image.open(img_path)
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)
writer = SummaryWriter("logs")
writer.add_image("Tensor_img",tensor_img)
writer.close()