You can add a customized classifier as follows:

Check the architecture of your model, in this case it is a Densenet-161. Printing it yields and displaying here the last layers:
…
)
(denselayer24): _DenseLayer(
(norm1): BatchNorm2d(2160, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu1): ReLU(inplace=True)
(conv1): Conv2d(2160, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)
(norm2): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(relu2): ReLU(inplace=True)
(conv2): Conv2d(192, 48, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)
)
)
(norm5): BatchNorm2d(2208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(classifier): Linear(in_features=2208, out_features=1000, bias=True)
)

So you can see that “norm5” has an output size of 2208, that is the output size of the Densenet-161 without classifier.

You can verifiy the number features (equals output size) by

num_ftrs = model_transfer.classifier.in_features
num_ftrs

This prints again 2208.

Now you can  add your classifier to the network:

model_transfer.classifier = nn.Sequential(
                        nn.Linear(num_ftrs, 256),  
                        nn.ReLU(), 
                        nn.Dropout(0.4),
                        nn.Linear(256, n_classes),                   
                        nn.Softmax(dim=1))


Verify by printing model with

model_transfer

yields:
…
(norm5): BatchNorm2d(2208, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
)
(classifier): Sequential(
(0): Linear(in_features=2208, out_features=256, bias=True)
(1): ReLU()
(2): Dropout(p=0.4, inplace=False)
(3): Linear(in_features=256, out_features=133, bias=True)
(4): LogSoftmax()
)
)
Now you are ready to use your own Densenet-161!