Ejemplo n.º 1
0
def test():
    def truths_length(truths):
        for i in range(50):
            if truths[i][1] == 0:
                return i
        return 50

    model.eval()
    num_classes = model.num_classes
    total = 0.0
    proposals = 0.0
    correct = 0.0
    device = torch.device("cuda" if use_cuda else "cpu")

    if model.net_name() == 'region':  # region_layer
        shape = (0, 0)
    else:
        shape = (model.width, model.height)
    for data, target, org_w, org_h in test_loader:
        print("======")
        data = data.to(device)
        output = model(data)
        all_boxes = get_all_boxes(output,
                                  shape,
                                  conf_thresh,
                                  num_classes,
                                  use_cuda=use_cuda)

        for k in range(len(all_boxes)):
            boxes = all_boxes[k]
            correct_yolo_boxes(boxes, org_w[k], org_h[k], model.width,
                               model.height)
            boxes = np.array(nms(boxes, nms_thresh))
            truths = target[k].view(-1, 5)
            num_gts = truths_length(truths)
            total = total + num_gts
            num_pred = len(boxes)
            if num_pred == 0:
                continue

            proposals += int((boxes[:, 4] > conf_thresh).sum())
            for i in range(num_gts):
                gt_boxes = torch.FloatTensor([
                    truths[i][1], truths[i][2], truths[i][3], truths[i][4],
                    1.0, 1.0, truths[i][0]
                ])
                gt_boxes = gt_boxes.repeat(num_pred, 1).t()
                pred_boxes = torch.FloatTensor(boxes).t()
                best_iou, best_j = torch.max(
                    multi_bbox_ious(gt_boxes, pred_boxes, x1y1x2y2=False), 0)
                # pred_boxes and gt_boxes are transposed for torch.max
                if best_iou > iou_thresh and pred_boxes[6][best_j] == gt_boxes[
                        6][0]:
                    correct += 1

    precision = 1.0 * correct / (proposals + eps)
    recall = 1.0 * correct / (total + eps)
    fscore = 2.0 * precision * recall / (precision + recall + eps)
    logging("correct: %d, precision: %f, recall: %f, fscore: %f" %
            (correct, precision, recall, fscore))
Ejemplo n.º 2
0
def test():
    def truths_length(truths):
        for i in range(50):
            if truths[i][1] == 0:
                return i
        return 50

    model.eval()
    num_classes = model.num_classes
    print("num", num_classes)
    total = 0.0
    proposals = 0.0
    correct = 0.0
    device = torch.device("cuda" if use_cuda else "cpu")

    for _, (data, target) in enumerate(test_loader):
        data = data.to(device)
        output = model(data)
        all_boxes = get_all_boxes(output, conf_thresh, num_classes)

        for k in range(data.size(0)):
            boxes = all_boxes[k]
            boxes = np.array(nms(boxes, nms_thresh))
            truths = target[k].view(-1, 5)
            num_gts = truths_length(truths)
            total = total + num_gts
            num_pred = len(boxes)

            if num_pred == 0:
                continue

            proposals += int((boxes[:, 4] > conf_thresh).sum())
            for i in range(num_gts):
                gt_boxes = torch.FloatTensor([
                    truths[i][1], truths[i][2], truths[i][3], truths[i][4],
                    1.0, 1.0, truths[i][0]
                ])
                gt_boxes = gt_boxes.repeat(num_pred, 1).t()
                pred_boxes = torch.FloatTensor(boxes).t()
                best_iou, best_j = torch.max(
                    multi_bbox_ious(gt_boxes, pred_boxes, x1y1x2y2=False), 0)
                # pred_boxes and gt_boxes are transposed for torch.max
                if best_iou > iou_thresh and pred_boxes[6][best_j] == gt_boxes[
                        6][0]:
                    correct += 1

    precision = 1.0 * correct / (proposals + eps)
    recall = 1.0 * correct / (total + eps)
    fscore = 2.0 * precision * recall / (precision + recall + eps)
    logging("precision: %f, recall: %f, fscore: %f" %
            (precision, recall, fscore))
Ejemplo n.º 3
0
def inference(model, test_loader,**kwargs):
    assert kwargs['device'] != None, 'Device error'
    device = kwargs['device']
    print(device)
    model.to(device)

    for w in FLAGS.weights:
        model.load_weights(w)
        logging('evaluating ... %s' % (w))

    def truths_length(truths):
        for i in range(50):
            if truths[i][1] == 0:
                return i
        return 50

    model.eval()
    num_classes = model.num_classes
    total       = 0.0
    proposals   = 0.0
    correct     = 0.0

    if model.net_name() == 'region': # region_layer
        shape=(0,0)
    else:
        shape=(model.width, model.height)
    print(len(test_loader))
    '''
    for data, target, org_w, org_h in test_loader:
        data = data.to(device)
        output = model(data)
        all_boxes = get_all_boxes(output, shape, conf_thresh, num_classes, use_cuda=False if device == "cpu" else True)
        for k in range(len(all_boxes)):
            boxes = all_boxes[k]
            correct_yolo_boxes(boxes, org_w[k], org_h[k], model.width, model.height)
            boxes = np.array(nms(boxes, nms_thresh))
            truths = target[k].view(-1, 5)
            num_gts = truths_length(truths)
            total = total + num_gts
            num_pred = len(boxes)
            if num_pred == 0:
                continue
            proposals += int((boxes[:,4]>conf_thresh).sum())
            for i in range(num_gts):
                gt_boxes = torch.FloatTensor([truths[i][1], truths[i][2], truths[i][3], truths[i][4], 1.0, 1.0, truths[i][0]])
                gt_boxes = gt_boxes.repeat(num_pred,1).t()
                pred_boxes = torch.FloatTensor(boxes).t()
                best_iou, best_j = torch.max(multi_bbox_ious(gt_boxes, pred_boxes, x1y1x2y2=False),0)
                # pred_boxes and gt_boxes are transposed for torch.max
                if best_iou > iou_thresh and pred_boxes[6][best_j] == gt_boxes[6][0]:
                    correct += 1
        #print('fps: ', len(data)/(time.time()-t0))
    '''
    for idx in range(len(test_loader)):
        data[idx] = data[idx].to(device)
        output = model(data[idx])
        all_boxes = get_all_boxes(output, shape, conf_thresh, num_classes, use_cuda=False if device == "cpu" else True)
        for k in range(len(all_boxes)):
            boxes = all_boxes[k]
            correct_yolo_boxes(boxes, org_w[idx][k], org_h[idx][k], model.width, model.height)
            boxes = np.array(nms(boxes, nms_thresh))
            truths = target[idx][k].view(-1, 5)
            num_gts = truths_length(truths)
            total = total + num_gts
            num_pred = len(boxes)
            if num_pred == 0:
                continue

            proposals += int((boxes[:,4]>conf_thresh).sum())
            for i in range(num_gts):
                gt_boxes = torch.FloatTensor([truths[i][1], truths[i][2], truths[i][3], truths[i][4], 1.0, 1.0, truths[i][0]])
                gt_boxes = gt_boxes.repeat(num_pred,1).t()
                pred_boxes = torch.FloatTensor(boxes).t()
                best_iou, best_j = torch.max(multi_bbox_ious(gt_boxes, pred_boxes, x1y1x2y2=False),0)
                # pred_boxes and gt_boxes are transposed for torch.max
                if best_iou > iou_thresh and pred_boxes[6][best_j] == gt_boxes[6][0]:
                    correct += 1
    precision = 1.0*correct/(proposals+eps)
    recall = 1.0*correct/(total+eps)
    fscore = 2.0*precision*recall/(precision+recall+eps)
    #logging("correct: %d, precision: %f, recall: %f, fscore: %f" % (correct, precision, recall, fscore))

    return fscore
Ejemplo n.º 4
0
    def build_targets(self, pred_boxes, target, anchors, nA, nH, nW):
        nB = target.size(0)
        anchor_step = anchors.size(1)  # anchors[nA][anchor_step]
        noobj_mask = torch.ones(nB, nA, nH, nW)
        obj_mask = torch.zeros(nB, nA, nH, nW)
        coord_mask = torch.zeros(nB, nA, nH, nW)
        tcoord = torch.zeros(4, nB, nA, nH, nW)
        tconf = torch.zeros(nB, nA, nH, nW)
        tcls = torch.zeros(nB, nA, nH, nW, self.num_classes)

        nAnchors = nA * nH * nW
        nPixels = nH * nW
        nGT = 0
        nRecall = 0
        nRecall75 = 0

        # it works faster on CPU than on GPU.
        anchors = anchors.to("cpu")

        for b in range(nB):
            cur_pred_boxes = pred_boxes[b * nAnchors:(b + 1) * nAnchors].t()
            cur_ious = torch.zeros(nAnchors)
            tbox = target[b].view(-1, 5).to("cpu")

            for t in range(50):
                if tbox[t][1] == 0:
                    break
                gx, gy = tbox[t][1] * nW, tbox[t][2] * nH
                gw, gh = tbox[t][3] * self.net_width, tbox[t][
                    4] * self.net_height
                cur_gt_boxes = torch.FloatTensor([gx, gy, gw,
                                                  gh]).repeat(nAnchors, 1).t()
                cur_ious = torch.max(
                    cur_ious,
                    multi_bbox_ious(cur_pred_boxes,
                                    cur_gt_boxes,
                                    x1y1x2y2=False))
            ignore_ix = (cur_ious > self.ignore_thresh).view(nA, nH, nW)
            noobj_mask[b][ignore_ix] = 0

            for t in range(50):
                if tbox[t][1] == 0:
                    break
                nGT += 1
                gx, gy = tbox[t][1] * nW, tbox[t][2] * nH
                gw, gh = tbox[t][3] * self.net_width, tbox[t][
                    4] * self.net_height
                gw, gh = gw.float(), gh.float()
                gi, gj = int(gx), int(gy)

                tmp_gt_boxes = torch.FloatTensor([0, 0, gw, gh]).repeat(nA,
                                                                        1).t()
                anchor_boxes = torch.cat(
                    (torch.zeros(nA, anchor_step), anchors), 1).t()
                _, best_n = torch.max(
                    multi_bbox_ious(anchor_boxes, tmp_gt_boxes,
                                    x1y1x2y2=False), 0)

                gt_box = torch.FloatTensor([gx, gy, gw, gh])
                pred_box = pred_boxes[b * nAnchors + best_n * nPixels +
                                      gj * nW + gi]
                iou = bbox_iou(gt_box, pred_box, x1y1x2y2=False)

                obj_mask[b][best_n][gj][gi] = 1
                noobj_mask[b][best_n][gj][gi] = 0
                coord_mask[b][best_n][gj][gi] = 2. - tbox[t][3] * tbox[t][4]
                tcoord[0][b][best_n][gj][gi] = gx - gi
                tcoord[1][b][best_n][gj][gi] = gy - gj
                tcoord[2][b][best_n][gj][gi] = math.log(gw /
                                                        anchors[best_n][0])
                tcoord[3][b][best_n][gj][gi] = math.log(gh /
                                                        anchors[best_n][1])
                tcls[b][best_n][gj][gi][int(tbox[t][0])] = 1
                tconf[b][best_n][gj][gi] = iou if self.rescore else 1.

                if iou > 0.5:
                    nRecall += 1
                    if iou > 0.75:
                        nRecall75 += 1

        return nGT, nRecall, nRecall75, obj_mask, noobj_mask, coord_mask, tcoord, tconf, tcls
    def build_targets(self, pred_boxes, target, nH, nW):
        nB = target.size(0)
        nA = self.num_anchors
        noobj_mask = torch.ones (nB, nA, nH, nW)
        obj_mask   = torch.zeros(nB, nA, nH, nW)
        coord_mask = torch.zeros(nB, nA, nH, nW)
        tcoord     = torch.zeros( 4, nB, nA, nH, nW)
        tconf      = torch.zeros(nB, nA, nH, nW)
        tcls       = torch.zeros(nB, nA, nH, nW)

        nAnchors = nA*nH*nW
        nPixels  = nH*nW
        nGT = 0 # number of ground truth
        nRecall = 0
        # it works faster on CPU than on GPU.
        anchors = self.anchors.to("cpu")

        if self.seen < 12800:
            tcoord[0].fill_(0.5)
            tcoord[1].fill_(0.5)
            coord_mask.fill_(0.01)
            # initial w, h == 0 means log(1)==0, s.t, anchor is equal to ground truth.

        for b in range(nB):
            cur_pred_boxes = pred_boxes[b*nAnchors:(b+1)*nAnchors].t()
            cur_ious = torch.zeros(nAnchors)
            tbox = target[b].view(-1,5).to("cpu")
            for t in range(50):
                if tbox[t][1] == 0:
                    break
                gx, gw = [ i * nW for i in (tbox[t][1], tbox[t][3]) ]
                gy, gh = [ i * nH for i in (tbox[t][2], tbox[t][4]) ]
                cur_gt_boxes = torch.FloatTensor([gx, gy, gw, gh]).repeat(nAnchors,1).t()
                cur_ious = torch.max(cur_ious, multi_bbox_ious(cur_pred_boxes, cur_gt_boxes, x1y1x2y2=False))
            ignore_ix = (cur_ious>self.thresh).view(nA,nH,nW)
            noobj_mask[b][ignore_ix] = 0

            for t in range(50):
                if tbox[t][1] == 0:
                    break
                nGT += 1
                gx, gw = [ i * nW for i in (tbox[t][1], tbox[t][3]) ]
                gy, gh = [ i * nH for i in (tbox[t][2], tbox[t][4]) ]
                gw, gh = gw.float(), gh.float()
                gi, gj = int(gx), int(gy)

                tmp_gt_boxes = torch.FloatTensor([0, 0, gw, gh]).repeat(nA,1).t()
                anchor_boxes = torch.cat((torch.zeros(nA, 2), anchors),1).t()
                tmp_ious = multi_bbox_ious(anchor_boxes, tmp_gt_boxes, x1y1x2y2=False)
                best_iou, best_n = torch.max(tmp_ious, 0)

                if self.anchor_step == 4: # this part is not tested.
                    tmp_ious_mask = (tmp_ious==best_iou)
                    if tmp_ious_mask.sum() > 0:
                        gt_pos = torch.FloatTensor([gi, gj, gx, gy]).repeat(nA,1).t()
                        an_pos = anchor_boxes[4:6] # anchor_boxes are consisted of [0 0 aw ah ax ay]
                        dist = pow(((gt_pos[0]+an_pos[0])-gt_pos[2]),2) + pow(((gt_pos[1]+an_pos[1])-gt_pos[3]),2)
                        dist[1-tmp_ious_mask]=10000 # set the large number for the small ious
                        _, best_n = torch.min(dist,0)

                gt_box = torch.FloatTensor([gx, gy, gw, gh])
                pred_box = pred_boxes[b*nAnchors+best_n*nPixels+gj*nW+gi]
                iou = bbox_iou(gt_box, pred_box, x1y1x2y2=False)

                obj_mask  [b][best_n][gj][gi] = 1
                noobj_mask[b][best_n][gj][gi] = 0
                coord_mask[b][best_n][gj][gi] = 2. - tbox[t][3]*tbox[t][4]
                tcoord [0][b][best_n][gj][gi] = gx - gi
                tcoord [1][b][best_n][gj][gi] = gy - gj
                tcoord [2][b][best_n][gj][gi] = math.log(gw/anchors[best_n][0])
                tcoord [3][b][best_n][gj][gi] = math.log(gh/anchors[best_n][1])
                tcls      [b][best_n][gj][gi] = tbox[t][0]
                tconf     [b][best_n][gj][gi] = iou if self.rescore else 1.
                if iou > 0.5:
                    nRecall += 1

        return nGT, nRecall, obj_mask, noobj_mask, coord_mask, tcoord, tconf, tcls
Ejemplo n.º 6
0
def test():
    def truths_length(truths):
        for i in range(50):
            if truths[i][1] == 0:
                return i
        return 50

    conf_thresh = FLAGS.FB_thresh
    print("Conf_thresh%.2f" % conf_thresh)
    model.eval()
    num_classes = model.num_classes
    total = 0.0
    proposals = 0.0
    correct = 0.0
    total_c = np.zeros((num_classes))
    proposals_c = np.zeros((num_classes))
    correct_c = np.zeros((num_classes))
    device = torch.device("cuda" if use_cuda else "cpu")
    count_iter = 0.0
    if model.net_name() == 'region':  # region_layer
        shape = (0, 0)
    else:
        shape = (model.width, model.height)
    for data, target, org_w, org_h in test_loader:
        data = data.to(device)
        output = model(data)
        all_boxes = get_all_boxes(output,
                                  shape,
                                  conf_thresh,
                                  num_classes,
                                  use_cuda=use_cuda)

        for k in range(len(all_boxes)):
            boxes = all_boxes[k]
            correct_yolo_boxes(boxes, org_w[k], org_h[k], model.width,
                               model.height)
            boxes = np.array(nms(boxes, nms_thresh))
            truths = target[k].view(-1, 5)
            num_gts = truths_length(truths)
            total_c[range(num_classes)] += np.histogram(
                truths[:, 0], range(num_classes + 1))[0]

            total = total + num_gts
            num_pred = len(boxes)
            if num_pred == 0:
                continue
            Fg_mask = boxes[:, 4] > conf_thresh
            proposals += int(Fg_mask.sum())
            [numberofdata, _] = np.histogram(boxes[Fg_mask, 6],
                                             range(num_classes + 1))
            proposals_c[range(num_classes)] += numberofdata
            for i in range(num_gts):
                gt_boxes = torch.FloatTensor([
                    truths[i][1], truths[i][2], truths[i][3], truths[i][4],
                    1.0, 1.0, truths[i][0]
                ])
                gt_boxes = gt_boxes.repeat(num_pred, 1).t()
                pred_boxes = torch.FloatTensor(boxes).t()
                best_iou, best_j = torch.max(
                    multi_bbox_ious(gt_boxes, pred_boxes, x1y1x2y2=False), 0)
                # pred_boxes and gt_boxes are transposed for torch.max
                if best_iou > iou_thresh and pred_boxes[6][best_j] == gt_boxes[
                        6][0]:
                    correct += 1
                    correct_c[int(gt_boxes[6][0])] += 1
    precision = 1.0 * correct / (proposals + eps)
    recall = 1.0 * correct / (total + eps)
    fscore = 2.0 * precision * recall / (precision + recall + eps)
    logging("Total correct: %d, precision: %f, recall: %f, fscore: %f" %
            (correct, precision, recall, fscore))
    print("%d", len(namelist))
    print("%d,%d,%d" % (len(correct_c), len(total_c), len(proposals_c)))
    for i in range(num_classes):
        precision = 1.0 * correct_c[i] / (proposals_c[i] + eps)
        recall = 1.0 * correct_c[i] / (total_c[i] + eps)
        fscore = 2.0 * precision * recall / (precision + recall + eps)
        logging("%s\t correct: %d, precision: %f, recall: %f, fscore: %f" %
                (namelist[i], correct_c[i], precision, recall, fscore))
Ejemplo n.º 7
0
    def build_targets(self, pred_boxes, target, anchors, nH, nW):
        self.ignore_thresh = 0.5
        self.truth_thresh = 1.

        # Works faster on CPU than on GPU.
        devi = torch.device('cpu')
        pred_boxes = pred_boxes.to(devi)
        target = target.to(devi)
        anchors = anchors.to(devi)

        # max_targets = target[0].view(-1,5).size(0) # 50
        nB = target.size(0)
        nA = len(anchors)

        anchor_step = anchors.size(1)  # anchors[nA][anchor_step]
        conf_mask = torch.ones(nB, nA, nH, nW)
        coord_mask = torch.zeros(nB, nA, nH, nW)
        cls_mask = torch.zeros(nB, nA, nH, nW)
        tcoord = torch.zeros(4, nB, nA, nH, nW)
        tconf = torch.zeros(nB, nA, nH, nW)
        tcls = torch.zeros(nB, nA, nH, nW)
        # twidth, theight = self.net_width/self.stride, self.net_height/self.stride
        twidth, theight = nW, nH
        nAnchors = nA * nH * nW

        for b in range(nB):
            cur_pred_boxes = pred_boxes[b * nAnchors:(b + 1) * nAnchors].t()
            cur_ious = torch.zeros(nAnchors)
            tbox = target[b].view(-1, 5)

            # If the bounding box prior is not the best but does overlap a ground truth object by
            # more than some threshold we ignore the prediction (conf_mask)
            for t in range(tbox.size(0)):
                if tbox[t][1] == 0:
                    break
                gx, gy = tbox[t][1] * nW, tbox[t][2] * nH
                gw, gh = tbox[t][3] * twidth, tbox[t][4] * theight
                cur_gt_boxes = torch.FloatTensor([gx, gy, gw,
                                                  gh]).repeat(nAnchors, 1).t()
                cur_ious = torch.max(
                    cur_ious,
                    multi_bbox_ious(cur_pred_boxes,
                                    cur_gt_boxes,
                                    x1y1x2y2=False))
            ignore_ix = cur_ious > self.ignore_thresh
            conf_mask[b][ignore_ix.view(nA, nH, nW)] = 0

            for t in range(tbox.size(0)):
                if tbox[t][1] == 0:
                    break
                # nGT += 1
                gx, gy = tbox[t][1] * nW, tbox[t][2] * nH
                gw, gh = tbox[t][3] * twidth, tbox[t][4] * theight
                gw, gh = gw.float(), gh.float()
                gi, gj = int(gx), int(gy)

                tmp_gt_boxes = torch.FloatTensor([0, 0, gw, gh]).repeat(nA,
                                                                        1).t()
                anchor_boxes = torch.cat(
                    (torch.zeros(nA, anchor_step), anchors), 1).t()
                _, best_n = torch.max(
                    multi_bbox_ious(tmp_gt_boxes, anchor_boxes,
                                    x1y1x2y2=False), 0)

                coord_mask[b][best_n][gj][gi] = 1
                cls_mask[b][best_n][gj][gi] = 1
                conf_mask[b][best_n][gj][gi] = 1
                tcoord[0][b][best_n][gj][gi] = gx - gi
                tcoord[1][b][best_n][gj][gi] = gy - gj
                tcoord[2][b][best_n][gj][gi] = math.log(gw /
                                                        anchors[best_n][0])
                tcoord[3][b][best_n][gj][gi] = math.log(gh /
                                                        anchors[best_n][1])
                tcls[b][best_n][gj][gi] = tbox[t][0]
                tconf[b][best_n][gj][
                    gi] = 1  # yolov1 would have used iou-value here

        return coord_mask, conf_mask, cls_mask, tcoord, tconf, tcls