Transfer Learning With Swint

I’m trying to do transfer learning with swin_t. I ran the following code to hopefully freeze the layers and add an output one like this:

    weights = Swin_T_Weights.DEFAULT
    model = swin_t(weights=weights)

    for param in model.parameters():
        param.requires_grad = False

     model.fc = nn.Linear(768, 37)
     model =

However when I try training I get the following error
“Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn”

Not sure what’s wrong. Resnet50 worked fine using the same code.

Would the following work?

      for m in model.modules():
          if isinstance(m, nn.Linear):
              m.requires_grad_ = False

swin_t does not have a .fc attribute and you are thus creating it as a new and unused attribute:

model = torchvision.models.swin_t()
AttributeError: 'SwinTransformer' object has no attribute 'fc'

model.fc = nn.Linear(768, 37)
# Linear(in_features=768, out_features=37, bias=True)

I guess you might want to replace model.head.