-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
320 lines (271 loc) · 9.77 KB
/
run.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import os
import yaml
import random
import time
import math
import numpy as np
import multiprocessing as mp
from box import Box
import torch
import torch.nn as nn
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel as DDP
from preprocessing.create_dataloaders import data_loaders
from models.unet import UNet
from models.cnn import CNN
from models.clip import ClipVisionEncoder
from models.roberta import RobertaEncoder
from models.llama2 import Llama2Decoder
from models.model import ClevrMath_model
from models.adaptor import Adaptor
from src.training import train
from src.testing import evaluate
with open("config/config.yaml") as f:
cfg = Box(yaml.safe_load(f))
def set_random_seed(SEED):
# set up seed
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
def count_parameters(model):
"""
counting total number of parameters
"""
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def epoch_time(start_time, end_time):
"""
epoch timing
"""
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
def define_model(max_len):
encoder = cfg.training.model_type.encoder
decoder1 = cfg.training.model_type.decoder1
decoder2 = cfg.training.model_type.decoder2
dropout = cfg.training.general.dropout
if encoder == "unet":
# Image Auto-Encoder
image_length = (cfg.dataset.image_width * cfg.dataset.image_height)
features = cfg.training.unet_encoder.features
dim = features[0]
ENC = UNet(
Cin_UNet=cfg.training.unet_encoder.input_channels,
features=features,
dropout=dropout,
image_length=image_length,
)
elif encoder == "cnn":
# CNN encoder
image_length = 4800
dim = cfg.training.cnn_encoder.hid_dim
ENC = CNN(input_channels=cfg.training.cnn_encoder.input_channels,
dec_hid_dim=cfg.training.cnn_encoder.hid_dim,
dropout=dropout,
image_length=image_length)
elif encoder == "clip":
ENC = ClipVisionEncoder()
if decoder1 == "roberta":
# Text Encoder
DEC1 = RobertaEncoder()
if decoder2 == "llama2":
DEC2 = Llama2Decoder()
ADA = Adaptor(cfg.training.adaptor.in_dim,
cfg.training.adaptor.features)
model = ClevrMath_model(ENC,
DEC1,
DEC2,
ADA,
dim,
image_length,
max_len,
num_classes=11)
return model
def train_model(rank=None):
# set_random_seed
set_random_seed(cfg.general.seed)
# to save trained model and logs
FOLDER = ["trained_models", "logs"]
for f in FOLDER:
if not os.path.exists(f):
os.mkdir(f)
# to log losses
loss_file = open("logs/loss_file.txt", "w")
# defining model using DataParallel
if torch.cuda.is_available() and cfg.general.device == "cuda":
if not cfg.general.ddp:
print(f"using single gpu:{cfg.general.gpus}...")
os.environ["CUDA_VISIBLE_DEVICES"] = str(cfg.general.gpus)
device = torch.device(f"cuda:{cfg.general.gpus}")
(
train_dataloader,
test_dataloader,
val_dataloader,
vocab,
max_len,
) = data_loaders()
model = define_model(max_len).to(device)
elif cfg.general.ddp:
# create default process group
dist.init_process_group("nccl", rank=rank, world_size=world_size)
# add rank to config
cfg.general.rank = rank
device = f"cuda:{rank}"
(
train_dataloader,
test_dataloader,
val_dataloader,
vocab,
max_len,
) = data_loaders()
model = define_model(max_len)
model = DDP(
model.to(f"cuda:{rank}"),
device_ids=[rank],
output_device=rank,
find_unused_parameters=True,
)
else:
import warnings
warnings.warn("No GPU input has provided. Falling back to CPU. ")
device = torch.device("cpu")
(
train_dataloader,
test_dataloader,
val_dataloader,
vocab,
max_len,
) = data_loaders()
model = define_model(max_len).to(device)
print("MODEL: ")
print(f"The model has {count_parameters(model)} trainable parameters")
# intializing loss function
criterion = torch.nn.CrossEntropyLoss(ignore_index=vocab["<pad>"])
# optimizer
optimizer = torch.optim.AdamW(
params=model.parameters(),
lr=cfg.training.general.learning_rate,
weight_decay=cfg.training.general.weight_decay,
betas=cfg.training.general.betas,
)
best_valid_loss = float("inf")
if not cfg.general.load_trained_model_for_testing:
count_es = 0
for epoch in range(cfg.training.general.epochs):
if count_es <= cfg.training.general.early_stopping:
start_time = time.time()
# training and validation
train_loss = train(
model,
cfg.dataset.path_to_data,
train_dataloader,
optimizer,
criterion,
cfg.training.general.clip,
device,
clip_enc=cfg.training.model_type.clip_enc,
ddp=cfg.general.ddp,
rank=rank,
)
val_loss,_ = evaluate(
model,
cfg.dataset.path_to_data,
val_dataloader,
criterion,
device,
clip_enc=cfg.training.model_type.clip_enc,
)
end_time = time.time()
# total time spent on training an epoch
epoch_mins, epoch_secs = epoch_time(start_time, end_time)
# saving the current model for transfer learning
if (not cfg.general.ddp) or (cfg.general.ddp and rank == 0):
torch.save(
model.state_dict(),
f"trained_models/latest_model.pt",
)
if val_loss < best_valid_loss:
best_valid_loss = val_loss
count_es = 0
if (not cfg.general.ddp) or (cfg.general.ddp and rank == 0):
torch.save(
model.state_dict(),
f"trained_models/best_model.pt",
)
else:
count_es += 1
# logging
if (not cfg.general.ddp) or (cfg.general.ddp and rank == 0):
print(
f"Epoch: {epoch+1:02} | Time: {epoch_mins}m {epoch_secs}s"
)
print(
f"\tTrain Loss: {train_loss:.3f} | Train PPL: {math.exp(train_loss):7.3f}"
)
print(
f"\t Val. Loss: {val_loss:.3f} | Val. PPL: {math.exp(val_loss):7.3f}"
)
loss_file.write(
f"Epoch: {epoch+1:02} | Time: {epoch_mins}m {epoch_secs}s\n"
)
loss_file.write(
f"\tTrain Loss: {train_loss:.3f} | Train PPL: {math.exp(train_loss):7.3f}\n"
)
loss_file.write(
f"\t Val. Loss: {val_loss:.3f} | Val. PPL: {math.exp(val_loss):7.3f}\n"
)
else:
print(
f"Terminating the training process as the validation \
loss hasn't been reduced from last {cfg.training.general.early_stopping} epochs."
)
break
print(
"best model saved as: ",
f"trained_models/best_model.pt",
)
if cfg.general.ddp:
dist.destroy_process_group()
time.sleep(3)
print(
"loading best saved model: ",
f"trained_models/best_model.pt",
)
# loading pre_tained_model
model.load_state_dict(
torch.load(
f"trained_models/best_model.pt"
)
)
test_loss, accuracy = evaluate(
model,
cfg.dataset.path_to_data,
test_dataloader,
criterion,
device,
is_test=True,
)
if (not cfg.general.ddp) or (cfg.general.ddp and rank == 0):
print(
f"| Test Loss: {test_loss:.3f} | Test PPL: {math.exp(test_loss):7.3f} | Test Accuracy: {accuracy: .3f}"
)
loss_file.write(
f"| Test Loss: {test_loss:.3f} | Test PPL: {math.exp(test_loss):7.3f} | Test Accuracy: {accuracy: .3f}"
)
# stopping time
print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()))
def ddp_main(world_size,):
os.environ["CUDA_VISIBLE_DEVICES"] = gpus
mp.spawn(train_model, args=(), nprocs=world_size, join=True)
if __name__ == "__main__":
if cfg.general.ddp:
gpus = cfg.general.gpus
world_size = cfg.general.world_size
os.environ["MASTER_ADDR"] = "localhost"
os.environ["MASTER_PORT"] = "29800"
ddp_main(world_size)
else:
train_model()