File size: 809 Bytes
7e5ec66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
392c0b9
7e5ec66
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# import the needed dependencies for this py file
import torch
import torchvision

from torch import nn

#Turning train as a function as 1. setup, 2 getr transforms 3. setup model instance 4. Freeze base layers and adjust output layers
def create_vit_model(num_classes: int = 10,
                     seed: int = 24):
  # 1, 2, 3, Create ViT weights, transform and model
  weights = torchvision.models.ViT_B_16_Weights.DEFAULT
  transforms = weights.transforms()
  model = torchvision.models.vit_b_16(weights=weights)

  # 4. Freeze the base layers
  for param in model.parameters():
    param.requires_grad = False

  # 5. Adjust the number of heads(output)
  torch.manual_seed(seed)
  model.heads = nn.Sequential(nn.Linear(in_features=768, out_features=num_classes, bias=True))

  return model, transforms