from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision import transforms
img_dir = "5.jpg"
img = Image.open(img_dir) # PIL RGB 768x512
writer = SummaryWriter("log") # torch.Tensor, numpy.array
# ToTensor
tensor = transforms.ToTensor()
tensor_img = tensor(img)
print(tensor_img.shape)
writer.add_image("ToTensor",tensor_img,1)
# Normalize
normalize = transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
normalize_img = normalize(tensor_img)
print(normalize_img.shape)
writer.add_image("Normalize",normalize_img,1)
# Resize
resize = transforms.Resize((40,100)) # (h, w)
resize_img = resize(tensor_img)
print(resize_img.shape)
writer.add_image("Resize",resize_img,1)
# Compose
compose = transforms.Compose([
transforms.ToTensor(),
transforms.Resize((200,300)),
transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),
])
compose_img = compose(img)
print(compose_img.shape)
writer.add_image("Compose",compose_img,1)
# RandomCrop
randomcrop = transforms.RandomCrop((200))
randomcrop_img = randomcrop(tensor_img)
print(randomcrop_img.shape)
writer.add_image("RandomCrop",randomcrop_img,1
writer.close()
![在这里插入图片描述](https://img-blo
查看日志 写log的绝对路径
Tensorboard --logdir “E:\python_file\pytorch\log” # (默认6006端口)
Tensorboard --logdir “E:\python_file\pytorch\log” --port=6007 # (打开6007端口)