torch_test/transforms_test.py
2023-07-31 19:29:27 +08:00

15 lines
539 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_path = r"C:\Users\12642\Desktop\Picture\dataset\train\ants\6240338_93729615ec.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
# transforms.ToTensor() 将 PIL.Image 或者 numpy.ndarray 转化为 tensor
tensor_trans = transforms.ToTensor()
tensor_img = transforms.ToTensor()(img)
writer.add_image("tensor_img", tensor_img) # 这里的tensor_img是一个三维的tensor所以可以直接显示
writer.close()