Get the input channels for the conv2d from previous layer?

Hi guys,
I was wondering if there are many convolutional layers (conv1 --> conv2 ). How can we get the input channels parameter for the conv2 from the conv1 output channel?

class MyModel(nn.Module):
  def __init__(self, in_ch, num_features, out_ch2):
    super(MyModel, self).__init__()
    self.conv1 = nn.Conv2D(in_channels,num_features)
    self.conv2 = nn.Conv2D(in_channnels_from_out_channels_of_conv1,out_ch2)

Can I get the out_channels from the conv1 layer and use it as in_ch for conv2?

Yes, you can directly access this property via:


For your code snippet, this should work:

self.conv1 = nn.Conv2D(in_channels,num_features)
self.conv2 = nn.Conv2D(self.conv1.out_channels,out_ch2)
1 Like

@ptrblck Thanks, I dont think I framed my question correctly earlier.

Actually what I am looking for is something this

# Class for a single Block
class SingleBlock(nn.Module):

  def __init__(self, in_channels, num_channels, drop_out=0.2):
    super(SingleBlock, self).__init__()

    self.conv = conv3X3(in_channels, num_channels)
#    self.outchannels = self.conv.out_channels

# def get_out_channels(self):
#   return _outchannels
  def forward(self, x):
    concat = x
    out = self.conv(out)
    out =,concat), axis=1)

    ## Only while Q.C. to see output shape
    # out = self.lrelu(out)

    return out

# Q. C. single SingleBlock
qc_toggle = 1
if qc_toggle == 1:
  # print the model summary (Not exactly great)
  temp_block = DenseBlock(3,32)

When I make this network I want to access the out_channels in this (after the so that I can use it in some other place.

Can I do some get function or is there any property to get the out_channels in this network?