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

7
在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是否允许将这些变换应用于图像的边界框坐标,并以后保存它们作为新的注释?谢谢。
1个回答

6
你所使用的变换示例不会改变边界框的坐标。ToTensor() 将PIL图像转换为torch张量,Normalize() 用于归一化图像通道。 RandomCrop()RandomRotation() 等变换将导致边界框的位置与(修改后)图像不匹配。
但是,Pytorch使您非常灵活地创建自己的转换,并控制边界框坐标的处理方式。
更多详细信息请参见文档:https://pytorch.org/docs/stable/torchvision/transforms.html#functional-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

希望这有所帮助 =)

谢谢你的回答,维克多。我看到了你发布的代码的好处。你能具体说明一下“bonding_box_coordinate”是什么类型的对象吗?因为通常大多数注释都以.txt或.xml格式存储。在原始文档中,不清楚是哪种对象分割。谢谢。 - Karen
我只是假设所有的输入都是torch张量。你可以在数据加载器中将输入转换为torch张量,或者创建一个转换器来帮助你完成这个过程。 - Victor Zuanazzi
在Pytorch中有一个YOLO3的实现,你可以参考一下:https://github.com/eriklindernoren/PyTorch-YOLOv3/blob/47b7c912877ca69db35b8af3a38d6522681b3bb3/utils/datasets.py#L130 - Victor Zuanazzi
我真的不明白你怎么会把输入数据作为张量使用,例如[0.12 0.45 0.48 0.34]。如果在TF.rotate中使用它,它将无法工作——因为这不是一张图像。 - Guenter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接