Exemple #1
0
def inference(image_path, keep_thresh=0.5, savepath=None):
    """
    Args: 
        image_path: image path
        keep_thresh: 置信度大于多少的保留
        savepath: 预览图保存路径,为None不保存
    Returns:
        a tuple (bboxes, labels, scores)
        bboxes:
            [[x1, y1, x2, y2], [x1, y1, x2, y2], ...]
        labels:
            [label1, label2, ...]
        scores:
            [score1, score2, ...]  # 降序排列
    """
    assert os.path.isfile(image_path), f'{image_path} not exists.'
    filaname = utils.get_file_name(image_path)

    image_org = cv2.imread(image_path)
    image_org = cv2.cvtColor(image_org, cv2.COLOR_BGR2RGB).astype(np.float32)
    image = image_org / 255.0

    sample = test_transform(**{
        'image': image,
    })
    image = sample['image']
    image = image.unsqueeze(0).to(opt.device)
    batch_bboxes, batch_labels, batch_scores = model.forward_test(image)
    bboxes = batch_bboxes[0]
    labels = batch_labels[0]
    scores = batch_scores[0]
    keep = scores > keep_thresh
    bboxes = bboxes[keep]
    labels = labels[keep]
    scores = scores[keep]

    R = lambda x: int(round(x, 0))  # 四舍五入
    visualize_boxes(image=image_org,
                    boxes=bboxes,
                    labels=labels,
                    probs=scores,
                    class_labels=class_names)

    bboxes_arr = []
    for i in range(len(scores)):
        x1, y1, x2, y2 = bboxes[i]
        x1, y1, x2, y2 = R(x1), R(y1), R(x2), R(y2)
        bboxes_arr.append([x1, y1, x2, y2])
    bboxes = np.array(bboxes_arr)

    if savepath:
        print(f'save image to {savepath}/{filaname}.png')
        image = cv2.cvtColor(np.asarray(image_org), cv2.COLOR_RGB2BGR)
        cv2.imwrite(f'{savepath}/{filaname}.png', image)

    return bboxes, labels, scores
Exemple #2
0
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)
    img /= 255.0  # 转成0~1之间

    img_input = val_transform(image=img)['image']
    img_input = img_input.unsqueeze(0).to(opt.device)

    batch_bboxes, batch_labels, batch_scores = model(img_input)

    img = tensor2im(img_input).copy()
    # for x1, y1, x2, y2 in gt_bbox[0]:
    #     cv2.rectangle(img, (x1,y1), (x2,y2), (0, 255, 0), 2)  # 绿色的是gt

    num = len(batch_scores[0])
    visualize_boxes(image=img,
                    boxes=batch_bboxes[0],
                    labels=batch_labels[0].astype(np.int32),
                    probs=batch_scores[0],
                    class_labels=config.DATA.CLASS_NAMES)

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # img=np.rot90(img)
    frames = frames + 1
    nt = time.time()
    if nt - t > 1.0:
        fps = frames
        frames = 0
        t = nt

        # Draw fps
Exemple #3
0
    def eval_mAP(self, dataloader, epoch, writer, logger, data_name='val'):
        # eval_yolo(self.detector, dataloader, epoch, writer, logger, dataname=data_name)
        pred_bboxes = []
        pred_labels = []
        pred_scores = []
        gt_bboxes = []
        gt_labels = []
        gt_difficults = []

        with torch.no_grad():
            for i, sample in enumerate(dataloader):
                utils.progress_bar(i, len(dataloader), 'Eva... ')
                image = sample['image'].to(opt.device)
                gt_bbox = sample['bboxes']
                labels = sample['labels']
                paths = sample['path']

                batch_bboxes, batch_labels, batch_scores = self.forward(image)
                pred_bboxes.extend(batch_bboxes)
                pred_labels.extend(batch_labels)
                pred_scores.extend(batch_scores)

                for b in range(len(gt_bbox)):
                    gt_bboxes.append(gt_bbox[b].detach().cpu().numpy())
                    gt_labels.append(labels[b].int().detach().cpu().numpy())
                    gt_difficults.append(np.array([False] * len(gt_bbox[b])))

                if opt.vis:  # 可视化预测结果
                    img = tensor2im(image).copy()
                    # for x1, y1, x2, y2 in gt_bbox[0]:
                    #     cv2.rectangle(img, (x1,y1), (x2,y2), (0, 255, 0), 2)  # 绿色的是gt

                    num = len(batch_scores[0])
                    visualize_boxes(image=img,
                                    boxes=batch_bboxes[0],
                                    labels=batch_labels[0].astype(np.int32),
                                    probs=batch_scores[0],
                                    class_labels=opt.class_names)

                    write_image(writer, f'{data_name}/{i}', 'image', img,
                                epoch, 'HWC')

            result = []
            for iou_thresh in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75]:
                AP = eval_detection_voc(pred_bboxes,
                                        pred_labels,
                                        pred_scores,
                                        gt_bboxes,
                                        gt_labels,
                                        gt_difficults=None,
                                        iou_thresh=iou_thresh,
                                        use_07_metric=False)

                APs = AP['ap']
                mAP = AP['map']
                result.append(mAP)

                logger.info(
                    f'Eva({data_name}) epoch {epoch}, IoU: {iou_thresh}, APs: {str(APs[:opt.num_classes])}, mAP: {mAP}'
                )

                write_loss(writer, f'val/{data_name}', 'mAP', mAP, epoch)

            logger.info(
                f'Eva({data_name}) epoch {epoch}, mean of (AP50-AP75): {sum(result)/len(result)}'
            )
# class_names = opt.class_names
class_names = opt.class_names

preview = val_dataloader  # train_dataloader, val_dataloader

from utils.vis import visualize_boxes
for i, sample in enumerate(preview):
    # if i > 30:
    #     break
    utils.progress_bar(i, len(preview), 'Handling...')
    if opt.debug:
        ipdb.set_trace()

    image = sample['image'][0].detach().cpu().numpy().transpose([1, 2, 0])
    image = (image.copy() * 255).astype(np.uint8)

    bboxes = sample['bboxes'][0].cpu().numpy()
    labels = sample['labels'][0].cpu().numpy().astype(np.int32)

    visualize_boxes(image=image,
                    boxes=bboxes,
                    labels=labels,
                    probs=np.array(
                        np.random.randint(100, 101, size=[len(bboxes)]) / 100),
                    class_labels=class_names)

    write_image(writer, f'preview_{opt.dataset}/{i}', 'image', image, 0, 'HWC')

writer.flush()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB).astype(np.float32)
    img /= 255.0  # 转成0~1之间

    img_input = val_transform(image=img)['image']
    img_input = img_input.unsqueeze(0).to(opt.device)

    batch_bboxes, batch_labels, batch_scores = model(img_input)

    img = tensor2im(img_input).copy()
    # for x1, y1, x2, y2 in gt_bbox[0]:
    #     cv2.rectangle(img, (x1,y1), (x2,y2), (0, 255, 0), 2)  # 绿色的是gt

    num = len(batch_scores[0])
    visualize_boxes(image=img,
                    boxes=batch_bboxes[0],
                    labels=batch_labels[0].astype(np.int32),
                    probs=batch_scores[0],
                    class_labels=opt.class_names)

    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    # img=np.rot90(img)
    frames = frames + 1
    nt = time.time()
    if nt - t > 1.0:
        fps = frames
        frames = 0
        t = nt

        # Draw fps