forked from piegu/fastai-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learner_xc_to_3c.py
509 lines (448 loc) · 26.6 KB
/
learner_xc_to_3c.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
"Modified fastai `Learner` support for computer vision to create as first layer a ConvNet that will transform images "
"with n channels to 3 channels. Look for #change to get changed lines from fastai learner.py"
from fastai.torch_core import * #change
from fastai.basic_train import * #change
from fastai.basic_data import * #change
# from .image import * #change
# from . import models #change
from fastai.callback import * #change
from fastai.layers import * #change
from fastai.callbacks.hooks import * #change
from fastai.train import ClassificationInterpretation #change
from fastai.vision import * #change
__all__ = ['cnn_learner', 'create_cnn', 'create_cnn_model', 'create_body', 'create_head', 'unet_learner']
# By default split models between first and second layer
def _default_split(m:nn.Module): return (m[1],)
# Split a resnet style model
# def _resnet_split(m:nn.Module): return (m[0][6],m[1]) #change
def _resnet_split(m:nn.Module): return (m[3][6],m[4]) #change
# Split squeezenet model on maxpool layers
def _squeezenet_split(m:nn.Module): return (m[0][0][5], m[0][0][8], m[1])
def _densenet_split(m:nn.Module): return (m[0][0][7],m[1])
def _vgg_split(m:nn.Module): return (m[0][0][22],m[1])
def _alexnet_split(m:nn.Module): return (m[0][0][6],m[1])
_default_meta = {'cut':None, 'split':_default_split}
_resnet_meta = {'cut':-2, 'split':_resnet_split }
_squeezenet_meta = {'cut':-1, 'split': _squeezenet_split}
_densenet_meta = {'cut':-1, 'split':_densenet_split}
_vgg_meta = {'cut':-1, 'split':_vgg_split}
_alexnet_meta = {'cut':-1, 'split':_alexnet_split}
model_meta = {
models.resnet18 :{**_resnet_meta}, models.resnet34: {**_resnet_meta},
models.resnet50 :{**_resnet_meta}, models.resnet101:{**_resnet_meta},
models.resnet152:{**_resnet_meta},
models.squeezenet1_0:{**_squeezenet_meta},
models.squeezenet1_1:{**_squeezenet_meta},
models.densenet121:{**_densenet_meta}, models.densenet169:{**_densenet_meta},
models.densenet201:{**_densenet_meta}, models.densenet161:{**_densenet_meta},
# models.vgg11_bn:{**_vgg_meta}, models.vgg13_bn:{**_vgg_meta}, models.vgg16_bn:{**_vgg_meta}, models.vgg19_bn:{**_vgg_meta}, #change
models.alexnet:{**_alexnet_meta}}
def cnn_config(arch):
"Get the metadata associated with `arch`.""Modified fastai `Learner` support for computer vision to create as first layer a ConvNet that will transform images with n channels to 3 channels"
from fastai.torch_core import * #change
from fastai.basic_train import * #change
from fastai.basic_data import * #change
# from .image import * #change
# from . import models #change
from fastai.callback import * #change
from fastai.layers import * #change
from fastai.callbacks.hooks import * #change
from fastai.train import ClassificationInterpretation #change
from fastai.vision import * #change
__all__ = ['cnn_learner', 'create_cnn', 'create_cnn_model', 'create_body', 'create_head', 'unet_learner']
# By default split models between first and second layer
def _default_split(m:nn.Module): return (m[1],)
# Split a resnet style model
# def _resnet_split(m:nn.Module): return (m[0][6],m[1]) #change
def _resnet_split(m:nn.Module): return (m[2][6],m[3]) #change
# Split squeezenet model on maxpool layers
def _squeezenet_split(m:nn.Module): return (m[0][0][5], m[0][0][8], m[1])
def _densenet_split(m:nn.Module): return (m[0][0][7],m[1])
def _vgg_split(m:nn.Module): return (m[0][0][22],m[1])
def _alexnet_split(m:nn.Module): return (m[0][0][6],m[1])
_default_meta = {'cut':None, 'split':_default_split}
_resnet_meta = {'cut':-2, 'split':_resnet_split }
_squeezenet_meta = {'cut':-1, 'split': _squeezenet_split}
_densenet_meta = {'cut':-1, 'split':_densenet_split}
_vgg_meta = {'cut':-1, 'split':_vgg_split}
_alexnet_meta = {'cut':-1, 'split':_alexnet_split}
model_meta = {
models.resnet18 :{**_resnet_meta}, models.resnet34: {**_resnet_meta},
models.resnet50 :{**_resnet_meta}, models.resnet101:{**_resnet_meta},
models.resnet152:{**_resnet_meta},
models.squeezenet1_0:{**_squeezenet_meta},
models.squeezenet1_1:{**_squeezenet_meta},
models.densenet121:{**_densenet_meta}, models.densenet169:{**_densenet_meta},
models.densenet201:{**_densenet_meta}, models.densenet161:{**_densenet_meta},
# models.vgg11_bn:{**_vgg_meta}, models.vgg13_bn:{**_vgg_meta}, models.vgg16_bn:{**_vgg_meta}, models.vgg19_bn:{**_vgg_meta}, #change
models.alexnet:{**_alexnet_meta}}
def cnn_config(arch):
"Get the metadata associated with `arch`."
torch.backends.cudnn.benchmark = True
return model_meta.get(arch, _default_meta)
def has_pool_type(m):
if is_pool_type(m): return True
for l in m.children():
if has_pool_type(l): return True
return False
def create_body(arch:Callable, pretrained:bool=True, cut:Optional[Union[int, Callable]]=None):
"Cut off the body of a typically pretrained `model` at `cut` (int) or cut the model as specified by `cut(model)` (function)."
model = arch(pretrained)
cut = ifnone(cut, cnn_config(arch)['cut'])
if cut is None:
ll = list(enumerate(model.children()))
cut = next(i for i,o in reversed(ll) if has_pool_type(o))
if isinstance(cut, int): return nn.Sequential(*list(model.children())[:cut])
elif isinstance(cut, Callable): return cut(model)
else: raise NamedError("cut must be either integer or a function")
def create_head(nf:int, nc:int, lin_ftrs:Optional[Collection[int]]=None, ps:Floats=0.5,
concat_pool:bool=True, bn_final:bool=False):
"Model head that takes `nf` features, runs through `lin_ftrs`, and about `nc` classes."
lin_ftrs = [nf, 512, nc] if lin_ftrs is None else [nf] + lin_ftrs + [nc]
ps = listify(ps)
if len(ps) == 1: ps = [ps[0]/2] * (len(lin_ftrs)-2) + ps
actns = [nn.ReLU(inplace=True)] * (len(lin_ftrs)-2) + [None]
pool = AdaptiveConcatPool2d() if concat_pool else nn.AdaptiveAvgPool2d(1)
layers = [pool, Flatten()]
for ni,no,p,actn in zip(lin_ftrs[:-1], lin_ftrs[1:], ps, actns):
layers += bn_drop_lin(ni, no, True, p, actn)
if bn_final: layers.append(nn.BatchNorm1d(lin_ftrs[-1], momentum=0.01))
return nn.Sequential(*layers)
class Normalize(torch.nn.Module): #change
def __init__(self, stats): #change
super().__init__() #change
self.stats = stats #change
def forward(self, x): #change
return (x-tensor(self.stats[0])[...,None,None].cuda()) / tensor(self.stats[1])[...,None,None].cuda() #change
def create_cnn_model(base_arch:Callable, nc:int, n_channels:int, kernel_size:int, padding:int, cut:Union[int,Callable]=None, pretrained:bool=True,
lin_ftrs:Optional[Collection[int]]=None, ps:Floats=0.5, custom_head:Optional[nn.Module]=None,
bn_final:bool=False, concat_pool:bool=True):
"Create custom convnet architecture"
body = create_body(base_arch, pretrained, cut)
if custom_head is None:
nf = num_features_model(nn.Sequential(*body.children())) * (2 if concat_pool else 1)
head = create_head(nf, nc, lin_ftrs, ps=ps, concat_pool=concat_pool, bn_final=bn_final)
else: head = custom_head
xc_to_3c = nn.Conv2d(n_channels, 3, kernel_size, stride=1, padding=padding) #change
norm = Normalize(imagenet_stats) #change
return nn.Sequential(xc_to_3c, norm, body, head) #change
def cnn_learner_xc_to_3c(data:DataBunch, base_arch:Callable, n_channels:int=3, kernel_size:int=3, padding:int=1, cut:Union[int,Callable]=None, pretrained:bool=True,
lin_ftrs:Optional[Collection[int]]=None, ps:Floats=0.5, custom_head:Optional[nn.Module]=None,
split_on:Optional[SplitFuncOrIdxList]=None, bn_final:bool=False, init=nn.init.kaiming_normal_,
concat_pool:bool=True, **kwargs:Any)->Learner: #change
"Build convnet style learner."
meta = cnn_config(base_arch)
model = create_cnn_model(base_arch, data.c, n_channels, kernel_size, padding, cut, pretrained, lin_ftrs, ps=ps, custom_head=custom_head,
bn_final=bn_final, concat_pool=concat_pool)
learn = Learner(data, model, **kwargs)
learn.split(split_on or meta['split'])
if pretrained:
learn.freeze()
if init: apply_init(model[1], init)
return learn
def create_cnn(data, base_arch, **kwargs):
warn("`create_cnn` is deprecated and is now named `cnn_learner`.")
return cnn_learner(data, base_arch, **kwargs)
def unet_learner(data:DataBunch, arch:Callable, pretrained:bool=True, blur_final:bool=True,
norm_type:Optional[NormType]=NormType, split_on:Optional[SplitFuncOrIdxList]=None, blur:bool=False,
self_attention:bool=False, y_range:Optional[Tuple[float,float]]=None, last_cross:bool=True,
bottle:bool=False, cut:Union[int,Callable]=None, **learn_kwargs:Any)->Learner:
"Build Unet learner from `data` and `arch`."
meta = cnn_config(arch)
body = create_body(arch, pretrained, cut)
try: size = data.train_ds[0][0].size
except: size = next(iter(data.train_dl))[0].shape[-2:]
model = to_device(models.unet.DynamicUnet(body, n_classes=data.c, img_size=size, blur=blur, blur_final=blur_final,
self_attention=self_attention, y_range=y_range, norm_type=norm_type, last_cross=last_cross,
bottle=bottle), data.device)
learn = Learner(data, model, **learn_kwargs)
learn.split(ifnone(split_on, meta['split']))
if pretrained: learn.freeze()
apply_init(model[2], nn.init.kaiming_normal_)
return learn
@classmethod
def _cl_int_from_learner(cls, learn:Learner, ds_type:DatasetType=DatasetType.Valid, activ:nn.Module=None, tta=False):
"Create an instance of `ClassificationInterpretation`. `tta` indicates if we want to use Test Time Augmentation."
preds = learn.TTA(ds_type=ds_type, with_loss=True) if tta else learn.get_preds(ds_type=ds_type, activ=activ, with_loss=True)
return cls(learn, *preds, ds_type=ds_type)
def _test_cnn(m):
if not isinstance(m, nn.Sequential) or not len(m) == 2: return False
return isinstance(m[1][0], (AdaptiveConcatPool2d, nn.AdaptiveAvgPool2d))
def _cl_int_gradcam(self, idx, ds_type:DatasetType=DatasetType.Valid, heatmap_thresh:int=16, image:bool=True):
m = self.learn.model.eval()
im,cl = self.learn.data.dl(ds_type).dataset[idx]
cl = int(cl)
xb,_ = self.data.one_item(im, detach=False, denorm=False) #put into a minibatch of batch size = 1
with hook_output(m[0]) as hook_a:
with hook_output(m[0], grad=True) as hook_g:
preds = m(xb)
preds[0,int(cl)].backward()
acts = hook_a.stored[0].cpu() #activation maps
if (acts.shape[-1]*acts.shape[-2]) >= heatmap_thresh:
grad = hook_g.stored[0][0].cpu()
grad_chan = grad.mean(1).mean(1)
mult = F.relu(((acts*grad_chan[...,None,None])).sum(0))
if image:
xb_im = Image(xb[0])
_,ax = plt.subplots()
sz = list(xb_im.shape[-2:])
xb_im.show(ax,title=f"pred. class: {self.pred_class[idx]}, actual class: {self.learn.data.classes[cl]}")
ax.imshow(mult, alpha=0.4, extent=(0,*sz[::-1],0),
interpolation='bilinear', cmap='magma')
return mult
ClassificationInterpretation.GradCAM =_cl_int_gradcam
def _cl_int_plot_top_losses(self, k, largest=True, figsize=(12,12), heatmap:bool=False, heatmap_thresh:int=16,
alpha:float=0.6, cmap:str="magma", show_text:bool=True,
return_fig:bool=None)->Optional[plt.Figure]:
"Show images in `top_losses` along with their prediction, actual, loss, and probability of actual class."
assert not heatmap or _test_cnn(self.learn.model), "`heatmap=True` requires a model like `cnn_learner` produces."
if heatmap is None: heatmap = _test_cnn(self.learn.model)
tl_val,tl_idx = self.top_losses(k, largest)
classes = self.data.classes
cols = math.ceil(math.sqrt(k))
rows = math.ceil(k/cols)
fig,axes = plt.subplots(rows, cols, figsize=figsize)
if show_text: fig.suptitle('Prediction/Actual/Loss/Probability', weight='bold', size=14)
for i,idx in enumerate(tl_idx):
im,cl = self.data.dl(self.ds_type).dataset[idx]
cl = int(cl)
title = f'{classes[self.pred_class[idx]]}/{classes[cl]} / {self.losses[idx]:.2f} / {self.preds[idx][cl]:.2f}' if show_text else None
im.show(ax=axes.flat[i], title=title)
if heatmap:
mult = self.GradCAM(idx,self.ds_type,heatmap_thresh,image=False)
if mult is not None:
sz = list(im.shape[-2:])
axes.flat[i].imshow(mult, alpha=alpha, extent=(0,*sz[::-1],0), interpolation='bilinear', cmap=cmap)
if ifnone(return_fig, defaults.return_fig): return fig
def _cl_int_plot_multi_top_losses(self, samples:int=3, figsize:Tuple[int,int]=(8,8), save_misclassified:bool=False):
"Show images in `top_losses` along with their prediction, actual, loss, and probability of predicted class in a multilabeled dataset."
if samples >20:
print("Max 20 samples")
return
losses, idxs = self.top_losses(self.data.c)
l_dim = len(losses.size())
if l_dim == 1: losses, idxs = self.top_losses()
infolist, ordlosses_idxs, mismatches_idxs, mismatches, losses_mismatches, mismatchescontainer = [],[],[],[],[],[]
truthlabels = np.asarray(self.y_true, dtype=int)
classes_ids = [k for k in enumerate(self.data.classes)]
predclass = np.asarray(self.pred_class)
for i,pred in enumerate(predclass):
where_truth = np.nonzero((truthlabels[i]>0))[0]
mismatch = np.all(pred!=where_truth)
if mismatch:
mismatches_idxs.append(i)
if l_dim > 1 : losses_mismatches.append((losses[i][pred], i))
else: losses_mismatches.append((losses[i], i))
if l_dim > 1: infotup = (i, pred, where_truth, losses[i][pred], np.round(self.preds[i], decimals=3)[pred], mismatch)
else: infotup = (i, pred, where_truth, losses[i], np.round(self.preds[i], decimals=3)[pred], mismatch)
infolist.append(infotup)
ds = self.data.dl(self.ds_type).dataset
mismatches = ds[mismatches_idxs]
ordlosses = sorted(losses_mismatches, key = lambda x: x[0], reverse=True)
for w in ordlosses: ordlosses_idxs.append(w[1])
mismatches_ordered_byloss = ds[ordlosses_idxs]
print(f'{str(len(mismatches))} misclassified samples over {str(len(self.data.valid_ds))} samples in the validation set.')
samples = min(samples, len(mismatches))
for ima in range(len(mismatches_ordered_byloss)):
mismatchescontainer.append(mismatches_ordered_byloss[ima][0])
for sampleN in range(samples):
actualclasses = ''
for clas in infolist[ordlosses_idxs[sampleN]][2]:
actualclasses = f'{actualclasses} -- {str(classes_ids[clas][1])}'
imag = mismatches_ordered_byloss[sampleN][0]
imag = show_image(imag, figsize=figsize)
imag.set_title(f"""Predicted: {classes_ids[infolist[ordlosses_idxs[sampleN]][1]][1]} \nActual: {actualclasses}\nLoss: {infolist[ordlosses_idxs[sampleN]][3]}\nProbability: {infolist[ordlosses_idxs[sampleN]][4]}""",
loc='left')
plt.show()
if save_misclassified: return mismatchescontainer
ClassificationInterpretation.from_learner = _cl_int_from_learner
ClassificationInterpretation.plot_top_losses = _cl_int_plot_top_losses
ClassificationInterpretation.plot_multi_top_losses = _cl_int_plot_multi_top_losses
def _learner_interpret(learn:Learner, ds_type:DatasetType=DatasetType.Valid, tta=False):
"Create a `ClassificationInterpretation` object from `learner` on `ds_type` with `tta`."
return ClassificationInterpretation.from_learner(learn, ds_type=ds_type, tta=tta)
Learner.interpret = _learner_interpret
torch.backends.cudnn.benchmark = True
return model_meta.get(arch, _default_meta)
def has_pool_type(m):
if is_pool_type(m): return True
for l in m.children():
if has_pool_type(l): return True
return False
def create_body(arch:Callable, pretrained:bool=True, cut:Optional[Union[int, Callable]]=None):
"Cut off the body of a typically pretrained `model` at `cut` (int) or cut the model as specified by `cut(model)` (function)."
model = arch(pretrained)
cut = ifnone(cut, cnn_config(arch)['cut'])
if cut is None:
ll = list(enumerate(model.children()))
cut = next(i for i,o in reversed(ll) if has_pool_type(o))
if isinstance(cut, int): return nn.Sequential(*list(model.children())[:cut])
elif isinstance(cut, Callable): return cut(model)
else: raise NamedError("cut must be either integer or a function")
def create_head(nf:int, nc:int, lin_ftrs:Optional[Collection[int]]=None, ps:Floats=0.5,
concat_pool:bool=True, bn_final:bool=False):
"Model head that takes `nf` features, runs through `lin_ftrs`, and about `nc` classes."
lin_ftrs = [nf, 512, nc] if lin_ftrs is None else [nf] + lin_ftrs + [nc]
ps = listify(ps)
if len(ps) == 1: ps = [ps[0]/2] * (len(lin_ftrs)-2) + ps
actns = [nn.ReLU(inplace=True)] * (len(lin_ftrs)-2) + [None]
pool = AdaptiveConcatPool2d() if concat_pool else nn.AdaptiveAvgPool2d(1)
layers = [pool, Flatten()]
for ni,no,p,actn in zip(lin_ftrs[:-1], lin_ftrs[1:], ps, actns):
layers += bn_drop_lin(ni, no, True, p, actn)
if bn_final: layers.append(nn.BatchNorm1d(lin_ftrs[-1], momentum=0.01))
return nn.Sequential(*layers)
class Normalize(torch.nn.Module): #change
def __init__(self, stats): #change
super().__init__() #change
self.stats = stats #change
def forward(self, x): #change
return (x-tensor(self.stats[0])[...,None,None].cuda()) / tensor(self.stats[1])[...,None,None].cuda() #change
def create_cnn_model(base_arch:Callable, nc:int, cut:Union[int,Callable]=None, pretrained:bool=True,
lin_ftrs:Optional[Collection[int]]=None, ps:Floats=0.5, custom_head:Optional[nn.Module]=None,
bn_final:bool=False, concat_pool:bool=True):
"Create custom convnet architecture"
body = create_body(base_arch, pretrained, cut)
if custom_head is None:
nf = num_features_model(nn.Sequential(*body.children())) * (2 if concat_pool else 1)
head = create_head(nf, nc, lin_ftrs, ps=ps, concat_pool=concat_pool, bn_final=bn_final)
else: head = custom_head
xc_to_3c = nn.Conv2d(3,3,kernel_size=3, stride=1, padding=1) #change
norm = Normalize(imagenet_stats) #change
return nn.Sequential(xc_to_3c, nn.ReLU(), norm, body, head) #change
def cnn_learner_xc_to_3c(data:DataBunch, base_arch:Callable, n_channels:int=3, cut:Union[int,Callable]=None, pretrained:bool=True,
lin_ftrs:Optional[Collection[int]]=None, ps:Floats=0.5, custom_head:Optional[nn.Module]=None,
split_on:Optional[SplitFuncOrIdxList]=None, bn_final:bool=False, init=nn.init.kaiming_normal_,
concat_pool:bool=True, **kwargs:Any)->Learner: #change
"Build convnet style learner."
meta = cnn_config(base_arch)
model = create_cnn_model(base_arch, data.c, cut, pretrained, lin_ftrs, ps=ps, custom_head=custom_head,
bn_final=bn_final, concat_pool=concat_pool)
learn = Learner(data, model, **kwargs)
learn.split(split_on or meta['split'])
if pretrained:
learn.freeze()
if init: apply_init(model[1], init)
return learn
def create_cnn(data, base_arch, **kwargs):
warn("`create_cnn` is deprecated and is now named `cnn_learner`.")
return cnn_learner(data, base_arch, **kwargs)
def unet_learner(data:DataBunch, arch:Callable, pretrained:bool=True, blur_final:bool=True,
norm_type:Optional[NormType]=NormType, split_on:Optional[SplitFuncOrIdxList]=None, blur:bool=False,
self_attention:bool=False, y_range:Optional[Tuple[float,float]]=None, last_cross:bool=True,
bottle:bool=False, cut:Union[int,Callable]=None, **learn_kwargs:Any)->Learner:
"Build Unet learner from `data` and `arch`."
meta = cnn_config(arch)
body = create_body(arch, pretrained, cut)
try: size = data.train_ds[0][0].size
except: size = next(iter(data.train_dl))[0].shape[-2:]
model = to_device(models.unet.DynamicUnet(body, n_classes=data.c, img_size=size, blur=blur, blur_final=blur_final,
self_attention=self_attention, y_range=y_range, norm_type=norm_type, last_cross=last_cross,
bottle=bottle), data.device)
learn = Learner(data, model, **learn_kwargs)
learn.split(ifnone(split_on, meta['split']))
if pretrained: learn.freeze()
apply_init(model[2], nn.init.kaiming_normal_)
return learn
@classmethod
def _cl_int_from_learner(cls, learn:Learner, ds_type:DatasetType=DatasetType.Valid, activ:nn.Module=None, tta=False):
"Create an instance of `ClassificationInterpretation`. `tta` indicates if we want to use Test Time Augmentation."
preds = learn.TTA(ds_type=ds_type, with_loss=True) if tta else learn.get_preds(ds_type=ds_type, activ=activ, with_loss=True)
return cls(learn, *preds, ds_type=ds_type)
def _test_cnn(m):
if not isinstance(m, nn.Sequential) or not len(m) == 2: return False
return isinstance(m[1][0], (AdaptiveConcatPool2d, nn.AdaptiveAvgPool2d))
def _cl_int_gradcam(self, idx, ds_type:DatasetType=DatasetType.Valid, heatmap_thresh:int=16, image:bool=True):
m = self.learn.model.eval()
im,cl = self.learn.data.dl(ds_type).dataset[idx]
cl = int(cl)
xb,_ = self.data.one_item(im, detach=False, denorm=False) #put into a minibatch of batch size = 1
with hook_output(m[0]) as hook_a:
with hook_output(m[0], grad=True) as hook_g:
preds = m(xb)
preds[0,int(cl)].backward()
acts = hook_a.stored[0].cpu() #activation maps
if (acts.shape[-1]*acts.shape[-2]) >= heatmap_thresh:
grad = hook_g.stored[0][0].cpu()
grad_chan = grad.mean(1).mean(1)
mult = F.relu(((acts*grad_chan[...,None,None])).sum(0))
if image:
xb_im = Image(xb[0])
_,ax = plt.subplots()
sz = list(xb_im.shape[-2:])
xb_im.show(ax,title=f"pred. class: {self.pred_class[idx]}, actual class: {self.learn.data.classes[cl]}")
ax.imshow(mult, alpha=0.4, extent=(0,*sz[::-1],0),
interpolation='bilinear', cmap='magma')
return mult
ClassificationInterpretation.GradCAM =_cl_int_gradcam
def _cl_int_plot_top_losses(self, k, largest=True, figsize=(12,12), heatmap:bool=False, heatmap_thresh:int=16,
alpha:float=0.6, cmap:str="magma", show_text:bool=True,
return_fig:bool=None)->Optional[plt.Figure]:
"Show images in `top_losses` along with their prediction, actual, loss, and probability of actual class."
assert not heatmap or _test_cnn(self.learn.model), "`heatmap=True` requires a model like `cnn_learner` produces."
if heatmap is None: heatmap = _test_cnn(self.learn.model)
tl_val,tl_idx = self.top_losses(k, largest)
classes = self.data.classes
cols = math.ceil(math.sqrt(k))
rows = math.ceil(k/cols)
fig,axes = plt.subplots(rows, cols, figsize=figsize)
if show_text: fig.suptitle('Prediction/Actual/Loss/Probability', weight='bold', size=14)
for i,idx in enumerate(tl_idx):
im,cl = self.data.dl(self.ds_type).dataset[idx]
cl = int(cl)
title = f'{classes[self.pred_class[idx]]}/{classes[cl]} / {self.losses[idx]:.2f} / {self.preds[idx][cl]:.2f}' if show_text else None
im.show(ax=axes.flat[i], title=title)
if heatmap:
mult = self.GradCAM(idx,self.ds_type,heatmap_thresh,image=False)
if mult is not None:
sz = list(im.shape[-2:])
axes.flat[i].imshow(mult, alpha=alpha, extent=(0,*sz[::-1],0), interpolation='bilinear', cmap=cmap)
if ifnone(return_fig, defaults.return_fig): return fig
def _cl_int_plot_multi_top_losses(self, samples:int=3, figsize:Tuple[int,int]=(8,8), save_misclassified:bool=False):
"Show images in `top_losses` along with their prediction, actual, loss, and probability of predicted class in a multilabeled dataset."
if samples >20:
print("Max 20 samples")
return
losses, idxs = self.top_losses(self.data.c)
l_dim = len(losses.size())
if l_dim == 1: losses, idxs = self.top_losses()
infolist, ordlosses_idxs, mismatches_idxs, mismatches, losses_mismatches, mismatchescontainer = [],[],[],[],[],[]
truthlabels = np.asarray(self.y_true, dtype=int)
classes_ids = [k for k in enumerate(self.data.classes)]
predclass = np.asarray(self.pred_class)
for i,pred in enumerate(predclass):
where_truth = np.nonzero((truthlabels[i]>0))[0]
mismatch = np.all(pred!=where_truth)
if mismatch:
mismatches_idxs.append(i)
if l_dim > 1 : losses_mismatches.append((losses[i][pred], i))
else: losses_mismatches.append((losses[i], i))
if l_dim > 1: infotup = (i, pred, where_truth, losses[i][pred], np.round(self.preds[i], decimals=3)[pred], mismatch)
else: infotup = (i, pred, where_truth, losses[i], np.round(self.preds[i], decimals=3)[pred], mismatch)
infolist.append(infotup)
ds = self.data.dl(self.ds_type).dataset
mismatches = ds[mismatches_idxs]
ordlosses = sorted(losses_mismatches, key = lambda x: x[0], reverse=True)
for w in ordlosses: ordlosses_idxs.append(w[1])
mismatches_ordered_byloss = ds[ordlosses_idxs]
print(f'{str(len(mismatches))} misclassified samples over {str(len(self.data.valid_ds))} samples in the validation set.')
samples = min(samples, len(mismatches))
for ima in range(len(mismatches_ordered_byloss)):
mismatchescontainer.append(mismatches_ordered_byloss[ima][0])
for sampleN in range(samples):
actualclasses = ''
for clas in infolist[ordlosses_idxs[sampleN]][2]:
actualclasses = f'{actualclasses} -- {str(classes_ids[clas][1])}'
imag = mismatches_ordered_byloss[sampleN][0]
imag = show_image(imag, figsize=figsize)
imag.set_title(f"""Predicted: {classes_ids[infolist[ordlosses_idxs[sampleN]][1]][1]} \nActual: {actualclasses}\nLoss: {infolist[ordlosses_idxs[sampleN]][3]}\nProbability: {infolist[ordlosses_idxs[sampleN]][4]}""",
loc='left')
plt.show()
if save_misclassified: return mismatchescontainer
ClassificationInterpretation.from_learner = _cl_int_from_learner
ClassificationInterpretation.plot_top_losses = _cl_int_plot_top_losses
ClassificationInterpretation.plot_multi_top_losses = _cl_int_plot_multi_top_losses
def _learner_interpret(learn:Learner, ds_type:DatasetType=DatasetType.Valid, tta=False):
"Create a `ClassificationInterpretation` object from `learner` on `ds_type` with `tta`."
return ClassificationInterpretation.from_learner(learn, ds_type=ds_type, tta=tta)
Learner.interpret = _learner_interpret