Here is the correct code for Transfer learning with densenet
dset_classes_number = len(class_names)
model_ft = models.densenet121(pretrained=True)
model_ft.classifier = nn.Linear(1024, dset_classes_number)