示例#1
0
def demo(img_id=0):
    net = build_ssd('test', 512, 21)  # initialize SSD
    print(net)
    net.load_weights(
        '/media/sunwl/Datum/Projects/GraduationProject/SSD_VHR_512/weights/ssd512_voc_resume_95000.pth'
    )
    testset = VOCDetection(VOCroot, [('2012', 'val')], None,
                           AnnotationTransform())
    image = testset.pull_image(img_id)
    # image = cv2.imread('demos/04.png')
    rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    # View the sampled input image before transform
    plt.figure(figsize=(10, 10))
    plt.imshow(rgb_image)

    x = cv2.resize(rgb_image, (512, 512)).astype(np.float32)
    x -= (104.0, 117.0, 123.0)
    x = x.astype(np.float32)
    x = x[:, :, ::-1].copy()
    x = torch.from_numpy(x).permute(2, 0, 1)

    xx = Variable(x.unsqueeze(0))  # wrap tensor in Variable
    if torch.cuda.is_available():
        xx = xx.cuda()
    y = net(xx)

    plt.figure(figsize=(10, 10))
    colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
    plt.imshow(rgb_image.astype(np.uint8))  # plot the image for matplotlib
    currentAxis = plt.gca()

    detections = y.data

    # scale each detection back up to the image
    scale = torch.Tensor(rgb_image.shape[1::-1]).repeat(2)
    for i in range(detections.size(1)):
        j = 0
        while detections[0, i, j, 0] >= 0.5:
            score = detections[0, i, j, 0]
            label_name = labels[i - 1]
            display_txt = '%s: %.2f' % (label_name, score)
            pt = (detections[0, i, j, 1:] * scale).cpu().numpy()
            coords = (pt[0], pt[1]), pt[2] - pt[0] + 1, pt[3] - pt[1] + 1
            color = colors[i]
            currentAxis.add_patch(
                plt.Rectangle(*coords,
                              fill=False,
                              edgecolor=color,
                              linewidth=2))
            currentAxis.text(pt[0],
                             pt[1],
                             display_txt,
                             bbox={
                                 'facecolor': color,
                                 'alpha': 0.5
                             })
            j += 1
    plt.show()
示例#2
0
def demo_cv2(img_id=0):
    net = build_msc('test', 21)  # initialize SSD
    print(net)
    net.load_weights(
        '/media/sunwl/Datum/Projects/GraduationProject/Multi_Scale_CNN_512/weights/v2_voc.pth'
    )
    testset = VOCDetection(VOCroot, [('2012', 'val')], None,
                           AnnotationTransform)
    image = testset.pull_image(img_id)
    # image = cv2.imread('demos/047.jpg')
    rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    x = cv2.resize(rgb_image, (512, 512)).astype(np.float32)
    x -= (104.0, 117.0, 123.0)
    x = x.astype(np.float32)
    x = x[:, :, ::-1].copy()
    x = torch.from_numpy(x).permute(2, 0, 1)

    xx = Variable(x.unsqueeze(0))  # wrap tensor in Variable
    if torch.cuda.is_available():
        xx = xx.cuda()
    y = net(xx)
    colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
    detections = y.data

    # scale each detection back up to the image
    scale = torch.Tensor(rgb_image.shape[1::-1]).repeat(2)
    bgr_image = cv2.cvtColor(rgb_image, cv2.COLOR_BGR2RGB)
    im2show = np.copy(bgr_image)
    for i in range(detections.size(1)):
        j = 0
        while detections[0, i, j, 0] >= 0.5:
            score = detections[0, i, j, 0]
            label_name = labels[i - 1]
            display_txt = '%s: %.2f' % (label_name, score)
            pt = (detections[0, i, j, 1:] * scale).cpu().numpy()
            color = colors[i]
            color = [int(c * 255) for c in color[:3]]
            coords = pt[0], pt[1], pt[2], pt[3]
            cv2.rectangle(im2show,
                          coords[0:2],
                          coords[2:4],
                          color,
                          thickness=2)
            cv2.putText(im2show,
                        display_txt, (int(coords[0]), int(coords[1]) - 3),
                        cv2.FONT_HERSHEY_PLAIN,
                        1.0,
                        color,
                        thickness=1)
            j += 1
    cv2.imshow('original', bgr_image)
    cv2.imshow('demo', im2show)
    # cv2.imwrite(os.path.join('/media/sunwl/Datum/Projects/GraduationProject/Multi_Scale_CNN_512', "outputs",
    #                          "{:03d}.jpg".format(img_id)), im2show)
    cv2.waitKey(0)
示例#3
0
if torch.cuda.is_available():
    torch.set_default_tensor_type('torch.cuda.FloatTensor')

from ssd import build_ssd
# from models import build_ssd as build_ssd_v1 # uncomment for older pool6 model

net = build_ssd('test', 300, 21)    # initialize SSD
net.load_weights('../weights/ssd300_mAP_77.43_v2.pth')
# image = cv2.imread('./data/example.jpg', cv2.IMREAD_COLOR)  # uncomment if dataset not downloaded
%matplotlib inline
from matplotlib import pyplot as plt
from data import VOCDetection, VOCroot, AnnotationTransform
# here we specify year (07 or 12) and dataset ('test', 'val', 'train') 
testset = VOCDetection(VOCroot, [('2007', 'val')], None, AnnotationTransform())
img_id = 60
image = testset.pull_image(img_id)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# View the sampled input image before transform
plt.figure(figsize=(10,10))
plt.imshow(rgb_image)
plt.show()

x = cv2.resize(image, (300, 300)).astype(np.float32)
x -= (104.0, 117.0, 123.0)
x = x.astype(np.float32)
x = x[:, :, ::-1].copy()
plt.imshow(x)
x = torch.from_numpy(x).permute(2, 0, 1)

xx = Variable(x.unsqueeze(0))     # wrap tensor in Variable
if torch.cuda.is_available():
示例#4
0
    elif dataset == 'coco':
        dataset = COCODataset(data_dir=coco_root,
                              img_size=size,
                              transform=BaseTransform([size, size]))

    boxes = []
    print("The dataset size: ", len(dataset))
    print("Loading the dataset ...")
    for i in range(len(dataset)):
        if i % 5000 == 0:
            print('Loading datat [%d / %d]' % (i + 1, len(dataset)))

        if dataset == 'coco':
            # For COCO
            img, _ = dataset.pull_image(i)
            w, h = img.shape[1], img.shape[0]
            annotation = dataset.pull_anno(i)

        elif dataset == 'voc':
            # For VOC
            img = dataset.pull_image(i)
            w, h = img.shape[1], img.shape[0]
            _, annotation = dataset.pull_anno(i)

        # prepare bbox datas
        for box_and_label in annotation:
            box = box_and_label[:-1]
            xmin, ymin, xmax, ymax = box
            bw = (xmax - xmin) / w * size
            bh = (ymax - ymin) / h * size
示例#5
0
import cv2
from matplotlib import pyplot as plt
from data import VOCDetection, VOC_ROOT, VOCAnnotationTransform
from ssd import build_ssd

if torch.cuda.is_available():
    torch.set_default_tensor_type('torch.cuda.FloatTensor')

net = build_ssd('test', 300, 21)    # initialize SSD
net.load_weights('weights/ssd300_mAP_77.43_v2.pth')


# here we specify year (07 or 12) and dataset ('test', 'val', 'train')
testset = VOCDetection(VOC_ROOT, [('2007', 'val')], None, VOCAnnotationTransform())
img_id = 345  # change here for image id
image, image_name = testset.pull_image(img_id)
print(image_name)
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# View the sampled input image before transform
# plt.figure(figsize=(6,6))
# plt.imshow(rgb_image)
# plt.show()


def base_transform(image):
    x = cv2.resize(image, (300, 300)).astype(np.float32)
    x -= (104.0, 117.0, 123.0)
    x = x.astype(np.float32)
    x = x[:, :, ::-1].copy()
    # plt.imshow(x)
    x = torch.from_numpy(x).permute(2, 0, 1)
示例#6
0
show_threshold = 0.05
net = build_ssd('test', 300, 2)
net.load_state_dict(torch.load('weights/ssd300_0712_115000.pth'))
testset = VOCDetection(VOCroot, [('2007', 'test')], None,
                       AnnotationTransform())
transform = BaseTransform(net.size, (104, 117, 123))
net.eval()
net.cuda()
cudnn.benchmark = True

print('Finished loading model!')

num_images = len(testset)
for i in range(num_images):
    img = testset.pull_image(i)
    img_id, annotation = testset.pull_anno(i)
    gtloc = tuple(np.array(annotation[0][:-1]).astype('int32'))
    x = torch.from_numpy(transform(img)[0]).permute(2, 0, 1)
    x = Variable(x.unsqueeze(0))
    x = x.cuda()
    y = net(x)
    detections = y.data
    scale = torch.Tensor(
        [img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
    im2show = np.copy(img)
    for i in range(1, detections.size(1)):
        j = 0
        while detections[0, i, j, 0] >= show_threshold:
            score = detections[0, i, j, 0]
            label_name = labelmap[i - 1]
示例#7
0
def do_test(args, model, detector, max_per_image=200, thresh=0.01):
    if args.dataset == 'VOC':
        dataset = VOCDetection(
            args, VOCroot, [('2007', 'test')], None,
            AnnotationTransform(0 if args.setting ==
                                'transfer' else args.split), True)
    elif args.dataset == 'COCO':
        dataset = COCODetection(COCOroot, [('2014', 'split_nonvoc_minival')],
                                None)
    else:
        raise ValueError(f"Unknown dataset: {args.dataset}")

    num_images = len(dataset)
    all_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)]
    transform = BaseTransform(model.size, rgb_means, (2, 0, 1))

    _t = {'im_detect': Timer(), 'misc': Timer()}
    det_file = os.path.join(args.save_folder, 'detections.pkl')

    if args.retest:
        f = open(det_file, 'rb')
        all_boxes = pickle.load(f)
        logger.info('Evaluating detections')
        dataset.evaluate_detections(all_boxes, args.save_folder)
        return

    for i in range(num_images):
        img = dataset.pull_image(i)
        scale = torch.Tensor(
            [img.shape[1], img.shape[0], img.shape[1],
             img.shape[0]]).to(model.device)
        with torch.no_grad():
            x = transform(img).unsqueeze(0)

        _t['im_detect'].tic()

        pred = model(x)  # forward pass
        boxes, scores = detector.forward(pred, priors)
        detect_time = _t['im_detect'].toc()
        boxes = boxes[0]  # percent and point form detection boxes
        scores = scores[0]  # [1, num_priors, num_classes]

        boxes *= scale  # scale each detection back up to the image
        boxes = boxes.cpu().numpy()
        scores = scores.cpu().numpy()

        _t['misc'].tic()

        for j in range(1, num_classes):
            inds = np.where(scores[:, j] > thresh)[0]
            if len(inds) == 0:
                all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
                continue
            c_bboxes = boxes[inds]
            c_scores = scores[inds, j]
            c_dets = np.hstack(
                (c_bboxes, c_scores[:, np.newaxis])).astype(np.float32,
                                                            copy=False)

            keep = nms(c_dets, 0.45, force_cpu=args.cpu)
            c_dets = c_dets[keep, :]
            all_boxes[j][i] = c_dets
        if max_per_image > 0:
            image_scores = np.hstack(
                [all_boxes[j][i][:, -1] for j in range(1, num_classes)])
            if len(image_scores) > max_per_image:
                image_thresh = np.sort(image_scores)[-max_per_image]
                for j in range(1, num_classes):
                    keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                    all_boxes[j][i] = all_boxes[j][i][keep, :]

        nms_time = _t['misc'].toc()

        if i % 20 == 0:
            logger.info('im_detect: {:d}/{:d} {:.3f}s {:.3f}s'.format(
                i + 1, num_images, detect_time, nms_time))
            _t['im_detect'].clear()
            _t['misc'].clear()

    with open(det_file, 'wb') as f:
        pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)

    logger.info('Evaluating detections')
    dataset.evaluate_detections(all_boxes, args.save_folder)
示例#8
0
def ssd_detect(limit_detection, dataset):
    # image = cv2.imread('./data/example.jpg', cv2.IMREAD_COLOR)  # uncomment if dataset not downloaded
    # here we specify year (07 or 12) and dataset ('test', 'val', 'train')
    testset = VOCDetection(VOC_ROOT, [('2007', dataset)], None,
                           VOCAnnotationTransform())
    for img_id in range(len(testset)):
        if img_id % 100 == 1:
            print(img_id, '/', len(testset))
        image = testset.pull_image(img_id)

        rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        x = cv2.resize(image, (300, 300)).astype(np.float32)
        x -= (104.0, 117.0, 123.0)
        x = x.astype(np.float32)
        x = x[:, :, ::-1].copy()
        x = torch.from_numpy(x).permute(2, 0, 1)

        xx = Variable(x.unsqueeze(0))  # wrap tensor in Variable
        if torch.cuda.is_available():
            xx = xx.cuda()
        y = net(xx)

        from data import VOC_CLASSES as labels
        top_k = 10

        plt.figure(figsize=(10, 10))
        colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
        # plot the image for matplotlib
        currentAxis = plt.gca()

        detections = y.data
        # scale each detection back up to the image
        scale = torch.Tensor(rgb_image.shape[1::-1]).repeat(2)
        for i in range(detections.size(1)):
            j = 0
            while detections[0, i, j, 0] >= limit_detection:
                score = detections[0, i, j, 0]
                label_name = labels[i - 1]
                display_txt = '%s: %.2f' % (label_name, score)
                pt = (detections[0, i, j, 1:] * scale).cpu().numpy()
                coords = (pt[0], pt[1]), pt[2] - pt[0] + 1, pt[3] - pt[1] + 1
                color = colors[i]
                currentAxis.add_patch(
                    plt.Rectangle(*coords,
                                  fill=False,
                                  edgecolor=color,
                                  linewidth=2))
                currentAxis.text(pt[0],
                                 pt[1],
                                 display_txt,
                                 bbox={
                                     'facecolor': color,
                                     'alpha': 0.5
                                 })
                j += 1

            #显示正确的检测框
            [imageid, gts] = testset.pull_anno(img_id)
            for gt in gts:
                coords = (gt[0], gt[1]), gt[2] - gt[0] + 1, gt[3] - gt[1] + 1
                currentAxis.add_patch(
                    plt.Rectangle(*coords,
                                  fill=False,
                                  edgecolor=colors[15],
                                  linewidth=2))
        plt.imshow(rgb_image)
        plt.savefig('result/data/' + str(img_id) + '.jpg')
        plt.close()