Spaces:
Sleeping
Sleeping
| # 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 | |