Merge two convolution layers

I wanted to try out changing the resnet code in pytorch and experimenting a bit with it… So I was going to try using different types of kernels in the convolution layer. For now I’m only trying it with dilated and normal kernels.

Original code in resnet in pytorch.vision for making a conv2d layer:

def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation)

I want to do something like this

def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
    """3x3 convolution with padding"""
    l1 = nn.Conv2d(in_planes, out_planes//2, kernel_size=3, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation)
    l2 = nn.Conv2d(in_planes, out_planes - out_planes//2, kernel_size=3, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation+1)

    l3 =  torch.stack([l1, l2], dim=0)
    print(l3)
    return l3

But torch.stack expects 2 tensors and therefore wont be able to merge the two convolution layers. So is there any way I can merge them? I dont want to change the interface resnet uses, that is not change any other part of the resnet code.

Thanks a lot

Hi Narang,

You will need to study Module, init, and forward to construct your new first layer. Layers cannot be stacked or concatenated, but their output activations can be.

HTH, Malcolm

So in the end I would have to make my own version of a convolutional layer?

class conv3x3(nn.Module):
  def __init__(self, in_planes, out_planes, stride=1, groups=1, dilation=1):
    """3x3 convolution with padding"""
    super().__init__()
    self.l1 = nn.Conv2d(in_planes, out_planes//2, kernel_size=3, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation)
    self.l2 = nn.Conv2d(in_planes, out_planes - out_planes//2, kernel_size=3, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation+1)

  def forward(self, x):
    l1 = self.l1(x)
    l2 = self.l2(x)
    l3 =  torch.stack([l1, l2], dim=1) # you don't want to stack along the batch (not dim=0)
    print(l3)
    return l3

I haven’t tried it, but you should do something like this (I also had not take a look if nn.Conv2d is all right or not as the question was not about that part.)

Oh this seems pretty good. Thanks a lot :v:

1 Like

Welcome, hope you can make it work.