import torch


def change_box_order(boxes, order, dim=-1):
    """Change box order between (x_min, y_min, x_max, y_max) and (x_center, y_center, width, height).

    Args:
      boxes: (tensor) bounding boxes, sized [N, 4].
      order: (str) either 'xyxy2xywh' or 'xywh2xyxy'.

    Returns:
      (tensor) converted bounding boxes, sized [N, 4].
    """
    assert order in ["xyxy2xywh", "xywh2xyxy"]
    a = boxes[..., 0:2]
    b = boxes[..., 2:4]
    if order == "xyxy2xywh":
        return torch.cat([(a + b) / 2, b - a], dim)
    return torch.cat([a - b / 2, a + b / 2], dim)