格式 | 使用到的库 | 读取方式 |
---|---|---|
PIL | from PIL import Image | Image.open() |
ndarray | opencv-python | cv.imread() |
tensor | from torchvision import transforms | transforms.ToTensor() |
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
writer = SummaryWriter('log')
image_path = 'hymenoptera_data/train/bees/17209602_fe5a5a746f.jpg'
PIL格式的图片可以理解为图片的基准类型,在此类型上进行各种的格式转换
img = Image.open(image_path)
print(type(img))
print(img)
将 PIL Image
或者 numpy.ndarray
类型的图片格式转换为 tensor
格式
# ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
print(type(trans_totensor))
writer.add_image('ToTensor', img_tensor)
writer.close()
根据传入的mean
和standard
将tensor
格式的图片做归一化处理。只支持tensor
格式,不支持PIL
格式
处理公式:output[channel] = (input[channel] - mean[channel]) / std[channel]
# Normalize
trans_normal = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_normal = trans_normal(img_tensor)
writer.add_image('Normal', img_normal)
writer.close()
对PIL
或者 Tensor
类型图片重新调正大小,可以接受sequence
或者int
两种类型的参数。
sequence
:(H, W)。高H,宽W
int
: int。当参数为int时,会将图片W/H中较小边缘将会按int进行调整
# transforms.Resize
print(img.size)
trans_resize = transforms.Resize((100, 512))
img_resize = trans_resize(img) # pil
img_resize = trans_totensor(img_resize) # tensor
print(img_resize)
writer.add_image('Resize', img_resize)
writer.close()
将transfroms中的各种图像操作串行结合,但是要注意,前一个变换的output的数据类型必须与后一个变换的input的数据类型相匹配
# Compose
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor]) # 先进行resize,在进行totensor
img_resize_2 = trans_compose(img)
print(type(img_resize_2))
writer.add_image('RandomCrop', img_resize_2)
writer.close()
对PIL
或者 Tensor
类型图片进行随机裁剪,可以接受sequence
或者int
两种类型的参数
sequence
:(H, W)。高H,宽W
int
: int。当参数为int时,将会进行int*int的裁剪
# RandomCrop
trans_random_crop = transforms.RandomCrop(400)
trans_compose_2 = transforms.Compose([trans_random_crop, trans_totensor])
img_crop = trans_compose_2(img)
writer.add_image('RandomCrop', img_crop)
writer.close()
在conda的pytorch环境中执行 tensorboard --logdir=log --port=6007
点击本地连接,即可查看,不再一一进行展示
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
from PIL import Image
writer = SummaryWriter('log')
image_path = 'hymenoptera_data/train/bees/17209602_fe5a5a746f.jpg'
img = Image.open(image_path)
print(type(img))
print(img)
# ToTensor
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
print(type(trans_totensor))
writer.add_image('ToTensor', img_tensor)
writer.close()
# Normalize
trans_normal = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_normal = trans_normal(img_tensor)
writer.add_image('Normal', img_normal)
writer.close()
# Resize
print(img.size)
trans_resize = transforms.Resize((100, 512))
img_resize = trans_resize(img) # PIL
img_resize = trans_totensor(img_resize) # tensor
print(img_resize)
writer.add_image('Resize', img_resize)
writer.close()
# Compose 将transfroms中的各种图像操作串行结合,但是要注意,前一个变换的output的数据类型必须与后一个变换的input的数据类型相匹配
trans_resize_2 = transforms.Resize(512)
trans_compose = transforms.Compose([trans_resize_2, trans_totensor])
img_resize_2 = trans_compose(img)
print(type(img_resize_2))
writer.add_image('RandomCrop', img_resize_2)
writer.close()
# RandomCrop
trans_random_crop = transforms.RandomCrop(400)
trans_compose_2 = transforms.Compose([trans_random_crop, trans_totensor])
img_crop = trans_compose_2(img)
writer.add_image('RandomCrop', img_crop)
writer.close()