-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_wandb.py
284 lines (233 loc) · 14.3 KB
/
train_wandb.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import sys
sys.path.extend([r'C:\Users\karim\PycharmProjects\m2LSpTemp'])
import argparse
from datasets.visualization import decode_predictions_and_compute_bleu_score
import logging
from datasets.loader import build_data
import os,random
import torch
import numpy as np
import torch.nn as nn
import torch.optim as optim
import wandb
from torch.nn.utils.rnn import pad_sequence
import yaml
import torchtext
# -------------------- SET THE SEED FOR REPRODUCIBILITY -----------------#
SEED = 1234
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
def run_batch(model, batch, data_loader, mode, teacher_force_ratio, device=None, optimizer=None,
multiple_references=None, beam_size=1,lambdas=(0,0),attention_type=None,file_beam=None):
epoch_loss = 0
TRG_PAD_IDX = data_loader.dataset.lang.token_to_idx["<pad>"]
L_spat,L_adapt = lambdas
n_joint = batch[0].shape[2]//3
if "relative" in attention_type:
poses_input = batch[0] - batch[0][:, :, :3].unsqueeze(-1).expand(batch[0][:, :, :3].size() + (n_joint,)).permute(0,1,3,2).reshape(
batch[0].size()[:2] + (n_joint*3,))
poses_input[:,:,:3] = batch[0][:, :, :3]
src = poses_input.to(device).permute(1, 0, 2)
else:
src = batch[0].to(device).permute(1, 0, 2)
src = torch.as_tensor(src, dtype=torch.float32)
# shape (batch_size,src_len,flatten joint dim = n_joint*3)
trg = batch[1].to(device).permute(1, 0) if not multiple_references else \
pad_sequence([torch.as_tensor(refs[0]) for refs in batch[1]], batch_first=False, padding_value=0).to(device)
src_lens = batch[2]
gth_gates = torch.nn.utils.rnn.pad_sequence([torch.as_tensor(ik) for ik in batch[3]], batch_first=True, padding_value=-1)
gth_gates = gth_gates[:,1:].float().to(device)
gth_alphas = torch.nn.utils.rnn.pad_sequence([torch.as_tensor(ik) for ik in batch[4]], batch_first=True, padding_value=-1)
gth_alphas = gth_alphas[:,1:,:].expand(max(src_lens),-1,-1,-1).permute(2,0,1,3).float().to(device)
num_grams = 4
vocab_obj = data_loader.dataset.lang
if beam_size==1:
if "test" in mode: logging.info("START Greedy SEARCH ")
## Run model
output_pose = model(src, trg,teacher_force_ratio=teacher_force_ratio, src_lens=src_lens)
bleu_score, pred, refs = decode_predictions_and_compute_bleu_score(output_pose.squeeze(0),
batch[1] if multiple_references else trg,
vocab_obj, num_grams=num_grams,
batch_first=False,multiple_references=multiple_references)
# ADAPTIVE loss----------------------------------------------------------------------------
# SHAPE gth_gates = [TRG_LEN,BATCH_SIZE]
loss_adap = torch.nn.BCELoss(reduction='none')(model.beta.permute(1, 0).float(), gth_gates.float())
loss_adap = loss_adap.masked_fill(gth_gates == -1, 0) # mask unsupervised tokens
loss_adap = torch.sum(loss_adap)/torch.count_nonzero(loss_adap)
# SPATIAL loss----------------------------------------------------------------------------
# SHAPE gth_alphas = [TRG_LEN, SRC_LEN, Bsize , K]
loss_spatial = torch.nn.BCELoss(reduction='none')(model.spatial_attentions.float(), gth_alphas.float())
loss_spatial = loss_spatial.masked_fill(gth_alphas==-1,0) # mask unsupervised tokens
loss_spatial = torch.sum(loss_spatial)/torch.count_nonzero(loss_spatial)
# Language Loss --------------------------------------------------------------------------
criterion = nn.CrossEntropyLoss(ignore_index=TRG_PAD_IDX, reduction='mean')
loss_lang = criterion(output_pose.permute(1, 2, 0), trg[1:, :].permute(1, 0))
# ----------------------- COMBINED LOSS SUPERVISION --------------------------------------
loss = loss_lang + L_adapt*loss_adap + L_spat*loss_spatial
logging.info(f"loss_LANG ----> {loss_lang.item()}")
logging.info(f"loss_SPAT ----> {loss_spatial.item()}")
logging.info(f"loss_ADAPT----> {loss_adap.item()}")
if mode == "train":
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1)
optimizer.step()
epoch_loss += loss_lang.item() # Loss_lang used to have training curve scale compared to no superv for comparison
torch.cuda.empty_cache()
return loss, bleu_score, pred, refs
else: # only for evaluation
logging.info("START BEAM SEARCH")
decoded_preds = model(src, trg, teacher_force_ratio=0, src_lens=src_lens)
predicted_sentences = []
_dec_numeric_sentence = vocab_obj.decode_numeric_sentence
for hyps in decoded_preds:
predicted_sentences += [
[_dec_numeric_sentence(beam_path, remove_sos_eos=True).split(
" ") for beam_path in hyps]]
logging.info("Write beam predictions ...")
with open(file_beam, "a") as g:
for m in predicted_sentences:
g.writelines([" ".join(k) + "," for k in m] + ["\n"])
Yrefs = batch[1] if multiple_references else trg
ref_sentences = [[_dec_numeric_sentence(ref, remove_sos_eos=True).split(" ") for ref in refs] for refs in Yrefs]
# TODO REMOVE THE DEBUG PARAM
bleu_score_beam = [torchtext.data.metrics.bleu_score(
candidate_corpus=[m[k] if len(m) >= k + 1 else m[-1] for m in predicted_sentences],
references_corpus=ref_sentences,
max_n=num_grams, weights=[1 / num_grams] * num_grams) for k in range(beam_size)]
return bleu_score_beam, predicted_sentences, Yrefs
def train_m2l():
# CREATE A DIRECTORY PER PROJECT
abs_path = r"C:\\Users\karim\PycharmProjects\SemMotion\Wandb"
os.makedirs(abs_path,exist_ok=True)
os.makedirs(abs_path + PROJECT_NAME, exist_ok=True)
with wandb.init() as run:
# DIR TO SAVE MODEL WITH UNIQUE ID GENERATED PER RUN FOR THE SPECIFIED PROJECT NAME
unique_path = abs_path + PROJECT_NAME + f'/model_{wandb.run.id}'
config = dict(wandb.config)
config["path"] = unique_path
train_data_loader, val_data_loader, test_data_loader = build_data(dataset_class=dataset_class,
min_freq=config["min_freq"],
train_batch_size=config["batch_size"],
test_batch_size=config["batch_size"],
return_lengths=True,
path_txt=path_txt,
return_trg_len=True,
joint_angles= False,
multiple_references=args.multiple_references)
input_dim = train_data_loader.dataset.lang.vocab_size_unk
logging.info("VOCAB SIZE = %d " % (input_dim))
# -------------------- CREATE MODEL (MLP-Mixer-2-LSTM) -----------------------
model = seq2seq(input_dim, hidden_size=config["hidden_size"], embedding_dim=config["embedding_dim"], num_layers = config["num_layers"], device = config["device"],
dropout = config["rate_dropout"],attention = config["attention_type"], hidden_dim = config["hidden_dim"], K = config["K"] )
model = model.to(config["device"])
logging.info(f"Model Architecture {model}")
optimizer = optim.AdamW(model.parameters(),lr=config["lr"],weight_decay=config['weight_decay'])
n_epochs = int(config["n_epochs"])
start = 0
best_valid_bleu = 0
logging.info("************ START TRAINING ************")
for epoch in range(start,n_epochs):
model.train()
teacher_force_ratio = config["teacher_force_ratio"]
epoch_loss = 0
BLEU_scores = []
mode = "train"
# ------------------------- BATCH TRAINING ----------------------
for i, batch in enumerate(train_data_loader):
loss_train_b, bleu_score,_,_ = run_batch(model,batch,train_data_loader, mode=mode,optimizer=optimizer,
teacher_force_ratio=teacher_force_ratio,device=config["device"],
attention_type=config["attention_type"],lambdas=config["lambdas"])
BLEU_scores += [bleu_score]
loss_train_b = loss_train_b.item()
epoch_loss += loss_train_b
logging.info(f"Loss/{mode}_batch %d --> %.3f BLEU score_batch %.3f" % (i, loss_train_b, bleu_score))
# ----------------------------------------------------------------
loss_train = epoch_loss / len(train_data_loader)
BLEU_score_train = sum(BLEU_scores) / len(BLEU_scores)
logging.info(f"\nEpoch %d Train Loss --> %.3f BLEU_train score %.3f\n" % (epoch, loss_train, BLEU_score_train))
# ----------------------------- EVALUATE --------------------------
evaluate = True
if evaluate:
mode = "val"
model.eval()
epoch_loss = 0
BLEU_scores = []
for i, batch in enumerate(val_data_loader):
loss_val_b, bleu_score, _, _ = run_batch(model, batch, val_data_loader, mode=mode,optimizer=optimizer,
teacher_force_ratio=teacher_force_ratio,device=config["device"],
attention_type=config["attention_type"],lambdas=config["lambdas"])
BLEU_scores += [bleu_score]
BLEU_scores += [bleu_score]
loss_val_b = loss_val_b.item()
epoch_loss += loss_val_b
logging.info(f"Loss/{mode}_batch %d --> %.3f BLEU score_batch %.3f" % (i, loss_val_b, bleu_score))
loss_val = epoch_loss / len(val_data_loader)
BLEU_score_val = sum(BLEU_scores) / len(BLEU_scores)
logging.info("LOSS VAL %.3f BLEU score %.3f" % (loss_val, BLEU_score_val))
logging.info(f"\nEpoch %d LOSS VAL %.3f BLEU_val score %.3f" % (epoch, loss_val, BLEU_score_val))
# ---------------------------------------------- LOG TO WANDB ----------------------------------------------------
wandb.log({"loss_val": loss_val,"bleu_val": BLEU_score_val,
"loss_train": loss_train,"bleu_train": BLEU_score_train},
step=epoch)
# SAVE BEST MODEL
if BLEU_score_val >= best_valid_bleu:
best_valid_bleu = BLEU_score_val
# -------------ARTIFACTS------------
model_artifact = wandb.Artifact(f"LSTM_{wandb.run.id}", type="model",
description="Guided attention for Interpretable Motion Captioning",
metadata=config)
# SAVE THE PYTORCH MODEL TO DIRECTORY
torch.save({'model': model.state_dict(), 'optimizer': optimizer.state_dict(),
'epoch': epoch, 'val_bleu': BLEU_score_val, 'val_loss': loss_val,
'train_bleu': BLEU_score_train, 'metadata': config},
unique_path)
model_artifact.add_file(unique_path)
# wandb.save(unique_path)
# LOG ARTIFACTS
run.log_artifact(model_artifact)
model_artifact.finalize()
if __name__=="__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--path",type=str,default=".",help="Path where to save checkpoints")
parser.add_argument("--dataset_name",type=str,default="kit",choices=["h3D","kit"])
parser.add_argument("--device",type=str,default="cuda")
parser.add_argument("--config",type=str,default="./configs/lstm_kit.yaml")
parser.add_argument("--multiple_references",type=bool,default=False,help="Specify evaluation mode use flattened references or all at one")
parser.add_argument("--encoder_type",type=str,default="MLP")
parser.add_argument("--attention_type",type=str,default="relative_bahdanau")
parser.add_argument("--experience_suffix_name",type=str,default="",help='Run name')
parser.add_argument("--epoch",type=int,default=200,help='Number of epoch')
parser.add_argument("--save_checkpoint",type=bool,default=True,help="save checkpoint at each end")
args = parser.parse_args()
# with open(args.config,'r') as f:
# choices= yaml.load(f,Loader=yaml.Loader)
# parser.set_defaults(**choices)
# args = parser.parse_args()
# args.dataset_name = "h3D"
project_path = r"C:\Users\karim\PycharmProjects\SemMotion"
aug_path = r"C:\Users\karim\PycharmProjects\HumanML3D"
if args.dataset_name=="kit":
# -------------KIT IMPORTS------------------
from architectures.LSTM_kit import seq2seq
from datasets.kit_h3mld import dataset_class
path_txt = project_path+"\datasets\sentences_corrections.csv"
path_motion = aug_path+"\kit_with_splits_2023.npz"
elif args.dataset_name=="h3D":
# -----------H3D IMPORTS---------------------
from architectures.LSTM_h3D import seq2seq
from datasets.h3d_m2t_dataset_ import dataset_class
path_txt = aug_path+"\sentences_corrections_h3d.csv"
path_motion = aug_path+"\\all_humanML3D.npz"
# Login in your wandb account
wandb.login()
# Set the config file defining the search space
args.config = f"./configs/lstm_{args.dataset_name}.yaml"
with open(args.config,"r") as f:
sweep_config = yaml.safe_load(f)
PROJECT_NAME = f"Interpretable_MC_{args.dataset_name}_f"
sweep_id = wandb.sweep(sweep_config,project=PROJECT_NAME)
wandb.agent(sweep_id,function=train_m2l)