I’m trying to insert residual connection in my small 3 conv-layer.
Here’s what it looks like -
class ResClassifier(nn.Module):
def __init__(self, num_classes):
super(ResClassifier( self).__init__()
self.block1 = self.conv_block(c_in=1, c_out=16, dropout=0.1, kernel_size=5, stride=1, padding=2)
self.block2 = self.conv_block(c_in=16, c_out=32, dropout=0.1, kernel_size=3, stride=1, padding=1)
self.block3 = self.conv_block(c_in=32, c_out=64, dropout=0.1, kernel_size=3, stride=1, padding=1)
self.lastcnn = nn.Conv2d(in_channels=64, out_channels=num_classes, kernel_size=28, stride=1, padding=0)
def forward(self, x):
x = self.block1(x)
x = self.block2(x)
x = self.block3(x)
x = self.lastcnn(x)
return x
def conv_block(self, c_in, c_out, dropout, **kwargs):
seq_block = nn.Sequential(
nn.Conv2d(in_channels=c_in, out_channels=c_out, **kwargs),
nn.BatchNorm2d(num_features=c_out),
nn.ReLU(),
nn.Dropout2d(p=dropout)
)
return seq_block
Let’s say I want to add skip connection from block-1
to block-3
. To do that, I can simply do -
def forward(self, x):
residual_1 = x
x = self.block1(x)
x = self.block2(x)
x += residual_1
x = self.block3(x)
x = self.lastcnn(x)
return x
But the issue is, output of block-3
has 64 channels while the input (residual_1
) has only 1.
Because of this mismatch, I can’t add the residual connection. How do I increase the number of channels in residual_1
before I can add it to the output of block-3
.