I’m using the MobileNet v3 model from torchvision but I want to change the padding of every convolutional layer to be causal in the “width” dimension. To do this, I might need to insert nn.ZeroPad
right before every convolutional layer. For example, I want to go from
nn.Conv2d(1, 3, kernel_size=3, padding=1)
to something like
nn.ZeroPad2d((2, 0, 1, 1))
nn.Conv2d(1, 3, kernel_size=3)
Edit: What I’ve tried doing is
def adjust_model(model):
for child_name, child in model.named_children():
if isinstance(child, nn.Conv2d):
padding = (2 * child.padding[1], 0,
child.padding[0], child.padding[0])
child.padding = (0, 0)
setattr(model, "CausalConv2d", nn.Sequential(
nn.ZeroPad2d(padding),
child
))
else:
adjust_model(child)
but returns an error saying
RuntimeError: OrderedDict mutated during iteration