Import fastai deep learning library including pretrained vision models.
from fastai2.basics import *
from fastai2.callback.all import *
from fastai2.vision.all import *
from torch import torch
Import the fa_convnav.navigator module
from fa_convnav.navigator import *
Create a fastai datablock and dataloader using the Oxford PetsII dataset (included with fastai install), and apply some simple image transforms in the process.
pets = DataBlock(blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(),
get_y=RegexLabeller(pat = r'/([^/]+)_\d+.jpg$'),
item_tfms=Resize(460),
batch_tfms=[*aug_transforms(size=224, max_rotate=30, min_scale=0.75), Normalize.from_stats(*imagenet_stats)])
dls = pets.dataloaders(untar_data(URLs.PETS)/"images", bs=128)
Download the pretrained model we want to use
model = resnet18
Create a fastai Learner object from the dataloader, the chosen model, an optimiser. We will use the error rate as our metric.
learn = cnn_learner(
dls,
model,
opt_func=partial(Adam, lr=slice(3e-3), wd=0.01, eps=1e-8),
metrics=error_rate,
config=cnn_config(ps=0.33)).to_fp16()
The model is ready to be trained but, now we have a Learner, it can also be viewed in detail using fa_convnav. Create a ConvNav instance.
cn = ConvNav(learn, learn.summary())
Creating an instance automatically builds a datraframe representation of the model (a CNDF dataframe). This step can take a few moments, especially for larger models such as densenets or xresnets which have hundreds of modules. If you have more than one Learner in your project, instantiate a separate ConvNav instance for each one.
Once the dataframe is built we can view, search and select from it. For example, print some summary information:
print(cn.model_info)
Examine summary information about the body and head of the model.
cn.divs
View the whole CNDF dataframe. For brevity, only the first ten modules are displayed here, but set top = False
(or remove) and all 79 rows of the dataframe will be shown. Run the notebook and try it!
cn.view(top=True)
We can examine the layers of the head only.
cn.head
Select a single block of modules.
block = cn.search('0.4.0')
Look at just those layers with dimension change between input and outputs.
layers = cn.dim_transitions
When selections are made, the selected modules are shown in a dataframe (above) but the corresponding module objects are returned to the user in a list (below). Module objects can be used to apply Pytorch hooks and fastai callbacks to further investigate model function.
layers
Or select a number of blocks evenly spaced over the model. This, and many of the fa_convnav selection methods, are trivial on a small model like vgg or resnet18 but come into their own when used with large, complex models such as densenets and xresnets where there may be many dozens of blocks and hundreds of layers.
spread = cn.spread('block', 4)
Selected blocks objects are also returned for further model investigation.
for b in spread:
print(f'{b}\n')
Finally CNDF dataframes can be saved and retrieved from persistenmt storage (see documentation). This allows you to work with a CNDF dataframe without the need to create it again from scratch every time.