def detect(self, im, conf_thresh=0.7):
        im_resized = cv2.resize(im, self.__shape)
        im_rgb = cv2.cvtColor(im_resized, cv2.COLOR_BGR2RGB)
        im_torch = torch.from_numpy(im_rgb.transpose(
            2, 0, 1)).float().div(255.0).unsqueeze(0)
        im_torch = im_torch.to(torch.device("cuda"))
        output = self.__net(im_torch)

        boxes = get_all_boxes(output,
                              self.__shape,
                              conf_thresh,
                              self.__net.num_classes,
                              use_cuda=True)[0]

        boxes = nms(boxes, self.__nms_thresh)

        result = []
        w = im.shape[1]
        h = im.shape[0]
        for i in range(len(boxes)):
            box = boxes[i]

            x1 = int(round((box[0] - box[2] / 2.0) * w))
            y1 = int(round((box[1] - box[3] / 2.0) * h))
            x2 = int(round((box[0] + box[2] / 2.0) * w))
            y2 = int(round((box[1] + box[3] / 2.0) * h))

            x1 = 0 if x1 < 0 else x1
            y1 = 0 if y1 < 0 else y1
            x2 = w - 1 if x2 >= w else x2
            y2 = h - 1 if y2 >= h else y2

            result.append([x1, y1, x2, y2])

        return result
예제 #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))
예제 #3
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))
예제 #4
0
def eval_list(cfgfile, namefile, weightfile, testfile):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    use_cuda = 1
    if use_cuda:
        m.cuda()

    class_names = load_class_names(namefile)

    file_list = []
    with open(testfile, "r") as fin:
        for f in fin:
            file_list.append(f.strip())

    for imgfile in file_list:
        img = Image.open(imgfile).convert('RGB')
        sized = img.resize((m.width, m.height))
        filename = os.path.basename(imgfile)
        filename = os.path.splitext(filename)[0]
        #print(filename, img.width, img.height, sized_width, sized_height)

        if m.width * m.height > 1024 * 2560:
            print('omit %s' % filename)
            continue

        if False:
            boxes = do_detect(m, sized, conf_thresh, nms_thresh, use_cuda)
        else:
            m.eval()
            sized = image2torch(sized).cuda();
            #output = m(Variable(sized, volatile=True)).data
            output = m(sized)
            #boxes = get_region_boxes(output, conf_thresh, m.num_classes, m.anchors, m.num_anchors, 0, 1)[0]
            boxes = get_all_boxes(output, conf_thresh, m.num_classes)[0]
            boxes = np.array(nms(boxes, nms_thresh))

        if False:
            savename = get_det_image_name(imgfile)
            print('img: save to %s' % savename)
            plot_boxes(img, boxes, savename, class_names)

        if False:
            savename = get_det_result_name(imgfile)
            print('det: save to %s' % savename)
            save_boxes(imgfile, img, boxes, savename)
예제 #5
0
def test(val_loader, conf_thresh, nms_thresh, iou_thresh, out_path, batch_size):
    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
    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)
    map = []
    for i, (imgpath, data, target, org_w, org_h) in enumerate(val_loader):
        print('Computing boxes for batch', i, 'of size', batch_size, '. Number computed is:', i * batch_size)
        data = data.to(device)
        output = model(data)
        all_boxes, det_confs = get_all_boxes(output, shape, conf_thresh, num_classes, use_cuda=use_cuda,
                                             output_confidence=True)

        for k in range(len(all_boxes)):
            boxes = np.array(all_boxes[k])
            boxes = boxes[boxes[:, 4] > conf_thresh]
            boxes = nms(boxes, nms_thresh)
            boxes = np.stack(boxes)
            boxes_true = target.cpu().numpy().reshape(-1, 5)
            assert len(boxes_true) % 50 == 0, 'max_boxes in image.py "fill_truth_detection" is different from 50 with ' \
                                          'shape {}'.format(boxes_true.shape)
            boxes_true = boxes_true[50*k:50*(k+1)]
            boxes_true = boxes_true[boxes_true.max(1) > 0, 1:5]
            out_boxes = np.array([imgpath[k], boxes_true, boxes], dtype=object)
            np.save(out_path + str(i*len(imgpath) + k), out_boxes)

            boxes_pred = boxes[:, :4].copy()
            scores = boxes[:, 4].copy()
            boxes_pred = boxes_pred[scores > 0.03]
            scores = scores[scores > 0.03]
            map.append(map_iou(boxes_true, boxes_pred, scores)) if len(scores) > 0 else None

    map = np.array(map).mean()
    print('Validation output saved at ' + out_path)
    print('The mAP IoU is: {}'.format(map))
예제 #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
예제 #7
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()
예제 #8
0
def valid_NB(datafile, cfgfile, weightfile, epoch, prefix='result'):
    model = Darknet(cfgfile)
    options = read_data_file(datafile)
    data_root = options['valid']
    class_root = options['names']
    names = read_class_names(class_root)

    device = 'cuda' if torch.cuda.is_available() else 'cpu'

    with open(data_root, 'r') as f:
        lines = f.readlines()
        valid_files = [item.strip() for item in lines]

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

    NB_model = joblib.load('NB models/epoch_{}.pkl'.format(int(epoch)))

    data = YoloDataset(data_root,
                       shape=(model.width, model.height),
                       transform=transforms.Compose([transforms.ToTensor()]),
                       train=False)
    batch_size = 2
    kwargs = {'num_workers': 4, 'pin_memory': True}
    data_loader = DataLoader(data,
                             batch_size=batch_size,
                             shuffle=False,
                             **kwargs)

    fs = [None] * model.num_classes
    if not os.path.exists(prefix):
        os.makedirs(prefix)
    for i in range(model.num_classes):
        filename = prefix + '/' + str(names[i]) + '.txt'
        fs[i] = open(filename, 'w')
    net_shape = (model.width, model.height)

    conf_thresh = 0.005
    nms_thresh = 0.45

    fileIndex = 0
    for index, (imgs, labels, org_w, org_h) in enumerate(data_loader):
        imgs = imgs.to(device)
        output = model(imgs)

        batch_boxes = get_all_boxes(output,
                                    net_shape,
                                    conf_thresh,
                                    model.num_classes,
                                    device,
                                    validation=True)

        for i in range(len(batch_boxes)):
            fileId = os.path.basename(valid_files[fileIndex]).split('.')[
                0]  # gei naive image name without suffix
            w, h = float(org_w[i]), float(org_h[i])
            # print(valid_files[fileIndex], '{}/{}'.format(fileIndex+1, len(data_loader) * batch_size))
            fileIndex += 1
            boxes = batch_boxes[i]
            correct_yolo_boxes(boxes, w, h, model.width, model.height)
            boxes = auto_thresh_nms(boxes, NB_model)
            for box in boxes:
                x1 = (box[0] - box[2] / 2.0) * w
                y1 = (box[1] - box[3] / 2.0) * h
                x2 = (box[0] + box[2] / 2.0) * w
                y2 = (box[1] + box[3] / 2.0) * h

                # 包含物体的概率,乘以每一类的概率
                det_conf = box[4]
                for j in range((len(box) - 5) // 2):
                    cls_conf = box[5 + 2 * j]
                    cls_id = int(box[5 + 2 * j + 1])
                    prob = det_conf * cls_conf
                    fs[cls_id].write('{:s} {:f} {:f} {:f} {:f} {:f}\n'.format(
                        fileId, prob, x1, y1, x2, y2))

    for i in range(len(fs)):
        fs[i].close()
예제 #9
0
def detect(model, img, conf_thresh, nms_thresh, use_cuda):
    img = image2torch(img)
    img = img.to(torch.device('cuda' if use_cuda else 'cpu'))
    out_boxes = model(img)
    boxes = get_all_boxes(out_boxes, conf_thresh, model.num_classes, use_cuda=use_cuda)[0]
    return nms(boxes, nms_thresh)
예제 #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))
예제 #11
0
def valid(datacfg, cfgfile, weightfile, save_path, use_cuda = False, size = 416):
    options = read_data_cfg(datacfg)
    valid_images = options['valid']
    name_list = options['names']
    if os.path.exists(save_path) == False:
        os.mkdir(save_path)
    prefix = save_path
    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.load_weights(weightfile)
    num_classes = len(names)

    if use_cuda:
        m.cuda()
    m.eval()

    valid_dataset = MyDataset(valid_images, shape=(size, size),
                       is_train = False,
                       transform=transforms.Compose([
                           transforms.ToTensor(),
                       ]))
    valid_batchsize = 10
    assert(valid_batchsize > 1)
    
    if use_cuda:
        kwargs = {'num_workers': 4, 'pin_memory': True}
    else:
        kwargs = {}
    valid_loader = torch.utils.data.DataLoader(
        valid_dataset, batch_size=valid_batchsize, shuffle=False, **kwargs) 

    fps = [0]*num_classes
    if not os.path.exists('results'):
        os.mkdir('results')
    for i in range(num_classes):
        buf = '%s/%s.txt' % (prefix, names[i])
        fps[i] = open(buf, 'w')
   
    lineId = -1
    
    conf_thresh = 0.01
    nms_thresh = 0.5
    for batch_id, (data, target) in enumerate(valid_loader):
        if use_cuda:
            data = data.cuda()
        print('start processing batch{}'.format(batch_id))
        start1 = time.time()
        output = m(data)
        batch_boxes = get_all_boxes(output, conf_thresh, num_classes, only_objectness=0, validation=True, use_cuda = use_cuda)
        for i in range(data.size(0)):
            lineId = lineId + 1
            fileId = os.path.basename(valid_files[lineId]).split('.')[0]
            width, height = get_image_size(valid_files[lineId])
            boxes = batch_boxes[i]
            if boxes.numel() == 0:
                continue
            for cls_id in range(num_classes):
                cls_ind = (boxes[:, 6] == cls_id)
                cls_boxes = nms(boxes[cls_ind],nms_thresh)
                if cls_boxes.numel == 0:
                    continue
                for box in cls_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 
                    fps[cls_id].write('%s %f %f %f %f %f\n' %(fileId, box[4] * box[5], x1, y1, x2, y2))
        end1 = time.time()
        print('average time {}s'.format((end1 - start1) / len(data)))
        del data,target
    for i in range(num_classes):
        fps[i].close()