为图像变换的函数库。
python脚本内输入:
from torchvision import transforms
按住Ctrl
,点击transforms
。
进入__init__.py
。
from .transforms import *
from .autoaugment import *
按住Ctrl
,点击.transforms
。
进入transforms.py
。
transforms.py
工具箱内的函数主要对图像进行变换,包括数据结构转换、尺度变换、高斯模糊、归一化、流水线、裁剪等。
from PIL import Image
from torchvision import transforms
img_path_abs = "G:\\ant.jpg"
img = Image.open(img_path_abs)
data_transform = transforms.Compose([
transforms.Resize((256, 256)), # 调整图像大小为256x256
transforms.RandomCrop((224, 224)), # 随机裁剪图像为224x224
transforms.ToTensor(), # 将图像转换为张量
transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]) # 标准化图像张量
])
transformed_data = data_transform(image)
python脚本代码如下:
from PIL import Image
from torchvision import transforms
from torch.utils.tensorboard import SummaryWriter
img_path_abs = "G:\\ant.jpg"
img = Image.open(img_path_abs)
print(img)
# 如果取消下方注释,则会在tensorboard中写入图像
# writer = SummaryWriter("logs")
# transforms.ToTensor PIL -> tensor
trans_totensor = transforms.ToTensor() # 创建具体工具,实例化对象
tensor_img = trans_totensor(img)
# 以上两步也可以简化为:
# tensor_img = transforms.ToTensor()(img)
print(tensor_img)
try:
writer.add_image("ToTensor_img", tensor_img, 1)
except NameError:
print("writer未定义")
# transforms.ToPILImage tensor -> PIL
trans_PILImage = transforms.ToPILImage()
pil_img = trans_PILImage(tensor_img)
pil_img.show()
# transforms.Normalize 归一化
trans_norm = transforms.Normalize([1, 1, 2], [1, 2, 1])
img_norm = trans_norm(tensor_img)
print("tensor_img")
print(tensor_img[0][0][0])
print("\r\nimg_norm")
print(img_norm[0][0][0])
try:
writer.add_image("Normalize_img", img_norm, 1)
except NameError:
print("writer未定义")
# transforms.Resize -1 改变图像比例
print("img.size:" + str(img.size))
trans_resize = transforms.Resize((512, 512))
img_resize = trans_resize(img) # PIL(原图尺寸) -> PIL(512,512)
print("img_resize.size:" + str(img_resize.size))
print("img_resize:" + str(img_resize))
img.show()
img_resize.show()
try:
writer.add_image("Resize_img", img_resize, 1)
except NameError:
print("writer未定义")
# Resize -2 不改变图像比例
trans_resize_2 = transforms.Resize(1024) # PIL(原图尺寸) -> PIL(1024,*)
img_resize_2 = trans_resize_2(img)
print("img.size:" + str(img.size))
print("img_resize_2.size:" + str(img_resize_2.size))
img.show()
img_resize_2.show()
try:
writer.add_image("Resize_img_2", img_resize_2, 1)
except NameError:
print("writer未定义")
# RandomCrop
trans_randomcrop = transforms.RandomCrop(512)
img_randomcrop = trans_randomcrop(img)
img_randomcrop.show()
# Compose 流水线操作,将多个图像变换函数集成到一起,串联起来
trans_blur = transforms.GaussianBlur(kernel_size=5, sigma=(10, 10)) # 高斯模糊函数
trans_compose = transforms.Compose([trans_resize_2, trans_randomcrop, trans_totensor, trans_blur, trans_PILImage])
# 流水线操作,将多个变换串联在一起:等比缩放,PIL转tensor,高斯模糊,tensor转PIL
img_blur = trans_compose(img)
img_blur.show()
try:
writer.close()
except NameError:
print("writer未定义,无需关闭")