Exemple #1
0
def detect(cfgfile, weightfile, imgfile):
    m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    # if m.num_classes == 20:
    #     namesfile = 'data/voc.names'
    # elif m.num_classes == 80:
    #     namesfile = 'data/coco.names'
    # else:
    #     namesfile = 'data/names'

    use_cuda = torch.cuda.is_available()
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = letterbox_image(img, m.width, m.height)

    start = time.time()
    boxes = do_detect(m, sized, 0.5, 0.1, use_cuda)
    correct_yolo_boxes(boxes, img.width, img.height, m.width, m.height)

    finish = time.time()
    print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, 'predictions.jpg', class_names)
Exemple #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
    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))
Exemple #3
0
def inference(model, test_loader, **kwargs):
    beta = kwargs['beta']
    #device = kwargs['device']
    beta_s = beta * beta

    model = model.to(device)
    model.eval()

    total = 0.0
    proposals = 0.0
    correct = 0.0

    for batch_idx, (data, target, org_w, org_h) in enumerate(test_loader):
        data = data.to(device)
        output = model(data)
        all_boxes = get_all_boxes(output,
                                  shape,
                                  conf_thresh,
                                  80,
                                  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], 416, 416)
            boxes = np.array(nms(boxes, nms_thresh))
            num_pred = len(boxes)
            if num_pred == 0:
                continue

            truths = target[k].view(-1, 5)
            num_gts = truths_length(truths)
            total = total + num_gts

            pred_boxes = torch.FloatTensor(boxes).t()
            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()
                best_iou, best_j = torch.max(
                    multi_bbox_ious(gt_boxes, pred_boxes, x1y1x2y2=False), 0)
                if best_iou > iou_thresh and pred_boxes[6][best_j] == gt_boxes[
                        6][0]:
                    correct += 1

        #prec_i = 1.0*correct/(proposals+eps)
        #reca_i = 1.0*correct/(total+eps)
        #fsco_i = (1.0+beta_s)*prec_i*reca_i/(beta_s*prec_i+reca_i+eps)
        #print('%03d-th test, correct: %03d, precision: %f, recall: %f, fscore: %f' % (batch_idx, correct, prec_i, reca_i, fsco_i))

    precision = 1.0 * correct / (proposals + eps)
    recall = 1.0 * correct / (total + eps)
    fscore = (1.0 + beta_s) * precision * recall / (beta_s * precision +
                                                    recall + eps)
    print("Final correct: %03d, precision: %f, recall: %f, fscore: %f" %
          (correct, precision, recall, fscore))
    return fscore
def detect(m, img, img_name, use_cuda):
    sized = letterbox_image(img, m.width, m.height)
    start = time.time()
    boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
    correct_yolo_boxes(boxes, img.width, img.height, m.width, m.height)
    finish = time.time()
    print('%s: Predicted in %f seconds.' % (img_name, (finish - start)))
    return boxes
Exemple #5
0
def camera_detect(cfgfile, weightfile):
    """
    - camera detect
    :cfgfile    use tiny config file
    :weightfile use tiny weight file 
    """
    model = DarkNet(cfgfile)
    model.print_net()
    model.load_weights(weightfile)
    print('load weights done!')

    num_classes = 80
    if num_classes == 20:
        namesfile = '../data/voc.names'
    elif num_classes == 80:
        namesfile = '../data/coco.names'
    else:
        namesfile = '../data/names'

    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("Unable to open camera")
        exit(-1)
    while True:
        res, img = cap.read()
        if res:
            img = Image.fromarray(img, mode='RGB')  # numpy.array -> PIL.Image
            sized = letterbox_image(img, model.width, model.height)

            boxes = do_detect(model, sized, 0.5, 0.4, False)
            correct_yolo_boxes(boxes, img.width, img.height, model.width,
                               model.height)
            class_names = load_class_names(namesfile)
            image_draw = plot_boxes(img, boxes, None, class_names)

            np_img = np.asarray(image_draw)  # PIL.Image -> numpy.array
            cv2.imshow(cfgfile, np_img)
            cv2.waitKey(1)
        else:
            print("Unable to read image")
            exit(-1)
Exemple #6
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
Exemple #7
0
def PR_Valid(epoch, valid_sampler):
    '''
    This function is the same with precision and recall function, but validate on the valid set (not on test set).
    :param epoch:
    :param valid_sampler:
    :return:
    '''
    def truths_length(truths):
        for i in range(50):
            if truths[i][1] == 0:
                return i
        return 50

    model.eval()
    cur_model = curmodel()

    valid_batchsize = 1
    kwargs = {
        'num_workers': num_workers,
        'pin_memory': True
    } if use_cuda else {}
    valid_loader = torch.utils.data.DataLoader(valid_dataset,
                                               batch_size=valid_batchsize,
                                               sampler=valid_sampler,
                                               **kwargs)

    num_classes = cur_model.num_classes
    total = 0.0
    proposals = 0.0
    correct = 0.0

    if cur_model.net_name() == 'region':  # region_layer
        shape = (0, 0)
    else:
        shape = (cur_model.width, cur_model.height)
    with torch.no_grad():
        for data, target, org_w, org_h in tqdm.tqdm(valid_loader):
            data = data.to(device)
            if condition:
                output, cls_output = model(data)
            else:
                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], cur_model.width,
                                   cur_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)
    # savelog("[%03d] vfscore: %.4f vpre: %.4f vrec: %.4f \n" % (epoch, fscore, precision, recall))
    # with open(logfile,"a+") as myfile:
    #     myfile.writelines("vfscore: %.4f vpre: %.4f vrec: %.4f \n"%(fscore,precision,recall))
    return fscore, precision, recall
from utils import *
from image import letterbox_image, correct_yolo_boxes
from darknet import Darknet

globals()["namesfile"] = "coco.names"

def detect(m, cfgfile, weightfile, imgfile, destfile):
	m = Darknet(cfgfile)
	
    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))
    
    use_cuda = torch.cuda.is_available()
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = letterbox_image(img, m.width, m.height)

    start = time.time()
    boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
    correct_yolo_boxes(boxes, img.width, img.height, m.width, m.height)

    finish = time.time()
    print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, destfile, class_names)
Exemple #9
0
def valid(datacfg, cfgfile, weightfile, outfile):
    options = read_data_cfg(datacfg)
    valid_images = options['valid']
    name_list = options['names']
    prefix = 'results'
    names = load_class_names(name_list)

    with open(valid_images) as fp:
        tmp_files = fp.readlines()
        valid_files = [item.rstrip() for item in tmp_files]

    m = Darknet(cfgfile)
    m.print_network()
    m.load_weights(weightfile)
    m.cuda()
    m.eval()

    valid_dataset = dataset.listDataset(valid_images,
                                        shape=(m.width, m.height),
                                        shuffle=False,
                                        transform=transforms.Compose([
                                            transforms.ToTensor(),
                                        ]))
    valid_batchsize = 2
    assert (valid_batchsize > 1)

    kwargs = {'num_workers': 4, 'pin_memory': True}
    valid_loader = torch.utils.data.DataLoader(valid_dataset,
                                               batch_size=valid_batchsize,
                                               shuffle=False,
                                               **kwargs)

    fps = [0] * m.num_classes
    if not os.path.exists('results'):
        os.mkdir('results')
    for i in range(m.num_classes):
        buf = '%s/%s%s.txt' % (prefix, outfile, names[i])
        fps[i] = open(buf, 'w')

    lineId = -1

    conf_thresh = 0.005
    nms_thresh = 0.45
    if m.net_name() == 'region':  # region_layer
        shape = (0, 0)
    else:
        shape = (m.width, m.height)
    for _, (data, target, org_w, org_h) in enumerate(valid_loader):
        data = data.cuda()
        output = m(data)
        batch_boxes = get_all_boxes(output,
                                    shape,
                                    conf_thresh,
                                    m.num_classes,
                                    only_objectness=0,
                                    validation=True)

        for i in range(len(batch_boxes)):
            lineId += 1
            fileId = os.path.basename(valid_files[lineId]).split('.')[0]
            #width, height = get_image_size(valid_files[lineId])
            width, height = float(org_w[i]), float(org_h[i])
            print(valid_files[lineId])
            boxes = batch_boxes[i]
            correct_yolo_boxes(boxes, width, height, m.width, m.height)
            boxes = nms(boxes, nms_thresh)
            for box in boxes:
                x1 = (box[0] - box[2] / 2.0) * width
                y1 = (box[1] - box[3] / 2.0) * height
                x2 = (box[0] + box[2] / 2.0) * width
                y2 = (box[1] + box[3] / 2.0) * height

                det_conf = box[4]
                for j in range((len(box) - 5) // 2):
                    cls_conf = box[5 + 2 * j]
                    cls_id = int(box[6 + 2 * j])
                    prob = det_conf * cls_conf
                    fps[cls_id].write('%s %f %f %f %f %f\n' %
                                      (fileId, prob, x1, y1, x2, y2))

    for i in range(m.num_classes):
        fps[i].close()
Exemple #10
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))