二、Transforms基本使用
Transforms主要是对特定格式的图片进行一些变化。
1、Transforms的结构及用法
Compose:
ToTensor:
1.1 如何使用
PIL Image
to Tensor
from torchvision import transforms from PIL import Image img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg" img = Image.open(img_path) # <class 'PIL.JpegImagePlugin.JpegImageFile'> tensor_trans = transforms.ToTensor() tensor_img = tensor_trans(img) print(tensor_img)
tensor([[[0.3137, 0.3137, 0.3137, ..., 0.3176, 0.3098, 0.2980], [0.3176, 0.3176, 0.3176, ..., 0.3176, 0.3098, 0.2980], [0.3216, 0.3216, 0.3216, ..., 0.3137, 0.3098, 0.3020], ..., ..., [0.9294, 0.9294, 0.9255, ..., 0.5529, 0.9216, 0.8941], [0.9294, 0.9294, 0.9255, ..., 0.8863, 1.0000, 0.9137], [0.9294, 0.9294, 0.9255, ..., 0.9490, 0.9804, 0.9137]]])
numpy.ndarry
to Tensor
from torchvision import transforms import cv2 img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg" img = cv2.imread(img_path) # <class 'numpy.ndarray'> tensor_trans = transforms.ToTensor() tensor_img = tensor_trans(img) print(tensor_img)
tensor([[[0.9137, 0.9137, 0.9137, ..., 0.9176, 0.9098, 0.8980], [0.9176, 0.9176, 0.9176, ..., 0.9176, 0.9098, 0.8980], [0.9216, 0.9216, 0.9216, ..., 0.9137, 0.9098, 0.9020], ..., ..., [0.3412, 0.3412, 0.3373, ..., 0.1725, 0.3725, 0.3529], [0.3412, 0.3412, 0.3373, ..., 0.3294, 0.3529, 0.3294], [0.3412, 0.3412, 0.3373, ..., 0.3098, 0.3059, 0.3294]]])
1.2 TensorBoard查看
from torch.utils.tensorboard import SummaryWriter from torchvision import transforms from PIL import Image img_path = "../data/tensorboard_data/train/ants_image/0013035.jpg" img = Image.open(img_path) writer = SummaryWriter("logs") tensor_trans = transforms.ToTensor() tensor_img = tensor_trans(img) writer.add_image("tensor_img", tensor_img) writer.close()
2、常用的Transforms
2.1 ToTensor
from torch.utils.tensorboard import SummaryWriter from torchvision import transforms from PIL import Image writer = SummaryWriter("logs") img = Image.open("../images/pytorch.webp") trans_totensor = transforms.ToTensor() img_totensor = trans_totensor(img) writer.add_image("ToTensor", img_totensor) writer.close()
2.2 Normalize
1、计算公式:
output[channel] = (input[channel] - mean[channel]) / std[channel]
2、本实例中即:
(input - 0.5)/0.5 = 2 * input - 1
3、大致范围:
input[0, 1] —> result[-1, 1]
from torch.utils.tensorboard import SummaryWriter from torchvision import transforms from PIL import Image writer = SummaryWriter("logs") img = Image.open("../images/pytorch.webp") # 1.ToTensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # 2.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.add_image("Normalize", img_norm) writer.close()
2.3 Resize
from torch.utils.tensorboard import SummaryWriter from torchvision import transforms from PIL import Image writer = SummaryWriter("logs") img = Image.open("../images/pytorch.webp") # 1.ToTensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # 2.Resize print(img.size) # (889, 500) # img PIL -> resize -> img_resize PIL trans_resize = transforms.Resize((450, 450)) # img_resize PIL -> totensor -> img_resize tensor img_resize = trans_resize(img) print(img_resize) # <PIL.Image.Image image mode=RGB size=450x450 at 0x1485DFE6310> img_resize = trans_totensor(img_resize) writer.add_image("Resize", img_resize, 0) writer.close()
2.4 Compose
还可以直接使用Compose!
from torch.utils.tensorboard import SummaryWriter from torchvision import transforms from PIL import Image writer = SummaryWriter("logs") img = Image.open("../images/pytorch.webp") # 1.ToTensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # 2.Compose - Resize - 2 trans_resize_2 = transforms.Resize(100) # PIL -> PIL -> tensor trans_compose = transforms.Compose([trans_resize_2, trans_totensor]) img_resize_2 = trans_compose(img) writer.add_image("Compose - Resize", img_resize_2, 0) writer.close()
2.5 RandomCrop
from torch.utils.tensorboard import SummaryWriter from torchvision import transforms from PIL import Image writer = SummaryWriter("logs") img = Image.open("../images/pytorch.webp") # 1.ToTensor trans_totensor = transforms.ToTensor() img_tensor = trans_totensor(img) writer.add_image("ToTensor", img_tensor) # 2.RandomCrop trans_random = transforms.RandomCrop((250, 444)) 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()