Esempio n. 1
0
def eval(net, test_num=10000):
    net.eval()

    def transform(img, boxes, labels):
        img, boxes = resize(img, boxes, size=(opt.img_size, opt.img_size))
        img = transforms.Compose([transforms.ToTensor(), caffe_normalize])(img)
        return img, boxes, labels

    dataset = ListDataset(root=opt.eval_img_root,
                          list_file=opt.eval_img_list,
                          transform=transform)
    box_coder = SSDBoxCoder(net.module)

    pred_boxes = []
    pred_labels = []
    pred_scores = []
    gt_boxes = []
    gt_labels = []

    #    with open('torchcv/datasets/voc/voc07_test_difficult.txt') as f:
    #        gt_difficults = []
    #        for line in f.readlines():
    #            line = line.strip().split()
    #            d = np.array([int(x) for x in line[1:]])
    #            gt_difficults.append(d)

    nums_img = dataset.__len__()
    for i in tqdm(range(nums_img)):
        inputs, box_targets, label_targets = dataset.__getitem__(i)
        gt_boxes.append(box_targets)
        gt_labels.append(label_targets)

        inputs = inputs.unsqueeze(0)
        with torch.no_grad():
            loc_preds, cls_preds = net(Variable(inputs.cuda()))
        box_preds, label_preds, score_preds = box_coder.decode(
            loc_preds.cpu().data.squeeze(),
            F.softmax(cls_preds.squeeze(), dim=1).cpu().data,
            score_thresh=0.1)

        pred_boxes.append(box_preds)
        pred_labels.append(label_preds)
        pred_scores.append(score_preds)

    aps = (voc_eval(pred_boxes,
                    pred_labels,
                    pred_scores,
                    gt_boxes,
                    gt_labels,
                    gt_difficults=None,
                    iou_thresh=0.5,
                    use_07_metric=False))
    net.train()
    return aps
Esempio n. 2
0
                  # bgr and 0-255
        ])(x)
def transform(img, boxes, labels):
    img, boxes = resize(img, boxes, size=(opt.img_size, opt.img_size))
    img = transforms.Compose([
        transforms.ToTensor(),
        caffe_normalize
    ])(img)
    return img, boxes, labels

dataset = ListDataset(root=opt.eval_img_root, list_file=opt.eval_img_list, transform=transform)
box_coder = SSDBoxCoder(net.module)

nums_img = dataset.__len__()
idx = random.randint(0, nums_img)
inputs, _, _ = dataset.__getitem__(idx)
inputs = inputs.unsqueeze(0)
with torch.no_grad():
    loc_preds, cls_preds = net(Variable(inputs.cuda()))
boxes, labels, scores = box_coder.decode(
    loc_preds.cpu().data.squeeze(), F.softmax(cls_preds.squeeze(), dim=1).cpu().data, score_thresh=0.5)

img = Image.open(opt.eval_img_root + dataset.fnames[idx])
sw = float(img.size[0])/float(opt.img_size)
sh = float(img.size[1])/float(opt.img_size)
boxes = boxes.type(torch.FloatTensor) * torch.tensor([sw, sh, sw, sh])
draw = ImageDraw.Draw(img)
nums_boxes = boxes.size()[0]
for i in range(nums_boxes):
    draw.rectangle(list(boxes[i]), outline='red')
    draw.text((boxes[i][0], boxes[i][1]), 'category: %s' % labels[i].item(), 'yellow')
Esempio n. 3
0
pred_labels = []
pred_scores = []
gt_boxes = []
gt_labels = []

#with open('torchcv/datasets/voc/voc07_test_difficult.txt') as f:
#    gt_difficults = []
#    for line in f.readlines():
#        line = line.strip().split()
#        d = np.array([int(x) for x in line[1:]])
#        gt_difficults.append(d)

print('Processing img..')
nums_img = dataset.__len__()
for i in tqdm(range(nums_img)):
    inputs, box_targets, label_targets = dataset.__getitem__(i)
    gt_boxes.append(box_targets)
    gt_labels.append(label_targets)

    inputs = inputs.unsqueeze(0)
    with torch.no_grad():
        loc_preds, cls_preds = net(Variable(inputs.cuda()))
    box_preds, label_preds, score_preds = box_coder.decode(
        loc_preds.cpu().data.squeeze(),
        F.softmax(cls_preds.squeeze(), dim=1).cpu().data,
        score_thresh=0.1)

    pred_boxes.append(box_preds)
    pred_labels.append(label_preds)
    pred_scores.append(score_preds)