chenyuntc / simple-faster-rcnn-pytorch

A simplified implemention of Faster R-CNN that replicate performance from origin paper
Other
3.98k stars 1.14k forks source link

Use the default threshold, too many negitive sample #68

Open Kyle1993 opened 6 years ago

Kyle1993 commented 6 years ago

I find the AnchorTargetCreator and ProposalTargetCreator will sample too many negitive samples when use the defalut iou_threshold, is it correct? in my code, when inference, the rois always be judged as ‘background’(which means class_label=0) in ROIhHead network

      foreground  background
------------------------
RPN samples:54  202
ROI samples:13  115
------------------------
RPN samples:22  234
ROI samples:32  96
------------------------
RPN samples:27  229
ROI samples:32  96
------------------------
RPN samples:43  213
ROI samples:16  112
------------------------
RPN samples:15  241
ROI samples:32  96
------------------------
RPN samples:7   249
ROI samples:6   122
------------------------
RPN samples:45  211
ROI samples:32  96
------------------------
RPN samples:6   250
ROI samples:14  114
------------------------
RPN samples:23  233
ROI samples:12  116
------------------------
RPN samples:36  220
ROI samples:14  114
------------------------
RPN samples:50  206
ROI samples:25  103
------------------------
RPN samples:12  244
ROI samples:10  118
------------------------
RPN samples:22  234
ROI samples:18  110
...
Kyle1993 commented 6 years ago

Actually I reset the iou_threshold in AnchorTargetCreator and ProposalTargetCreator to make the ratio of positive and negitive be 1:1 almost

in AnchorTargetCreator:
pos_iou_thresh=0.6, neg_iou_thresh=0.2
in ProposalTargetCreator:
pos_ratio=0.8, pos_iou_thresh=0.4,
neg_iou_thresh_hi=0.3, neg_iou_thresh_lo=0.1

but I still meet the same problem, the rois always be judged as ‘background’(which means class_label=0) in inference

FortiLeiZhang commented 6 years ago

there is a critical bug in this implementation, refer to the issue

Kyle1993 commented 6 years ago

@FortiLeiZhang I find the same problem and have changed the code, but dosen't work

        rpn_scores = self.score(h)
        rpn_scores = rpn_scores.permute(0, 2, 3, 1).contiguous()
        rpn_scores = F.softmax(rpn_scores.view(n, hh, ww, n_anchor, 2),dim=4)
        rpn_fg_scores = rpn_scores[:, :, :, :, 1].contiguous()

        rpn_fg_scores = rpn_fg_scores.view(n, -1)

        rpn_scores = rpn_scores.view(n, -1, 2)
chenyuntc commented 6 years ago

@Kyle1993 It is natural to have more background than foreground. Also, you have to adjust anchor size to your target.

BTW, something is wrong here:

rpn_scores = F.softmax(rpn_scores.view(n, hh, ww, n_anchor, 2),dim=4)

There is softmax in cross entropy loss, so you should not modify rpn_scores here.

Actually it should be

rpn_softmax_scores = F.softmax(rpn_scores.view(n, hh, ww, n_anchor, 2), dim=4)
rpn_fg_scores = rpn_softmax_scores[:, :, :, :, 1].contiguous()
fatalfeel commented 3 years ago

why use rpn_softmax_scores[:, :, :, :, 1].contiguous() be foreground can rpn_softmax_scores[:, :, :, :, 0].contiguous() be foreground ???

h = self.conv1(vggfeatures) h = tnf.relu(h)

    # UNNOTE: check whether need contiguous
    rpn_scores = self.score(h)
    rpn_scores = rpn_scores.permute(0, 2, 3, 1).contiguous()

    rpn_locs = self.loc(h)
    rpn_locs = rpn_locs.permute(0, 2, 3, 1).contiguous().view(n, -1, 4)

    #rpn_softmax_scores = tnf.softmax(rpn_scores.view(n, hh, ww, n_anchor, 2), dim=4)
    scores_view         = rpn_scores.view(n, hh, ww, n_anchor, 2)
    rpn_softmax_scores  = tnf.softmax(scores_view, dim=4)

    rpn_fg_scores   = rpn_softmax_scores[:, :, :, :, 1].contiguous()