예제 #1
0
파일: train_rels.py 프로젝트: galsina/lml
def main():
    fname = os.path.join(conf.save_dir, 'train_losses.csv')
    train_f = open(fname, 'w')
    train_f.write(
        'iter,class_loss,rel_loss,total,recall20,recall50,recall100,recall20_con,recall50_con,recall100_con\n'
    )
    train_f.flush()

    fname = os.path.join(conf.save_dir, 'val_losses.csv')
    val_f = open(fname, 'w')
    val_f.write(
        'recall20,recall50,recall100,recall20_con,recall50_con,recall100_con\n'
    )
    val_f.flush()

    train, val, _ = VG.splits(num_val_im=conf.val_size,
                              filter_duplicate_rels=True,
                              use_proposals=conf.use_proposals,
                              filter_non_overlap=conf.mode == 'sgdet')
    train_loader, val_loader = VGDataLoader.splits(
        train,
        val,
        mode='rel',
        batch_size=conf.batch_size,
        num_workers=conf.num_workers,
        num_gpus=conf.num_gpus)

    detector = RelModel(
        classes=train.ind_to_classes,
        rel_classes=train.ind_to_predicates,
        num_gpus=conf.num_gpus,
        mode=conf.mode,
        require_overlap_det=True,
        use_resnet=conf.use_resnet,
        order=conf.order,
        nl_edge=conf.nl_edge,
        nl_obj=conf.nl_obj,
        hidden_dim=conf.hidden_dim,
        use_proposals=conf.use_proposals,
        pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
        pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
        pooling_dim=conf.pooling_dim,
        rec_dropout=conf.rec_dropout,
        use_bias=conf.use_bias,
        use_tanh=conf.use_tanh,
        limit_vision=conf.limit_vision,
        lml_topk=conf.lml_topk,
        lml_softmax=conf.lml_softmax,
        entr_topk=conf.entr_topk,
        ml_loss=conf.ml_loss)

    # Freeze the detector
    for n, param in detector.detector.named_parameters():
        param.requires_grad = False

    print(print_para(detector), flush=True)

    ckpt = torch.load(conf.ckpt)
    if conf.ckpt.split('-')[-2].split('/')[-1] == 'vgrel':
        print("Loading EVERYTHING")
        start_epoch = ckpt['epoch']

        if not optimistic_restore(detector, ckpt['state_dict']):
            start_epoch = -1
            # optimistic_restore(
            #     detector.detector,
            #     torch.load('checkpoints/vgdet/vg-28.tar')['state_dict']
            # )
    else:
        start_epoch = -1
        optimistic_restore(detector.detector, ckpt['state_dict'])

        detector.roi_fmap[1][0].weight.data.copy_(
            ckpt['state_dict']['roi_fmap.0.weight'])
        detector.roi_fmap[1][3].weight.data.copy_(
            ckpt['state_dict']['roi_fmap.3.weight'])
        detector.roi_fmap[1][0].bias.data.copy_(
            ckpt['state_dict']['roi_fmap.0.bias'])
        detector.roi_fmap[1][3].bias.data.copy_(
            ckpt['state_dict']['roi_fmap.3.bias'])

        detector.roi_fmap_obj[0].weight.data.copy_(
            ckpt['state_dict']['roi_fmap.0.weight'])
        detector.roi_fmap_obj[3].weight.data.copy_(
            ckpt['state_dict']['roi_fmap.3.weight'])
        detector.roi_fmap_obj[0].bias.data.copy_(
            ckpt['state_dict']['roi_fmap.0.bias'])
        detector.roi_fmap_obj[3].bias.data.copy_(
            ckpt['state_dict']['roi_fmap.3.bias'])

    detector.cuda()

    print("Training starts now!")
    optimizer, scheduler = get_optim(detector,
                                     conf.lr * conf.num_gpus * conf.batch_size)
    best_eval = None
    for epoch in range(start_epoch + 1, start_epoch + 1 + conf.num_epochs):
        rez = train_epoch(epoch, detector, train, train_loader, optimizer,
                          conf, train_f)
        print("overall{:2d}: ({:.3f})\n{}".format(epoch,
                                                  rez.mean(1)['total'],
                                                  rez.mean(1)),
              flush=True)

        mAp = val_epoch(detector, val, val_loader, val_f)
        scheduler.step(mAp)

        if conf.save_dir is not None:
            if best_eval is None or mAp > best_eval:
                torch.save(
                    {
                        'epoch': epoch,
                        'state_dict': detector.state_dict(),
                        # 'optimizer': optimizer.state_dict(),
                    },
                    os.path.join(conf.save_dir, 'best-val.tar'))
                best_eval = mAp
예제 #2
0
else:
	val = train
	set_name = 'train'

train_loader, val_loader = VGDataLoader.splits(train, val, mode='rel',
											   batch_size=conf.batch_size,
											   num_workers=conf.num_workers,
											   num_gpus=conf.num_gpus)

detector = RelModel(classes=train.ind_to_classes, rel_classes=train.ind_to_predicates,
					num_gpus=conf.num_gpus, mode=conf.mode, require_overlap_det=False,
					use_resnet=conf.use_resnet, order=conf.order,
					nl_edge=conf.nl_edge, nl_obj=conf.nl_obj, hidden_dim=conf.hidden_dim,
					use_proposals=conf.use_proposals,
					pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
					pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
					pooling_dim=conf.pooling_dim,
					rec_dropout=conf.rec_dropout,
					use_bias=conf.use_bias,
					use_tanh=conf.use_tanh,
					limit_vision=conf.limit_vision
					)
detector.cuda()
ckpt = torch.load(conf.ckpt)

optimistic_restore(detector, ckpt['state_dict'])

def val_epoch():
	detector.eval()
	sg_dict = OrderedDict()
	object_counter = 0
예제 #3
0
                                               batch_size=conf.batch_size,
                                               num_workers=conf.num_workers,
                                               num_gpus=conf.num_gpus)

# ipdb.set_trace()

detector = RelModel(
    classes=train.ind_to_classes,
    rel_classes=train.ind_to_predicates,
    num_gpus=conf.num_gpus,
    mode=conf.mode,
    require_overlap_det=True,
    use_resnet=conf.use_resnet,
    order=conf.order,
    nl_edge=conf.nl_edge,
    nl_obj=conf.nl_obj,
    hidden_dim=conf.hidden_dim,
    use_proposals=conf.use_proposals,
    pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
    pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
    pooling_dim=conf.pooling_dim,
    rec_dropout=conf.rec_dropout,
    use_bias=conf.use_bias,
    use_tanh=conf.use_tanh,
    limit_vision=conf.limit_vision)

# Freeze the detector
# .named_parameters(): returns (string, Parameter), Tuple containing the name and parameter itself
for n, param in detector.detector.named_parameters():
    param.requires_grad = False
    """
예제 #4
0
    start_epoch = -1

###### now load the relation detector and set it to test mode!!! ###################################
detector = RelModel(classes=train.ind_to_classes, rel_classes=train.ind_to_predicates,
                    num_gpus=conf.num_gpus, mode=conf.mode, require_overlap_det=True,
                    use_resnet=conf.use_resnet, order=conf.order, pick_parent=conf.pick_parent,
                    nl_edge=conf.nl_edge, nl_obj=conf.nl_obj, hidden_dim=conf.hidden_dim,
                    use_proposals=conf.use_proposals,
                    pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
                    pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
                    pooling_dim=conf.pooling_dim,
                    rec_dropout=conf.rec_dropout,
                    use_bias=conf.use_bias,
                    use_tanh=conf.use_tanh,
                    use_encoded_box=conf.use_encoded_box,
                    draw_tree=conf.draw_tree,
                    limit_vision=conf.limit_vision,
                    need_relpn=conf.relpn,
                    need_relrank=conf.relrank,
                    use_CE=conf.use_CE,
                    dbname=dbname,
                    sal_input=conf.sal_input,
                    use_depth=conf.use_depth,
                    has_grad=conf.has_grad,
                    use_dist=conf.use_dist,
                    return_forest=conf.test_forest,
                    need_caption=conf.captioning,
                    gcn_caption=conf.gcn_captioning,
                    )

detector.cuda()
ckpt = torch.load(conf.ckpt)
예제 #5
0
파일: _visualize.py 프로젝트: galsina/lml
from functools import reduce

conf = ModelConfig()
train, val, test = VG.splits(num_val_im=conf.val_size)
if conf.test:
    val = test

train_loader, val_loader = VGDataLoader.splits(train,
                                               val,
                                               mode='rel',
                                               batch_size=conf.batch_size,
                                               num_workers=conf.num_workers,
                                               num_gpus=conf.num_gpus)

detector = RelModel(classes=train.ind_to_classes,
                    rel_classes=train.ind_to_predicates,
                    num_gpus=conf.num_gpus,
                    mode=conf.mode)
detector.cuda()
ckpt = torch.load(conf.ckpt)

optimistic_restore(detector, ckpt['state_dict'])

############################################ HELPER FUNCTIONS ###################################


def get_cmap(N):
    import matplotlib.cm as cmx
    import matplotlib.colors as colors
    """Returns a function that maps each index in 0, 1, ... N-1 to a distinct RGB color."""
    color_norm = colors.Normalize(vmin=0, vmax=N - 1)
    scalar_map = cmx.ScalarMappable(norm=color_norm, cmap='hsv')
예제 #6
0
                                               batch_size=conf.batch_size,
                                               num_workers=conf.num_workers,
                                               num_gpus=conf.num_gpus,
                                               # -- Depth dataset parameters
                                               use_depth=conf.load_depth)

# -- Create the specified Relation-Detection model
detector = RelModel(classes=train.ind_to_classes, rel_classes=train.ind_to_predicates,
                    num_gpus=conf.num_gpus, mode=conf.mode, require_overlap_det=True,
                    use_resnet=conf.use_resnet, order=conf.order,
                    nl_edge=conf.nl_edge, nl_obj=conf.nl_obj, hidden_dim=conf.hidden_dim,
                    use_proposals=conf.use_proposals,
                    pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
                    pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
                    pooling_dim=conf.pooling_dim,
                    rec_dropout=conf.rec_dropout,
                    use_bias=conf.use_bias,
                    use_tanh=conf.use_tanh,
                    use_vision=conf.use_vision,
                    # -- The proposed model parameters
                    depth_model=conf.depth_model,
                    pretrained_depth=conf.pretrained_depth,
                    active_features=conf.active_features,
                    frozen_features=conf.frozen_features,
                    use_embed=conf.use_embed)

# -- Freeze the detector (Faster-RCNN)
for n, param in detector.detector.named_parameters():
    param.requires_grad = False

# -- Print model parameters
print(print_para(detector), flush=True)
예제 #7
0
파일: eval_vis.py 프로젝트: galsina/lml
def main():
    args = 'X -m predcls -model motifnet -order leftright -nl_obj 2 -nl_edge 4 -b 6 -clip 5 -p 100 -hidden_dim 512 -pooling_dim 4096 -lr 1e-3 -ngpu 1 -test -ckpt checkpoints/vgrel-motifnet-sgcls.tar -nepoch 50 -use_bias -multipred -cache motifnet_predcls1'
    sys.argv = args.split(' ')
    conf = ModelConfig()

    if conf.model == 'motifnet':
        from lib.rel_model import RelModel
    elif conf.model == 'stanford':
        from lib.rel_model_stanford import RelModelStanford as RelModel
    else:
        raise ValueError()

    train, val, test = VG.splits(
        num_val_im=conf.val_size, filter_duplicate_rels=True,
        use_proposals=conf.use_proposals,
        filter_non_overlap=conf.mode == 'sgdet',
    )
    if conf.test:
        val = test
    train_loader, val_loader = VGDataLoader.splits(
        train, val, mode='rel', batch_size=conf.batch_size,
        num_workers=conf.num_workers, num_gpus=conf.num_gpus
    )

    detector = RelModel(
        classes=train.ind_to_classes, rel_classes=train.ind_to_predicates,
        num_gpus=conf.num_gpus, mode=conf.mode, require_overlap_det=True,
        use_resnet=conf.use_resnet, order=conf.order,
        nl_edge=conf.nl_edge, nl_obj=conf.nl_obj, hidden_dim=conf.hidden_dim,
        use_proposals=conf.use_proposals,
        pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
        pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
        pooling_dim=conf.pooling_dim,
        rec_dropout=conf.rec_dropout,
        use_bias=conf.use_bias,
        use_tanh=conf.use_tanh,
        limit_vision=conf.limit_vision
    )


    detector.cuda()
    ckpt = torch.load(conf.ckpt)

    optimistic_restore(detector, ckpt['state_dict'])

    evaluator = BasicSceneGraphEvaluator.all_modes(
        multiple_preds=conf.multi_pred)

    mode, N = 'test.multi_pred', 20
    recs = pkl.load(open('{}.{}.pkl'.format(mode, N), 'rb'))

    np.random.seed(0)
    # sorted_idxs = np.argsort(recs)
    selected_idxs = np.random.choice(range(len(recs)), size=100, replace=False)
    sorted_idxs = selected_idxs[np.argsort(np.array(recs)[selected_idxs])]
    print('Sorted idxs: {}'.format(sorted_idxs.tolist()))

    save_dir = '/nethome/bamos/2018-intel/data/2018-07-31/sgs.multi'

    for idx in selected_idxs:
        gt_entry = {
            'gt_classes': val.gt_classes[idx].copy(),
            'gt_relations': val.relationships[idx].copy(),
            'gt_boxes': val.gt_boxes[idx].copy(),
        }

        detector.eval()
        det_res = detector[vg_collate([test[idx]], num_gpus=1)]

        boxes_i, objs_i, obj_scores_i, rels_i, pred_scores_i = det_res
        pred_entry = {
            'pred_boxes': boxes_i * BOX_SCALE/IM_SCALE,
            'pred_classes': objs_i,
            'pred_rel_inds': rels_i,
            'obj_scores': obj_scores_i,
            'rel_scores': pred_scores_i,
        }


        unique_cnames = get_unique_cnames(gt_entry, test)
        save_img(idx, recs, test, gt_entry, det_res, unique_cnames, save_dir)
        save_gt_graph(idx, test, gt_entry, det_res, unique_cnames, save_dir)
        save_pred_graph(idx, test, pred_entry, det_res,
                        unique_cnames, save_dir,
                        multi_pred=conf.multi_pred, n_pred=20)
예제 #8
0
                                               num_workers=conf.num_workers,
                                               num_gpus=conf.num_gpus)

detector = RelModel(
    classes=train.ind_to_classes,
    rel_classes=train.ind_to_predicates,
    num_gpus=conf.num_gpus,
    mode=conf.mode,
    require_overlap_det=True,
    use_resnet=conf.use_resnet,
    order=conf.order,
    nl_edge=conf.nl_edge,
    nl_obj=conf.nl_obj,
    hidden_dim=conf.hidden_dim,
    use_proposals=conf.use_proposals,
    pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
    pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
    pooling_dim=conf.pooling_dim,
    rec_dropout=conf.rec_dropout,
    use_bias=conf.use_bias,
    use_tanh=conf.use_tanh,
    limit_vision=conf.limit_vision,
    use_rank=conf.use_rank,
    use_score=conf.use_score,
    use_rdist=(not conf.not_rdist)
    # rel_embed_dim=conf.rel_dim,
    # rel_embed=pred_embed
)

detector.cuda()
ckpt = torch.load(conf.ckpt)
예제 #9
0
    val = test
train_loader, val_loader = VGLoader.splits(train, val, mode='rel',
                                           batch_size=conf.batch_size,
                                           num_workers=conf.num_workers,
                                           num_gpus=conf.num_gpus)

#'''
detector = RelModel(classes=train.ind_to_classes, rel_classes=train.ind_to_predicates,
                    num_gpus=conf.num_gpus, mode=conf.mode, require_overlap_det=True,
                    use_resnet=conf.use_resnet, order=conf.order,
                    nl_edge=conf.nl_edge, nl_obj=conf.nl_obj, hidden_dim=conf.hidden_dim,
                    use_proposals=conf.use_proposals,
                    pass_in_obj_feats_to_decoder=conf.pass_in_obj_feats_to_decoder,
                    pass_in_obj_feats_to_edge=conf.pass_in_obj_feats_to_edge,
                    pooling_dim=conf.pooling_dim,
                    rec_dropout=conf.rec_dropout,
                    use_bias=conf.use_bias,
                    use_tanh=conf.use_tanh,
                    use_encoded_box=conf.use_encoded_box,
                    draw_tree=conf.draw_tree,
                    limit_vision=conf.limit_vision,
                    need_relpn=conf.relpn,
                    need_relrank=conf.relrank,
                    dbname=dbname
                    )

detector.cuda()
ckpt = torch.load(conf.ckpt)

optimistic_restore(detector, ckpt['state_dict'])
#'''
# if conf.mode == 'sgdet':