问题
运行faster rcnn时出现该错误时,需要的bounding boxes格式为:[xmin, ymin, xmax, ymax],但数据集导入时格式可能为[xmin, ymin, width, height],就会出现这种错误。
解决方法
torchvision.ops.boxes.box_convert提供了一个bounding boxes的转换函数,其用法
def box_convert(boxes: Tensor, in_fmt: str, out_fmt: str) -> Tensor:
"""
Converts boxes from given in_fmt to out_fmt.
Supported in_fmt and out_fmt are:
'xyxy': boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right.
'xywh' : boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.
'cxcywh' : boxes are represented via centre, width and height, cx, cy being center of box, w, h
being width and height.
Arguments:
boxes (Tensor[N, 4]): boxes which will be converted.
in_fmt (str): Input format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh'].
out_fmt (str): Output format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh']
Returns:
boxes (Tensor[N, 4]): Boxes into converted format.
"""
案例:
from torchvision.ops.boxes import box_convert
"""
... 数据导入部分省略
"""
boxes = box_convert(boxes=torch.tensor(boxes), in_fmt='xywh', out_fmt='xyxy')