Get torch.nn.modules attributes

Hi, guys.

So… I’m building a script to convert weights and configuration files from a model. And to do that, I’d like to access the attributes list from the modules. For example:

for name, m in model.named_modules():
t = type(m)
if t is torch.nn.modules.conv.Conv2d:
print(m.in_channels)
print(m.out_channels)
print(m.kernel_size)
print(m.stride)
print(m.dilation)

In that case, above, I defined the attributes to the Conv2d manually. But if the next module is models.common.Conv?

I’d like to know if is it possible to have access to this list?

Tks!

vars or dir might be useful in this case:

conv = nn.Conv2d(3, 6, 3, 1, 1)
attrs = vars(conv)
print(', '.join("{}: {}".format(item[0], item[1]) for item in attrs.items()))
print(dir(conv))

Tks! It works for me.