actually I replaced these two lines and seems the training is happening but I am not sure if replacing resnet50 with densenet161 would only need these two line replacements:

###num_ftrs = model_ft.fc.in_features
num_ftrs = model_ft.classifier.in_features
###model_ft.fc = nn.Linear(num_ftrs, 9)
model_ft.classifier = nn.Linear(num_ftrs, 9)