%matplotlib inline
from fastai2.vision.all import *
from nbdev.showdoc import *
bs = 64
path = untar_data(URLs.PETS)
tfms = partial(aug_transforms, max_rotate=20, max_zoom=1.3, max_lighting=0.4, max_warp=0.4,
p_affine=1., p_lighting=1.)
doc(aug_transforms)
def repeat_one(source, n=128): return [get_image_files(source)[0]]*n
def get_dls(size, bs, pad_mode='reflection', batch=False):
pets = DataBlock(blocks=(ImageBlock, CategoryBlock),
get_items=repeat_one,
splitter=RandomSplitter(0.2, seed=2),
get_y=RegexLabeller(r'([^/]+)_\d+.jpg$'),
item_tfms=Resize(460),
batch_tfms=[*tfms(size=size, pad_mode=pad_mode, batch=batch), Normalize.from_stats(*imagenet_stats)])
return pets.dataloaders(path/'images', path=path, bs=bs)
dls = get_dls(224, bs, 'zeros')
dls.show_batch(max_n=9, figsize=(8,8))
dls = get_dls(224,bs)
dls.show_batch(max_n=9, figsize=(8,8))
batch=True means we pick the same random augmentation for all the images in the batch.
dls = get_dls(224,bs,batch=True)
dls.show_batch(max_n=9, figsize=(8,8))
def get_dls(size, bs, pad_mode='reflection'):
pets = DataBlock(blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(0.2, seed=2),
get_y=RegexLabeller(r'([^/]+)_\d+.jpg$'),
item_tfms=RandomResizedCrop(460, min_scale=0.75),
batch_tfms=[*tfms(size=size, pad_mode=pad_mode), Normalize.from_stats(*imagenet_stats)])
return pets.dataloaders(path/'images', path=path, bs=bs)
dls = get_dls(224,bs)
learn = cnn_learner(dls, resnet34, metrics=error_rate, config=cnn_config(bn_final=True))
learn.fit_one_cycle(3, slice(1e-2), pct_start=0.8)
| epoch | train_loss | valid_loss | error_rate | time |
|---|---|---|---|---|
| 0 | 1.680457 | 0.346096 | 0.080514 | 00:20 |
| 1 | 0.994624 | 0.260845 | 0.071719 | 00:15 |
| 2 | 0.730085 | 0.237081 | 0.069012 | 00:15 |
learn.unfreeze()
learn.fit_one_cycle(2, lr_max=slice(1e-6,1e-3), pct_start=0.8)
| epoch | train_loss | valid_loss | error_rate | time |
|---|---|---|---|---|
| 0 | 0.596662 | 0.227051 | 0.058863 | 00:19 |
| 1 | 0.575870 | 0.229266 | 0.059540 | 00:18 |
dls = get_dls(352,bs)
learn.dls = dls
learn.fit_one_cycle(2, lr_max=slice(1e-6,1e-4))
| epoch | train_loss | valid_loss | error_rate | time |
|---|---|---|---|---|
| 0 | 0.513675 | 0.213754 | 0.051421 | 00:35 |
| 1 | 0.452948 | 0.209984 | 0.051421 | 00:34 |
learn.save('352')
dls = get_dls(352,16)
learn = cnn_learner(dls, resnet34, metrics=error_rate, config=cnn_config(bn_final=True)).load('352')
idx=0
x,y = dls.valid_ds[idx]
show_at(dls.valid_ds, idx);
k = tensor([
[0. ,-5/3,1],
[-5/3,-5/3,1],
[1. ,1 ,1],
]).expand(1,3,3,3)/6
k
tensor([[[[ 0.0000, -0.2778, 0.1667],
[-0.2778, -0.2778, 0.1667],
[ 0.1667, 0.1667, 0.1667]],
[[ 0.0000, -0.2778, 0.1667],
[-0.2778, -0.2778, 0.1667],
[ 0.1667, 0.1667, 0.1667]],
[[ 0.0000, -0.2778, 0.1667],
[-0.2778, -0.2778, 0.1667],
[ 0.1667, 0.1667, 0.1667]]]])
k.shape
torch.Size([1, 3, 3, 3])
t = tensor(x).permute(2,0,1).float(); t.shape
torch.Size([3, 233, 300])
t[None].shape
torch.Size([1, 3, 233, 300])
edge = F.conv2d(t[None], k)
show_image(edge[0], figsize=(5,5));
dls.c
37
learn.model
Sequential(
(0): Sequential(
(0): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
(3): MaxPool2d(kernel_size=3, stride=2, padding=1, dilation=1, ceil_mode=False)
(4): Sequential(
(0): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(1): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(2): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(5): Sequential(
(0): BasicBlock(
(conv1): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(64, 128, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(2): BasicBlock(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(3): BasicBlock(
(conv1): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(6): Sequential(
(0): BasicBlock(
(conv1): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(128, 256, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(2): BasicBlock(
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(3): BasicBlock(
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(4): BasicBlock(
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(5): BasicBlock(
(conv1): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(7): Sequential(
(0): BasicBlock(
(conv1): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(downsample): Sequential(
(0): Conv2d(256, 512, kernel_size=(1, 1), stride=(2, 2), bias=False)
(1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
(1): BasicBlock(
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(2): BasicBlock(
(conv1): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu): ReLU(inplace=True)
(conv2): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
)
)
(1): Sequential(
(0): AdaptiveConcatPool2d(
(ap): AdaptiveAvgPool2d(output_size=1)
(mp): AdaptiveMaxPool2d(output_size=1)
)
(1): Flatten()
(2): BatchNorm1d(1024, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(3): Dropout(p=0.25, inplace=False)
(4): Linear(in_features=1024, out_features=512, bias=False)
(5): ReLU(inplace=True)
(6): BatchNorm1d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(7): Dropout(p=0.5, inplace=False)
(8): Linear(in_features=512, out_features=37, bias=False)
(9): BatchNorm1d(37, eps=1e-05, momentum=0.01, affine=True, track_running_stats=True)
)
)
print(learn.summary())
Sequential (Input shape: 16 x 3 x 352 x 352) ================================================================ Layer (type) Output Shape Param # Trainable ================================================================ Conv2d 16 x 64 x 176 x 176 9,408 False ________________________________________________________________ BatchNorm2d 16 x 64 x 176 x 176 128 True ________________________________________________________________ ReLU 16 x 64 x 176 x 176 0 False ________________________________________________________________ MaxPool2d 16 x 64 x 88 x 88 0 False ________________________________________________________________ Conv2d 16 x 64 x 88 x 88 36,864 False ________________________________________________________________ BatchNorm2d 16 x 64 x 88 x 88 128 True ________________________________________________________________ ReLU 16 x 64 x 88 x 88 0 False ________________________________________________________________ Conv2d 16 x 64 x 88 x 88 36,864 False ________________________________________________________________ BatchNorm2d 16 x 64 x 88 x 88 128 True ________________________________________________________________ Conv2d 16 x 64 x 88 x 88 36,864 False ________________________________________________________________ BatchNorm2d 16 x 64 x 88 x 88 128 True ________________________________________________________________ ReLU 16 x 64 x 88 x 88 0 False ________________________________________________________________ Conv2d 16 x 64 x 88 x 88 36,864 False ________________________________________________________________ BatchNorm2d 16 x 64 x 88 x 88 128 True ________________________________________________________________ Conv2d 16 x 64 x 88 x 88 36,864 False ________________________________________________________________ BatchNorm2d 16 x 64 x 88 x 88 128 True ________________________________________________________________ ReLU 16 x 64 x 88 x 88 0 False ________________________________________________________________ Conv2d 16 x 64 x 88 x 88 36,864 False ________________________________________________________________ BatchNorm2d 16 x 64 x 88 x 88 128 True ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 73,728 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ ReLU 16 x 128 x 44 x 44 0 False ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 147,456 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 8,192 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 147,456 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ ReLU 16 x 128 x 44 x 44 0 False ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 147,456 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 147,456 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ ReLU 16 x 128 x 44 x 44 0 False ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 147,456 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 147,456 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ ReLU 16 x 128 x 44 x 44 0 False ________________________________________________________________ Conv2d 16 x 128 x 44 x 44 147,456 False ________________________________________________________________ BatchNorm2d 16 x 128 x 44 x 44 256 True ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 294,912 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ ReLU 16 x 256 x 22 x 22 0 False ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 32,768 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ ReLU 16 x 256 x 22 x 22 0 False ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ ReLU 16 x 256 x 22 x 22 0 False ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ ReLU 16 x 256 x 22 x 22 0 False ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ ReLU 16 x 256 x 22 x 22 0 False ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ ReLU 16 x 256 x 22 x 22 0 False ________________________________________________________________ Conv2d 16 x 256 x 22 x 22 589,824 False ________________________________________________________________ BatchNorm2d 16 x 256 x 22 x 22 512 True ________________________________________________________________ Conv2d 16 x 512 x 11 x 11 1,179,648 False ________________________________________________________________ BatchNorm2d 16 x 512 x 11 x 11 1,024 True ________________________________________________________________ ReLU 16 x 512 x 11 x 11 0 False ________________________________________________________________ Conv2d 16 x 512 x 11 x 11 2,359,296 False ________________________________________________________________ BatchNorm2d 16 x 512 x 11 x 11 1,024 True ________________________________________________________________ Conv2d 16 x 512 x 11 x 11 131,072 False ________________________________________________________________ BatchNorm2d 16 x 512 x 11 x 11 1,024 True ________________________________________________________________ Conv2d 16 x 512 x 11 x 11 2,359,296 False ________________________________________________________________ BatchNorm2d 16 x 512 x 11 x 11 1,024 True ________________________________________________________________ ReLU 16 x 512 x 11 x 11 0 False ________________________________________________________________ Conv2d 16 x 512 x 11 x 11 2,359,296 False ________________________________________________________________ BatchNorm2d 16 x 512 x 11 x 11 1,024 True ________________________________________________________________ Conv2d 16 x 512 x 11 x 11 2,359,296 False ________________________________________________________________ BatchNorm2d 16 x 512 x 11 x 11 1,024 True ________________________________________________________________ ReLU 16 x 512 x 11 x 11 0 False ________________________________________________________________ Conv2d 16 x 512 x 11 x 11 2,359,296 False ________________________________________________________________ BatchNorm2d 16 x 512 x 11 x 11 1,024 True ________________________________________________________________ AdaptiveAvgPool2d 16 x 512 x 1 x 1 0 False ________________________________________________________________ AdaptiveMaxPool2d 16 x 512 x 1 x 1 0 False ________________________________________________________________ Flatten 16 x 1024 0 False ________________________________________________________________ BatchNorm1d 16 x 1024 2,048 True ________________________________________________________________ Dropout 16 x 1024 0 False ________________________________________________________________ Linear 16 x 512 524,288 True ________________________________________________________________ ReLU 16 x 512 0 False ________________________________________________________________ BatchNorm1d 16 x 512 1,024 True ________________________________________________________________ Dropout 16 x 512 0 False ________________________________________________________________ Linear 16 x 37 18,944 True ________________________________________________________________ BatchNorm1d 16 x 37 74 True ________________________________________________________________ Total params: 21,831,050 Total trainable params: 563,402 Total non-trainable params: 21,267,648 Optimizer used: <function Adam at 0x7f7778f8b440> Loss function: FlattenedLoss of CrossEntropyLoss() Model frozen up to parameter group number 2 Callbacks: - TrainEvalCallback - Recorder - ProgressCallback
m = learn.model.eval();
b = dls.one_batch()
xb_im = TensorImage(dls.train.decode(b)[0][0])
xb = b[0]
def hooked_backward(cat=y):
with hook_output(m[0]) as hook_a:
with hook_output(m[0], grad=True) as hook_g:
preds = m(xb)
preds[0,int(cat)].backward()
return hook_a,hook_g
hook_a,hook_g = hooked_backward()
acts = hook_a.stored[0].cpu()
acts.shape
torch.Size([512, 11, 11])
avg_acts = acts.mean(0)
avg_acts.shape
torch.Size([11, 11])
def show_heatmap(hm):
_,ax = plt.subplots()
xb_im.show(ctx=ax)
ax.imshow(hm, alpha=0.6, extent=(0,352,352,0),
interpolation='bilinear', cmap='magma');
show_heatmap(avg_acts)
grad = hook_g.stored[0][0].cpu()
grad_chan = grad.mean(1).mean(1)
grad.shape,grad_chan.shape
(torch.Size([512, 11, 11]), torch.Size([512]))
mult = (acts*grad_chan[...,None,None]).mean(0)
show_heatmap(mult)
fn = Path.home()/'tmp/bulldog_maine.png' #Replace with your own image
x = PILImage.create(fn); x
dl = dls.test_dl([fn])
b = dl.one_batch()
xb_im = TensorImage(dls.train.decode(b)[0][0])
xb = b[0]
hook_a,hook_g = hooked_backward()
acts = hook_a.stored[0].cpu()
grad = hook_g.stored[0][0].cpu()
grad_chan = grad.mean(1).mean(1)
mult = (acts*grad_chan[...,None,None]).mean(0)
show_heatmap(mult)
dls.vocab[0]
'Abyssinian'
hook_a,hook_g = hooked_backward(0)
acts = hook_a.stored[0].cpu()
grad = hook_g.stored[0][0].cpu()
grad_chan = grad.mean(1).mean(1)
mult = (acts*grad_chan[...,None,None]).mean(0)
show_heatmap(mult)