-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainbaseline.py
122 lines (101 loc) · 5.33 KB
/
mainbaseline.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
import warnings
warnings.simplefilter("ignore", UserWarning)
from tqdm import tqdm
import torch
import argparse
import numpy as np
from modules.tokenizers import Tokenizer
from modules.dataloaders import LADataLoader
from modules.metrics import compute_scores
from modules.optimizers import build_optimizer, build_lr_scheduler
from modules.trainer import BaseTrainer
from modules.loss import compute_loss
from models.baseline import LAMRGModel
from config import opts
class Trainer(BaseTrainer):
def __init__(self, model, criterion, metric_ftns, optimizer, args, lr_scheduler, train_dataloader, val_dataloader,
test_dataloader):
super(Trainer, self).__init__(model, criterion, metric_ftns, optimizer, args)
self.lr_scheduler = lr_scheduler
self.train_dataloader = train_dataloader
self.val_dataloader = val_dataloader
self.test_dataloader = test_dataloader
def _train_epoch(self, epoch):
train_loss = 0
self.model.train()
for batch_idx, (images_id, images, reports_ids, reports_masks, labels) in tqdm(enumerate(self.train_dataloader)):
images, reports_ids, reports_masks, labels = images.to(self.device), reports_ids.to(self.device), \
reports_masks.to(self.device), labels.to(self.device)
output, outlabels = self.model(images, reports_ids, labels, mode='train')
loss = self.criterion(output, reports_ids, reports_masks)
train_loss += loss.item()
self.optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_value_(self.model.parameters(), 0.1)
self.optimizer.step()
log = {'train_loss': train_loss / len(self.train_dataloader)}
self.model.eval()
with torch.no_grad():
val_gts, val_res, val_idxs = [], [], []
for batch_idx, (images_id, images, reports_ids, reports_masks, labels) in tqdm(enumerate(self.val_dataloader)):
images, reports_ids, reports_masks, labels = images.to(self.device), reports_ids.to(self.device), \
reports_masks.to(self.device), labels.to(self.device)
output, outlabels = self.model(images, labels=labels, mode='sample')
reports = self.model.tokenizer.decode_batch(output.cpu().numpy())
ground_truths = self.model.tokenizer.decode_batch(reports_ids[:, 1:].cpu().numpy())
val_res.extend(reports)
val_gts.extend(ground_truths)
val_idxs.extend(images_id)
val_met = self.metric_ftns({i: [gt] for i, gt in enumerate(val_gts)},
{i: [re] for i, re in enumerate(val_res)})
log.update(**{'val_' + k: v for k, v in val_met.items()})
self._output_generation(val_res, val_gts, val_idxs, epoch, log, 'val')
self.model.eval()
with torch.no_grad():
test_gts, test_res, test_idxs = [], [], []
for batch_idx, (images_id, images, reports_ids, reports_masks, labels) in tqdm(enumerate(self.test_dataloader)):
images, reports_ids, reports_masks, labels = images.to(self.device), reports_ids.to(self.device), \
reports_masks.to(self.device), labels.to(self.device)
output, outlabels = self.model(images, labels=labels, mode='sample')
reports = self.model.tokenizer.decode_batch(output.cpu().numpy())
ground_truths = self.model.tokenizer.decode_batch(reports_ids[:, 1:].cpu().numpy())
test_res.extend(reports)
test_gts.extend(ground_truths)
test_idxs.extend(images_id)
test_met = self.metric_ftns({i: [gt] for i, gt in enumerate(test_gts)},
{i: [re] for i, re in enumerate(test_res)})
log.update(**{'test_' + k: v for k, v in test_met.items()})
self._output_generation(test_res, test_gts, test_idxs, epoch, log, 'test')
self.lr_scheduler.step()
return log
def main():
# parse arguments
# args = parse_agrs()
args = opts.parse_opt()
args.version = 900
args.save_dir = args.save_dir + f'V{args.version}'
print(args)
# fix random seeds
torch.manual_seed(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(args.seed)
# create tokenizer
tokenizer = Tokenizer(args)
# create data loader
train_dataloader = LADataLoader(args, tokenizer, split='train', shuffle=True)
val_dataloader = LADataLoader(args, tokenizer, split='val', shuffle=False)
test_dataloader = LADataLoader(args, tokenizer, split='test', shuffle=False)
# build model architecture
model = LAMRGModel(args, tokenizer)
# get function handles of loss and metrics
criterion = compute_loss
metrics = compute_scores
# build optimizer, learning rate scheduler
optimizer = build_optimizer(args, model)
lr_scheduler = build_lr_scheduler(args, optimizer)
# build trainer and start to train
trainer = Trainer(model, criterion, metrics, optimizer, args, lr_scheduler, train_dataloader, val_dataloader, test_dataloader)
trainer.train()
if __name__ == '__main__':
main()