1 ToTensor()
作用:将 PIL Image 类型或者 numpy.ndarray 类型转为 tensor 类型
实例:
from torchvision import transforms
from PIL import Image
img_path = "data/train/bees/17209602_fe5a5a746f.jpg"
img = Image.open(img_path)
print(img)
print(type(img))
tensor_trans = transforms.ToTensor()
tensor_img = tensor_trans(img)
print(tensor_img)
print(type(tensor_img))
运行结果:
2 Normalize()
作用: tensor进行归一化操作,其中:mean和std是指定的均值和标准差,分别对应RGB三个通道
实例:
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path = "data/train/bees/17209602_fe5a5a746f.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
print(tensor_img[0][0][0])
# 计算公式:output[channel] = (input[channel] - mean[channel]) / std[channel]
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
norm_img = trans_norm(tensor_img)
# norm_img = transforms.Normalize([0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])(tensor_img)
print(norm_img[0][0][0])
writer.add_image("normalize", norm_img)
writer.close()
运行结果:
3 Resize()
作用:对图像进行缩放操作
实例:
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path = "data/train/bees/17209602_fe5a5a746f.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
print(img.size)
trans_resize = transforms.Resize((512, 512)) # 缩放后的尺寸是(512, 512)
resize_img = trans_resize(img) # resize_img是PIL.Image对象,需要转换为tensor才能显示
resize_img = trans_tensor(resize_img)
print(resize_img.size)
writer.add_image("resize", resize_img, 0)
writer.close()
结果:
4 compose()
将多个transform组合起来使用
实例:
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path = "data/train/bees/17209602_fe5a5a746f.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
trans_resize_2 = transforms.Resize(512) # 代表将较小的边缩放为256,另一边等比例缩放
trans_compose = transforms.Compose([trans_resize_2,trans_tensor]) # 先缩放再转换为tensor
resize_img_2 = trans_compose(img)
writer.add_image("resize_2", resize_img_2, 0)
writer.close()
结果:
5 RandomCrop()
对图像进行随机裁剪操作
实例:
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path = "data/train/bees/17209602_fe5a5a746f.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
trans_randomcrop = transforms.RandomCrop(256) # 随机裁剪后的尺寸是(256, 256)
trans_compose_2 = transforms.Compose([trans_randomcrop, trans_tensor]) # 先随机裁剪再转换为tensor
for i in range(10):
randomcrop_img = trans_compose_2(img)
writer.add_image("randomcrop", randomcrop_img, i)
writer.close()
结果:
汇总
from torchvision import transforms
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
img_path = "data/train/bees/17209602_fe5a5a746f.jpg"
img = Image.open(img_path)
writer = SummaryWriter("logs")
####################### totensor() #######################
# torensor()函数将PIL.Image或numpy.ndarray转换为torch.FloatTensor
trans_tensor = transforms.ToTensor()
tensor_img = trans_tensor(img)
# 以上两句可替换为:tensor_img = transforms.ToTensor()(img)
writer.add_image("tensor", tensor_img)
####################### normalize() #######################
# normalize()函数对tensor进行归一化操作
# mean和std是指定的均值和标准差,分别对应RGB三个通道
print(tensor_img[0][0][0])
# 计算公式:output[channel] = (input[channel] - mean[channel]) / std[channel]
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])
norm_img = trans_norm(tensor_img)
# norm_img = transforms.Normalize([0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5])(tensor_img)
print(norm_img[0][0][0])
writer.add_image("normalize", norm_img)
####################### resize() #######################
# resize()函数对图像进行缩放操作
print(img.size)
trans_resize = transforms.Resize((512, 512)) # 缩放后的尺寸是(512, 512)
resize_img = trans_resize(img) # resize_img是PIL.Image对象,需要转换为tensor才能显示
resize_img = trans_tensor(resize_img)
print(resize_img.size)
writer.add_image("resize", resize_img, 0)
####################### compose() #######################
# compose()函数将多个transform组合起来使用
trans_resize_2 = transforms.Resize(512) # 代表将较小的边缩放为256,另一边等比例缩放
trans_compose = transforms.Compose([trans_resize_2,trans_tensor]) # 先缩放再转换为tensor
resize_img_2 = trans_compose(img)
writer.add_image("resize_2", resize_img_2, 0)
######################## RandomCrop() #######################
# RandomCrop()函数对图像进行随机裁剪操作
trans_randomcrop = transforms.RandomCrop(256) # 随机裁剪后的尺寸是(256, 256)
trans_compose_2 = transforms.Compose([trans_randomcrop, trans_tensor]) # 先随机裁剪再转换为tensor
for i in range(10):
randomcrop_img = trans_compose_2(img)
writer.add_image("randomcrop", randomcrop_img, i)
# 想在tensorbosrd中显示图像,需要在命令行中输入:tensorboard --logdir=logs(注意:先运行再输入)
writer.close()