Easiest way to get a layer's kernel size, stride, channels, etc...?

Like for example from “2d” layers like these:

Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)

Does PyTorch have any way to extract the information that I desire?

If you just want to have a look, you could just print them:

conv = nn.Conv2d(
    in_channels=100,
    out_channels=5,
    kernel_size=3,
    stride=1,
    padding=1,
    groups=1)
print(conv)
> Conv2d(100, 5, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

Otherwise if you need to access them, you can do it directly:

conv.in_channels
conv.out_channels
conv.kernel_size
conv.stride
conv.padding
conv.groups

Is this what you are looking for?

1 Like

Is this what you are looking for?

Yes! I was trying to print the layers in a way that more closely resembled Lua/Torch7’s printing, like this:

Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))

To:

Conv2d(3 -> 64, 3x3, 1,1, 1,1)

I had a complicated setup with replace and split, before using regex, but things still weren’t working. Hopefully I can make my code a lot more simple and easier to understand now!

ptrblck

Is there a way to get the kernel?

For example if I have:

layer = Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
W = layer.weight
print(W.size())

The result will be:

torch.Size([128, 128, 3, 3])

But I want to save the kernel at the end of training, so only the 3x3 weights to be able to initialize the layer using this kernel in another network with requires_grad=False

How can I do this?

Based on the setup of nn.Conv2d you would use 128 kernels each with a shape of [in_channels=128, height=3, width=3]. Your layer won’t have a single 3x3 kernel, so you should instead save the layer.weight tensor (or better save layer.state_dict()).

1 Like