Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support autoenvolved anchors as ultralytics #119

Closed
Root970103 opened this issue Jun 15, 2021 · 4 comments · Fixed by #120
Closed

Support autoenvolved anchors as ultralytics #119

Root970103 opened this issue Jun 15, 2021 · 4 comments · Fixed by #120
Labels
enhancement New feature or request

Comments

@Root970103
Copy link

Root970103 commented Jun 15, 2021

When converting the yolov5 custom model to yolort, the configuration of Anchors is fixed. This will cause a large gap between the two prediction results.

This is my test results for reference.

Detection results with ultralytics
tensor([[ 60.20518, 275.15906, 227.31180, 460.78314,   0.95166,   2.00000],
        [239.06186,  78.73691, 411.81058, 264.97150,   0.95022,   0.00000],
        [245.05939, 421.12323, 407.59259, 505.68793,   0.94337,   2.00000],
        [241.37396, 348.25543, 413.31641, 406.12311,   0.93951,   0.00000],
        [ 68.77753, 465.43747, 217.82889, 519.14197,   0.93248,   5.00000],
        [245.88387, 509.54541, 404.57898, 541.54272,   0.92447,   5.00000],
        [ 60.48768,  78.21830, 225.84097, 269.68933,   0.91452,   0.00000],
        [242.17107, 550.16064, 413.19885, 585.58118,   0.90575,   0.00000],
        [ 60.04885, 564.58539, 184.14281, 573.51935,   0.88148,   6.00000],
        [257.21490, 295.98309, 397.26947, 315.52979,   0.87154,   7.00000],
        [270.75903, 335.44797, 379.67609, 346.24161,   0.86142,   7.00000],
        [276.42148, 269.73453, 380.06650, 281.44186,   0.85676,   7.00000],
        [ 58.77311, 539.35565, 129.32153, 550.79681,   0.84660,   6.00000],
        [299.25656, 317.71469, 353.17801, 333.91336,   0.84569,   7.00000],
        [ 70.90355, 576.13129, 228.42677, 586.76593,   0.82126,   0.00000],
        [272.83978, 282.16409, 379.86005, 293.18692,   0.67504,   7.00000]])
Detection results with yolort

boxes:

tensor([[114.73997, 286.17834, 172.77701, 449.76385],
        [296.58661, 128.59970, 354.28583, 215.10870],
        [298.10165, 426.14325, 354.55032, 500.66791],
        [310.67633, -24.03447, 340.96069, 367.68439],
        [298.63022, 363.74905, 356.06015, 390.62949],
        [310.64130, 375.32629, 340.15778, 551.87183],
        [279.98190,  71.63794, 372.35941, 273.37885],
        [281.49362, 345.05667, 372.36020, 408.86185],
        [117.42003, 468.62558, 169.18639, 515.95392],
        [318.71570, 317.67377, 335.81323, 436.61597],
        [226.89404,  79.70534, 424.16278, 265.10114],
        [ 46.96851, 273.46609, 239.71344, 461.19077],
        [315.01773, 509.35077, 335.44510, 541.73737],
        [302.44864, 511.76874, 347.06296, 539.38751],
        [114.45029,  89.58469, 171.87836, 258.32294],
        [317.77310, 491.92383, 333.15158, 558.55872],
        [299.12274, 559.64423, 356.24719, 576.09760],
        [318.85751, 531.31708, 335.88858, 604.20856],
        [135.67125, 436.88367, 151.06697, 547.62537],
        [ 44.50909,  77.33535, 241.77551, 270.65427],
        [114.10909, 564.53101, 130.08257, 573.57373],
        [128.94606, 174.29877, 157.97704, 560.87738],
        [318.22821, 295.86420, 336.25616, 315.64868],
        [127.40814, -24.54239, 158.43588, 373.00723],
        [231.56476, 419.62057, 420.35730, 506.79044],
        [105.16776, 565.24457, 139.56566, 572.73053],
        [319.92688, 283.30414, 333.55621, 328.24921],
        [316.60138, 550.55896, 338.71069, 585.45984],
        [318.20761, 335.38226, 332.22751, 346.30725],
        [307.69409, 297.45999, 346.46869, 314.14407],
        [310.38986, 336.16382, 340.24734, 345.19263],
        [321.57333, 269.66330, 334.91464, 281.51309],
        [116.27255, 558.97729, 128.35686, 579.16809],
        [319.82718, 328.34348, 330.08755, 353.56308],
        [ 89.50679, 539.28607,  98.58784, 550.86639],
        [322.74689, 317.61615, 329.68768, 334.01190],
        [313.31296, 270.62665, 342.22092, 280.61163],
        [318.55316, 318.89578, 333.70508, 332.48337],
        [323.54080, 307.02875, 328.81540, 344.24860],
        [ 84.36114, 540.25403, 104.05352, 550.03149],
        [323.00452, 262.09003, 333.10120, 289.10217],
        [ 90.60516, 532.02325,  97.54469, 558.19513],
        [127.85805, 576.88733, 171.47224, 586.00989],
        [141.52585, 569.54669, 157.04645, 593.17780],
        [139.58984, 575.94946, 159.91479, 586.71265],
        [319.46204, 282.09702, 333.23779, 293.25394],
        [311.79651, 283.07559, 341.74689, 292.15579],
        [321.21085, 275.04596, 331.59256, 300.28229],
        [322.56943, 287.68039, 336.84097, 324.22037]])

scores:

tensor([0.95166, 0.95022, 0.94337, 0.94313, 0.93951, 0.93825, 0.93778, 0.93444, 0.93248, 0.93215, 0.92636, 0.92569, 0.92447, 0.92406, 0.91452, 0.90793, 0.90575, 0.90452, 0.89434, 0.89394, 0.88148, 0.8779
0, 0.87154, 0.86893, 0.86798, 0.86740, 0.86400, 0.86210, 0.86142, 0.86071, 0.85727, 0.85676, 0.85073, 0.85071,
        0.84660, 0.84569, 0.84113, 0.83803, 0.83582, 0.83308, 0.82758, 0.82343, 0.82126, 0.79421, 0.78466, 0.67504, 0.66505, 0.66226, 0.28443])

labels:

tensor([2, 0, 2, 0, 0, 2, 0, 0, 5, 0, 0, 2, 5, 5, 0, 5, 0, 0, 5, 0, 6, 2, 7, 0, 2, 6, 7, 0, 7, 7, 7, 7, 6, 7, 6, 7, 7, 7, 7, 6, 7, 6, 0, 0, 0, 7, 7, 7, 0])

I also converted the official model, and there is no difference between the two models. So I hope someone can provide the method to change the default configuration of Anchors.

@zhiqwang zhiqwang changed the title Change the default anchor Support autoenvolved anchors as ultralytics Jun 15, 2021
@zhiqwang zhiqwang added the enhancement New feature or request label Jun 15, 2021
@Tomakko
Copy link
Contributor

Tomakko commented Jun 15, 2021

I just fixed this issue today, I will make a PR for this in the next days!

@zhiqwang
Copy link
Owner

I just fixed this issue today, I will make a PR for this in the next days!

Thanks @Tomakko , we are eager for your contribution!

@zhiqwang
Copy link
Owner

zhiqwang commented Jun 16, 2021

FYI, Now we can attach the anchor_grids from ultralytics as following:

model = torch.hub.load('ultralytics/yolov5', 'custom', path=ultralytics_weights_path)
anchor_grids = model.model.model[-1].anchor_grid.view(3, -1).cpu().numpy().tolist()

And then pass it into yolort:

from yolort.models.yolo import yolov5_darknet_pan_s_r40 as yolov5s
model = yolov5s(anchor_grids=anchor_grids, num_classes=10)

@Tomakko
Copy link
Contributor

Tomakko commented Jun 17, 2021

Make sure to disable autoshape in
model = torch.hub.load('ultralytics/yolov5', 'custom', path=ultralytics_weights_path, autoshape=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants