How to print a model after load it?

Mata_Fu: <p>It confused me because in torch you can directly print the loaded model. But in pytorch I just saw parameters inside.</p>

SimonW: <p><code>print(model)</code> should just work.</p>

Mata_Fu: <p>It looks like a dictionary in python and print can just show the key/value rather than the structure of net.</p>

ptrblck: <p>Try <a href="https://github.com/szagoruyko/functional-zoo/blob/master/visualize.py" rel="nofollow noopener">this script</a> from szagoruyko</p>

richard: <p>There’s also a summary view of a model in the works: <a href="https://github.com/pytorch/pytorch/issues/2001" rel="nofollow noopener">https://github.com/pytorch/pytorch/issues/2001</a></p>

ptrblck: <p>Nice! Didn’t know that. Seems to be quite useful <img src="//discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

sksq96: <p>Yes, you can get exact Keras representation, using this <a href="https://github.com/sksq96/pytorch-summary" rel="nofollow noopener">code</a>.</p>
<h3>Example for VGG16</h3>
<pre><code>from torchvision import models
from summary import summary

vgg = models.vgg16()
summary(vgg, (3, 224, 224))

----------------------------------------------------------------
        Layer (type)               Output Shpae         Param #
================================================================
            Conv2d-1         [-1, 64, 224, 224]            1792
              ReLU-2         [-1, 64, 224, 224]               0
            Conv2d-3         [-1, 64, 224, 224]           36928
              ReLU-4         [-1, 64, 224, 224]               0
         MaxPool2d-5         [-1, 64, 112, 112]               0
            Conv2d-6        [-1, 128, 112, 112]           73856
              ReLU-7        [-1, 128, 112, 112]               0
            Conv2d-8        [-1, 128, 112, 112]          147584
              ReLU-9        [-1, 128, 112, 112]               0
        MaxPool2d-10          [-1, 128, 56, 56]               0
           Conv2d-11          [-1, 256, 56, 56]          295168
             ReLU-12          [-1, 256, 56, 56]               0
           Conv2d-13          [-1, 256, 56, 56]          590080
             ReLU-14          [-1, 256, 56, 56]               0
           Conv2d-15          [-1, 256, 56, 56]          590080
             ReLU-16          [-1, 256, 56, 56]               0
        MaxPool2d-17          [-1, 256, 28, 28]               0
           Conv2d-18          [-1, 512, 28, 28]         1180160
             ReLU-19          [-1, 512, 28, 28]               0
           Conv2d-20          [-1, 512, 28, 28]         2359808
             ReLU-21          [-1, 512, 28, 28]               0
           Conv2d-22          [-1, 512, 28, 28]         2359808
             ReLU-23          [-1, 512, 28, 28]               0
        MaxPool2d-24          [-1, 512, 14, 14]               0
           Conv2d-25          [-1, 512, 14, 14]         2359808
             ReLU-26          [-1, 512, 14, 14]               0
           Conv2d-27          [-1, 512, 14, 14]         2359808
             ReLU-28          [-1, 512, 14, 14]               0
           Conv2d-29          [-1, 512, 14, 14]         2359808
             ReLU-30          [-1, 512, 14, 14]               0
        MaxPool2d-31            [-1, 512, 7, 7]               0
           Linear-32                 [-1, 4096]       102764544
             ReLU-33                 [-1, 4096]               0
          Dropout-34                 [-1, 4096]               0
           Linear-35                 [-1, 4096]        16781312
             ReLU-36                 [-1, 4096]               0
          Dropout-37                 [-1, 4096]               0
           Linear-38                 [-1, 1000]         4097000
================================================================
Total params: 138357544
Trainable params: 138357544
Non-trainable params: 0
----------------------------------------------------------------</code></pre>

Mata_Fu: <p>Thx. But how about the net with multiple inputs?<br>
It looks that this code can’t work with more than one input.</p>

HuynhLam: <p><strong>torchsummary</strong> also works with more than one input. Here is the snippet from <a class="mention" href="/u/sksq96">@sksq96</a></p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
from torchsummary import summary

class SimpleConv(nn.Module):
    def __init__(self):
        super(SimpleConv, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(1, 1, kernel_size=3, stride=1, padding=1),
            nn.ReLU(),
        )

    def forward(self, x, y):
        x1 = self.features(x)
        x2 = self.features(y)
        return x1, x2
    
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = SimpleConv().to(device)

summary(model, [(1, 16, 16), (1, 28, 28)])
</code></pre>
<p>Hope it helps.</p>

angelinaG: <aside class="quote no-group" data-username="sksq96" data-post="7" data-topic="9879">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/sksq96/48/13282_2.png" class="avatar"> sksq96:</div>
<blockquote>
<p>from torchvision import models from summary import summary vgg = models.vgg16() summary(vgg, (3, 224, 224))</p>
</blockquote>
</aside>
<p>Thank you very much! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>

angelinaG: <p>Thank you very much <a class="mention" href="/u/huynhlam">@HuynhLam</a>!</p>

angelinaG: <p>Thank you very much <a class="mention" href="/u/sksq96">@sksq96</a></p>

Janani_Sundara_gandh: <p>Is there any way to print each layer’s parameters like stride, kernel size, padding from the torch model architecture?</p>

ptrblck: <p>These arguments are only defined for some layers, so you would need to filter them out e.g. via:</p>
<pre><code class="lang-python">for name, module in model.named_modules():
    if isinstance(module, nn.Conv2d):
        print(name, module.kernel_size, module.stride, ...)
</code></pre>

akt42: <p>Seems like the up to date library is <a href="https://github.com/TylerYep/torchinfo" rel="noopener nofollow ugc">torchinfo</a>.</p>