Download src/kernel_transfer/models.py from Diveshj/sorghum-kernel-transfer: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/Diveshj/sorghum-kernel-transfer/resolve/main/src/kernel_transfer/models.py
- Command line
-
hf download hf://Diveshj/sorghum-kernel-transfer/src/kernel_transfer/models.py
-
curl -L -o models.py https://huggingface.co/Diveshj/sorghum-kernel-transfer/resolve/main/src/kernel_transfer/models.py
1.34 kB
| """The model families used in the paper, each built with an eight-class output layer. | |
| We only need the architecture here: the trained GrainSet weights are loaded afterwards from a | |
| checkpoint file (see scripts/01_finetune.py). So `pretrained=False` is the default. | |
| """ | |
| import timm | |
| import torch.nn as nn | |
| from torchvision import models | |
| N_CLASSES = 8 | |
| def build_model(name: str, n_classes: int = N_CLASSES, pretrained: bool = False) -> nn.Module: | |
| """name is one of: vit_small, convnextv2_tiny, efficientnet_b0, mobilenetv2.""" | |
| if name == "vit_small": | |
| return timm.create_model("vit_small_patch16_224", pretrained=pretrained, num_classes=n_classes) | |
| if name == "convnextv2_tiny": | |
| return timm.create_model("convnextv2_tiny.fcmae_ft_in22k_in1k", pretrained=pretrained, num_classes=n_classes) | |
| if name == "efficientnet_b0": | |
| model = models.efficientnet_b0(weights="IMAGENET1K_V1" if pretrained else None) | |
| model.classifier[-1] = nn.Linear(model.classifier[-1].in_features, n_classes) # new 8-class output | |
| return model | |
| if name == "mobilenetv2": | |
| model = models.mobilenet_v2(weights="IMAGENET1K_V1" if pretrained else None) | |
| model.classifier[-1] = nn.Linear(model.classifier[-1].in_features, n_classes) | |
| return model | |
| raise ValueError(f"unknown model name: {name}") | |