Exemplo n.º 1
0
    def _get_64_anchors_new(self, gtbox):
        pos = {}
        neg = {}
        zero = {}
        for a in range(17):
            for b in range(17):
                for c in range(5):
                    anchor = [self.grid_len//2+self.grid_len*a, self.grid_len//2+self.grid_len*b, self.anchor_shape[c][0], self.anchor_shape[c][1]]
                    anchor = xywh_to_x1y1x2y2(anchor)
                    anchor = clip_anchor(anchor,cfg.detection_size)
                    iou = self._IOU(anchor, gtbox)
                    # if iou>0.3:
                        # print(x1y1x2y2_to_xywh(anchor), gtbox, iou)

                    if iou >= cfg.pos_iou_thresh:
                        pos[(a,b,c)] = (iou, anchor)
                    elif iou <= cfg.neg_iou_thresh and iou > 0:
                        neg[(a,b,c)] = (iou, anchor)
                    else :
                        zero[(a,b,c)] = (iou, anchor)
        # print(len(pos), len(neg), len(zero))
        pos = sorted(pos.items(), key=lambda tup: tup[1][0], reverse=True)
        pos = [(*tup[0], tup[1][1]) for tup in pos[:16]]
        num = math.ceil((64-len(pos))/2)
        neg = sorted(neg.items(), key=lambda tup: tup[1][0], reverse=True)
        neg = [(*tup[0], tup[1][1]) for tup in neg[:num]]
        zero = list(zero.items())
        np.random.shuffle(zero)
        neg += [(*tup[0], tup[1][1]) for tup in zero[:64-len(pos)-len(neg)]]
        return pos, neg
Exemplo n.º 2
0
 def _get_label(self, gtbox):
     clabel = np.zeros([5, 17, 17]) - 100
     rlabel = np.zeros([20, 17, 17], dtype = np.float32)
     if self.phase in ['train','validation']:
         pos, neg = self._get_64_anchors_new(gtbox)
         assert len(pos)+len(neg)==64
         for a,b,c,anchor_x1y1x2y2 in pos:
             anchor = x1y1x2y2_to_xywh(anchor_x1y1x2y2)
             clabel[c,a,b] = 1
             channel0 = (gtbox[0] - anchor[0])/anchor[2]
             channel1 = (gtbox[1] - anchor[1])/anchor[3]
             channel2 = math.log(gtbox[2]/anchor[2])
             channel3 = math.log(gtbox[3]/anchor[3])
             rlabel[c*4:c*4+4,a,b] = [channel0, channel1, channel2, channel3]
         for a,b,c,anchor in neg:
             clabel[c,a,b] = 0
     else :
         for a in range(17):
             for b in range(17):
                 for c in range(5):
                     anchor = [self.grid_len//2+self.grid_len*a, self.grid_len//2+self.grid_len*b, self.anchor_shape[c][0], self.anchor_shape[c][1]]
                     anchor = xywh_to_x1y1x2y2(anchor)
                     anchor = clip_anchor(anchor,cfg.detection_size)
                     iou = self._IOU(anchor, gtbox)
                     anchor = x1y1x2y2_to_xywh(anchor)
                     if iou >= cfg.pos_iou_thresh:
                         clabel[c,a,b] = 1
                         channel0 = (gtbox[0] - anchor[0])/anchor[2]
                         channel1 = (gtbox[1] - anchor[1])/anchor[3]
                         channel2 = math.log(gtbox[2]/anchor[2])
                         channel3 = math.log(gtbox[3]/anchor[3])
                         rlabel[c*4:c*4+4,a,b] = [channel0, channel1, channel2, channel3]
                     elif iou <= cfg.neg_iou_thresh:
                         clabel[c,a,b] = 0
     return torch.Tensor(clabel).long(), torch.Tensor(rlabel).float()
Exemplo n.º 3
0
 def _get_64_anchors_multiflags(self,idx,gtboxList):
     pos = {}
     neg = {}
     zero = {}
     brother = {}
     for a in range(17):
         for b in range(17):
             for c in range(5):
                 anchor = [self.grid_len//2+self.grid_len*a, self.grid_len//2+self.grid_len*b, self.anchor_shape[c][0], self.anchor_shape[c][1]]
                 anchor_x1y1x2y2 = xywh_to_x1y1x2y2(anchor)
                 anchor_x1y1x2y2 = clip_anchor(anchor_x1y1x2y2,cfg.detection_size)
                 iouList = list(map(lambda gtbox_xywh: self._IOU(anchor_x1y1x2y2, gtbox_xywh) , gtboxList))
                 if iouList[idx]>cfg.pos_iou_thresh:
                     pos[(a,b,c)] = (iou,anchor_x1y1x2y2)
                 else:
                     iou = max(iouList[:idx]+iouList[idx+1:])
                     if iou > cfg.pos_iou_thresh:
                         brother[(a,b,c)] = (iou,anchor_x1y1x2y2)
                     iou = max(iou, iouList[idx])
                     if iou <= cfg.neg_iou_thresh and iou > 0:
                         neg[(a,b,c)] = (iou, anchor_x1y1x2y2)
                     elif iou == 0:
                         zero[(a,b,c)] = (iou, anchor_x1y1x2y2)
     pos = sorted(pos.items(), key=lambda tup: tup[1][0], reverse=True)
     pos = [(*tup[0], tup[1][1]) for tup in pos[:16]]  # list of (a,b,c,anchor_x1y1x2y2)
     brother = sorted(brother.items(), key=lambda tup: tup[1][0], reverse=True)
     brother = [(*tup[0], tup[1][1]) for tup in brother[:16]]
     num = math.ceil((64-len(pos)-len(brother))/2)
     zero = list(zero.items())
     np.random.shuffle(zero)
     zero = [(*tup[0], tup[1][1]) for tup in zero[:num]]
     neg = sorted(neg.items(), key=lambda tup: tup[1][0], reverse=True)
     neg = [(*tup[0], tup[1][1]) for tup in neg[:64-len(pos)-len(brother)-len(zero)]]
     neg += brother + zero
     return pos, neg
Exemplo n.º 4
0
def outputConvertor(coutput, routput, anchor_shape):
    coutput = coutput.detach().cpu().numpy()
    routput = routput.detach().cpu().numpy().reshape(5, 4, 17, 17)
    bboxList = []
    maxProb = 0
    for a in range(17):
        for b in range(17):
            for c in range(5):
                anchor = [
                    cfg.grid_len // 2 + cfg.grid_len * a,
                    cfg.grid_len // 2 + cfg.grid_len * b, anchor_shape[c][0],
                    anchor_shape[c][1]
                ]
                anchor_x1y1x2y2 = xywh_to_x1y1x2y2(anchor)
                anchor_x1y1x2y2 = clip_anchor(anchor_x1y1x2y2,
                                              cfg.detection_size)
                anchor = x1y1x2y2_to_xywh(anchor_x1y1x2y2)
                delta = coutput[c, 0, a, b] - coutput[c, 1, a, b]
                if delta > 10:
                    prob = 0
                elif delta < -10:
                    prob = 1
                else:
                    prob = 1 / (1 + math.exp(delta))
                maxProb = max(maxProb, prob)
                if prob >= 0.5:
                    # if coutput[c,1,a,b]>0.2:
                    bbox = [0, 0, 0, 0]
                    channel0, channel1, channel2, channel3 = routput[c, :, a,
                                                                     b]
                    bbox[0] = channel0 * anchor[2] + anchor[0]
                    bbox[1] = channel1 * anchor[3] + anchor[1]
                    bbox[2] = math.exp(channel2) * anchor[2]
                    bbox[3] = math.exp(channel3) * anchor[3]
                    bbox = xywh_to_x1y1x2y2(bbox)
                    try:
                        bbox = np.array(bbox, dtype=np.int32)
                    except:
                        print(channel0, channel1, channel2, channel3)
                        print(bbox)
                        continue
                        # raise OverflowError
                    bboxList.append((bbox, prob))
    bboxList.sort(key=lambda tup: tup[1], reverse=True)
    return list(map(lambda tup: tup[0], bboxList))[:5], maxProb