-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
228 lines (188 loc) · 8.56 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
import argparse
import random
import torch
import numpy as np
from pathlib import Path
from self_labelling.PL_CP_fusion_methods import get_save_local_fusion, merge_PL_CP, CMPL, extract_CP
from utils.utils import create_folders
from model import Trainer
from datasets.dataloader import DataLoader
from batch_gen import BatchGenerator
def main(args, device, model_load_dir, model_save_dir, results_save_dir):
if args.action == 'train' and args.extract_save_pseudo_labels == 0:
# load train dataset and test dataset
print(f'Load train data: {args.train_data}')
train_loader = DataLoader(args, args.train_data, 'train')
print(f'Load test data: {args.test_data}')
test_loader = DataLoader(args, args.test_data, 'test')
print(f'Start training.')
trainer = Trainer(
args.num_stages,
args.num_layers,
args.num_f_maps,
args.features_dim,
train_loader.num_classes,
device,
train_loader.weights,
model_save_dir
)
eval_args = [
args,
model_save_dir,
results_save_dir,
test_loader.features_dict,
test_loader.gt_dict,
test_loader.eval_gt_dict,
test_loader.vid_list,
args.num_epochs,
device,
'eval',
args.classification_threshold,
]
batch_gen = BatchGenerator(
train_loader.num_classes,
train_loader.gt_dict,
train_loader.features_dict,
train_loader.eval_gt_dict
)
batch_gen.read_data(train_loader.vid_list)
trainer.train(
model_save_dir,
batch_gen,
args.num_epochs,
args.bz,
args.lr,
device,
eval_args,
pretrained=model_load_dir)
elif args.extract_save_pseudo_labels and args.pseudo_label_type != 'PL':
# extract/ generate pseudo labels and save in "data/pseudo_labels"
print(f'Load test data: {args.test_data}')
test_loader = DataLoader(args, args.test_data, args.extract_set, results_dir=results_save_dir)
print(f'Extract {args.pseudo_label_type}')
if args.pseudo_label_type == 'local':
get_save_local_fusion(args, test_loader.features_dict, test_loader.gt_dict)
elif args.pseudo_label_type == 'merge':
merge_PL_CP(args, test_loader.features_dict, test_loader.gt_dict)
elif args.pseudo_label_type == 'CMPL':
CMPL(args, test_loader.features_dict, test_loader.gt_dict)
elif args.pseudo_label_type == 'CP':
extract_CP(args, test_loader.features_dict)
print('Self labelling process finished')
else:
print(f'Load test data: {args.test_data}')
test_loader = DataLoader(args, args.test_data, args.extract_set, results_dir=results_save_dir)
if args.extract_save_pseudo_labels and args.pseudo_label_type == 'PL':
print(f'Extract {args.pseudo_label_type}')
extract_save_PL = 1
else:
print(f'Start inference.')
extract_save_PL = 0
trainer = Trainer(
args.num_stages,
args.num_layers,
args.num_f_maps,
args.features_dim,
test_loader.num_classes,
device,
test_loader.weights,
results_save_dir)
trainer.predict(
args,
model_load_dir,
results_save_dir,
test_loader.features_dict,
test_loader.gt_dict,
test_loader.eval_gt_dict,
test_loader.vid_list,
args.num_epochs,
device,
'test',
args.classification_threshold,
uniform=args.uniform,
save_pslabels=extract_save_PL,
CP_dict=test_loader.CP_dict,
)
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
parser = argparse.ArgumentParser()
# General settings
parser.add_argument('--refresh', action='store_true')
parser.add_argument('--seed', default=0, type=int)
parser.add_argument('--action', default='train', choices=['train', 'predict'])
parser.add_argument('--extract_set', default='train', choices=['train', 'test'])
parser.add_argument(
'--train_data',
default='bslcp',
choices=['bslcp', 'phoenix14']
)
parser.add_argument(
'--test_data',
default='bslcp',
choices=['bslcp', 'phoenix14']
)
parser.add_argument(
'--i3d_training',
default='i3d_kinetics_bslcp_981'
)
parser.add_argument('--num_in_frames', default=16, type=int)
parser.add_argument('--folder', default='', type=str, help="folder to save the results")
### viz
parser.add_argument('--test_subset', default=-1, type=int, help='use only a subset of the test set for evaluation and visualization')
parser.add_argument('--viz_results', action='store_true', help="save visualizations of results and gt for each video sequence")
### target_finetuning settings
parser.add_argument('--extract_save_pseudo_labels', default=0, type=int, choices=[0,1], help="extract and save pseudo_labels of type pseudo_label_type")
parser.add_argument('--pseudo_label_type', default='CP', choices=['CMPL', 'merge', 'local', 'PL', 'CP'], type=str)
parser.add_argument('--use_test', action='store_true', help="append test set to extract_set (for combined train and test set")
parser.add_argument('--pretrained', default='', type=str, help="Path to pretrained model")
parser.add_argument('--use_pseudo_labels', action='store_true', help="Use already extracted pseudo-labels for training ")
parser.add_argument('--load_label_path', default='', type=str, help="Path to pseudo-labels")
# parser.add_argument('--PL_info_savefolder', default=False)
parser.add_argument('--eval_use_CP', action='store_true', help="CP Baseline, use changepoints as predictions (without training)")
### fusion
parser.add_argument('--local_fusion_model', default='l2')
parser.add_argument('--local_fusion_pen', default=80, type=int)
parser.add_argument('--local_fusion_jump', default=2, type=int)
parser.add_argument('--local_fusion_th_min', default=13, type=int)
parser.add_argument('--local_fusion_th_max', default=60, type=int)
parser.add_argument('--merge_model', default='l2')
parser.add_argument('--merge_pen', default=80, type=int)
parser.add_argument('--merge_jump', default=2, type=int)
parser.add_argument('--CMSL_model', default='l2')
parser.add_argument('--CMSL_pen', default=[100,100])
parser.add_argument('--CMSL_jump', default=2, type=int)
parser.add_argument('--CMSL_th_insert', default=4, type=int)
parser.add_argument('--CMSL_th_refine', default=4, type=int)
### MS-TCN HYPERPARAMETER
parser.add_argument('--num_stages', default=4, type=int)
parser.add_argument('--num_layers', default=10, type=int)
parser.add_argument('--num_f_maps', default=64, type=int)
parser.add_argument('--features_dim', default=1024, type=int)
parser.add_argument('--bz', default=8, type=int)
parser.add_argument('--lr', default=0.0005, type=float)
parser.add_argument('--num_epochs', default=50, type=int)
parser.add_argument('--extract_epoch', default=10, type=int)
parser.add_argument('--weights', default='opt', help="None, [1., 5.], 'opt'")
parser.add_argument('--uniform', default=0, type=int)
parser.add_argument('--regression', default=0, type=int)
parser.add_argument('--std', default=1, type=int)
parser.add_argument('--classification_threshold', default=0.5, type=float)
#### Other settings
parser.add_argument('--feature_normalization', default=0, type=int)
parser.add_argument('--num_boundary_frames', default=2, type=int)
args = parser.parse_args()
# if args.PL_info_savefolder is False:
args.PL_info_savefolder = str(Path(args.load_label_path).stem)
if args.pretrained == '':
args.pretrained = False
# set seed
seed = args.seed
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# create models and save args
model_load_dir, model_save_dir, results_save_dir = create_folders(args)
main(args, device, model_load_dir, model_save_dir, results_save_dir)