Beispiel #1
0
def filter_detections_for_dumping(args, scores, decoded_boxes_batch,
                                  confidences):
    c_mask = scores.gt(args.GEN_CONF_THRESH)  # greater than minmum threshold
    scores = scores[c_mask].squeeze()
    if scores.dim() == 0 or scores.shape[0] == 0:
        return np.zeros((0, 5)), np.zeros((0, 200))

    boxes = decoded_boxes_batch[c_mask, :].clone().view(-1, 4)
    numc = confidences.shape[-1]
    confidences = confidences[c_mask, :].clone().view(-1, numc)

    # sorted_ind = np.argsort(-scores.cpu().numpy())
    # sorted_ind = sorted_ind[:topk*10]
    # boxes_np = boxes.cpu().numpy()
    # confidences_np = confidences.cpu().numpy()
    # save_data = np.hstack((boxes_np[sorted_ind,:], confidences_np[sorted_ind, :]))
    # args.GEN_TOPK, args.GEN_NMS

    max_k = min(args.GEN_TOPK * 500, scores.shape[0])
    ids, counts = nms(boxes, scores, args.GEN_NMS,
                      max_k)  # idsn - ids after nms
    scores = scores[ids[:min(args.GEN_TOPK, counts)]].cpu().numpy()
    boxes = boxes[ids[:min(args.GEN_TOPK, counts)], :].cpu().numpy()
    confidences = confidences[
        ids[:min(args.GEN_TOPK, counts)], :].cpu().numpy()
    cls_dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32,
                                                                copy=True)
    save_data = np.hstack((cls_dets, confidences[:, 1:])).astype(np.float32)
    #print(save_data.shape)
    return cls_dets, save_data
Beispiel #2
0
def apply_nms(boxes, scores, sf_conf, num_nodes, conf_thresh, nms_thresh):

    all_ids = torch.arange(0, scores.size(0), 1).cuda()

    c_mask = scores.gt(conf_thresh)  # greater than minmum threshold
    new_scores = scores[c_mask].squeeze()
    all_ids = all_ids[c_mask].squeeze()

    if new_scores.dim() == 0:
        all_ids = torch.arange(0, scores.size(0), dtype=torch.cuda.LongTensor)
        c_mask = scores.gt(conf_thresh *
                           0.001)  # greater than minmum threshold
        new_scores = scores[c_mask].squeeze()
        all_ids = all_ids[c_mask].squeeze()

    # boxes = decoded_boxes.clone()
    l_mask = c_mask.unsqueeze(1).expand_as(boxes)
    boxes = boxes[l_mask].view(-1, 4)
    l_mask = c_mask.unsqueeze(1).expand_as(sf_conf)
    # pdb.set_trace()
    sf_conf = sf_conf[l_mask].view(-1, sf_conf.size(1))
    # idx of highest scoring and non-overlapping boxes per class
    ids, counts = nms(boxes, new_scores, nms_thresh, num_nodes * 20)

    news = ids[:counts]
    new_scores = new_scores[news]
    all_ids = all_ids[news]
    boxes = boxes[news]
    sf_conf = sf_conf[news]

    # pdb.set_trace()
    return boxes, all_ids, sf_conf, new_scores
Beispiel #3
0
def filter_detections(args, scores, decoded_boxes_batch):
    c_mask = scores.gt(args.CONF_THRESH)  # greater than minmum threshold
    scores = scores[c_mask].squeeze()
    if scores.dim() == 0 or scores.shape[0] == 0:
        return np.asarray([])

    boxes = decoded_boxes_batch[c_mask, :].view(-1, 4)
    ids, counts = nms(boxes, scores, args.NMS_THRESH,
                      args.TOPK * 5)  # idsn - ids after nms
    scores = scores[ids[:min(args.TOPK, counts)]].cpu().numpy()
    boxes = boxes[ids[:min(args.TOPK, counts)]].cpu().numpy()
    cls_dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32,
                                                                copy=True)

    return cls_dets
Beispiel #4
0
def filter_detections_for_tubing(args, scores, decoded_boxes_batch,
                                 confidences):
    c_mask = scores.gt(args.CONF_THRESH)  # greater than minmum threshold
    scores = scores[c_mask].squeeze()
    if scores.dim() == 0 or scores.shape[0] == 0:
        return np.zeros((0, 200))

    boxes = decoded_boxes_batch[c_mask, :].clone().view(-1, 4)
    numc = confidences.shape[-1]
    confidences = confidences[c_mask, :].clone().view(-1, numc)

    max_k = min(args.TOPK * 60, scores.shape[0])
    ids, counts = nms(boxes, scores, args.NMS_THRESH,
                      max_k)  # idsn - ids after nms
    scores = scores[ids[:min(args.TOPK, counts)]].cpu().numpy()
    boxes = boxes[ids[:min(args.TOPK, counts)], :].cpu().numpy()
    confidences = confidences[ids[:min(args.TOPK, counts)], :].cpu().numpy()
    cls_dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32,
                                                                copy=True)
    save_data = np.hstack((cls_dets, confidences[:, 1:])).astype(np.float32)
    #print(save_data.shape)
    return save_data
Beispiel #5
0
def validate(args,
             net,
             anchors,
             val_data_loader,
             val_dataset,
             iteration_num,
             iou_thresh=0.5):
    """Test a FPN network on an image database."""
    print('Validating at ', iteration_num)
    num_images = len(val_dataset)
    num_classes = args.num_classes

    det_boxes = [[] for _ in range(num_classes - 1)]
    gt_boxes = []
    print_time = True
    val_step = 20
    count = 0
    torch.cuda.synchronize()
    ts = time.perf_counter()
    softmax = nn.Softmax(dim=2).cuda()
    with torch.no_grad():
        for val_itr, (images, targets,
                      img_indexs) in enumerate(val_data_loader):

            torch.cuda.synchronize()
            t1 = time.perf_counter()

            batch_size = images.size(0)
            height, width = images.size(2), images.size(3)

            images = images.cuda(0, non_blocking=True)
            loc_data, conf_data = net(images)

            conf_scores_all = softmax(conf_data).clone()

            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                tf = time.perf_counter()
                print('Forward Time {:0.3f}'.format(tf - t1))
            for b in range(batch_size):
                gt = targets[b].numpy()
                gt[:, 0] *= width
                gt[:, 2] *= width
                gt[:, 1] *= height
                gt[:, 3] *= height
                gt_boxes.append(gt)
                decoded_boxes = decode(loc_data[b], anchors,
                                       [0.1, 0.2]).clone()
                conf_scores = conf_scores_all[b].clone()
                #Apply nms per class and obtain the results
                for cl_ind in range(1, num_classes):
                    # pdb.set_trace()
                    scores = conf_scores[:, cl_ind].squeeze()
                    c_mask = scores.gt(
                        args.conf_thresh)  # greater than minmum threshold
                    scores = scores[c_mask].squeeze()
                    # print('scores size',scores.size())
                    if scores.dim() == 0:
                        # print(len(''), ' dim ==0 ')
                        det_boxes[cl_ind - 1].append(np.asarray([]))
                        continue
                    boxes = decoded_boxes.clone()
                    l_mask = c_mask.unsqueeze(1).expand_as(boxes)
                    boxes = boxes[l_mask].view(-1, 4)
                    # idx of highest scoring and non-overlapping boxes per class
                    ids, counts = nms(boxes, scores, args.nms_thresh,
                                      args.topk)  # idsn - ids after nms
                    scores = scores[ids[:counts]].cpu().numpy()
                    boxes = boxes[ids[:counts]].cpu().numpy()
                    # print('boxes sahpe',boxes.shape)
                    boxes[:, 0] *= width
                    boxes[:, 2] *= width
                    boxes[:, 1] *= height
                    boxes[:, 3] *= height

                    for ik in range(boxes.shape[0]):
                        boxes[ik, 0] = max(0, boxes[ik, 0])
                        boxes[ik, 2] = min(width, boxes[ik, 2])
                        boxes[ik, 1] = max(0, boxes[ik, 1])
                        boxes[ik, 3] = min(height, boxes[ik, 3])

                    cls_dets = np.hstack(
                        (boxes, scores[:, np.newaxis])).astype(np.float32,
                                                               copy=True)
                    det_boxes[cl_ind - 1].append(cls_dets)
                count += 1
            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                te = time.perf_counter()
                print('im_detect: {:d}/{:d} time taken {:0.3f}'.format(
                    count, num_images, te - ts))
                torch.cuda.synchronize()
                ts = time.perf_counter()
            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                te = time.perf_counter()
                print('NMS stuff Time {:0.3f}'.format(te - tf))

    print('Evaluating detections for itration number ', iteration_num)
    return evaluate_detections(gt_boxes,
                               det_boxes,
                               val_dataset.classes,
                               iou_thresh=iou_thresh)
Beispiel #6
0
def validate(args,
             net,
             val_data_loader,
             val_dataset,
             iteration_num,
             iou_thresh=0.5):
    """Test a FPN network on an image database."""
    print('Validating at ', iteration_num)
    num_images = len(val_dataset)
    num_classes = args.num_classes

    det_boxes = [[] for _ in range(num_classes - 1)]
    gt_boxes = []
    print_time = True
    val_step = 20
    count = 0
    torch.cuda.synchronize()
    ts = time.perf_counter()
    activation = nn.Sigmoid().cuda()
    if args.loss_type == 'mbox':
        activation = nn.Softmax(dim=2).cuda()

    dict_for_json_dump = {}

    with torch.no_grad():
        for val_itr, (images, targets, batch_counts, img_indexs,
                      wh) in enumerate(val_data_loader):

            torch.cuda.synchronize()
            t1 = time.perf_counter()

            batch_size = images.size(0)

            images = images.cuda(0, non_blocking=True)
            decoded_boxes, conf_data = net(images)

            conf_scores_all = activation(conf_data).clone()

            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                tf = time.perf_counter()
                print('Forward Time {:0.3f}'.format(tf - t1))

            for b in range(batch_size):
                width, height = wh[b][0], wh[b][1]
                gt = targets[b, :batch_counts[b]].numpy()
                gt_boxes.append(gt)
                # decoded_boxes = decode(loc_data[b], anchors).clone()
                conf_scores = conf_scores_all[b]
                #Apply nms per class and obtain the results
                decoded_boxes_b = decoded_boxes[b]
                for cl_ind in range(1, num_classes):
                    # pdb.set_trace()
                    scores = conf_scores[:, cl_ind].squeeze()
                    if args.loss_type == 'yolo':
                        scores = conf_scores[:, cl_ind].squeeze(
                        ) * conf_scores[:, 0].squeeze() * 5.0
                    c_mask = scores.gt(
                        args.conf_thresh)  # greater than minmum threshold
                    scores = scores[c_mask].squeeze()
                    # print('scores size',c_mask.sum())
                    if scores.dim() == 0:
                        # print(len(''), ' dim ==0 ')
                        det_boxes[cl_ind - 1].append(np.asarray([]))
                        continue
                    # boxes = decoded_boxes_b.clone()
                    l_mask = c_mask.unsqueeze(1).expand_as(decoded_boxes_b)
                    boxes = decoded_boxes_b[l_mask].clone().view(-1, 4)
                    # idx of highest scoring and non-overlapping boxes per class
                    ids, counts = nms(boxes, scores, args.nms_thresh,
                                      args.topk * 20)  # idsn - ids after nms
                    scores = scores[ids[:min(args.topk, counts)]].cpu().numpy()
                    # pick = min(scores.shape[0], 20)
                    # scores = scores[:pick]
                    boxes = boxes[ids[:min(args.topk, counts)]].cpu().numpy()

                    for ik in range(boxes.shape[0]):
                        boxes[ik, 0] = max(0, boxes[ik, 0])
                        boxes[ik, 2] = min(width, boxes[ik, 2])
                        boxes[ik, 1] = max(0, boxes[ik, 1])
                        boxes[ik, 3] = min(height, boxes[ik, 3])

                    cls_dets = np.hstack(
                        (boxes, scores[:, np.newaxis])).astype(np.float32,
                                                               copy=True)
                    det_boxes[cl_ind - 1].append(cls_dets)
                count += 1

            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                te = time.perf_counter()
                print('im_detect: {:d}/{:d} time taken {:0.3f}'.format(
                    count, num_images, te - ts))
                torch.cuda.synchronize()
                ts = time.perf_counter()
            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                te = time.perf_counter()
                print('NMS stuff Time {:0.3f}'.format(te - tf))

    print('Evaluating detections for itration number ', iteration_num)
    return evaluate_detections(gt_boxes,
                               det_boxes,
                               val_dataset.classes,
                               iou_thresh=iou_thresh)
Beispiel #7
0
def validate_coco(args,
                  net,
                  anchors,
                  val_data_loader,
                  val_dataset,
                  iteration_num,
                  iou_thresh=0.5):
    """Test a FPN network on an image database."""
    print('Validating at ', iteration_num)

    annFile = '{}/instances_{}.json'.format(args.data_dir, args.val_sets[0])
    cocoGT = COCO(annFile)
    coco_dets = []
    resFile = args.save_root + 'detections-{:05d}.json'.format(args.det_itr)
    resFile_txt = open(
        args.save_root + 'detections-{:05d}.txt'.format(args.det_itr), 'w')
    num_images = len(val_dataset)
    num_classes = args.num_classes

    det_boxes = [[] for _ in range(num_classes - 1)]
    gt_boxes = []
    print_time = True
    val_step = 20
    count = 0
    torch.cuda.synchronize()
    ts = time.perf_counter()
    softmax = nn.Softmax(dim=2).cuda()
    idlist = val_dataset.idlist
    all_ids = val_dataset.ids

    with torch.no_grad():
        for val_itr, (images, targets, img_indexs,
                      wh) in enumerate(val_data_loader):
            # if val_itr>1:
            #     break
            torch.cuda.synchronize()
            t1 = time.perf_counter()

            batch_size = images.size(0)
            height, width = images.size(2), images.size(3)

            images = images.cuda(0, non_blocking=True)
            loc_data, conf_data = net(images)

            conf_scores_all = softmax(conf_data).clone()

            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                tf = time.perf_counter()
                print('Forward Time {:0.3f}'.format(tf - t1))
            for b in range(batch_size):

                coco_image_id = int(all_ids[img_indexs[b]][1][8:])
                width, height = wh[b][0], wh[b][1]
                gt = targets[b].numpy()
                gt[:, 0] *= width
                gt[:, 2] *= width
                gt[:, 1] *= height
                gt[:, 3] *= height
                gt_boxes.append(gt)
                decoded_boxes = decode(loc_data[b], anchors,
                                       [0.1, 0.2]).clone()
                conf_scores = conf_scores_all[b].clone()
                #Apply nms per class and obtain the results
                for cl_ind in range(1, num_classes):
                    # pdb.set_trace()
                    scores = conf_scores[:, cl_ind].squeeze()
                    c_mask = scores.gt(
                        args.conf_thresh)  # greater than minmum threshold
                    scores = scores[c_mask].squeeze()
                    # print('scores size',scores.size())
                    if scores.dim() == 0:
                        # print(len(''), ' dim ==0 ')
                        det_boxes[cl_ind - 1].append(np.asarray([]))
                        continue
                    boxes = decoded_boxes.clone()
                    l_mask = c_mask.unsqueeze(1).expand_as(boxes)
                    boxes = boxes[l_mask].view(-1, 4)
                    # idx of highest scoring and non-overlapping boxes per class
                    ids, counts = nms(boxes, scores, args.nms_thresh,
                                      args.topk)  # idsn - ids after nms
                    scores = scores[ids[:counts]].cpu().numpy()
                    pick = min(scores.shape[0], 20)
                    scores = scores[:pick]
                    boxes = boxes[ids[:counts]].cpu().numpy()
                    boxes = boxes[:pick, :]
                    # print('boxes sahpe',boxes.shape)
                    boxes[:, 0] *= width
                    boxes[:, 2] *= width
                    boxes[:, 1] *= height
                    boxes[:, 3] *= height
                    cls_id = cl_ind - 1
                    if len(idlist) > 0:
                        cls_id = idlist[cl_ind - 1]
                    # pdb.set_trace()
                    for ik in range(boxes.shape[0]):
                        boxes[ik, 0] = max(0, boxes[ik, 0])
                        boxes[ik, 2] = min(width, boxes[ik, 2])
                        boxes[ik, 1] = max(0, boxes[ik, 1])
                        boxes[ik, 3] = min(height, boxes[ik, 3])
                        box_ = [
                            round(boxes[ik, 0], 1),
                            round(boxes[ik, 1], 1),
                            round(boxes[ik, 2], 1),
                            round(boxes[ik, 3], 1)
                        ]
                        box_[2] = round(box_[2] - box_[0], 1)
                        box_[3] = round(box_[3] - box_[1], 1)
                        box_ = [float(b) for b in box_]
                        coco_dets.append({
                            "image_id": int(coco_image_id),
                            "category_id": int(cls_id),
                            "bbox": box_,
                            "score": float(scores[ik]),
                        })

                    cls_dets = np.hstack(
                        (boxes, scores[:, np.newaxis])).astype(np.float32,
                                                               copy=True)
                    det_boxes[cl_ind - 1].append(cls_dets)
                count += 1
            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                te = time.perf_counter()
                print('im_detect: {:d}/{:d} time taken {:0.3f}'.format(
                    count, num_images, te - ts))
                torch.cuda.synchronize()
                ts = time.perf_counter()
            if print_time and val_itr % val_step == 0:
                torch.cuda.synchronize()
                te = time.perf_counter()
                print('NMS stuff Time {:0.3f}'.format(te - tf))

    # print('Evaluating detections for itration number ', iteration_num)

    mAP, ap_all, ap_strs, det_boxes = evaluate_detections(
        gt_boxes, det_boxes, val_dataset.classes, iou_thresh=iou_thresh)

    for ap_str in ap_strs:
        print(ap_str)
        resFile_txt.write(ap_str + '\n')
    ptr_str = '\nMEANAP:::=>' + str(mAP) + '\n'
    print(ptr_str)
    resFile_txt.write(ptr_str)

    print('saving results :::::')
    with open(resFile, 'w') as f:
        json.dump(coco_dets, f)

    cocoDt = cocoGT.loadRes(resFile)
    # running evaluation
    cocoEval = COCOeval(cocoGT, cocoDt, 'bbox')
    # cocoEval.params.imgIds  = imgIds
    cocoEval.evaluate()
    cocoEval.accumulate()
    cocoEval.summarize()

    resFile_txt.write(ptr_str)
    # pdb.set_trace()

    ptr_str = ''
    for s in cocoEval.stats:
        ptr_str += str(s) + '\n'
    print('\n\nPrintning COCOeval Generated results\n\n ')
    print(ptr_str)
    resFile_txt.write(ptr_str)
    return mAP, ap_all, ap_strs, det_boxes