forked from JiehongLin/VI-Net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
142 lines (119 loc) · 5.02 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import os
import sys
import argparse
import logging
import random
import torch
import gorilla
import numpy as np
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(os.path.join(BASE_DIR, 'provider'))
sys.path.append(os.path.join(BASE_DIR, 'model'))
sys.path.append(os.path.join(BASE_DIR, 'utils'))
sys.path.append(os.path.join(BASE_DIR, 'lib', 'sphericalmap_utils'))
sys.path.append(os.path.join(BASE_DIR, 'lib', 'pointnet2'))
from solver import test_func, get_logger
from dataset_pair import TestDataset, TrainingDataset
from evaluation_utils import evaluate
def get_parser():
parser = argparse.ArgumentParser(
description="VI-Net")
# pretrain
parser.add_argument("--gpus",
type=str,
default="0",
help="gpu num")
parser.add_argument("--config",
type=str,
default="config/base.yaml",
help="path to config file")
parser.add_argument("--dataset",
type=str,
default="REAL275",
help="[REAL275 | CAMERA25]")
parser.add_argument("--test_epoch",
type=int,
default=0,
help="test epoch")
args_cfg = parser.parse_args()
return args_cfg
def init():
args = get_parser()
cfg = gorilla.Config.fromfile(args.config)
cfg.dataset = args.dataset
cfg.gpus = args.gpus
cfg.test_epoch = args.test_epoch
cfg.log_dir = os.path.join('log', args.dataset)
cfg.save_path = os.path.join(cfg.log_dir, 'results')
if not os.path.isdir(cfg.save_path):
os.makedirs(cfg.save_path)
logger = get_logger(
level_print=logging.INFO, level_save=logging.WARNING, path_file=cfg.log_dir+"/test_logger.log")
gorilla.utils.set_cuda_visible_devices(gpu_ids=cfg.gpus)
return logger, cfg
if __name__ == "__main__":
logger, cfg = init()
logger.warning(
"************************ Start Logging ************************")
logger.info(cfg)
logger.info("using gpu: {}".format(cfg.gpus))
random.seed(cfg.rd_seed)
torch.manual_seed(cfg.rd_seed)
torch.cuda.manual_seed(cfg.rd_seed)
torch.cuda.manual_seed_all(cfg.rd_seed)
# model
logger.info("=> loading model ...")
from PN2 import Net
ts_model = Net(cfg.n_cls)
from VI_Net_match import Net
r_model = Net(cfg.resolution, cfg.ds_rate)
from SIM_Net import Net
sim_model = Net(cfg.resolution, cfg.ds_rate)
if len(cfg.gpus)>1:
ts_model = torch.nn.DataParallel(ts_model, range(len(cfg.gpus.split(","))))
r_model = torch.nn.DataParallel(r_model, range(len(cfg.gpus.split(","))))
sim_model = torch.nn.DataParallel(sim_model, range(len(cfg.gpus.split(","))))
ts_model = ts_model.cuda()
r_model = r_model.cuda()
sim_model= sim_model.cuda()
checkpoint = os.path.join(cfg.log_dir, 'PN2', 'epoch_' + str(9) + '.pth')
logger.info("=> loading PN2 checkpoint from path: {} ...".format(checkpoint))
gorilla.solver.load_checkpoint(model=ts_model, filename=checkpoint)
checkpoint = os.path.join(cfg.log_dir, 'VI_Net', 'epoch_' + str(cfg.test_epoch) + '.pth')
logger.info("=> loading VI-Net checkpoint from path: {} ...".format(checkpoint))
gorilla.solver.load_checkpoint(model=r_model, filename=checkpoint)
checkpoint = os.path.join(cfg.log_dir, 'SIM_Net', 'epoch_' + str(5) + '.pth')
logger.info("=> loading SIM-Net checkpoint from path: {} ...".format(checkpoint))
gorilla.solver.load_checkpoint(model=sim_model, filename=checkpoint)
feature_file = os.path.join(BASE_DIR, cfg.feature.feature_path, cfg.feature.ref_feature_file)
with open(feature_file, 'rb') as f:
ref_feature= np.load(f, allow_pickle=True)
ref_feature = torch.FloatTensor(ref_feature)
# train_dataset = TrainingDataset(
# cfg.train_dataset,
# cfg.dataset,
# 'r',
# resolution = cfg.resolution,
# ds_rate = cfg.ds_rate,
# num_img_per_epoch=cfg.num_mini_batch_per_epoch*cfg.train_dataloader.bs,
# )
# # data loader
# train_dataloder = torch.utils.data.DataLoader(
# train_dataset,
# batch_size=cfg.train_dataloader.bs,
# num_workers=int(cfg.train_dataloader.num_workers),
# shuffle=cfg.train_dataloader.shuffle,
# sampler=None,
# drop_last=cfg.train_dataloader.drop_last,
# pin_memory=cfg.train_dataloader.pin_memory
# )
dataset = TestDataset(cfg.test, cfg.dataset, cfg.resolution, cfg.ds_rate)
dataloder = torch.utils.data.DataLoader(
dataset,
batch_size=1,
num_workers=0,
shuffle=False,
drop_last=False
)
test_func(ts_model, r_model, sim_model, dataloder,ref_feature, cfg.save_path)
evaluate(cfg.save_path, logger)