Spaces:
Running
Running
Download model.py from febryanpk/FoodVision: direct link, hf CLI and curl.
- Browser
- Download file 819 Bytes
-
https://huggingface.co/spaces/febryanpk/FoodVision/resolve/main/model.py
- Command line
-
hf download hf://spaces/febryanpk/FoodVision/model.py
-
curl -L -o model.py https://huggingface.co/spaces/febryanpk/FoodVision/resolve/main/model.py
819 Bytes
| import torch | |
| import torchvision | |
| from torch import nn | |
| def create_vit(pretrained_weights: torchvision.models.Weights, | |
| model: torchvision.models, | |
| in_features: int, | |
| out_features: int, | |
| device: torch.device): | |
| """Creates a Vision Transformer (ViT) instance from torchvision | |
| and returns it. | |
| """ | |
| # Create a pretrained ViT model | |
| model = torchvision.models.vit_b_16(weights=pretrained_weights).to(device) | |
| transforms = pretrained_weights.transforms() | |
| # Freeze the feature extractor | |
| for param in model.parameters(): | |
| param.requires_grad = False | |
| # Change the head of the ViT | |
| model.heads = nn.Sequential( | |
| nn.Linear(in_features=in_features, out_features=out_features) | |
| ).to(device) | |
| return model, transforms | |