How do I insert a layer to a pretrained model?

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
1 Like

Solved it using the function below

def adjust_model(model):
    for child_name, child in model.named_children():
        if isinstance(child, nn.Conv2d):
            causal_padding = (2 * child.padding[1], 0, child.padding[0],
                child.padding[0])
            child.padding = (0, 0)
            setattr(model, child_name, nn.Sequential(
                nn.ZeroPad2d(causal_padding),
                child
            ))
        else:
            adjust_model(child)
1 Like

You can simply keep adding layers in a sequential model just by calling add method. The other is functional API, which lets you create more complex models that might contain multiple input and output.

www.dgcustomerfirst.com