1. 程式人生 > 其它 >【Pytorch基礎】Torchvision中transform的指令碼化

【Pytorch基礎】Torchvision中transform的指令碼化

Transforms從torch1.7開始新增了該特性,之前transform進行資料增強的方式是如下的,i.e. 使用compose的方式:

default_configure = T.Compose([
            T.RandomCrop(32, 4),
            T.RandomHorizontalFlip(),
            T.RandomResizedCrop((32, 32)),  
            T.RandomRotation(15)
        ])

現在Transforms支援以下方式:

import torch
import torchvision.transforms as T

# to fix random seed, use torch.manual_seed
# instead of random.seed
torch.manual_seed(12)

transforms = torch.nn.Sequential(
    T.RandomCrop(224),
    T.RandomHorizontalFlip(p=0.3),
    T.ConvertImageDtype(torch.float),
    T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
)
scripted_transforms = torch.jit.script(transforms)

tensor_image = torch.randint(0, 256, size=(3, 256, 256), dtype=torch.uint8)
# works directly on Tensors
out_image1 = transforms(tensor_image)
# on the GPU
out_image1_cuda = transforms(tensor_image.cuda())
# with batches
batched_image = torch.randint(0, 256, size=(4, 3, 256, 256), dtype=torch.uint8)
out_image_batched = transforms(batched_image)
# and has torchscript support
out_image2 = scripted_transforms(tensor_image)

Compose和指令碼化的合作也是可行的:

Note: we can similarly use T.Compose to define transforms
transforms = T.Compose([...]) and 
scripted_transforms = torch.jit.script(torch.nn.Sequential(*transforms.transforms))

以上方法有幾點特徵:

  • 資料增強可以支援GPU加速
  • batch化 transformation,視訊處理中使用更方便。
  • 可以支援多channel的tensor增強,而不僅僅是3通道或者4通道的tensor。