Spaces:
Runtime error
Runtime error
upload model_builder.py
Browse files- model_builder.py +50 -0
model_builder.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
contains pytorch model code to instantiate a TinyVGG model.
|
| 3 |
+
"""
|
| 4 |
+
import torch
|
| 5 |
+
from torch import nn
|
| 6 |
+
import torchvision
|
| 7 |
+
|
| 8 |
+
def create_model_baseline_effnetb0(out_feats: int, device: torch.device = None) -> torch.nn.Module:
|
| 9 |
+
weights = torchvision.models.EfficientNet_B0_Weights.DEFAULT
|
| 10 |
+
model = torchvision.models.efficientnet_b0(weights=weights).to(device)
|
| 11 |
+
|
| 12 |
+
for param in model.features.parameters():
|
| 13 |
+
param.requires_grad = False
|
| 14 |
+
|
| 15 |
+
torch.manual_seed(42)
|
| 16 |
+
torch.cuda.manual_seed(42)
|
| 17 |
+
|
| 18 |
+
# change the output layer
|
| 19 |
+
model.classifier = torch.nn.Sequential(
|
| 20 |
+
torch.nn.Dropout(p=0.2, inplace=True),
|
| 21 |
+
torch.nn.Linear(in_features=1280,
|
| 22 |
+
out_features=out_feats,
|
| 23 |
+
bias=True)).to(device)
|
| 24 |
+
|
| 25 |
+
model.name = "effnetb0"
|
| 26 |
+
print(f"[INFO] created a model {model.name}")
|
| 27 |
+
|
| 28 |
+
return model
|
| 29 |
+
|
| 30 |
+
def create_model_baseline_effnetb2(out_feats: int, device: torch.device = None) -> torch.nn.Module:
|
| 31 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
| 32 |
+
model = torchvision.models.efficientnet_b2(weights=weights).to(device)
|
| 33 |
+
|
| 34 |
+
for param in model.features.parameters():
|
| 35 |
+
param.requires_grad = False
|
| 36 |
+
|
| 37 |
+
torch.manual_seed(42)
|
| 38 |
+
torch.cuda.manual_seed(42)
|
| 39 |
+
|
| 40 |
+
model.classifier = nn.Sequential(
|
| 41 |
+
nn.Dropout(p=0.3, inplace=True),
|
| 42 |
+
nn.Linear(in_features=1408,
|
| 43 |
+
out_features=out_feats,
|
| 44 |
+
bias=True)
|
| 45 |
+
).to(device)
|
| 46 |
+
|
| 47 |
+
model.name = "effnetb2"
|
| 48 |
+
print(f"[INFO] created a model {model.name}")
|
| 49 |
+
|
| 50 |
+
return model
|