-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
154 lines (133 loc) · 4.32 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
import argparse
import random
import torch
import numpy as np
from pathlib import Path
from datasets.dataloader import DataLoader
from model import Trainer
from utils.utils import create_folders
from batch_gen import BatchGenerator
from asformer import MyTransformer, ASFormerTrainer
def main(args, device, model_load_dir, model_save_dir, results_save_dir):
if args.action == 'train' :
# 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 = ASFormerTrainer(
args.num_layers,
args.r1,
args.r2,
args.num_f_maps,
args.input_dim,
train_loader.num_classes,
args.channel_masking_rate,
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,
eval_args)
else:
print(f'Load test data: {args.test_data}')
test_loader = DataLoader(args, args.test_data, args.extract_set, results_dir=results_save_dir)
trainer = ASFormerTrainer(
args.num_layers,
args.r1,
args.r2,
args.num_f_maps,
args.input_dim,
test_loader.num_classes,
args.channel_masking_rate,
test_loader.weights,
model_save_dir)
trainer.test(
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=0,
save_pslabels=False,
CP_dict=test_loader.CP_dict,
)
class Args():
def __init__(self, *args, **kwargs):
self.train_data = 'bslcp'
self.test_data = 'bslcp'
self.i3d_training = 'i3d_kinetics_bslcp_981'
self.num_in_frames = 16
self.features_dim = 1024
self.weights = 'opt'
self.regression = 0
self.feature_normalization = 0
self.eval_use_CP = 0
self.bz = 1
self.action = 'train'
self.seed = 0
self.refresh = 'store_true'
## Transformer :
self.nhead = 8
self.nhid = 1024
self.dim_feedforward = 1024
self.num_layers = 6
self.dropout = 0.3
## ASFormer :
self.num_layers = 7
self.num_decoders = 3
self.r1 = 2
self.r2 = 2
self.channel_masking_rate = 0.3
self.input_dim = 1024
self.num_f_maps = 64
## Optimization
self.num_epochs = 20
self.lr = 0.0005
## inference
self.classification_threshold = 0.5
self.folder = ''
if __name__ == "__main__":
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"using {device}")
args = Args()
# 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)