Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jantic
GitHub Repository: jantic/deoldify
Path: blob/master/fastai/train.py
781 views
1
"Provides advanced training extensions to `fastai.basic_train`. Includes half-precision, learning rate finder, mixup, and one-cycle"
2
from .torch_core import *
3
from .callbacks import *
4
from .basic_data import *
5
from .basic_train import *
6
7
__all__ = ['BnFreeze', 'GradientClipping', 'ShowGraph', 'Interpretation', 'ClassificationInterpretation', 'MultiLabelClassificationInterpretation',
8
'fit_one_cycle', 'lr_find', 'one_cycle_scheduler', 'to_fp16', 'to_fp32', 'mixup', 'AccumulateScheduler']
9
10
def one_cycle_scheduler(lr_max:float, **kwargs:Any)->OneCycleScheduler:
11
"Instantiate a `OneCycleScheduler` with `lr_max`."
12
return partial(OneCycleScheduler, lr_max=lr_max, **kwargs)
13
14
def fit_one_cycle(learn:Learner, cyc_len:int, max_lr:Union[Floats,slice]=defaults.lr,
15
moms:Tuple[float,float]=(0.95,0.85), div_factor:float=25., pct_start:float=0.3, final_div:float=None,
16
wd:float=None, callbacks:Optional[CallbackList]=None, tot_epochs:int=None, start_epoch:int=None,
17
batch_multiplier:int=1)->None:
18
"Fit a model following the 1cycle policy."
19
max_lr = learn.lr_range(max_lr)
20
callbacks = listify(callbacks)
21
callbacks.append(OneCycleScheduler(learn, max_lr, moms=moms, div_factor=div_factor, pct_start=pct_start,
22
final_div=final_div, tot_epochs=tot_epochs, start_epoch=start_epoch))
23
learn.fit(cyc_len, max_lr, wd=wd, callbacks=callbacks, batch_multiplier=batch_multiplier)
24
25
def lr_find(learn:Learner, start_lr:Floats=1e-7, end_lr:Floats=10, num_it:int=100, stop_div:bool=True, wd:float=None,
26
batch_multiplier:int=1):
27
"Explore lr from `start_lr` to `end_lr` over `num_it` iterations in `learn`. If `stop_div`, stops when loss diverges."
28
start_lr = learn.lr_range(start_lr)
29
start_lr = np.array(start_lr) if is_listy(start_lr) else start_lr
30
end_lr = learn.lr_range(end_lr)
31
end_lr = np.array(end_lr) if is_listy(end_lr) else end_lr
32
cb = LRFinder(learn, start_lr, end_lr, num_it, stop_div)
33
epochs = int(np.ceil(num_it/len(learn.data.train_dl)))
34
learn.fit(epochs, start_lr, callbacks=[cb], wd=wd, batch_multiplier=batch_multiplier)
35
36
def to_fp16(learn:Learner, loss_scale:float=None, max_noskip:int=1000, dynamic:bool=True, clip:float=None,
37
flat_master:bool=False, max_scale:float=2**24)->Learner:
38
"Put `learn` in FP16 precision mode."
39
learn.to_fp32()
40
learn.model = model2half(learn.model)
41
learn.data.add_tfm(batch_to_half)
42
learn.mp_cb = MixedPrecision(learn, loss_scale=loss_scale, max_noskip=max_noskip, dynamic=dynamic, clip=clip,
43
flat_master=flat_master, max_scale=max_scale)
44
learn.callbacks.append(learn.mp_cb)
45
return learn
46
47
def to_fp32(learn:Learner):
48
"Put `learn` back to FP32 precision mode."
49
learn.data.remove_tfm(batch_to_half)
50
for cb in learn.callbacks:
51
if isinstance(cb, MixedPrecision): learn.callbacks.remove(cb)
52
learn.model = learn.model.float()
53
return learn
54
55
def mixup(learn:Learner, alpha:float=0.4, stack_x:bool=False, stack_y:bool=True) -> Learner:
56
"Add mixup https://arxiv.org/abs/1710.09412 to `learn`."
57
learn.callback_fns.append(partial(MixUpCallback, alpha=alpha, stack_x=stack_x, stack_y=stack_y))
58
return learn
59
60
Learner.fit_one_cycle = fit_one_cycle
61
Learner.lr_find = lr_find
62
Learner.to_fp16 = to_fp16
63
Learner.to_fp32 = to_fp32
64
Learner.mixup = mixup
65
66
class ShowGraph(LearnerCallback):
67
"Update a graph of learner stats and metrics after each epoch."
68
def on_epoch_end(self, n_epochs:int, last_metrics:MetricsList, **kwargs)->bool:
69
"If we have `last_metrics` plot them in our pbar graph"
70
if last_metrics is not None and last_metrics[0] is not None:
71
rec = self.learn.recorder
72
iters = range_of(rec.losses)
73
val_iter = np.array(rec.nb_batches).cumsum()
74
x_bounds = (0, (n_epochs - len(rec.nb_batches)) * rec.nb_batches[-1] + len(rec.losses))
75
y_bounds = (0, max((max(Tensor(rec.losses)), max(Tensor(rec.val_losses)))))
76
rec.pbar.update_graph([(iters, rec.losses), (val_iter, rec.val_losses)], x_bounds, y_bounds)
77
return {}
78
79
class BnFreeze(LearnerCallback):
80
"Freeze moving average statistics in all non-trainable batchnorm layers."
81
def on_epoch_begin(self, **kwargs:Any)->None:
82
"Put bn layers in eval mode just after `model.train()`."
83
set_bn_eval(self.learn.model)
84
85
class GradientClipping(LearnerCallback):
86
"Gradient clipping during training."
87
def __init__(self, learn:Learner, clip:float = 0.):
88
super().__init__(learn)
89
self.clip = clip
90
91
def on_backward_end(self, **kwargs):
92
"Clip the gradient before the optimizer step."
93
if self.clip: nn.utils.clip_grad_norm_(self.learn.model.parameters(), self.clip)
94
95
def clip_grad(learn:Learner, clip:float=0.1)->Learner:
96
"Add gradient clipping of `clip` during training."
97
learn.callback_fns.append(partial(GradientClipping, clip=clip))
98
return learn
99
Learner.clip_grad = clip_grad
100
101
class AccumulateScheduler(LearnerCallback):
102
"Does accumlated step every nth step by accumulating gradients"
103
104
def __init__(self, learn:Learner, n_step:int = 1, drop_last:bool = False):
105
super().__init__(learn)
106
self.n_step,self.drop_last = n_step,drop_last
107
108
def on_train_begin(self, **kwargs):
109
"check if loss is reduction"
110
if hasattr(self.loss_func, "reduction") and (self.loss_func.reduction != "sum"):
111
warn("For better gradients consider 'reduction=sum'")
112
113
def on_epoch_begin(self, **kwargs):
114
"init samples and batches, change optimizer"
115
self.acc_samples, self.acc_batches = 0., 0.
116
117
def on_batch_begin(self, last_input, last_target, **kwargs):
118
"accumulate samples and batches"
119
self.acc_samples += last_input.shape[0]
120
self.acc_batches += 1
121
122
def on_backward_end(self, **kwargs):
123
"accumulated step and reset samples, True will result in no stepping"
124
if (self.acc_batches % self.n_step) == 0:
125
for p in (self.learn.model.parameters()):
126
if p.requires_grad: p.grad.div_(self.acc_samples)
127
self.acc_samples = 0
128
else: return {'skip_step':True, 'skip_zero':True}
129
130
def on_epoch_end(self, **kwargs):
131
"step the rest of the accumulated grads if not perfectly divisible"
132
for p in (self.learn.model.parameters()):
133
if p.requires_grad: p.grad.div_(self.acc_samples)
134
if not self.drop_last: self.learn.opt.step()
135
self.learn.opt.zero_grad()
136
137
138
class Interpretation():
139
"Interpretation base class, can be inherited for task specific Interpretation classes"
140
def __init__(self, learn:Learner, preds:Tensor, y_true:Tensor, losses:Tensor, ds_type:DatasetType=DatasetType.Valid):
141
self.data,self.preds,self.y_true,self.losses,self.ds_type, self.learn = \
142
learn.data,preds,y_true,losses,ds_type,learn
143
self.ds = (self.data.train_ds if ds_type == DatasetType.Train else
144
self.data.test_ds if ds_type == DatasetType.Test else
145
self.data.valid_ds if ds_type == DatasetType.Valid else
146
self.data.single_ds if ds_type == DatasetType.Single else
147
self.data.fix_ds)
148
149
@classmethod
150
def from_learner(cls, learn: Learner, ds_type:DatasetType=DatasetType.Valid, activ:nn.Module=None):
151
"Gets preds, y_true, losses to construct base class from a learner"
152
preds_res = learn.get_preds(ds_type=ds_type, activ=activ, with_loss=True)
153
return cls(learn, *preds_res)
154
155
def top_losses(self, k:int=None, largest=True):
156
"`k` largest(/smallest) losses and indexes, defaulting to all losses (sorted by `largest`)."
157
return self.losses.topk(ifnone(k, len(self.losses)), largest=largest)
158
159
# def top_scores(self, metric:Callable=None, k:int=None, largest=True):
160
# "`k` largest(/smallest) metric scores and indexes, defaulting to all scores (sorted by `largest`)."
161
# self.scores = metric(self.preds, self.y_true)
162
# return self.scores.topk(ifnone(k, len(self.scores)), largest=largest)
163
164
165
class ClassificationInterpretation(Interpretation):
166
"Interpretation methods for classification models."
167
def __init__(self, learn:Learner, preds:Tensor, y_true:Tensor, losses:Tensor, ds_type:DatasetType=DatasetType.Valid):
168
super(ClassificationInterpretation, self).__init__(learn,preds,y_true,losses,ds_type)
169
self.pred_class = self.preds.argmax(dim=1)
170
171
def confusion_matrix(self, slice_size:int=1):
172
"Confusion matrix as an `np.ndarray`."
173
x=torch.arange(0,self.data.c)
174
if slice_size is None: cm = ((self.pred_class==x[:,None]) & (self.y_true==x[:,None,None])).sum(2)
175
else:
176
cm = torch.zeros(self.data.c, self.data.c, dtype=x.dtype)
177
for i in range(0, self.y_true.shape[0], slice_size):
178
cm_slice = ((self.pred_class[i:i+slice_size]==x[:,None])
179
& (self.y_true[i:i+slice_size]==x[:,None,None])).sum(2)
180
torch.add(cm, cm_slice, out=cm)
181
return to_np(cm)
182
183
def plot_confusion_matrix(self, normalize:bool=False, title:str='Confusion matrix', cmap:Any="Blues", slice_size:int=1,
184
norm_dec:int=2, plot_txt:bool=True, return_fig:bool=None, **kwargs)->Optional[plt.Figure]:
185
"Plot the confusion matrix, with `title` and using `cmap`."
186
# This function is mainly copied from the sklearn docs
187
cm = self.confusion_matrix(slice_size=slice_size)
188
if normalize: cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
189
fig = plt.figure(**kwargs)
190
plt.imshow(cm, interpolation='nearest', cmap=cmap)
191
plt.title(title)
192
tick_marks = np.arange(self.data.c)
193
plt.xticks(tick_marks, self.data.y.classes, rotation=90)
194
plt.yticks(tick_marks, self.data.y.classes, rotation=0)
195
196
if plot_txt:
197
thresh = cm.max() / 2.
198
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
199
coeff = f'{cm[i, j]:.{norm_dec}f}' if normalize else f'{cm[i, j]}'
200
plt.text(j, i, coeff, horizontalalignment="center", verticalalignment="center", color="white" if cm[i, j] > thresh else "black")
201
202
plt.tight_layout()
203
plt.ylabel('Actual')
204
plt.xlabel('Predicted')
205
plt.grid(False)
206
if ifnone(return_fig, defaults.return_fig): return fig
207
208
def most_confused(self, min_val:int=1, slice_size:int=1)->Collection[Tuple[str,str,int]]:
209
"Sorted descending list of largest non-diagonal entries of confusion matrix, presented as actual, predicted, number of occurrences."
210
cm = self.confusion_matrix(slice_size=slice_size)
211
np.fill_diagonal(cm, 0)
212
res = [(self.data.classes[i],self.data.classes[j],cm[i,j])
213
for i,j in zip(*np.where(cm>=min_val))]
214
return sorted(res, key=itemgetter(2), reverse=True)
215
216
217
def _learner_interpret(learn:Learner, ds_type:DatasetType=DatasetType.Valid):
218
"Create a `ClassificationInterpretation` object from `learner` on `ds_type` with `tta`."
219
return ClassificationInterpretation.from_learner(learn, ds_type=ds_type)
220
Learner.interpret = _learner_interpret
221
222
class MultiLabelClassificationInterpretation(Interpretation):
223
"Interpretation methods for classification models."
224
def __init__(self, learn:Learner, preds:Tensor, y_true:Tensor, losses:Tensor, ds_type:DatasetType=DatasetType.Valid,
225
sigmoid:bool=True, thresh:float=0.3):
226
raise NotImplementedError
227
super(MultiLabelClassificationInterpretation, self).__init__(learn,preds,y_true,losses,ds_type)
228
self.pred_class = self.preds.sigmoid(dim=1)>thresh if sigmoid else self.preds>thresh
229
230