Spaces:
Sleeping
Sleeping
create model.py
Browse files
model.py
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
|
5 |
+
from torch import nn
|
6 |
+
|
7 |
+
def create_effnetb2_model(num_classes: int=3,
|
8 |
+
seed: int =42):
|
9 |
+
|
10 |
+
#weights, transforms and model instance
|
11 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
12 |
+
transforms = weights.transforms()
|
13 |
+
|
14 |
+
model = torchvision.models.efficientnet_b2(weights= weights)
|
15 |
+
|
16 |
+
# freeze all layers of base model
|
17 |
+
for param in model.parameters():
|
18 |
+
param.requires_grad = False # i.e don't keep track of gradients
|
19 |
+
|
20 |
+
# change classifier head
|
21 |
+
torch.manual_seed(seed)
|
22 |
+
model.classifier = nn.Sequential(
|
23 |
+
nn.Dropout(p=0.3, inplace=True),
|
24 |
+
nn.Linear(in_features=1408, out_features= num_classes)
|
25 |
+
)
|
26 |
+
|
27 |
+
return model,transforms
|