# ---
# 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
# ---

# %reload_ext autoreload
# %autoreload 2
# %matplotlib inline

# ## 1.

from fastai.conv_learner import *
from fastai.models import darknet
from pathlib import Path

PATH = Path('data/imagenet')
PATH_TRAIN = PATH/'train'

# +
# sz = 256
# bs = 32
# darknet53 = darknet.darknet_53()
# tfms = tfms_from_model(darknet53, sz)
# model_data = ImageClassifierData.from_paths(PATH, bs=bs, tfms=tfms, val_name='train')
# learner = ConvLearner.from_model_data(darknet53, model_data)
# -

learner.crit

model_data

learner.crit

learner.crit = F.cross_entropy

learner.crit

# + [markdown] heading_collapsed=true
# ## 2.

# + [markdown] hidden=true
# Checking behavior of `.pretrained` in fastai/conv_learner.py. Looking at number of FC layers at the end of a stock PyTorch model, compared to loading through FastAI:

# + hidden=true
from torchvision.models import resnet18

# + hidden=true
resnet18

# + hidden=true
resnet18 = resnet18()

# + hidden=true
# resnet18

# + [markdown] hidden=true
# 1 linear layer at the end.

# + hidden=true
from fastai.conv_learner import *

# + hidden=true
resnet18

# + [markdown] hidden=true
# What fastai originally imports is the resnet18 constructor function from torchvision.models.resnet. Good to know.

# + hidden=true
learner = ConvLearner.from_model_data(resnet18(), model_data)

# + hidden=true
# learner

# + hidden=true
from pathlib import Path
PATH = Path('data/cifar10')

# + hidden=true
sz = 32
bs = 64
tfms = tfms_from_model(resnet18, sz)
model_data = ImageClassifierData.from_paths(PATH, bs, tfms=tfms, val_name='test')

# + hidden=true
learner = ConvLearner.pretrained(resnet18, model_data)

# + hidden=true
# learner

# + [markdown] hidden=true
# Right. fastai strips the FC layer and its associated pooling layers, and replaces them with adaptive pooling and 2 linear layers with batchNorm and dropout; with a LogSoftmax output layer.

# + hidden=true
from fastai.models import darknet

# + hidden=true
darknet.darknet_53

# + hidden=true
tfms = tfms_from_stats(imagenet_stats, sz)
model_data = ImageClassifierData.from_paths(PATH, bs=bs, tfms=tfms, val_name='test')

darknet53 = darknet.darknet_53

# + hidden=true
# learner = ConvLearner.from_model_data(darknet53(num_classes=10), model_data)
# learner

# + hidden=true
# learner = ConvLearner.pretrained(darknet53(num_classes=10), model_data)
# learner

# + [markdown] heading_collapsed=true
# ## 3.

# + hidden=true
# %matplotlib inline
# %reload_ext autoreload
# %autoreload 2
from fastai.conv_learner import *
from fastai.models import darknet
from pathlib import Path
PATH = Path('data/imagenet')
PATH_TRAIN = PATH/'train'

# + hidden=true
bs = 32
sz = 256

tfms = tfms_from_stats(imagenet_stats, sz)
model_data = ImageClassifierData.from_paths(PATH, bs=bs, tfms=tfms)

darknet53 = darknet.darknet_53

# + [markdown] hidden=true
# `.from_model_data`

# + hidden=true
learner = ConvLearner.from_model_data(darknet53(), model_data)

# + hidden=true
# learner.summary()

# + [markdown] hidden=true
# head:
# ```
#              ('AdaptiveAvgPool2d-232',
#               OrderedDict([('input_shape', [-1, 1024, 14, 14]),
#                            ('output_shape', [-1, 1024, 1, 1]),
#                            ('nb_params', 0)])),
#              ('Flatten-233',
#               OrderedDict([('input_shape', [-1, 1024, 1, 1]),
#                            ('output_shape', [-1, 1024]),
#                            ('nb_params', 0)])),
#              ('Linear-234',
#               OrderedDict([('input_shape', [-1, 1024]),
#                            ('output_shape', [-1, 1000]),
#                            ('trainable', True),
#                            ('nb_params', 1025000)]))])
# ```

# + [markdown] hidden=true
# learner with NLL loss fails Learning-Rate Finder phase due to negative losses:

# + hidden=true
learner.lr_find()
learner.sched.plot()

# + hidden=true
learner.crit

# + [markdown] hidden=true
# `.pretrained`

# + hidden=true
learner = ConvLearner.pretrained(darknet53(), model_data)

# + hidden=true
learner.summary()

# + hidden=true
learner.lr_find()
learner.sched.plot()
# -

# ## Testing Fix
#
# Testing addition of LogSoftmax layer to fastai.models.darknet.Darknet definition

from pathlib import Path
from fastai.conv_learner import *
from fastai.models import darknet

# +
PATH = Path('data/imagenet')

sz = 256; bs=32

tfms = tfms_from_stats(imagenet_stats, sz)
model_data = ImageClassifierData.from_paths(PATH, bs=bs, tfms=tfms, val_name='train')

darknet53 = darknet.darknet_53
# -

learner = ConvLearner.from_model_data(darknet53(), model_data)

# +
# learner.summary()
# -

learner.lr_find()
learner.sched.plot()

learner.summary()

learner.crit











StarcoderdataJupyterScriptsDedupFiltered