Yolov5: pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5

Created on 3 Dec 2020  ·  16Comments  ·  Source: ultralytics/yolov5

However, I want to know about why:
pxy = ps[:, :2].sigmoid() * 2. - 0.5
pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]

do * 2. - 0.5 and * 2) ** 2 these things.
I don't understand. so, I wonder!

❔Question

    pxy = ps[:, :2].sigmoid() * 2. - 0.5
    pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]

    pbox = torch.cat((pxy, pwh), 1).to(device)  # predicted box
    #print(pbox)
    giou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)  # giou(prediction, target)
    #print(tbox[i])
    print(len(giou))
    lbox += (1.0 - giou).mean()  # giou loss

Additional context

question

All 16 comments

Hello @wandaoyi, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.[email protected].

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

In general, object detection network doesn't learn the bbox coordinates directly, the targets for them to learn are the correction offset with predefined anchor boxes.

Thank you. And what's the math's meaning? could you talk about it?

------------------ 原始邮件 ------------------
发件人: "Zhiqiang Wang"<[email protected]>;
发送时间: 2020年12月3日(星期四) 下午3:16
收件人: "ultralytics/yolov5"<[email protected]>;
抄送: "求道"<[email protected]>; "Mention"<[email protected]>;
主题: Re: [ultralytics/yolov5] pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5 (#1585)

In general, object detection network doesn't learn the bbox coordinates directly, the targets for them to learn are the correction offset with the Anchor.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

This #471 is a good reference.

Thank you very much! 

Ori formula: b_x = sigmoid(t_x) * 2 - 0.5 + c_x

But, in the yolov5: pxy = ps[:, :2].sigmoid() * 2. - 0.5 It didn't add c_x, x_y

------------------ 原始邮件 ------------------
发件人: "Zhiqiang Wang"<[email protected]>;
发送时间: 2020年12月3日(星期四) 下午3:40
收件人: "ultralytics/yolov5"<[email protected]>;
抄送: "求道"<[email protected]>; "Mention"<[email protected]>;
主题: Re: [ultralytics/yolov5] pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5 (#1585)

This #471 is a good reference.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Hi @wandaoyi , your typing showing in GitHub is a little confusing, and I guess that self.grid as below should be understand to c_x and c_y

https://github.com/ultralytics/yolov5/blob/f0101475788590720a3a4b1152a89d531f311dff/models/yolo.py#L57-L58

In https://github.com/ultralytics/yolov5/issues/471 :  The function under the "And try to formularize it:" word in the page

The function is : 
b_x = sigmoid(t_x) * 2 - 0.5 + c_x
b_y = sigmoid(t_y) * 2 - 0.5 + c_y

But in the code:pxy = ps[:, :2].sigmoid() * 2. - 0.5
pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]

It means:
b_x = sigmoid(t_x) * 2 - 0.5
b_y = sigmoid(t_y) * 2 - 0.5

So, where is our c_x and c_y ?

------------------ 原始邮件 ------------------
发件人: "Zhiqiang Wang"<[email protected]>;
发送时间: 2020年12月3日(星期四) 晚上6:26
收件人: "ultralytics/yolov5"<[email protected]>;
抄送: "求道"<[email protected]>; "Mention"<[email protected]>;
主题: Re: [ultralytics/yolov5] pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5 (#1585)

Hi @wandaoyi , your typing showing here is a little confusing, and I guess that self.grid should be understand as c_x and c_y

https://github.com/ultralytics/yolov5/blob/f0101475788590720a3a4b1152a89d531f311dff/models/yolo.py#L57-L58


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Your formula miss the self.grid[i] as in L57, it play a role of c_x and c_y

I don't understand the code in:
yolov5/utils/utils.py
Lines 472 to 475 in 1e95337

GIoU 

pxy = ps[:, :2].sigmoid() * 2. - 0.5 
pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i] 
pbox = torch.cat((pxy, pwh), 1).to(device)  # predicted box 

There are not having any grid infos
So, there is nothing to role of c_x and c_y

------------------ 原始邮件 ------------------
发件人: "Zhiqiang Wang"<[email protected]>;
发送时间: 2020年12月3日(星期四) 晚上7:14
收件人: "ultralytics/yolov5"<[email protected]>;
抄送: "求道"<[email protected]>; "Mention"<[email protected]>;
主题: Re: [ultralytics/yolov5] pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5 (#1585)

Your formula miss the self.grid[i] as in L57, it play a role of c_x and c_y


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Hi @wandaoyi , So sad a story, I can't find the link you point to, I think you should double check the typing and the link showing on the GitHub pages.

OMG, The code of that in /utils/loss.py function: compute_loss (120~121 line) at yolov5 version of 3.0 But at 1.0 and 2.0 version, it's in utils/utils.py function: compute_loss (1.0: 452~453; 2.0: 474~475)

------------------ 原始邮件 ------------------
发件人: "Zhiqiang Wang"<[email protected]>;
发送时间: 2020年12月3日(星期四) 晚上7:24
收件人: "ultralytics/yolov5"<[email protected]>;
抄送: "求道"<[email protected]>; "Mention"<[email protected]>;
主题: Re: [ultralytics/yolov5] pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5 (#1585)

Hi @wandaoyi , So sad a story, I can't find the link you point to, I think you should double check the typing and the link showing on the GitHub pages.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Ah-hah, The learning stage don't need to add c_x and c_y, the formula concluded as in #471 is just used at the inference stage.

In general, object detection network doesn't learn the bbox coordinates directly, the targets for them to learn are the correction offset with predefined anchor boxes.

And that is the meaning of this line, we actually just learn the offset with predefined anchor boxes.

Thank you very much! Got it

------------------ 原始邮件 ------------------
发件人: "Zhiqiang Wang"<[email protected]>;
发送时间: 2020年12月3日(星期四) 晚上7:50
收件人: "ultralytics/yolov5"<[email protected]>;
抄送: "求道"<[email protected]>; "Mention"<[email protected]>;
主题: Re: [ultralytics/yolov5] pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5 (#1585)

In general, object detection network doesn't learn the bbox coordinates directly, the targets for them to learn are the correction offset with predefined anchor boxes.

And that is the meaning of this line, we actually just learn the offset with predefined anchor boxes.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

And if you're trying to actually understand the n * 2 - 0.5 part... At first it looked to me like a coding error, because converting from a range of 0..1 to a range of -1..1 requires (n - 0.5) * 2 which is similar. It's not an error. What's actually going on is the image is subdivided into a grid of squares, and the coordinates in grid[] are the coordinates of the upper-left corner of that square. The neural network provides x,y coordinates in the range 0..1 (enforced by sigmoid) which covers the square, centered at 0.5. Multiplying by two allows detected x,y coordinates to cover a larger range, slightly outside the square -- otherwise it's difficult to detect objects centered at grid boundaries. Subtracting 0.5 shifts the resulting range to -0.5 .. 1.5 which is centered around 0 .. 1.

Thank you very much.
I saw it in issue about bbox function:
b_x = sigmoid(t_x) * 2 - 0.5 + c_x
b_y = sigmoid(t_y) * 2 - 0.5 + c_y
In compute_loss(), there are no c_x or c_y, due to training is not needing about  that

------------------ 原始邮件 ------------------
发件人: "egbit"<[email protected]>;
发送时间: 2020年12月5日(星期六) 上午6:39
收件人: "ultralytics/yolov5"<[email protected]>;
抄送: "求道"<[email protected]>; "Mention"<[email protected]>;
主题: Re: [ultralytics/yolov5] pxy = ps[:, :2].sigmoid() * 2. - 0.5; why the sigmoid number multiply by 2 and minus 0.5 (#1585)

And if you're trying to actually understand the n * 2 - 0.5 part... At first it looked to me like a coding error, because converting from a range of 0..1 to a range of -1..1 requires (n - 0.5) * 2 which is similar. It's not an error. What's actually going on is the image is subdivided into a grid of squares, and the coordinates in grid[] are the coordinates of the upper-left corner of that square. The neural network provides x,y coordinates in the range 0..1 (enforced by sigmoid) which covers the square, centered at 0.5. Multiplying by two allows detected x,y coordinates to cover a larger range, slightly outside the square -- otherwise it's difficult to detect objects centered at grid boundaries. Subtracting 0.5 shifts the resulting range to -0.5 .. 1.5 which is centered around 0 .. 1.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or unsubscribe.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cswwp picture cswwp  ·  4Comments

FSNStefan picture FSNStefan  ·  4Comments

DucTaiVu picture DucTaiVu  ·  3Comments

maykulkarni picture maykulkarni  ·  3Comments

lisa676 picture lisa676  ·  3Comments