Core functionality for fa_convnav

find_model[source]

find_model(n)

Returns tuple of model type and name (e.g. ('resnet', 'resnet50')) given n, the number of named_modules in Learner.model.named_modules()

test_eq(find_model(162), ('resnet', 'resnet50'))
test_eq(find_model(585), ('densenet', 'densenet161'))

get_row[source]

get_row(l, m)

Construct dataframe row from l (Learner.named_modules() module) and m (model_type)

test_learner, _, _ = get_test_vars()
module5 = list(test_learner.model.named_modules())[5]
module_last = list(test_learner.model.named_modules())[-1]

test_eq(get_row(module5, 'resnet')['Module_name'], '0.3')
test_eq(get_row(module_last, 'resnet')['Layer_description'], 'Linear(in_features=512, out_features=37, bias=False)')

Utilities

supported_models[source]

supported_models()

Prints list of models supported by fa_convnav (models.ipynb contains list of currently supported models).

get_inp_sz[source]

get_inp_sz(infos)

Slice first row of infos to give string representation of model input sizes

test_eq(get_inp_sz(["Sequential (Input shape: ['128 x 3 x 224 x 224'])"]), "128 x 3 x 224 x 224" )

infos_to_gen[source]

infos_to_gen(infos)

Slice the remaining rows of infos to give the layers, m, output dimensions o, parameters p, and trainable t of each layer and return (m,o,p,t) for all layers in a generator

_, test_summary, _ = get_test_vars()
gen = infos_to_gen(test_summary.split('\n'))
test_eq(next(gen), ('Conv2d', '[128 x 64 x 112 x 11]', '9,408', 'False'))

class CNDF[source]

CNDF(learner:any, learner_summary:str)

Compile information from fastai Learner.model and 'layer_info(Learner)` into a dataframe

CNDF class builds a dataframe using a fastai Learner and the output of Learner.summary() method. The resulting dataframe, called a CNDF dataframe, combines information from both sources and represents both the structure and information of Learner.model.

CNDF is a parent class of ConvNav and a CNDF dataframe is automatically built when a new ConvNav object is instantiated and for most use cases CNDF dataframes should be built this way. However, if there is a need to build a CNDF dataframe in isolation use:

cndf = CNDF(Learner, Learner.summary())
test_learner, test_summary, _ = get_test_vars()
cndf_test = CNDF(test_learner, test_summary)

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

CNDF.cndf[source]

CNDF.cndf(with_modules=False)

Returns a ConvNav dataframe

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. Set with_modules = True to include module objects. in returned dataframe.

CNDF.batch_size[source]

Returns the batch size of the current learner

CNDF.input_sizes[source]

Returns the sizes (dimensions bs, ch, h, w) of the model)

CNDF.output_dimensions[source]

Returns output dimensions of model (bs, classes)

CNDF.model_info[source]

Return an info string derived fromLearner.model

CNDF.layer_params[source]

CNDF.layer_params(idx)

Returns the parameters of layer with Index idx

CNDF.layer_dims[source]

CNDF.layer_dims(idx)

Return tuple of input and output dimensions (as strings) of layer with index idx

Element selectors

get_layer[source]

get_layer(df, idx)

Returns layer with Index idx from CNDF dataframe df or None if idx indexes a container element and an exception if idx is invalid.

_, _, cndf_test = get_test_vars()
test_eq(get_layer(cndf_test, 2)['Module_name'], '0.0')
test_eq(get_layer(cndf_test, 1), None)