File size: 507 Bytes
4b1b927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

import torchvision
import torch
def create_vit_b_16_model(num_classes=3):

  weights = torchvision.models.ViT_B_16_Weights.DEFAULT
  transform = torchvision.models.ViT_B_16_Weights.DEFAULT.transforms()
  model = torchvision.models.vit_b_16(weights=weights)

  # freeze the layers
  for param in model.parameters():
    param.requires_grad = False

  # modify the heads layer
  model.heads = torch.nn.Sequential(
      torch.nn.Linear(in_features=768,out_features=num_classes)
  )

  return model,transform