Can I change the channel for a specific Conv2d inside a network?

Hello world!

Just wondering if there is any way to change the channel of given network’s specific convolution layer.

I tried

for index, (name, module) in enumerate(model.named_modules()):
    if 'conv' in name:
        print(module) # nn.Conv2d(3, 32, 3 , 3)... something like this
        getattr(model, name) = nn.Conv2d(3,3,3,3)

but it won’t work
any suggestions??

Hi,

In this post I wrote a function that renames a name of module without changing the order of it. You can use it in reverse mode, instead of changing name of a module, change the module of a name!


from collections import OrderedDict
model = # your model
new_layer = # your desired layer
model.__dict__['_modules'] = OrderedDict([(k, new_layer) if k == 'name_of_layer_to_change' else (k, v) for k, v in model.__dict__['_modules'].items()])

Bests

2 Likes

hello!
thanks for your answer!
I greatly appreciate it

However I’m still unsure how I should go ahead and code this
below is the network I would like to change.

With your code, if I understood correctly, I must make a model that contains information on changed input and output channels but with a same model architecture that I want to change with…?

Could you give me an example code on it?

thanks a ton!

Let’s say I want to change _bn0 to something else.

from collections import OrderedDict
new_layer = nn.BatchNorm2d(32, eps=0.5)
model.__dict__['_modules'] = OrderedDict([(k, new_layer) if k == '_bn0' else (k, v) for k, v in model.__dict__['_modules'].items()])

But this code need to be changed to accept recursion to change hierarchical layers. In that case, I prefer manual change rather than coding it:
For instance, let’s say we want to change static_padding in first MBConvBlock:

model._blocks[0]._depthwise_conv.static_padding = nn.ZeroPad2d(padding=(2, 2, 2, 2), value=1.1313)

I think there should be better ways as there are different methods to read modules as you have mentioned. But I do not think there is any builtin method for such a case, so you need to program it. If you came up with a code that can generalize this behavior, would be great if you could share.

Bests

1 Like

I will!
Thanks a lot!