-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_unary_and_temporal.py
209 lines (163 loc) · 8.64 KB
/
train_unary_and_temporal.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
import pickle
import torch
import numpy as np
import os
import scipy.io as sio
import random
from tqdm import tqdm
from torch.optim.lr_scheduler import ReduceLROnPlateau
from dataloader.action_genome import AG, cuda_collate_fn
from pytictoc import TicToc
from lib.config import Config
from lib.Temporal_Model import MyTempTransformer
#from lib.Temporal_Model_Encoder_Only import MyTempTransformer
#from lib.Temporal_Model_Decoder_Only import MyTempTransformer
from lib.Unary_Model_Combined import MyUnaryTransformer
#from lib.Unary_Model_Combined_Encoder_Only import MyUnaryTransformer
#from lib.Unary_Model_Combined_Decoder_Only import MyUnaryTransformer
from lib.forward_pass_utils import compute_unary_loss, compute_temporal_loss
#from lib.forward_pass_utils_v5_only_encoder import compute_unary_loss, compute_temporal_loss
#from lib.forward_pass_utils_v5_only_encoder_mlm_loss import compute_unary_loss, compute_temporal_loss
#from lib.forward_pass_utils_v5_only_decoder import compute_unary_loss, compute_temporal_loss
#from lib.forward_pass_utils_v5_only_decoder_no_obj import compute_unary_loss, compute_temporal_loss
#from lib.forward_pass_utils_v5_no_obj import compute_unary_loss, compute_temporal_loss
conf = Config()
for i in conf.args:
print(i,':', conf.args[i])
gpu_device = torch.device('cuda:0')
unary_prior_model = MyUnaryTransformer(num_encoder_layers=1,
num_decoder_layers=1,
emb_size=3472,
nhead=8,
tgt_vocab_size=64 # (37 + 3 + 6 + 17 )
).to(device=gpu_device)
temporal_prior_model = MyTempTransformer(num_encoder_layers=1,
num_decoder_layers=1,
emb_size=3472,
nhead=8,
att_tgt_vocab_size=64,
spa_tgt_vocab_size=64,
con_tgt_vocab_size=64).to(device=gpu_device)
loss_fn = torch.nn.CrossEntropyLoss()
temporal_optimizer = torch.optim.Adam(
temporal_prior_model.parameters(),
lr=0.00001, betas=(0.9, 0.98), eps=1e-9)
unary_optimizer = torch.optim.Adam(
unary_prior_model.parameters(),
lr=0.00001, betas=(0.9, 0.98), eps=1e-9)
temporal_scheduler = ReduceLROnPlateau(temporal_optimizer, "max", patience=1, factor=0.5, verbose=True, threshold=1e-4,
threshold_mode="abs", min_lr=1e-7)
unary_scheduler = ReduceLROnPlateau(unary_optimizer, "max", patience=1, factor=0.5, verbose=True, threshold=1e-4,
threshold_mode="abs", min_lr=1e-7)
checkpointdir = 'unary_and_temporal_prior_checkpoints/'
temporal_checkpointdir = checkpointdir + '/temporal/'
unary_checkpointdir = checkpointdir + '/unary/'
if not os.path.exists(unary_checkpointdir):
os.makedirs(unary_checkpointdir)
if not os.path.exists(temporal_checkpointdir):
os.makedirs(temporal_checkpointdir)
# some parameters
tr = []
train_data_folder = 'results/' + conf.mode + '_backbone_training_with_dino/'
filelist = os.listdir(train_data_folder)
print('Loading results from ' + train_data_folder)
valList = []
with open('valFiles.txt', 'r') as fp:
for line in fp:
valfile = line[:-1]
valList.append(valfile)
print("Validation files loaded")
t = TicToc()
def add_frame_keys(pred, frame_names):
pred['frame_list'] = []
for i in range(0,len(pred['human_idx'])):
pred['frame_list'].append(frame_names[i])
return pred
def train_unary():
for epoch in range(0, 50):
t.tic()
random.shuffle(filelist)
if epoch != 0:
unary_model_path_epoch = unary_checkpointdir + '/unary_prior_model_' + str(epoch - 1) + '.pt'
unary_model_ckpt = torch.load(unary_model_path_epoch, map_location=gpu_device)
unary_prior_model.load_state_dict(unary_model_ckpt)
print('Unary models loaded from epoch no: ' + str(epoch - 1) + 'for unary training')
unary_prior_model.train()
unary_loss_att_iter = []
unary_loss_spa_iter = []
unary_loss_con_iter = []
for b, filename in enumerate(tqdm(filelist)):
if filename in valList:
#print(filename)
continue
results = torch.load(train_data_folder + filename , map_location=torch.device('cpu'))
pred_all = results[1]
#print(pred_all.keys())
unary_losses = compute_unary_loss(pred_all=pred_all, gpu_device=gpu_device, model=unary_prior_model, loss_fn=loss_fn)
unary_optimizer.zero_grad()
unary_loss = sum(unary_losses.values())
unary_loss.backward()
unary_optimizer.step()
unary_loss_att_iter.append(unary_losses["attention_relation_loss"].item())
unary_loss_spa_iter.append(unary_losses["spatial_relation_loss"].item())
unary_loss_con_iter.append(unary_losses["contact_relation_loss"].item())
un_loss_file_att_iter = unary_checkpointdir + '/att_unary_prior_loss_' + str(epoch)
un_loss_file_spa_iter = unary_checkpointdir + '/spa_unary_prior_loss_' + str(epoch)
un_loss_file_con_iter = unary_checkpointdir + '/con_unary_prior_loss_' + str(epoch)
unary_model_path_epoch = unary_checkpointdir + '/unary_prior_model_' + str(epoch) + '.pt'
un_att_file_iter = open(un_loss_file_att_iter, "wb")
un_spa_file_iter = open(un_loss_file_spa_iter, "wb")
un_con_file_iter = open(un_loss_file_con_iter, "wb")
np.save(un_att_file_iter, unary_loss_att_iter)
np.save(un_spa_file_iter, unary_loss_spa_iter)
np.save(un_con_file_iter, unary_loss_con_iter)
un_att_file_iter.close
un_spa_file_iter.close
un_con_file_iter.close
torch.save(unary_prior_model.state_dict(), unary_model_path_epoch)
t.toc()
print('All Unary Epochs Done')
def train_temporal():
for epoch in range(0, 50):
t.tic()
random.shuffle(filelist)
if epoch != 0:
temporal_model_path_epoch = temporal_checkpointdir + '/temp_prior_model_' + str(epoch - 1) + '.pt'
temporal_model_ckpt = torch.load(temporal_model_path_epoch, map_location=gpu_device)
temporal_prior_model.load_state_dict(temporal_model_ckpt)
print('Temporal Models loaded from epoch no: ' + str(epoch - 1) )
temporal_prior_model.train()
temporal_loss_att_iter = []
temporal_loss_spa_iter = []
temporal_loss_con_iter = []
for b, filename in enumerate(tqdm(filelist)):
if filename in valList:
#print(filename)
continue
results = torch.load(train_data_folder + filename, map_location=torch.device('cpu'))
pred_all = results[1]
temporal_losses = compute_temporal_loss(pred_all=pred_all, gpu_device=gpu_device, model=temporal_prior_model,
loss_fn=loss_fn)
temporal_optimizer.zero_grad()
temporal_loss = sum(temporal_losses.values())
temporal_loss.backward()
temporal_optimizer.step()
temporal_loss_att_iter.append(temporal_losses["attention_relation_loss"].item())
temporal_loss_spa_iter.append(temporal_losses["spatial_relation_loss"].item())
temporal_loss_con_iter.append(temporal_losses["contact_relation_loss"].item())
temporal_model_path_epoch = temporal_checkpointdir + '/temp_prior_model_' + str(epoch) + '.pt'
temp_att_file_iter = open(temporal_checkpointdir + '/att_temp_prior_loss_' + str(epoch), "wb")
temp_spa_file_iter = open(temporal_checkpointdir + '/spa_temp_prior_loss_' + str(epoch), "wb")
temp_con_file_iter = open(temporal_checkpointdir + '/con_temp_prior_loss_' + str(epoch), "wb")
np.save(temp_att_file_iter, temporal_loss_att_iter)
np.save(temp_spa_file_iter, temporal_loss_spa_iter)
np.save(temp_con_file_iter, temporal_loss_con_iter)
temp_att_file_iter.close
temp_spa_file_iter.close
temp_con_file_iter.close
torch.save(temporal_prior_model.state_dict(), temporal_model_path_epoch)
t.toc()
print('All Temporal Epochs Done')
if __name__ == "__main__":
train_unary()
train_temporal()