-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
257 lines (217 loc) · 9.05 KB
/
main.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import os
import logging
import codeLib
import ssg
import ssg.config as config
from ssg.checkpoints import CheckpointIO
import cProfile
import matplotlib
import torch_geometric
# disable GUI
matplotlib.pyplot.switch_backend('agg')
# change log setting
matplotlib.pyplot.set_loglevel("CRITICAL")
logging.getLogger('PIL').setLevel('CRITICAL')
logging.getLogger('trimesh').setLevel('CRITICAL')
logging.getLogger("h5py").setLevel(logging.INFO)
logger_py = logging.getLogger(__name__)
def main():
cfg = ssg.Parse()
# Shorthands
out_dir = os.path.join(cfg['training']['out_dir'], cfg.name)
# set random seed
codeLib.utils.util.set_random_seed(cfg.SEED)
# Output directory
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# Log
logging.basicConfig(filename=os.path.join(
out_dir, 'log'), level=cfg.log_level)
logger_py.setLevel(cfg.log_level)
if cfg.MODE == 'train':
logger_py.info('train')
n_workers = cfg['training']['data_workers']
''' create dataset and loaders '''
logger_py.info('get dataset')
dataset_train = config.get_dataset(cfg, 'train')
dataset_val = config.get_dataset(cfg, 'validation')
logger_py.info('create loader')
train_loader = torch_geometric.loader.DataLoader(
dataset_train,
batch_size=cfg.training.batch,
num_workers=n_workers,
pin_memory=True
)
val_loader = torch_geometric.loader.DataLoader(
dataset_val, batch_size=1, num_workers=n_workers,
shuffle=False,
drop_last=False,
pin_memory=False,
)
# try to load one data
logger_py.info('test loader')
for i, data in enumerate(train_loader):
break
continue
# Get logger
logger_py.info('get logger')
logger = config.get_logger(cfg)
if logger is not None:
logger, _ = logger
''' Create model '''
logger_py.info('create model')
relationNames = dataset_train.relationNames
classNames = dataset_train.classNames
num_obj_cls = len(dataset_train.classNames)
num_rel_cls = len(
dataset_train.relationNames) if relationNames is not None else 0
model = config.get_model(
cfg, num_obj_cls=num_obj_cls, num_rel_cls=num_rel_cls)
if cfg.VERBOSE:
print(cfg)
if cfg.VERBOSE:
print(model)
# trainer
logger_py.info('get trainner')
model_trainer = config.get_trainer(cfg, model, classNames, relationNames,
w_node_cls=dataset_train.w_node_cls,
w_edge_cls=dataset_train.w_edge_cls)
logger_py.info('setup training')
trainer = ssg.Trainer(
cfg=cfg,
model_trainer=model_trainer,
node_cls_names=classNames,
edge_cls_names=relationNames,
logger=logger,
)
logger_py.info('start training')
pr = cProfile.Profile()
pr.enable()
trainer.fit(train_loader=train_loader, val_loader=val_loader)
pr.disable()
logger_py.info('traning finished')
logger_py.info('save time profile to {}'.format(
os.path.join(out_dir, 'tp_train.dmp')))
pr.dump_stats(os.path.join(out_dir, 'tp_train.dmp'))
elif cfg.MODE == 'eval':
'''use CPU for memory issue'''
import torch
cfg.DEVICE = torch.device("cpu")
cfg.data.load_cache = False
eval_mode = cfg.eval.mode
assert eval_mode in ['segment', 'instance']
if eval_mode == 'segment':
dataset_test = config.get_dataset(cfg, 'test')
val_loader = torch_geometric.loader.DataLoader(
dataset_test, batch_size=1, num_workers=cfg['eval']['data_workers'],
shuffle=False, drop_last=False,
pin_memory=False,
)
#print(dataset_test) #chi lan
logger_py.info('test loader')
dataset_test.__getitem__(0)
for i, data in enumerate(train_loader):
break
continue
# Get logger
logger = config.get_logger(cfg)
if logger is not None:
logger, _ = logger
''' Create model '''
relationNames = dataset_test.relationNames
classNames = dataset_test.classNames
num_obj_cls = len(dataset_test.classNames)
num_rel_cls = len(
dataset_test.relationNames) if relationNames is not None else 0
model = config.get_model(
cfg, num_obj_cls=num_obj_cls, num_rel_cls=num_rel_cls)
model_trainer = config.get_trainer(cfg, model, classNames, relationNames,
w_node_cls=None,
w_edge_cls=None
)
checkpoint_io = CheckpointIO(out_dir, model=model)
load_dict = checkpoint_io.load('model_best.pt', device=cfg.DEVICE)
it = load_dict.get('it', -1)
#
logger_py.info('start evaluation')
pr = cProfile.Profile()
pr.enable()
eval_dict, eval_tool = model_trainer.evaluate(
val_loader, topk=cfg.eval.topK)
pr.disable()
logger_py.info('save time profile to {}'.format(
os.path.join(out_dir, 'tp_eval.dmp')))
pr.dump_stats(os.path.join(out_dir, 'tp_eval.dmp'))
#
print(eval_tool.gen_text())
_ = eval_tool.write(out_dir, cfg.name)
if logger:
for k, v in eval_dict['visualization'].items():
logger.add_figure('test/'+k, v, global_step=it)
for k, v in eval_dict.items():
if isinstance(v, dict):
continue
logger.add_scalar('test/%s' % k, v, it)
elif eval_mode == 'instance':
''' Get segment dataset '''
dataset_seg = config.get_dataset(cfg, 'test')
''' Get instance dataset'''
dataset_inst = config.get_dataset_inst(cfg, 'test')
logger_py.info('test loader')
dataset_inst.__getitem__(0)
for i in range(len(dataset_inst)):
dataset_inst.__getitem__(i)
break
continue
'''check'''
# assert len(dataset_seg.relationNames) == len(dataset_inst.relationNames)+1
assert len(dataset_seg.classNames) == len(dataset_inst.classNames)
''' Get logger '''
logger = config.get_logger(cfg)
if logger is not None:
logger, _ = logger
''' Create model '''
relationNames = dataset_seg.relationNames
classNames = dataset_seg.classNames
num_obj_cls = len(dataset_seg.classNames)
num_rel_cls = len(
dataset_seg.relationNames) if relationNames is not None else 0
model = config.get_model(
cfg, num_obj_cls=num_obj_cls, num_rel_cls=num_rel_cls)
model_trainer = config.get_trainer(cfg, model, classNames, relationNames,
w_node_cls=None,
w_edge_cls=None
)
'''check ckpt'''
checkpoint_io = CheckpointIO(out_dir, model=model)
load_dict = checkpoint_io.load('model_best.pt', device=cfg.DEVICE)
it = load_dict.get('it', -1)
'''start eval'''
logger_py.info('start evaluation')
pr = cProfile.Profile()
pr.enable()
eval_dict, eval_tools, eval_tool_upperbound = model_trainer.evaluate_inst(
dataset_seg, dataset_inst, topk=cfg.eval.topK)
pr.disable()
logger_py.info('save time profile to {}'.format(
os.path.join(out_dir, 'tp_eval_inst.dmp')))
pr.dump_stats(os.path.join(out_dir, 'tp_eval_inst.dmp'))
'''log'''
# ignore_missing=cfg.eval.ignore_missing
prefix = 'inst' if not cfg.eval.ignore_missing else 'inst_ignore'
eval_tool_upperbound.write(out_dir, 'upper_bound')
for eval_type, eval_tool in eval_tools.items():
print('======={}======'.format(eval_type))
print(eval_tool.gen_text())
_ = eval_tool.write(out_dir, eval_type+'_'+prefix)
if logger:
for k, v in eval_dict['visualization'].items():
logger.add_figure('test/'+prefix+'_'+k, v, global_step=it)
for k, v in eval_dict.items():
if isinstance(v, dict):
continue
logger.add_scalar('test/'+prefix+'_'+'%s' % k, v, it)
else:
raise NotImplementedError('unknown input mode')
if __name__ == '__main__':
main()