Beispiel #1
0
def get_data_loader(name='voc', train=True, batch_size=64, split='train'):
    if name == 'voc':
        from voc_dataset import VOCDataset
        dataset = VOCDataset(split, 64)
    else:
        raise NotImplementedError

    loader = DataLoader(
        dataset,
        batch_size=batch_size,
        shuffle=train,
        num_workers=4,
    )
    return loader
def nearest_neighbors():
    # PATH = "./checkpoints/resnet_pretrained.pth"
    # model = models.resnet18()
    # model.fc = nn.Linear(in_features=512, out_features=len(VOCDataset.CLASS_NAMES), bias=True)
    PATH = "./checkpoints/caffenet.pth"
    model = CaffeNet()
    model = CaffeNet(num_classes=len(VOCDataset.CLASS_NAMES),
                     inp_size=227,
                     c_dim=3).to(device)

    model.load_state_dict(
        torch.load(PATH, map_location=lambda storage, loc: storage))
    model.eval()
    model = model.to(device)
    test_loader = utils.get_data_loader('voc',
                                        train=False,
                                        batch_size=args.test_batch_size,
                                        split='test')

    model.max_pool5.register_forward_hook(feature_append)
    dataset = VOCDataset('test', 64)

    with torch.no_grad():
        for data, target, wgt in test_loader:
            data, target, wgt = data.to(device), target.to(device), wgt.to(
                device)
            output = model(data)

    pool_features = np.vstack(pool_features_list)

    nbrs = NearestNeighbors(n_neighbors=5,
                            algorithm='ball_tree').fit(pool_features)
    distances, indices = nbrs.kneighbors(pool_features)

    output_no = 0
    no_neighbors = 5
    selected_class_idx = []

    for i in range(pool_features.shape[0]):
        rand_idx = np.random.randint(low=0, high=pool_features.shape[0])
        class_label, _ = dataset.anno_list[rand_idx]
        class_label = np.squeeze(np.argwhere(class_label == 1)).tolist()

        if len(list(set(selected_class_idx) & set(class_label))) == 0:
            selected_class_idx.extend(class_label)
            for i in range(no_neighbors):
                vis_image(dataset, indices[rand_idx, i])
        import pdb
        pdb.set_trace()
def get_data_loader(name='voc', train=True, batch_size=64, split='train'):
    flag = 1

    if name == 'voc':
        from voc_dataset import VOCDataset
        dataset = VOCDataset(split, 227)
    else:
        raise NotImplementedError

    loader = DataLoader(
        dataset,
        batch_size=batch_size,
        shuffle=train,
        num_workers=4,
    )
    if (flag == 1):
        return loader

    else:
        return loader, dataset.index_list
Beispiel #4
0
def get_data_loader(name='voc',
                    train=True,
                    batch_size=64,
                    split='train',
                    args=None):

    if name == 'voc':
        from voc_dataset import VOCDataset
        size = 64 if args is None else args.size
        data_dir = '../data/VOCdevkit/VOC2007/'
        dataset = VOCDataset(split, size, data_dir)
    else:
        raise NotImplementedError

    loader = DataLoader(
        dataset,
        batch_size=batch_size,
        shuffle=train,
        num_workers=4,
    )
    return loader
    voc_root = args.voc_root
    imageset_txt = args.imageset_txt
    eval_results_dir = args.eval_results_dir[0]
    class_file = args.class_file

    #voc_root = "/datasets/VOCdevkit/VOC2007"
    #imageset_txt = "test.txt"
    #eval_results_dir = "./eval_results"
    #class_file = "./models/voc-model-labels.txt"
    save_file = 'static.txt'

    with open(class_file, "r") as f:
        lines = f.readlines()
    class_names = [t.strip("\n") for t in lines]

    dataset = VOCDataset(voc_root, is_test=True)
    true_case_stat, all_gb_boxes, all_difficult_cases = group_voc_annotation_by_class(dataset)

    static_result = open(save_file, "a")
    precs = []
    aps = []
    recs = []
    print("\n\nAverage Precision Per-class:")
    for confi_thresh in np.arange(0.3, 0.85, 0.05):
        static_result.write('conf_thresh: ' +str(confi_thresh) + "\n")
        for class_index, class_name in enumerate(class_names):
            if class_index == 0:
                continue
            prediction_path = get_voc_results_file_template(
                    out_dir=eval_results_dir).format(class_name)
            prec, ap, rec = compute_ap_from_predfile_per_class(
Beispiel #6
0
        return loss

    def test(self, img, labels):
        session = tf.get_default_session()
        feed_dict = {self.input_img: img, self.input_labels: labels}
        num1, num2 = session.run([self.num1, self.num2], feed_dict=feed_dict)
        return num1, num2


if __name__ == '__main__':
    import numpy as np

    from voc_dataset import VOCDataset, bbox_visualization
    from tool import array_img_save

    dataset = VOCDataset()
    network = FasterRCNN('FasterRCNN', 21, 64)
    network.buind()
    network.complete()
    #restorer_fc = tf.train.Saver(tf.trainable_variables(scope='vgg_16'))
    #session = tf.get_default_session()
    #restorer_fc.restore(session, "./VGG/vgg_16.ckpt")

    for step in range(100000):
        images, labels, _ = dataset.get_minbatch(1,
                                                 step,
                                                 selectivesearch=False)
        labels = np.reshape(labels, (-1, 5))

        loss = network.train(images, labels)
        if step % 5 == 0:
def tsne():

    PATH = "./checkpoints/caffenet.pth"
    model = CaffeNet()
    model = CaffeNet(num_classes=len(VOCDataset.CLASS_NAMES),
                     inp_size=227,
                     c_dim=3).to(device)

    model.load_state_dict(
        torch.load(PATH, map_location=lambda storage, loc: storage))
    model.eval()
    model = model.to(device)
    dataset = VOCDataset('test', 64)
    sample_size = 1000
    selected_idx = np.random.randint(low=0,
                                     high=dataset.__len__(),
                                     size=sample_size)
    test_loader = utils.get_data_loader('voc',
                                        train=False,
                                        batch_size=dataset.__len__(),
                                        split='test')

    model.fc2.register_forward_hook(feature_append)

    with torch.no_grad():
        for data, target, wgt in test_loader:
            data, target, wgt = data.to(device), target.to(device), wgt.to(
                device)
            data = data[selected_idx]
            target = target[selected_idx]
            output = model(data)

    target_label = np.zeros(sample_size)

    for i in range(sample_size):
        target_label[i] = int(
            np.mean(np.argwhere(target[i].data.cpu().numpy())))
    features = np.vstack(pool_features_list)
    pca = PCA(n_components=100)
    reduced_features = pca.fit_transform(features)
    tsne = TSNE(n_components=2,
                verbose=1,
                perplexity=50,
                n_iter=1000,
                n_jobs=8)
    tsne_results = tsne.fit_transform(reduced_features)

    df = pd.DataFrame(data=tsne_results,
                      columns=['tsne-2d-one', 'tsne-2d-two'])

    df['classes'] = target_label
    plt.figure(figsize=(16, 20))

    sns.scatterplot(x="tsne-2d-one",
                    y="tsne-2d-two",
                    hue="classes",
                    palette=sns.color_palette("hls", 20),
                    data=df,
                    legend="full",
                    alpha=0.3)
    plt.show()