I am guessing you want to make densnet161 take greyscale images and predict 22 classes right?
Why does it say



 Oscar_Rangel:

model_ft.classifier


and not model.classifier ? Do you have a second model called model_ft or is this a typo?
The original densenet161.classifier layer looked like this (classifier): Linear(in_features=2208, out_features=1000, bias=True).
It had 2208 in_features which you changed to 1024 (assuming model_ft is a typo or model_ft is also densenet161). Is this intentional?
You might have a size mismatch here.
You can probably debug your issue by printing your model using print(model) or alternatively print the passed tensors shapes on different locations using print(x.shape).