-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_basic.py
197 lines (165 loc) · 11.1 KB
/
main_basic.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
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.lamrg import BasicModel
from config import opts
def parse_agrs():
parser = argparse.ArgumentParser()
# Data input settings
parser.add_argument('--image_dir', type=str, default='data/iu_xray/images/', help='the path to the directory containing the data.')
parser.add_argument('--ann_path', type=str, default='data/iu_xray/annotation.json', help='the path to the directory containing the data.')
parser.add_argument('--label_path', type=str, default='/home/shuxinyang/data/mimic/finding/chexpert_labeled.csv', help='the path to the directory containing the label.')
# Data loader settings
parser.add_argument('--dataset_name', type=str, default='iu_xray', choices=['iu_xray', 'mimic_cxr', 'covid', 'covidall'], help='the dataset to be used.')
parser.add_argument('--max_seq_length', type=int, default=60, help='the maximum sequence length of the reports.')
parser.add_argument('--threshold', type=int, default=3, help='the cut off frequency for the words.')
parser.add_argument('--num_workers', type=int, default=2, help='the number of workers for dataloader.')
parser.add_argument('--batch_size', type=int, default=16, help='the number of samples for a batch')
# Model settings (for visual extractor)
parser.add_argument('--visual_extractor', type=str, default='efficientnet', choices=['densenet', 'efficientnet'],
help='the visual extractor to be used.')
parser.add_argument('--visual_extractor_pretrained', type=bool, default=True, help='whether to load the pretrained visual extractor')
parser.add_argument('--pretrain_cnn_file', type=str, default='', help='the visual extractor to be used.')
# Model settings (for Transformer)
parser.add_argument('--d_model', type=int, default=512, help='the dimension of Transformer.')
parser.add_argument('--d_ff', type=int, default=512, help='the dimension of FFN.')
parser.add_argument('--d_vf', type=int, default=1280, help='for densenet = 1024, for efficientnet = 1280')
parser.add_argument('--num_heads', type=int, default=8, help='the number of heads in Transformer.')
parser.add_argument('--num_layers', type=int, default=6, help='the number of layers of Transformer.')
parser.add_argument('--num_labels', type=int, default=14, help='the number of labels.')
parser.add_argument('--dropout', type=float, default=0.1, help='the dropout rate of Transformer.')
parser.add_argument('--logit_layers', type=int, default=1, help='the number of the logit layer.')
parser.add_argument('--bos_idx', type=int, default=0, help='the index of <bos>.')
parser.add_argument('--eos_idx', type=int, default=0, help='the index of <eos>.')
parser.add_argument('--pad_idx', type=int, default=0, help='the index of <pad>.')
parser.add_argument('--use_bn', type=int, default=0, help='whether to use batch normalization.')
parser.add_argument('--drop_prob_lm', type=float, default=0.5, help='the dropout rate of the output layer.')
# Sample related
parser.add_argument('--sample_method', type=str, default='beam_search', help='the sample methods to sample a report.')
parser.add_argument('--beam_size', type=int, default=3, help='the beam size when beam searching.')
parser.add_argument('--temperature', type=float, default=1.0, help='the temperature when sampling.')
parser.add_argument('--sample_n', type=int, default=1, help='the sample number per image.')
parser.add_argument('--group_size', type=int, default=1, help='the group size.')
parser.add_argument('--output_logsoftmax', type=int, default=1, help='whether to output the probabilities.')
parser.add_argument('--decoding_constraint', type=int, default=0, help='whether decoding constraint.')
parser.add_argument('--block_trigrams', type=int, default=1, help='whether to use block trigrams.')
# Trainer settings
parser.add_argument('--n_gpu', type=int, default=1, help='the number of gpus to be used.')
parser.add_argument('--epochs', type=int, default=100, help='the number of training epochs.')
parser.add_argument('--save_dir', type=str, default='results/iu_xray', help='the patch to save the models.')
parser.add_argument('--record_dir', type=str, default='records/', help='the patch to save the results of experiments')
parser.add_argument('--save_period', type=int, default=1, help='the saving period.')
parser.add_argument('--monitor_mode', type=str, default='max', choices=['min', 'max'], help='whether to max or min the metric.')
parser.add_argument('--monitor_metric', type=str, default='BLEU_4', help='the metric to be monitored.')
parser.add_argument('--early_stop', type=int, default=20, help='the patience of training.')
parser.add_argument('--label_smoothing', type=float, default=0.0)
# Optimization
parser.add_argument('--optim', type=str, default='Adam', help='the type of the optimizer.')
parser.add_argument('--lr_ve', type=float, default=5e-5, help='the learning rate for the visual extractor.')
parser.add_argument('--lr_ed', type=float, default=1e-4, help='the learning rate for the remaining parameters.')
parser.add_argument('--weight_decay', type=float, default=5e-5, help='the weight decay.')
parser.add_argument('--amsgrad', type=bool, default=True, help='.')
# Learning Rate Scheduler
parser.add_argument('--lr_scheduler', type=str, default='StepLR', help='the type of the learning rate scheduler.')
parser.add_argument('--step_size', type=int, default=50, help='the step size of the learning rate scheduler.')
parser.add_argument('--gamma', type=float, default=0.1, help='the gamma of the learning rate scheduler.')
# Others
parser.add_argument('--seed', type=int, default=9233, help='.')
parser.add_argument('--gpu', type=int, default=0, help='')
parser.add_argument('--resume', type=str, help='whether to resume the training from existing checkpoints.')
args = parser.parse_args()
return args
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()
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 = BasicModel(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()