-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Train Faster R-CNN with negative samples #1911
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
Conversation
I'm out of depth for this particular code-piece and will leave the review to @fmassa |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR looks great Monica, thanks a lot!
I have only a few minor comments, can you look into it? Once they are addressed this will be good to merge.
Also, I think that we should also make sure that mask r-cnn and keypoint r-cnn work with empty targets, could you look into it in a follow-up PR?
Once again thanks a lot!
@@ -730,7 +743,8 @@ def forward(self, features, proposals, image_shapes, targets=None): | |||
for t in targets: | |||
# TODO: https://github.com/pytorch/pytorch/issues/26731 | |||
floating_point_types = (torch.float, torch.double, torch.half) | |||
assert t["boxes"].dtype in floating_point_types, 'target boxes must of float type' | |||
if t["boxes"] is not None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this conditional is not needed anymore
|
||
gt_boxes_in_image = gt_boxes[img_id] | ||
if gt_boxes_in_image.numel() == 0: | ||
gt_boxes_in_image = torch.zeros((1, 4), dtype=dtype) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to take the device of gt_boxes_in_image
into account?
clamped_matched_idxs_in_image = torch.zeros( | ||
(proposals_in_image.shape[0],), dtype=torch.int64 | ||
) | ||
labels_in_image = torch.zeros((proposals_in_image.shape[0],), dtype=torch.int64) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be safer if we also take the device
of proposals_in_image
into account while creating those tensors
Codecov Report
@@ Coverage Diff @@
## master #1911 +/- ##
========================================
Coverage ? 0.48%
========================================
Files ? 92
Lines ? 7442
Branches ? 1133
========================================
Hits ? 36
Misses ? 7393
Partials ? 13
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot Monica!
Thanks for this feature!
One question though: why does target["labels"] look like this? Shouldn't it be of length 0 (like boxes), or with only one dimension (not two) like specified here: |
Thanks @mnc537 ! |
Thank you @mnc537. I've been using this PR and did not experience any issue yet. |
* modified FasterRCNN to accept negative samples * remove debug lines * Change torch.zeros_like to torch.zerros * Add unit tests * take the `device` into account Co-authored-by: Francisco Massa <[email protected]>
I have a question about the targe["labels"], According to the PR,
@mnc537 said there was a typo, so the target should look like I tried all above but there weren't any errors. Thank you for the feature. |
Does anyone encounter the problem that loss explodes after I add the negative background |
@mnc537 Thanks for this feature. Just wondering, do we need to increase the number of classes once we add the targets for negative samples? |
@samra-irshad The number of classes is the number of your objects + 1(negative samples, or image with only background). |
@Kirayue So background class (0) and negative samples (images with no object) should have same label? Or I should allocate an additional label to images with no objects? |
@samra-irshad, they are the same, so you do not need to add an additional label to indicate no objects. |
This error occurs when I try to use negative sampling on unlabelled images using: target["boxes"] = torch.zeros((0, 4), dtype=torch.float32) ValueError: Expected target boxes to be a tensorof shape [N, 4], got torch.Size([0]). Any ideas on how to address this? |
@mnc537 how should the target masks look like?? |
Hello, this is a feature that I wish was elaborated upon a bit further in the docs as it is quite useful and I am trying to get it to work. I believe I can implement it when using the Detecto library but I am having a couple of issues, thought it would be best to follow up on this thread if anyone is watching it.
|
Hello,
This PR allows FasterRCNN to train with negative samples.
Related to: #1598
When defining the dataset, one needs to set the field
boxes
in thetarget
dict astorch.zeros((0, 4), dtype=torch.float32)
for negative images sinceboxes
is required.This is how
target
should look like: