How do you determine the layer type?

I want to iterate through the children() of a module,
and identify all the convolutional layers (for instance), or maybe all the maxpool layers, to do something with them.

How can I determine the type of layer?

My code would be something like this:

for layer in net.children():
    if layer is a conv layer:  # ??? how do I do this ???
        do something with the layer

Thanks!

1 Like

Do you plan to treat Conv1d, Conv2d and so far as different? If you were only looking for Conv2d layers you can do something like:

for layer in net.children():
    if isinstance(layer, nn.Conv2d):
        do something with the layer

isinstance is a Python built-in https://docs.python.org/3/library/functions.html#isinstance

8 Likes

Great! Thanks!

That’s good enough for me, for now. I just need to distinguish between Conv2d and MaxPool2d.

Thank you!!

1 Like

Happy to help :slight_smile:

How can I check whether a layer is Conv2d or not for this kind of ResNet structure?
Please help.

ResNet(
(conv1): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(layer1): Sequential(
(0): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(shortcut): Sequential()
)
(1): BasicBlock(
(conv1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(conv2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
(bn2): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(shortcut): Sequential()
)
)

1 Like

The code of @reachtarunhere should work, since net.children() will be called recursively on all submodules.

But it is not working, it can find only if the 1st layer is conv or not. I guess its mostly because for the BasicBlocks the nns are not directly having Conv2d as its children directly. So, the code is failing. Any suggestion?

1 Like

Right, I was mistaken.
In that case, model.modules() or model.named_modules() should work. :wink:

2 Likes

Thanks, it worked fine! Is there any way I can specifically choose the shortcut layer conv2ds only?

I was searching for batchnorm layer and


model = models.densenet161()
for child in model.children():
  for layer in child.modules():
    if(isinstance(layer,torch.nn.modules.batchnorm.BatchNorm2d)):
      print(layer)

This code worked for me.

2 Likes

Thank you for the answer. How can I select the first conv layer of the ResNet model?

This should work:

model = models.resnet18()
print(model.conv1)

Thank you. it works:)

I want to decompose each Conv layer other than the first one in the ResNet.How can I re-assign the decomposed layers to its position?What is the equivalent of model.features._modules[module] of vgg in ResNet?

The architectures are a bit different for these models.
You can print out the layout using:

model = models.resnet18()
print(model)
print(model.layer1)

and check all submodules.

I got it in the following way. But do we have a better way to do it ? here net impies the model.

for n, m in net.named_children():
num_children = sum(1 for i in m.children())
if num_children != 0:
# in a layer of resnet
layer = getattr(net, n)
# decomp every bottleneck
for i in range(num_children):
BasicBlock = layer[i]
conv2 = getattr(BasicBlock, ‘conv2’)
#print(conv2)
decompose = function_call
# s += count_params(conv2)
setattr(BasicBlock, ‘conv2’,decompose)
#print(decompose)
conv1 = getattr(BasicBlock, ‘conv1’)
#print(conv1)
decompose = function_call
# s += count_params(conv2)
setattr(BasicBlock, ‘conv1’,decompose)
conv = getattr(BasicBlock, ‘downsample’)
if(conv) :
c = getattr(conv, ‘0’)
decompose = function_call
setattr(conv, ‘0’,decompose)

How to determine layer type from a model saved with torch.jit.
When I attempt to access modules, it returns RecursiveScriptModule(original_name=Linear) instead of Linear.