CNN viewer and navigator

convnav_supported_models[source]

convnav_supported_models()

Prints list of transfer learning models supported by fa_convnav

cndf_view[source]

cndf_view(df, verbose=3, truncate=0, tight=True, align_cols='left', top=False)

Display a valid CNDF dataframe df with optional arguments and styling

  • verbose: 1 = Index and Layer_name columns only; 2 = Model structure; 3 = Model Structure and layer_info (output dims, params and frozen/unfrozen); 4 = include layer_info and block/layer counts in container rows; 5 = expose hidden columns.
  • tight: True = tight layout with minimal space between rows (best for large models with many rows to display). False = display dataframe with normal row spacing. The default is True but less than 10 rows are automatically displayed with normal spacing.
  • truncate: truncate number displayed columns by integer value between -10 and 10. 0 = default. Negative values reveal hidden columns. Overidden when the verbose argument is set to a non-default setting.
  • align_cols: 'left' or 'right' alignment of column data
  • top: display first 10 rows only followed by a count of undisplayed rows

add_container_row_info[source]

add_container_row_info(df)

Add output dimensions and block/layer counts to container rows of df. These are not added when a CNDF dataframe is first built to avoid cluttering the display of larger dataframes.

cndf_search(df, searchterm, exact=True, show=True)

Search a CNDF dataframe, display the results in a dataframe and return matching module object(s)

Searchterm can be:

  • int : the module with Index number int is returned
  • float: module(s) where str(float) matches the Layer_name are returned
  • str: module(s) with str in one of 'Layer_name', 'Torch_class', 'Division', 'Module', 'Block', 'Layer_description' are returned. Columns are searched in this order with the search ending with the first column to make a match/matches.
  • dict, e.g. {'col', 'str'} matches str in column col

Searchterms can also be a combined as follows:

  • [101, 102, 105] logical OR of rows matching indexes 101, 102 plus 103
  • ('0.5', 'conv2d') logical AND of rows matching 0.5 in Layer_name and conv2d in Layer_description
  • {{'col1', 'str1'}, {'col2', 'str2'}} logical OR of matches str1 in col1 plus str2 in col'.

Return only exact matches between searchterm and column entry with exact = True (default).

.

%%capture
_, _, test_df = get_test_vars()
test_df['lyr_obj'] = None

test_eq(len(cndf_search(test_df, 12, )), 1)
test_eq(len(cndf_search(test_df, '0.6.1.conv2')), 1)
test_eq(len(cndf_search(test_df, 0.6, exact=False)), 16)
test_eq(len(cndf_search(test_df, {'Module_name': '0.6', 'Layer_description':'Conv2d'}, exact=True)), 1)
test_eq(len(cndf_search(test_df, ['0.6', '0.5'], exact=False)), 32)
test_eq(cndf_search(test_df, ('0.6', '0.5'), exact=False), None)

class ConvNav[source]

ConvNav(learner, learner_summary) :: CNDF

Builds a CNDF dataframe representation of a CNN model from a fastai Learner object and Learner.summary(). Provides methods to view, search and select model layers and modules for further investigation.

Fastai2 Learner objects are created from a dataloader, model and optimizer as decribed in the fastai documentation https://dev.fast.ai/ (also 03_examples00.ipynb and 04_examples01.ipynb). Run convnav_supported_models() from a notebook cell to see the list of supported models.

cn = ConvNav(Learner, Learner.summary()
test_learner, test_summary, _ = get_test_vars()
cn_test = ConvNav(test_learner, test_summary)

test_eq(type(cn_test._cndf), DataFrame) 
test_eq(len(cn_test._cndf), 79)             # rows
test_eq(len(cn_test._cndf.columns), 22)     # columns   

Search and view the CNDF dataframe.

ConvNav.search[source]

ConvNav.search(searchterm, **kwargs)

Find searchterm in instance dataframe, display the results and return matching module object(s). See cndf_search() for kwargs.

ConvNav.view[source]

ConvNav.view(**kwargs)

Display instance CNDF dataframe with optional arguments and styling (see cndf_view() for kwargs)

Additional methods:

ConvNav.head[source]

View module of model head

ConvNav.body[source]

View modules of model body

ConvNav.divs[source]

Summary info for model head and body

ConvNav.dim_transitions[source]

Finds layers with different input and output dimensions. These are useful points to apply hooks and callbacks for investigating model activity.

ConvNav.linear_layers[source]

Displays and returns all linear layers in the model

ConvNav.frozen[source]

ConvNav.frozen(col='child')

Displays and returns all frozen child container (col='child'), block container (col='block') or layers (col='layer').

ConvNav.unfrozen[source]

ConvNav.unfrozen(col='child')

Displays and returns all unfrozen child container (col='child'), block container (col='block') or layers (col='layer').

ConvNav.last_frozen[source]

ConvNav.last_frozen(col='child')

Displays and returns the last frozen child container (col='child'), block container (col='block') or layer (col='layer').

ConvNav.first_unfrozen[source]

ConvNav.first_unfrozen(col='child')

Displays and returns the first unfrozen child container (col='child'), block container (col='block') or layer (col='layer').

ConvNav.children[source]

Display and return child container modules (equivelent to fastai Learner.model.children())

ConvNav.blocks[source]

Display and return block container modules

ConvNav.find_conv[source]

ConvNav.find_conv(req='all', num=1, in_main=False)

Finds the first (req=First) or last (req=last) num Conv2d layers in the model body. Set in_main=True to return conv layers from the main body of the model only.

ConvNav.find_block[source]

ConvNav.find_block(b, layers=True, layers_only=False)

Finds, displays and returns container blocks by module name b

Searchterm b should be the block's module name from model.named_modules(). Preceeding '0.' are removed so '0.0.6.1', '0.6.1' and '6.1' are all equivelent and match Container_block 1 of Container_child 6. Block layers are included by default. To return the block container element only, set layers=False. To just get the block's layers, set layers_only=True.

ConvNav.spread[source]

ConvNav.spread(req='conv', num=5)

Returns num of equally spaced req elements over the model. req = 'conv', 'block', or 'child'

Spread is an ideal method for finding a small group of evenly spaced model elements (typically blocks) to use to investigate model activity, particularly for larger models (see 04_examples01.ipynb).

%%capture
test_learner, test_summary, _ = get_test_vars()
cn_test = ConvNav(test_learner, test_summary)

test_eq(len(cn_test.search(0.6, exact=False, show=False)), 16)
cn_test.view()
cn_test.head
cn_test.body
cn_test.divs 
test_eq(len(cn_test.linear_layers), 2)                                  
test_eq(len(cn_test.dim_transitions), 5)
test_eq(len(cn_test.find_block('0.5.1')), 6)
test_eq(len(cn_test.find_block('0.5.1', layers=False)), 1)
test_eq(len(cn_test.find_conv('first', 5)), 5)
test_eq(len(cn_test.children), 8)
test_eq(len(cn_test.blocks), 8)
test_eq(len(cn_test.spread('conv', 8)), 7)
del(cn_test)

Saving and loading CNDF dataframes.

cndf_save[source]

cndf_save(cn, filename, path='', with_modules=False)

Saves the instance dataframe cn._cndf to persistent storage at path with filename gzip compresseed

In native format, CNDF dataframes include the module objects in a 'lyr_obj' column and the combined size of the module objects can be quite large, 100-200mb for a complex model such as a densenet or xresnet. Thus, by default, module objects are removed from the dataframe before saving. To save the model objects as well, check you have enough space in the download location and set with_modules to True. Dataframes are gzip compressed.

cndf_load[source]

cndf_load(filename, path='')

Loads a CNDF dataframe from persistent storage at path+filename and unzips it.