# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# Put these at the top of every notebook, to get automatic reloading and inline plotting
# %reload_ext autoreload
# %autoreload 2
# %matplotlib inline


from fastai.conv_learner import *
PATH = "data/cifar10/"
os.makedirs(PATH,exist_ok=True)


classes = ('plane', 'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
stats = (np.array([ 0.4914 ,  0.48216,  0.44653]), np.array([ 0.24703,  0.24349,  0.26159]))


def get_data(sz,bs):
    tfms = tfms_from_stats(stats, sz, aug_tfms=[RandomFlip()], pad=sz//8)
    return ImageClassifierData.from_paths(PATH, val_name='test', tfms=tfms, bs=bs)


bs=128
data = get_data(32,4)
x,y=next(iter(data.trn_dl))
plt.imshow(data.trn_ds.denorm(x)[0]);


plt.imshow(data.trn_ds.denorm(x)[1]);


# +
from fastai.models.cifar10.resnext import resnext29_8_64

m = resnext29_8_64()
bm = BasicModel(m.cuda(), name='cifar10_rn29_8_64')

# -

data = get_data(8,bs*4)
learn = ConvLearner(data, bm)
learn.unfreeze()


lr=1e-2; wd=5e-4
learn.lr_find()


learn.sched.plot()


# %time learn.fit(lr, 1)


learn.fit(lr, 2, cycle_len=1)


learn.fit(lr, 3, cycle_len=1, cycle_mult=2, wds=wd)


learn.save('8x8_8')

learn.load('8x8_8')

learn.set_data(get_data(16,bs*2))

# %time learn.fit(1e-3, 1, wds=wd)

learn.unfreeze()


learn.lr_find()


learn.sched.plot()
lr=1e-2
learn.fit(lr, 2, cycle_len=1, wds=wd)
learn.fit(lr, 3, cycle_len=1, cycle_mult=2, wds=wd)
learn.save('16x16_8')


learn.load('16x16_8')












StarcoderdataJupyterScriptsDedupFiltered