For a pretrained model such as densenet161, how can I access one of the conv2d layers inside one of the dense blocks?
You could directly access this layer, e.g. via:
model = models.densenet161()
model.features.denseblock4.denselayer21.conv2
Generally, you could print the model architecture and use the specified module names to get the wanted layer.