-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtrainer.py
More file actions
306 lines (247 loc) · 10.8 KB
/
Copy pathtrainer.py
File metadata and controls
306 lines (247 loc) · 10.8 KB
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
import warnings
import numpy as np
import torch
from dmultipit.base import BaseTrainer
from dmultipit.utils import inf_loop, MetricTracker, set_device
class Trainer(BaseTrainer):
"""
Trainer class (with optionnal semi-supervised / pseudo-labelling strategy)
Parameters
----------
model: base_model.BaseModel object
Model to train.
criterion: callable with output and target inputs
Training criterion (i.e., loss)
metric_ftns: callable with output and target inputs
Training and validation metrics to monitor training
optimizer: torch.optim.Optimizer object
Optimization algorithms (e.g., torch.optim.Adam, torch.optim.SGD)
config: dict
Configuration dictionnary
device: str
Torch.device on which to allocate tensors
data_loader: DataLoader
Training data
valid_data_loader: DataLoader or None
Validation data. If None, no validation is performed. The default is None.
unlabelled_data_loader: Dataloader or None.
Unlabelled data for semi-supervised strategy (i.e., pseudo-labelling). If None, no unlabelled data is added to
the training. The default is None.
weight_unlabelled: Callable or None
Function to specify the weight of the unlabelled step depending on the training epoch (e.g., 0 until epoch 50
and 1 after). See dmultipit.model.loss.StepScheduler. The default is None.
criterion_unlabelled: Callable or None
Criterion for pseudo-labelling strategy. See dmultipit.model.loss.UnlabelledBCELoss. The default is None.
lr_scheduler: torch.optim.lr_scheduler object or None
Learning rate scheduler. If None the learning rate is kept constant throughout training. The default is None.
len_epoch: Int or None.
Number of batches within an epoch. If None, len_epoch will be set to the length of the provided data_loader.
The default is None.
log_dir: string or None.
Path to the directory to save Tensorboard logs in. The default is None.
ensembling_index: int or None
Index to associate the trained/saved model to an ensemble of other trained models for further ensembling
strategies. The default is None.
save_architecture: bool
If true, save model architecture as .yaml file. The default is False.
disable_checkpoint: bool
If true, no checkpoint is saved. The default is False.
"""
def __init__(
self,
model,
criterion,
metric_ftns,
optimizer,
config,
device,
data_loader,
valid_data_loader=None,
unlabelled_data_loader=None,
weight_unlabelled=None,
criterion_unlabelled=None,
lr_scheduler=None,
len_epoch=None,
log_dir=None,
ensembling_index=None,
save_architecture=False,
disable_checkpoint=False,
):
if log_dir is None:
log_dir = config.log_dir
super().__init__(
model,
criterion,
metric_ftns,
optimizer,
config,
log_dir,
ensembling_index,
save_architecture,
disable_checkpoint,
)
self.config = config
self.device = device
self.data_loader = data_loader
if len_epoch is None:
# epoch-based training
self.len_epoch = len(self.data_loader)
else:
# iteration-based training
self.data_loader = inf_loop(data_loader)
self.len_epoch = len_epoch
self.valid_data_loader = valid_data_loader
self.do_validation = self.valid_data_loader is not None
self.lr_scheduler = lr_scheduler
self.log_step = int(np.sqrt(data_loader.batch_size))
# initiate pseudo-labelling
self.unlabelled_data_loader = unlabelled_data_loader
self.pseudo_labelling = self.unlabelled_data_loader is not None
self.criterion_unlabelled = criterion_unlabelled
self.weigth_unlabelled = weight_unlabelled
if (self.pseudo_labelling
and ((self.criterion_unlabelled is None) or (self.weigth_unlabelled is None))):
raise ValueError("When unlabelled data are passed for semi-supervised learning criterion_unlabelled and"
"weight_unlabelled must be specified (i.e., not set to None).")
elif ((not self.pseudo_labelling)
and ((self.criterion_unlabelled is not None) or (self.weigth_unlabelled is not None))):
warnings.warn("criterion_unlabelled and/or weight_unlabelled are specified but no unlabelled_data_loader"
"is passed. No semi-supervised strategy will be performed and criterion_unlabelled and"
"weight_unlabelled will be reset to None.")
self.criterion_unlabelled = None
self.weigth_unlabelled = None
# initiate training metrics tracking
metric_keys = ["loss"] + [m.__name__ for m in self.metric_ftns]
self.valid_metrics = MetricTracker(metric_keys, writer=self.writer)
if self.pseudo_labelling:
metric_keys.append("unlabelled_loss")
self.train_metrics = MetricTracker(metric_keys, writer=self.writer)
def _train_epoch(self, epoch):
"""
Training logic for an epoch
Parameters
----------
epoch: Int
Current training epoch.
Returns
-------
log: A log that contains average loss and metric in this epoch.
"""
self.model.train()
self.train_metrics.reset()
for batch_idx, (*list_modas, mask, target) in enumerate(self.data_loader):
list_modas, mask, target = (
set_device(list_modas, self.device),
set_device(mask, self.device),
set_device(target, self.device),
)
self.optimizer.zero_grad()
output = self.model(list_modas, mask)
loss = self.criterion(output, target)
# Add optional L2 penalties (for model weights and/or attention weights)
if self.config["training"]["l2_penalty"] is not None:
l2_penalty = torch.stack(
[p.norm(p=2) for n, p in self.model.named_parameters() if "weight" in n]
).sum() # **2
loss += self.config["training"]["l2_penalty"] * l2_penalty
if self.config["training"]["attention_penalty"] is not None:
att_penalty = self.model.multimodalattention.attention_norm
loss += self.config["training"]["attention_penalty"] * att_penalty
loss.backward()
self.optimizer.step()
# self.writer.set_step((epoch - 1) * self.len_epoch + batch_idx)
self.train_metrics.update("loss", loss.item())
for met in self.metric_ftns:
self.train_metrics.update(met.__name__, met(output, target))
if batch_idx % self.log_step == 0:
self.logger.debug(
"Train Epoch: {} {} Loss: {:.6f}".format(
epoch, self._progress(batch_idx), loss.item()
)
)
if batch_idx == self.len_epoch:
break
# Semi-supervised / pseudo-labelling step
if self.pseudo_labelling:
self._train_unlabelled_epoch(epoch)
log = self.train_metrics.result()
# add training metrics to tensorboard
self.writer.set_step(epoch)
self.train_metrics.update_writer("loss")
for met in self.metric_ftns:
self.train_metrics.update_writer(met.__name__)
# add histogram of model parameters to the tensorboard
for name, p in self.model.named_parameters():
self.writer.add_histogram(name, p, bins="auto")
# perform validation
if self.do_validation:
val_log = self._valid_epoch(epoch)
log.update(**{"val_" + k: v for k, v in val_log.items()})
# adjust learning rate
if self.lr_scheduler is not None:
self.lr_scheduler.step()
return log
def _train_unlabelled_epoch(self, epoch):
"""
Train on unlabelled data
Parameters
----------
epoch: Int
Current epoch
"""
for batch_idx, (*list_modas, mask, _) in enumerate(self.unlabelled_data_loader):
list_modas, mask = set_device(list_modas, self.device), set_device(mask, self.device)
# compute the pseudo_labels
self.model.eval()
output_unlabelled = self.model(list_modas, mask)
self.model.train()
self.optimizer.zero_grad()
output = self.model(list_modas, mask)
loss = self.weigth_unlabelled(epoch) * self.criterion_unlabelled(output, output_unlabelled)
loss.backward()
self.optimizer.step()
self.train_metrics.update("unlabelled_loss", loss.item())
return
def _valid_epoch(self, epoch):
"""
Validate after training an epoch
Parameters
----------
epoch: Int
Current training epoch.
Returns
-------
log: A log that contains information about validation
"""
self.model.eval()
self.valid_metrics.reset()
with torch.no_grad():
for batch_idx, (*list_modas, mask, target) in enumerate(self.valid_data_loader):
list_modas, mask, target = (
set_device(list_modas, self.device),
set_device(mask, self.device),
set_device(target, self.device),
)
output = self.model(list_modas, mask)
loss = self.criterion(output, target)
# self.writer.set_step(
# (epoch - 1) * len(self.valid_data_loader) + batch_idx, "valid"
# )
self.valid_metrics.update("loss", loss.item())
for met in self.metric_ftns:
self.valid_metrics.update(met.__name__, met(output, target))
# add validation metrics to tensorboard
self.writer.set_step(epoch, "valid")
self.valid_metrics.update_writer("loss")
for met in self.metric_ftns:
self.valid_metrics.update_writer(met.__name__)
return self.valid_metrics.result()
def _progress(self, batch_idx):
base = "[{}/{} ({:.0f}%)]"
if hasattr(self.data_loader, "n_samples"):
current = batch_idx * self.data_loader.batch_size
total = self.data_loader.n_samples
else:
current = batch_idx
total = self.len_epoch
return base.format(current, total, 100.0 * current / total)