Skip to content

I found a bug and improved it with a huge performance boost #20

@18112330636

Description

@18112330636

The core idea of this model is to regress the deviation of the four vertices of the quadrilateral to the center point. To simplify the problem, sort the vertices of the quad such that y3, y4 >= y1, y2; x1 <= x2; x4 <= x3. But there is a flaw. If the diagonal of the quadrilateral is parallel to the x-axis, then y2=y3, then this method is ambiguous, and the order of the left and right vertices on the diagonal is undefined. This ambiguity results in greatly reduced performance of the model. Moreover, three for loops are written in the method of sorting vertices, resulting in very slow training. Before the improvement, my data required training for 1000epoch to reach the 0.5 threshold of 94map. After the improvement, my data only needs to train 120epoch to reach the 0.5 threshold of 96map. Also, the pre-improved model cannot be scaled and rotated. After the improvement, the degree of polygon_random_perspective is changed to 90, the scale is changed to 0.5, and the model can still reach 96map at about 150epoch. Below is my code。I want to change jobs in Shanghai, China, Interested can contact me。 28 years old with three years of CV experience。number 18112330636

def order_corners(boxes):
"""
Return sorted corners for loss.py::class Polygon_ComputeLoss::build_targets
Sorted corners have the following restrictions:
y3, y4 >= y1, y2; x1 <= x2; x4 <= x3
"""
if boxes.shape[0] == 0:
return torch.empty(0, 8, device=boxes.device)

boxes = boxes.view(-1, 4, 2)
x = boxes[..., 0]
y = boxes[..., 1]
y_sorted, y_indices = torch.sort(y) # sort y
idx = torch.arange(0, y.shape[0], dtype=torch.long, device=boxes.device)
complete_idx = idx[:, None].repeat(1, 4)
x_sorted = x[complete_idx, y_indices]
x_sorted[:, :2], x_bottom_indices = torch.sort(x_sorted[:, :2])
x_sorted[:, 2:4], x_top_indices = torch.sort(x_sorted[:, 2:4], descending=True)
y_sorted[idx, :2] = y_sorted[idx, :2][complete_idx[:, :2], x_bottom_indices]
y_sorted[idx, 2:4] = y_sorted[idx, 2:4][complete_idx[:, 2:4], x_top_indices]
special = (y_sorted[:, 1] == y_sorted[:, 2]) & (x_sorted[:, 1] > x_sorted[:, 2])
if idx[special].shape[0]  != 0:
    x_sorted_1 = x_sorted[idx[special], 1].clone()
    x_sorted[idx[special], 1] = x_sorted[idx[special], 2]
    x_sorted[idx[special], 2] = x_sorted_1
return torch.stack((x_sorted, y_sorted), dim=2).view(-1, 8).contiguous()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions