Transforms的常见用法
文章目录
- 一、封装函数与普通函数的用法区别
- 二、Image.open()打开图片的格式
- 三、ToTensor打开图片格式
- 四、ToTensor使用
- 五、Normalize归一化使用
- 六、Resize的使用
- 七、Compose - Resize 使用
- 八、RandomCrop() 随机裁剪用法
一、封装函数与普通函数的用法区别
class Person:
def __call__(self, name):
print("__call__" + "Hello" + name)
def hello(self,name):
print("hello" + name)
person = Person()
person("周杰伦")
person.hello("林俊杰")
运行结果:
注释:
- 双斜杠"__"封装的函数【
def __call__(self, name):
】,在调用的时候直接加参数就可以使用了
【person("周杰伦")
】 - 而直接定义的函数,则需要通过 “.” +函数名来进行使用,如
person.hello("林俊杰")
二、Image.open()打开图片的格式
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
print(img)
运行结果:
因此可以知道 Image.open()
打开图片的格式为PIL形
三、ToTensor打开图片格式
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
print(img_tensor)
运行结果:
注:可以看到最终得到的是 tensor 形
四、ToTensor使用
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
writer.close()
在终端输入 tensorboard --logdir=logs
得到链接
点开即可查看照片
五、Normalize归一化使用
归一化的计算方式:output[channel] = (input[channel] - mean[channel]) / std[channel]
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
writer.close()
#Normalize
print(img_tensor[0][0][0]) #将图片的第一层,第一行,第一列将他输出出来
trans_norm = transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
img_norm = trans_norm(img_tensor)
print(img_norm[0][0][0]) #经过归一化输出的第一层,第一行,第一列将他输出出来
writer.close()
运行结果:
注:最开始是 0.31 ,经过变换之后就变成了 -0.37
将上面的代码进行输出一下
writer.add_image("Normalize",img_norm)
可以看出图片的改变
六、Resize的使用
先观察Resize的输出类型
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
writer.close()
#Resize
print(img.size)
trans_resize = transforms.Resize((512,512))
img_resize = trans_resize(img)
print(img_resize)
writer.close()
运行结果:
可以知道Resize的输出结果是 PIL形,并且尺寸从(768,512) 变成了现在的 size = 512 x 512
观察图片
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
writer.close()
#Resize
print(img.size)
trans_resize = transforms.Resize((512,512))
#img PIL形 --> 经过resize -> img_resize PIL形
img_resize = trans_resize(img)
# img_resize PIL形 -> 经过totensor -> img_resize tensor形 ->进行图像的输出
img_resize = trans_totensor(img_resize)
#图片写出
writer.add_image("Resize",img_resize,0)
print(img_resize)
writer.close()
运行结果:
注:可以看出图片经过了裁剪
七、Compose - Resize 使用
如果给Resize()括号中写一个整数形int,那么图片不会改变高和宽,而是整体进行一个等比缩放
如:trans_resize_2 = transforms.Resize(512)
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
img_tensor = trans_totensor(img)
writer.add_image("ToTensor",img_tensor)
writer.close()
#Resize
print(img.size)
trans_resize = transforms.Resize((512,512))
#img PIL形 --> 经过resize -> img_resize PIL形
img_resize = trans_resize(img)
# img_resize PIL形 -> 经过totensor -> img_resize tensor形 ->进行图像的输出
img_resize = trans_totensor(img_resize)
#图片写出
writer.add_image("Resize",img_resize,0)
print(img_resize)
writer.close()
#Compose - Resize
trans_resize_2 = transforms.Resize(512)
# 创建一个transforms.Compose对象,它将多个图像变换操作组合成一个序列
tran_compose = transforms.Compose([trans_resize_2,trans_totensor])
img_resize_2 = tran_compose(img)
# "Resize"是图像的标签,img_resize_2是经过变换的图像,1是图像在TensorBoard中的步数(step)
writer.add_image("Resize",img_resize_2,1)
注:Compose方法要求前面的输出做后面的输入,比方说tran_compose = transforms.Compose([trans_resize_2,trans_totensor])
,前面的输出类型是PIL形,故要求后面的输入也要是PIL形,所以可以直接使用compose
但是如果tran_compose = transforms.Compose([trans_totensor,trans_resize_2])
,也就是前面的输出是totensor形,但是后面的输入要求PIL形,这样子进行compose的话就会产生报错。
运行结果:
八、RandomCrop() 随机裁剪用法
from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
trans_random = transforms.RandomCrop(512)
trans_compose_2 = transforms.Compose([trans_random,trans_totensor])
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop",img_crop, i)
writer.close()
运行结果:
自己设置高和宽:
**from PIL import Image
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import transforms
writer = SummaryWriter("logs")
img = Image.open('Dataset/train/ants/0013035.jpg')
trans_totensor = transforms.ToTensor()
#设置高为50,宽为100
trans_random = transforms.RandomCrop((50, 100))
trans_compose_2 = transforms.Compose([trans_random,trans_totensor])
for i in range(10):
img_crop = trans_compose_2(img)
writer.add_image("RandomCrop",img_crop, i)
writer.close()**
运行结果: