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)
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()])
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…?
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:
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.