-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain3_endpoint_train.py
More file actions
79 lines (61 loc) · 2.89 KB
/
Copy pathmain3_endpoint_train.py
File metadata and controls
79 lines (61 loc) · 2.89 KB
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
import json
import os
import six
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
os.environ["KMP_WARNINGS"] = "0"
import model3_endpoint_train as sketch_endpoint_model
from utils3_endpoint_train import load_dataset
def trainer(model_params):
print('Hyperparams:')
for key, val in six.iteritems(model_params.values()):
print('%s = %s' % (key, str(val)))
print('-' * 100)
datasets = load_dataset(model_params)
sub_log_root = os.path.join(model_params.log_root, model_params.workspace)
sub_log_img_root = os.path.join(model_params.log_img_root, model_params.workspace)
sub_snapshot_root = os.path.join(model_params.snapshot_root, model_params.workspace)
os.makedirs(sub_log_root, exist_ok=True)
os.makedirs(sub_log_img_root, exist_ok=True)
os.makedirs(sub_snapshot_root, exist_ok=True)
train_set = datasets[0]
val_set = datasets[1]
train_model_params = datasets[2]
val_model_params = datasets[3]
# Write config file to json file.
with open(os.path.join(sub_snapshot_root, 'model_config.json'), 'w') as f:
json.dump(train_model_params.values(), f, indent=True)
model = sketch_endpoint_model.FullModel(model_params, train_set, val_set,
sub_log_root, sub_snapshot_root, sub_log_img_root)
model.train()
model.evaluate()
def tester(model_params, mode):
print('Hyperparams:')
for key, val in six.iteritems(model_params.values()):
print('%s = %s' % (key, str(val)))
print('-' * 100)
datasets = load_dataset(model_params)
train_set = datasets[0]
val_set = datasets[1]
sub_snapshot_root = os.path.join(model_params.snapshot_root, model_params.workspace)
model = sketch_endpoint_model.FullModel(model_params, train_set, val_set,
None, sub_snapshot_root, None)
if mode == 'inference':
sub_inference_root = os.path.join(model_params.inference_root, model_params.workspace)
os.makedirs(sub_inference_root, exist_ok=True)
model.inference(sub_inference_root, show_data='selected') # ['selected', 'all', 'occluded']
elif mode == 'inference_full':
sub_inference_root = os.path.join(model_params.inference_full_root, model_params.workspace)
os.makedirs(sub_inference_root, exist_ok=True)
model.inference_full(sub_inference_root, show_data='selected') # ['selected', 'all']
else:
model.evaluate(load_trained_weights=True)
# model.evaluate(load_trained_weights=True, occluded_only=True)
if __name__ == '__main__':
mode = 'inference' # ['train', 'test', 'inference', 'inference_full']
model_params = sketch_endpoint_model.get_default_hparams()
if mode == 'train':
trainer(model_params)
elif mode == 'test' or mode == 'inference' or mode == 'inference_full':
tester(model_params, mode)
else:
raise Exception('Unknown mode:', mode)