Model Visualization

Hi all,

Is there way we can visualize model layers/structure in fastai library? I found that tracking the code to draw model structure it is really time consuming. I found a topic about this but it’s for Keras, while in fastai, model is created with PyTorch.

You can check here Visualizing your network in PyTorch

For printing the content of your model just type learn.model

2 Likes

I don’t really use the fastai library but a more lightweight library of my own (https://github.com/hollance/Ignition). It has some functions for printing information about the model that you might find useful.

The parameter sizes of the layers in the model:

>>> print_parameter_sizes(net)

Parameter                      | Size                     | Count        | Train?
conv1.conv.weight              | 16 × 3 × 3 × 3           |          432 | Yes   
conv1.conv.bias                | 16                       |           16 | Yes   
conv1.bn.weight                | 16                       |           16 | Yes   
conv1.bn.bias                  | 16                       |           16 | Yes   
conv2.conv.weight              | 32 × 16 × 3 × 3          |         4608 | Yes   
conv2.conv.bias                | 32                       |           32 | Yes   
conv2.bn.weight                | 32                       |           32 | Yes   
conv2.bn.bias                  | 32                       |           32 | Yes   
conv3.conv.weight              | 64 × 32 × 3 × 3          |        18432 | Yes   
conv3.conv.bias                | 64                       |           64 | Yes   
conv3.bn.weight                | 64                       |           64 | Yes   
conv3.bn.bias                  | 64                       |           64 | Yes   
fc1.weight                     | 128 × 1024               |       131072 | Yes   
fc1.bias                       | 128                      |          128 | Yes   
fc2.weight                     | 10 × 128                 |         1280 | Yes   
fc2.bias                       | 10                       |           10 | Yes   
Total params: 156,298

The sizes of the feature maps for a given input:

>>> print_activation_sizes(net, (1, 3, 32, 32))

Module                         | Input Size               | Output Size             
conv1                          | 1 × 3 × 32 × 32          | 1 × 16 × 32 × 32        
conv2                          | 1 × 16 × 16 × 16         | 1 × 32 × 16 × 16        
conv3                          | 1 × 32 × 8 × 8           | 1 × 64 × 8 × 8          
fc1                            | 1 × 1024                 | 1 × 128                 
fc2                            | 1 × 128                  | 1 × 10                  

You can see this example in the CIFAR10 notebook.

You should be able to use this code on fastai models too, since they’re PyTorch models.

2 Likes

Thanks for your help. Just check it and found what I need.