<jupyter_start><jupyter_text>Creating a databunch<jupyter_code>path = Path('../data/celeba/faces/')
## Function to filter validation samples
def validation_func(x):
    return 'validation' in x
tfms = get_transforms(do_flip=False, flip_vert=False, max_rotate=30, max_lighting=0.3)
src = (ImageItemList.from_csv(path, csv_name='labels.csv')
       .split_by_valid_func(validation_func)
       .label_from_df(cols='tags',label_delim=' '))

data = (src.transform(tfms, size=128)
       .databunch(bs=256).normalize(imagenet_stats))
print(data.c,'\n',data.classes)
data.show_batch(rows=2, figsize=(20,12))<jupyter_output><empty_output><jupyter_text>Model We are going to use a resnet50 pretrained model and do transfer learning on CelebA dataset.<jupyter_code>arch = models.resnet50
acc_02 = partial(accuracy_thresh, thresh=0.2)
acc_03 = partial(accuracy_thresh, thresh=0.3)
acc_04 = partial(accuracy_thresh, thresh=0.4)
acc_05 = partial(accuracy_thresh, thresh=0.5)
f_score = partial(fbeta, thresh=0.2)
learn = create_cnn(data, arch, metrics=[acc_02, acc_03, acc_04, acc_05, f_score])
learn.lr_find()
learn.recorder.plot()
lr = 1e-2
learn.fit_one_cycle(1, slice(lr))
learn.fit_one_cycle(4, slice(lr))
learn.save('ff_stage-1-rn50')<jupyter_output><empty_output><jupyter_text>Tuning the whole model<jupyter_code>learn.unfreeze()
learn.lr_find()
learn.recorder.plot()
learn.fit_one_cycle(5, slice(1e-5, lr/5))
learn.save('ff_stage-2-rn50')<jupyter_output><empty_output><jupyter_text>Further Training<jupyter_code>data = (src.transform(tfms, size=256)
       .databunch(bs=64).normalize(imagenet_stats))
acc_05 = partial(accuracy_thresh, thresh=0.5)
f_score = partial(fbeta, thresh=0.5)
learn = create_cnn(data, models.resnet50, pretrained=False,metrics=[acc_05, f_score])
learn.load("ff_stage-2-rn50")
learn.freeze()
learn.lr_find()
learn.recorder.plot()
lr = 0.01
learn.fit_one_cycle(1, slice(lr))
learn.save('ff_stage-1-256-rn50')<jupyter_output><empty_output><jupyter_text>Visualize<jupyter_code>learn = create_cnn(data, models.resnet50, pretrained=False)
learn.load("ff_stage-1-256-rn50")
m = learn.model.eval();
idx=5
x,y = data.valid_ds[idx]
x.show()
data.valid_ds.y[idx]
xb, _ = data.one_item(x)
xb_im = Image(data.denorm(xb)[0])
xb = xb.cuda()
from fastai.callbacks.hooks import *
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,str(data.valid_ds.y[idx])].backward()
    return hook_a,hook_g
hook_a,hook_g = hooked_backward()
acts  = hook_a.stored[0].cpu()
acts.shape
avg_acts = acts.mean(0)
avg_acts.shape
def show_heatmap(hm):
    _,ax = plt.subplots()
    xb_im.show(ax)
    ax.imshow(hm, alpha=0.6, extent=(0,256,256,0),
              interpolation='bilinear', cmap='magma');
avg_acts
show_heatmap(avg_acts)
idx=700
x,y = data.valid_ds[idx]
xb, _ = data.one_item(x)
xb_im = Image(data.denorm(xb)[0])
xb = xb.cuda()

hook_a,hook_g = hooked_backward()
acts  = hook_a.stored[0].cpu()
avg_acts = acts.mean(0)
show_heatmap(avg_acts)
avg_acts<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15464251051439665, 'fraction_numerical': 0.03817534778388871, 'mean_word_length': 6.224299065420561, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '401590', 'license': 'MIT', 'n_tokens_mistral': 1330, 'n_tokens_neox': 1271, 'n_words': 187, 'path': 'class.ipynb', 'repo_name': 'rishraj2000/labellerr_assignment', 'size': '3091'}