1. 学习前言
根据pytorch官方文档TRANSFORMING AND AUGMENTING IMAGES
2. 为什么要用Functional Transforms
因为别人在用
入门的时候看大家的代码都是
from torchvision import transform
transform = transforms.Compose([
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor(),
transforms.Normalize([0.672, 0.487, 0.648], [0.172, 0.181, 0.149]) # RGB
但是今天看到有人这么写,代码来自transform.py
# transforms.py
import random
from torchvision.transforms import functional as F
class Compose(object):
"""组合多个transform函数"""
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, image, target):
for t in self.transforms:
image, target = t(image, target)
return image, target
class ToTensor(object):
"""将PIL图像转为Tensor"""
def __call__(self, image, target):
image = F.to_tensor(image)
return image, target
class RandomHorizontalFlip(object):
"""随机水平翻转图像以及bboxes"""
def __init__(self, prob=0.5):
self.prob = prob
def __call__(self, image, target):
if random.random() < self.prob:
height, width = image.shape[-2:]
image = image.flip(-1) # 水平翻转图片
bbox = target["boxes"]
# bbox: xmin, ymin, xmax, ymax
bbox[:, [0, 2]] = width - bbox[:, [2, 0]] # 翻转对应bbox坐标信息
target["boxes"] = bbox
return image, target
官方文档说用函数形式的变换,没有随机生成的参数。也就是说变换得到的结果是根据用户自己定义的。
To be continue
9771



被折叠的 条评论
为什么被折叠?



