def get_unique_anchors(scales):
    # print(print_str)
    anchorbox = anchorBox('defined3', input_dim=input_dim, scale_ratios=scales)
    anchors = anchorbox.forward()
    print(anchors.size())
    unique_anchors = anchors.numpy()
    unique_anchors[:, 0] = unique_anchors[:, 0] * 0
    unique_anchors[:, 1] = unique_anchors[:, 1] * 0
    anchors = np.unique(unique_anchors, axis=0)
    return torch.from_numpy(anchors)
def just_whs():
    for dataset in ['voc', 'coco']:
        for scales in [[1., 1.35, 1.75], [1., 1.5], [1.]]:
            if dataset == 'coco':
                train_sets = ['train2017']
                val_sets = ['val2017']
            else:
                train_sets = ['train2007', 'val2007', 'train2012', 'val2012']
                val_sets = ['test2007']

            # classes, trainlist, print_str = make_lists(dataset=dataset, rootpath=base_dir+dataset+'/')
            classes, trainlist, print_str = make_object_lists(
                base_dir + dataset + '/', train_sets)
            # print(print_str)
            anchorbox = anchorBox(input_dim=input_dim, scale_ratios=scales)
            anchors = anchorbox.forward()
            print(anchors.size())
            unique_anchors = anchors.numpy()
            unique_anchors[:, 0] = unique_anchors[:, 0] * 0
            unique_anchors[:, 1] = unique_anchors[:, 1] * 0
            anchors = np.unique(unique_anchors, axis=0)
            anchors = torch.from_numpy(anchors)
            # print(anchors)
            all_recall = torch.FloatTensor(len(trainlist) * 30, 1)
            count = 0
            for index in range(len(trainlist)):
                annot_info = trainlist[index]
                img_id = annot_info[1]
                targets = np.asarray(annot_info[3])
                bboxes = torch.FloatTensor(annot_info[2])
                # print(bboxes)
                bboxes[:, 2] = bboxes[:, 2] - bboxes[:, 0]
                bboxes[:, 3] = bboxes[:, 3] - bboxes[:, 1]
                bboxes[:, 0] = bboxes[:, 0] * 0.0
                bboxes[:, 1] = bboxes[:, 1] * 0.0
                # print(bboxes)
                overlaps = jaccard(bboxes, anchors)
                best_anchor_overlap, best_anchor_idx = overlaps.max(
                    1, keepdim=True)
                # print(torch.sum(best_anchor_overlap>thresh))
                for bi in range(best_anchor_overlap.size(0)):
                    bo = best_anchor_overlap[bi]
                    # print(bo)
                    all_recall[count, :] = bo
                    count += 1
            all_recall = all_recall[:count]

            print(scales)
            print(
                '{:s} recall more than 0.5 {:.02f} average is {:.02f}'.format(
                    dataset, 100.0 * torch.sum(all_recall > thresh) / count,
                    torch.mean(all_recall)))
Пример #3
0
def main():
    args.step_values = [int(val) for val in args.step_values.split(',')]
    # args.loss_reset_step = 10
    args.log_step = 10
    args.dataset = args.dataset.lower()
    args.basenet = args.basenet.lower()

    args.bn = abs(args.bn)  # 0 freeze or else use bn
    if args.bn > 0:
        args.bn = 1  # update bn layer set the flag to 1

    args.shared_heads = abs(
        args.shared_heads)  # 0 no sharing of feature else yes
    if args.shared_heads > 0:
        args.shared_heads = 1

    args.exp_name = 'FPN{:d}-{:s}sh{:02d}-{:s}-bs{:02d}-{:s}-lr{:05d}-bn{:d}'.format(
        args.input_dim, args.anchor_type, args.shared_heads, args.dataset,
        args.batch_size, args.basenet, int(args.lr * 100000), args.bn)

    args.save_root += args.dataset + '/'
    args.save_root = args.save_root + 'cache/' + args.exp_name + '/'

    if not os.path.isdir(
            args.save_root):  # if save directory doesn't exist create it
        os.makedirs(args.save_root)

    source_dir = args.save_root + '/source/'  # where to save the source
    utils.copy_source(source_dir)

    anchors = 'None'
    with torch.no_grad():
        if args.anchor_type == 'kmeans':
            anchorbox = kanchorBoxes(input_dim=args.input_dim,
                                     dataset=args.dataset)
        else:
            anchorbox = anchorBox(args.anchor_type,
                                  input_dim=args.input_dim,
                                  dataset=args.dataset)
        anchors = anchorbox.forward()
        args.ar = anchorbox.ar

    args.num_anchors = anchors.size(0)

    if args.dataset == 'coco':
        args.train_sets = ['train2017']
        args.val_sets = ['val2017']
    else:
        args.train_sets = ['train2007', 'val2007', 'train2012', 'val2012']
        args.val_sets = ['test2007']

    args.means = [0.485, 0.456, 0.406]
    args.stds = [0.229, 0.224, 0.225]

    print('\nLoading Datasets')
    train_dataset = Detection(args,
                              train=True,
                              image_sets=args.train_sets,
                              transform=Augmentation(args.input_dim,
                                                     args.means, args.stds))
    print('Done Loading Dataset Train Dataset :::>>>\n',
          train_dataset.print_str)
    val_dataset = Detection(args,
                            train=False,
                            image_sets=args.val_sets,
                            transform=BaseTransform(args.input_dim, args.means,
                                                    args.stds),
                            full_test=False)
    print('Done Loading Dataset Validation Dataset :::>>>\n',
          val_dataset.print_str)

    args.num_classes = len(train_dataset.classes) + 1
    args.classes = train_dataset.classes

    args.head_size = 256
    if args.shared_heads > 0:
        net = build_fpn_shared_heads(args.basenet,
                                     args.model_dir,
                                     ar=args.ar,
                                     head_size=args.head_size,
                                     num_classes=args.num_classes)
    else:
        net = build_fpn_unshared(args.basenet,
                                 args.model_dir,
                                 ar=args.ar,
                                 head_size=args.head_size,
                                 num_classes=args.num_classes)

    net = net.cuda()

    if args.ngpu > 1:
        print('\nLets do dataparallel\n')
        net = torch.nn.DataParallel(net)

    optimizer = optim.SGD(net.parameters(),
                          lr=args.lr,
                          momentum=args.momentum,
                          weight_decay=args.weight_decay)

    criterion = MultiBoxLoss()

    scheduler = MultiStepLR(optimizer,
                            milestones=args.step_values,
                            gamma=args.gamma)

    train(args, net, anchors, optimizer, criterion, scheduler, train_dataset,
          val_dataset)
Пример #4
0
def main():

    args.eval_iters = [int(val) for val in args.eval_iters.split(',')]
    # args.loss_reset_step = 10
    args.log_step = 10
    args.dataset = args.dataset.lower()
    args.basenet = args.basenet.lower()

    args.bn = abs(args.bn)  # 0 freeze or else use bn
    if args.bn > 0:
        args.bn = 1  # update bn layer set the flag to 1

    args.exp_name = 'FPN{:d}-{:s}sh{:02d}-{:s}-bs{:02d}-{:s}-lr{:05d}-bn{:d}'.format(
        args.input_dim, args.anchor_type, args.shared_heads, args.dataset,
        args.batch_size, args.basenet, int(args.lr * 100000), args.bn)

    args.save_root += args.dataset + '/'
    args.save_root = args.save_root + 'cache/' + args.exp_name + '/'

    if not os.path.isdir(
            args.save_root):  # if save directory doesn't exist create it
        os.makedirs(args.save_root)

    source_dir = args.save_root + '/source/'  # where to save the source
    utils.copy_source(source_dir)

    anchors = 'None'
    with torch.no_grad():
        if args.anchor_type == 'kmeans':
            anchorbox = kanchorBoxes(input_dim=args.input_dim,
                                     dataset=args.dataset)
        else:
            anchorbox = anchorBox(args.anchor_type,
                                  input_dim=args.input_dim,
                                  dataset=args.dataset)
        anchors = anchorbox.forward()
        args.ar = anchorbox.ar

    args.num_anchors = anchors.size(0)
    anchors = anchors.cuda(0, non_blocking=True)
    if args.dataset == 'coco':
        args.train_sets = ['train2017']
        args.val_sets = ['val2017']
    else:
        args.train_sets = ['train2007', 'val2007', 'train2012', 'val2012']
        args.val_sets = ['test2007']

    args.means = [0.485, 0.456, 0.406]
    args.stds = [0.229, 0.224, 0.225]
    val_dataset = Detection(args,
                            train=False,
                            image_sets=args.val_sets,
                            transform=BaseTransform(args.input_dim, args.means,
                                                    args.stds),
                            full_test=False)
    print('Done Loading Dataset Validation Dataset :::>>>\n',
          val_dataset.print_str)
    args.data_dir = val_dataset.root
    args.num_classes = len(val_dataset.classes) + 1
    args.classes = val_dataset.classes
    args.bias_heads = args.bias_heads > 0
    args.head_size = 256
    if args.shared_heads > 0:
        net = build_fpn_shared_heads(args.basenet,
                                     args.model_dir,
                                     ar=args.ar,
                                     head_size=args.head_size,
                                     num_classes=args.num_classes,
                                     bias_heads=args.bias_heads)
    else:
        net = build_fpn_unshared(args.basenet,
                                 args.model_dir,
                                 ar=args.ar,
                                 head_size=args.head_size,
                                 num_classes=args.num_classes,
                                 bias_heads=args.bias_heads)

    net = net.cuda()

    if args.ngpu > 1:
        print('\nLets do dataparallel\n')
        net = torch.nn.DataParallel(net)
    net.eval()

    for iteration in args.eval_iters:
        args.det_itr = iteration
        log_file = open(
            "{:s}/testing-{:d}.log".format(args.save_root, iteration), "w", 1)
        log_file.write(args.exp_name + '\n')

        args.model_path = args.save_root + '/model_' + repr(iteration) + '.pth'
        log_file.write(args.model_path + '\n')

        net.load_state_dict(torch.load(args.model_path))

        print('Finished loading model %d !' % iteration)
        # Load dataset
        val_data_loader = data_utils.DataLoader(val_dataset,
                                                int(args.batch_size / 2),
                                                num_workers=args.num_workers,
                                                shuffle=False,
                                                pin_memory=True,
                                                collate_fn=custum_collate)

        # evaluation
        torch.cuda.synchronize()
        tt0 = time.perf_counter()
        log_file.write('Testing net \n')
        net.eval()  # switch net to evaluation mode
        if args.dataset != 'coco':
            mAP, ap_all, ap_strs, det_boxes = validate(
                args,
                net,
                anchors,
                val_data_loader,
                val_dataset,
                iteration,
                iou_thresh=args.iou_thresh)
        else:
            mAP, ap_all, ap_strs, det_boxes = validate_coco(
                args,
                net,
                anchors,
                val_data_loader,
                val_dataset,
                iteration,
                iou_thresh=args.iou_thresh)

        for ap_str in ap_strs:
            print(ap_str)
            log_file.write(ap_str + '\n')
        ptr_str = '\nMEANAP:::=>' + str(mAP) + '\n'
        print(ptr_str)
        log_file.write(ptr_str)

        torch.cuda.synchronize()
        print('Complete set time {:0.2f}'.format(time.perf_counter() - tt0))
        log_file.close()
Пример #5
0
def main(img_names):
    anchors = 'None'
    with torch.no_grad():
        if args.anchor_type == 'kmeans':
            anchorbox = kanchorBoxes(input_dim=args.input_dim,
                                     dataset=args.dataset)
        else:
            anchorbox = anchorBox(args.anchor_type,
                                  input_dim=args.input_dim,
                                  dataset=args.dataset)
        anchors = anchorbox.forward()
        args.ar = anchorbox.ar

    args.num_anchors = anchors.size(0)
    anchors = anchors.cuda(0, non_blocking=True)

    if args.dataset == 'coco':
        args.num_classes = 81
    else:
        args.num_classes = 21

    cmaps = plt.cm.get_cmap('jet', args.num_classes)
    bbox_colors = [cmaps(i) for i in range(args.num_classes)]
    random.shuffle(bbox_colors)
    args.classes = get_class_names(args.dataset)

    args.means = [0.485, 0.456, 0.406]
    args.stds = [0.229, 0.224, 0.225]
    transform = BaseTransform(args.input_dim, args.means, args.stds)
    args.bias_heads = args.bias_heads > 0
    args.head_size = 256
    if args.shared_heads > 0:
        net = build_fpn_shared_heads(args.basenet,
                                     '',
                                     ar=args.ar,
                                     head_size=args.head_size,
                                     num_classes=args.num_classes,
                                     bias_heads=args.bias_heads)
    else:
        net = build_fpn_unshared(args.basenet,
                                 '',
                                 ar=args.ar,
                                 head_size=args.head_size,
                                 num_classes=args.num_classes,
                                 bias_heads=args.bias_heads)

    net = net.cuda()

    with torch.no_grad():

        if args.ngpu > 1:
            print('\nLets do dataparallel\n')
            net = torch.nn.DataParallel(net)

        net.eval()
        net.load_state_dict(torch.load(args.model_path))

        softmax = nn.Softmax(dim=2).cuda().eval()
        for image_name in img_names:
            print(args.samples_path, image_name)
            img = np.asarray([cv2.imread(args.samples_path + image_name)])

            images, _, _ = transform(img, [], [])
            images = torch.stack(images, 0)

            obj_boxes, cls_scores, bin_scores, obj_ids = extract_boxes(
                args, images, net, softmax, anchors)
            # pdb.set_trace()
            img = img[0]
            height, width, ch = img.shape
            fig, ax = plt.subplots(1)
            ax.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
            print('Number of boxes detected', obj_boxes.shape[0])
            count = 0
            for ik in range(obj_boxes.size(0)):
                if bin_scores[
                        ik] > args.final_thresh:  ## only plot boxes that has higher score than this threshold
                    count += 1
                    scores = cls_scores[ik, 1:].squeeze().cpu().numpy()
                    win_class = np.argmax(scores)
                    win_label = args.classes[win_class]
                    box = obj_boxes[ik, :].cpu().numpy()
                    x1 = box[0] * width
                    x2 = box[2] * width
                    y1 = box[1] * height
                    y2 = box[3] * height
                    b_colour = bbox_colors[win_class]
                    bbox = patches.Rectangle((x1, y1),
                                             x2 - x1,
                                             y2 - y1,
                                             linewidth=2,
                                             edgecolor=b_colour,
                                             facecolor="none")
                    ax.add_patch(bbox)
                    plt.text(x1,
                             y1,
                             s=win_label,
                             color="white",
                             verticalalignment="top",
                             bbox={
                                 "color": b_colour,
                                 "pad": 0
                             })

            plt.axis("off")
            plt.gca().xaxis.set_major_locator(NullLocator())
            plt.gca().yaxis.set_major_locator(NullLocator())
            plt.savefig(args.outputs_path + image_name,
                        bbox_inches="tight",
                        pad_inches=0.0)
            plt.close()
            print('Number of object plotted', count)
Пример #6
0
def main(allimages):

    if not os.path.isdir(args.save_path):
        os.mkdir(args.save_path)
    log_file = open("{:s}extracting.log".format(args.save_path), "w", 1)
    # log_file.write(args.exp_name + '\n')
    log_file.write(args.model_path + '\n')

    anchors = 'None'
    with torch.no_grad():
        if args.anchor_type == 'kmeans':
            anchorbox = kanchorBoxes(input_dim=args.input_dim,
                                     dataset=args.dataset)
        else:
            anchorbox = anchorBox(args.anchor_type,
                                  input_dim=args.input_dim,
                                  dataset=args.dataset)
        anchors = anchorbox.forward()
        args.ar = anchorbox.ar

    args.num_anchors = anchors.size(0)
    anchors = anchors.cuda(0, non_blocking=True)

    if args.dataset == 'coco':
        args.num_classes = 81
    else:
        args.num_classes = 21

    args.classes = get_class_names(args.dataset)

    args.means = [0.485, 0.456, 0.406]
    args.stds = [0.229, 0.224, 0.225]
    transform = BaseTransform(args.input_dim, args.means, args.stds)
    args.bias_heads = args.bias_heads > 0
    args.head_size = 256
    if args.shared_heads > 0:
        net = build_fpn_shared_heads(args.basenet,
                                     '',
                                     ar=args.ar,
                                     head_size=args.head_size,
                                     num_classes=args.num_classes,
                                     bias_heads=args.bias_heads)
    else:
        net = build_fpn_unshared(args.basenet,
                                 '',
                                 ar=args.ar,
                                 head_size=args.head_size,
                                 num_classes=args.num_classes,
                                 bias_heads=args.bias_heads)

    net = net.cuda()
    feature_size = [75, 38, 19, 10, 5]
    kd = 3
    args.kd = kd

    with torch.no_grad():
        flatten_layers = nn.ModuleList([
            Spatialflatten(feature_size[0]**2, kd=kd),
            Spatialflatten(feature_size[1]**2, kd=kd),
            Spatialflatten(feature_size[2]**2, kd=kd),
            Spatialflatten(feature_size[3]**2, kd=kd),
            Spatialflatten(feature_size[4]**2, kd=kd)
        ])
        flatten_layers = flatten_layers.cuda().eval()

        if args.ngpu > 1:
            print('\nLets do dataparallel\n')
            net = torch.nn.DataParallel(net)

        net.eval()
        net.load_state_dict(torch.load(args.model_path))

        softmax = nn.Softmax(dim=2).cuda().eval()
        last_id = torch.LongTensor([anchors.size(0) + args.ar - 1]).cuda()
        empty_box = torch.zeros(1, 4).cuda()
        empty_score = torch.zeros(1, args.num_classes).cuda()
        torch.cuda.synchronize()
        t0 = time.perf_counter()
        print_num = 1
        num_all_images = len(allimages)
        for idx, image_name in enumerate(img_names):
            print(args.samples_path, image_name)

            save_filename = args.save_path + image_name + '.pth'
            if not os.path.isfile(
                    save_filename
            ):  # only compute features for images for which feature are not computed yet
                img = np.asarray([cv2.imread(args.samples_path + image_name)])
                # print(img.shape)
                images, _, _ = transform(img, [], [])
                images = torch.stack(images, 0)

                node_features, boxes, cls_scores, conf_scores = extract_feat(
                    args, images, net, softmax, flatten_layers, anchors,
                    args.num_nodes, last_id, empty_box, empty_score)

                if idx % print_num == 0:
                    torch.cuda.synchronize()
                    t1 = time.perf_counter()
                    pt_str = 'Done [{:03d}/{:d}] times taken for {:d} images is {:0.2f}'.format(
                        idx, num_all_images, print_num, t1 - t0)
                    log_file.write(pt_str + '\n')
                    print(pt_str)
                    t0 = t1

                fboxes = boxes.cpu().squeeze(0)
                fscores = conf_scores.cpu().view(-1,
                                                 1)  # append objectness score
                frame_feats = torch.cat((fscores, fboxes), 1)  # append boxes
                frame_feats = torch.cat(
                    (frame_feats, cls_scores.cpu().squeeze(0)),
                    1)  # inverse objectnes and clssification
                frame_feats = torch.cat(
                    (frame_feats, node_features.cpu().squeeze(0)),
                    1)  # features
                frame_feats = frame_feats.type(torch.FloatTensor)

                torch.save(
                    frame_feats, save_filename
                )  # Stores objectness score + boxes + inverse objectnes and clssification + features