<jupyter_start><jupyter_text>Imagenette training<jupyter_code>path = untar_data(URLs.IMAGENETTE_160)
size = 128
tfms = [make_rgb, RandomResizedCrop(size, scale=(0.35,1)), np_to_float, PilRandomFlip()]

bs = 64

il = ImageList.from_files(path, tfms=tfms)
sd = SplitData.split_by_func(il, partial(grandparent_splitter, valid_name='val'))
ll = label_by_func(sd, parent_labeler, proc_y=CategoryProcessor())
ll.valid.x.tfms = [make_rgb, CenterCrop(size), np_to_float]
data = ll.to_databunch(bs, c_in=3, c_out=10, num_workers=8)<jupyter_output><empty_output><jupyter_text>XResNet<jupyter_code>#export
def noop(x): return x

class Flatten(nn.Module):
    def forward(self, x): return x.view(x.size(0), -1)

def conv(ni, nf, ks=3, stride=1, bias=False):
    return nn.Conv2d(ni, nf, ks, stride,  padding=ks//2, bias=bias)
#export
act_fn = nn.ReLU(inplace=True)

def init_cnn(m):
    if getattr(m, 'bias', None) is not None: nn.init.constant_(m.bias, 0.)
    if isinstance(m, (nn.Conv2d, nn.Linear)): nn.init.kaiming_normal_(m.weight)
    for l in m.children(): init_cnn(l)
    
def conv_layer(ni, nf, ks=3, stride=1, zero_bn=False, act=True):
    bn = nn.BatchNorm2d(nf)
    nn.init.constant_(bn.weight, 0. if zero_bn else 1.)
    layers = [conv(ni, nf, ks, stride), bn]
    if act: layers.append(act_fn)
    return nn.Sequential(*layers)
#export
class ResBlock(nn.Module):
    def __init__(self, expansion, ni, nh, stride=1):
        super().__init__()
        nf,ni = nh*expansion,ni*expansion
        layers = [conv_layer(ni, nh, 3, stride),
                  conv_layer(nh, nf, 3, zero_bn=True, act=False)] if expansion == 1 else [
                  conv_layer(ni, nh, 1),
                  conv_layer(nh, nh, 3, stride),
                  conv_layer(nh, nf, 1, zero_bn=true, act=False)
        ]
        self.convs = nn.Sequential(*layers)
        self.idconv = noop if ni==nf else conv_layer(ni, nf, 1, act=False)
        self.pool = noop if stride==1 else nn.AvgPool2d(2, ceil_mode=True)
        
    def forward(self, x): return act_fn(self.convs(x) + self.idconv(self.pool(x)))
#export
class XResNet(nn.Sequential):
    @classmethod
    def create(cls, expansion, layers, c_in=3, c_out=1000):
        nfs = [c_in, (c_in+1)*8, 64, 64]
        stem = [conv_layer(nfs[i], nfs[i+1], stride=2 if i == 0 else 1) for i in range(3)]
        
        nfs = [64//expansion,64,128,256,512]
        res_layers = [cls._make_layer(expansion, nfs[i], nfs[i+1], n_blocks=l, stride=1 if i==0 else 2)
                     for i,l in enumerate(layers)]
        res = cls(
            *stem,
            nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
            *res_layers,
            nn.AdaptiveAvgPool2d(1), Flatten(),
            nn.Linear(nfs[-1]*expansion, c_out))
        init_cnn(res)
        return res
    
    @staticmethod
    def _make_layer(expansion, ni, nf, n_blocks, stride):
        return nn.Sequential(*[ResBlock(expansion, ni if i == 0 else nf, nf, stride if i == 0 else 1) for i in range(n_blocks)])
#export
def xresnet18(**kwargs): return XResNet.create(1, [2, 2, 2, 2], **kwargs)
def xresnet34(**kwargs): return XResNet.create(1, [3, 4, 6, 3], **kwargs)
def xresnet50(**kwargs): return xResNet.create(4, [3, 4, 6, 3], **kwargs)
def xresnet101(**kwargs): return XResNet.create(4, [3, 4, 23, 3], **kwargs)
def xresnet152(**kwargs): return XResNet.create(4, [3, 8, 36, 3], **kwargs)<jupyter_output><empty_output><jupyter_text>Train<jupyter_code>cbfs = [partial(AvgStatsCallback, accuracy), ProgressCallback, CudaCallback,
       partial(BatchTransformXCallback, norm_imagenette),
#        partial(MixUp, alpha=0.2)
       ]
loss_func = LabelSmoothingCrossEntropy()
arch = partial(xresnet18, c_out=10)
opt_func = adam_opt(mom=0.9, mom_sqr=0.99, eps=1e-6, wd=1e-2)
#export
def get_batch(dl, learn):
    learn.xb, learn.yb = next(iter(dl))
    learn.do_begin_fit(0) # inside is self('begin_fit')
    learn('begin_batch')
    learn('after_fit')
    return learn.xb, learn.yb
#export
def model_summary(learn, data, find_all=False, print_mod=False):
    xb, yb = get_batch(data.valid_dl, learn)
    mods = find_modules(learn.model, is_lin_layer) if find_all else learn.model.children()
    f = lambda hook,mod,inp,out: print(f"===\n{mod}\n" if print_mod else "", out.shape)
    with Hooks(mods, f) as hooks: learn.model(xb)
learn = Learner(arch(), data, loss_func, opt_func, 1, cb_funcs=cbfs)
model_summary(learn, data, print_mod=False)
arch = partial(xresnet34, c_out=10)
learn = Learner(arch(), data, loss_func, opt_func, 1, cb_funcs=cbfs)
learn.fit(1, cbs=[LR_Find(), Recorder()])
learn.recorder.plot(10)
#export
def create_phases(phases):
    phases = listify(phases)
    return phases + [1 - sum(phases)]
print(create_phases(0.3))
print(create_phases([0.3, 0.2]))
lr = 1e-2
pct_start = 0.5
phases = create_phases(pct_start)
sched_lr = combine_scheds(phases, cos_1cycle_anneal(lr/10., lr, lr/1e5))
sched_mom = combine_scheds(phases, cos_1cycle_anneal(0.95, 0.85, 0.95))
cbsched = [
    ParamScheduler('lr', sched_lr),
    ParamScheduler('mom', sched_mom)
]
learn =  Learner(arch(), data, loss_func, opt_func, lr, cb_funcs=cbfs)
learn.fit(5, cbs=cbsched)<jupyter_output><empty_output><jupyter_text>cnn_learner<jupyter_code>#export
def cnn_learner(arch, data, loss_func, opt_func, lr=1e-2, c_in=None, c_out=None, progress=True, cuda=True, norm=None, mixup=0, xtra_cb=None, **kwargs):
    cbfs = [partial(AvgStatsCallback, accuracy)] + listify(xtra_cb)
    if progress: cbfs.append(ProgressCallback)
    if cuda: cbfs.append(CudaCallback)
    if norm: cbfs.append(partial(BatchTransformXCallback, norm))
    if mixup: cbfs.append(partial(MixUp, mixup))
    arch_args = {}
    if not c_in: c_in = data.c_in
    if not c_out: c_out = data.c_out
    if c_in: arch_args['c_in'] = c_in
    if c_out: arch_args['c_out'] = c_out
    
    return Learner(arch(**arch_args), data, loss_func, opt_func, lr, cb_funcs=cbfs, **kwargs)
learn = cnn_learner(xresnet34, data, loss_func, opt_func, norm=norm_imagenette)
learn.fit(5, cbsched)
!python notebook2script.py 11_train_imagenette.ipynb<jupyter_output>Converted 11_train_imagenette.ipynb to exp/nb_11.py
{'fraction_non_alphanumeric': 0.14908854166666666, 'fraction_numerical': 0.030436197916666668, 'mean_word_length': 4.261130136986301, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '299408', 'license': 'Apache-2.0', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2429, 'n_words': 504, 'path': 'nbs/dl2/selfmade/11_train_imagenette.ipynb', 'repo_name': 'Hustens0hn/fastai-course-v3', 'size': '6144'}