Pytorch 是否允许将给定的变换应用于图像的边界框坐标?

Kar*_*ren 7 python bounding-box computer-vision yolo

在 Pytorch 中,我知道某些图像处理转换可以这样组成:

import torchvision.transforms as transforms
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

就我而言,每个图像都有相应的 YOLO 格式的边界框坐标注释。Pytorch 是否也允许将这些转换应用于图像的边界框坐标,然后将它们保存为新注释?谢谢。

Vic*_*zzi 6

您用作示例的转换不会更改边界框坐标。ToTensor()将 PIL 图像转换为 torch 张量,并Normalize()用于标准化图像的通道。

诸如RandomCrop()和 之类的变换RandomRotation()将导致边界框的位置与(修改后的)图像之间不匹配。

然而,Pytorch 使您可以非常灵活地创建自己的转换并控制边界框坐标发生的情况。

有关更多详细信息的文档: https://pytorch.org/docs/stable/torchvision/transforms.html#function-transforms

举个例子(根据文档修改):

import torchvision.transforms.functional as TF
import random

def my_rotation(image, bonding_box_coordinate):
    if random.random() > 0.5:
        angle = random.randint(-30, 30)
        image = TF.rotate(image, angle)
        bonding_box_coordinate = TF.rotate(bonding_box_coordinate, angle)
    # more transforms ...
    return image, bonding_box_coordinate
Run Code Online (Sandbox Code Playgroud)

希望有帮助=)