Hey,
The code seems ok to me(training loop).
why don’t you first freeze the feature extractor of the densenet and train only the classifier? If that works then you can tune the features too ? After you declare the model
for param in model_ft.features.parameters():
    param.requires_grad = False

Now in the optimizer just train the classifier by modifying optimizer to train only classifier parameters.
optimizer = optim.SGD(filter(lambda p: p.requires_grad=True, model_ft.parameters()), lr=0.001)

Also evaluate for fewer iterations to see if it is learning. If this works better then I would suggest you to train classifier with same learning rate but feature extractor with lower lr like 5e-4 or 1e-4 may be ?