Spaces:
Runtime error
Runtime error
Update model.py
Browse files
model.py
CHANGED
@@ -3,22 +3,32 @@ import torchvision
|
|
3 |
|
4 |
from torch import nn
|
5 |
|
6 |
-
def create_effnetb2_model(num_classes:int=3, # default output classes = 3 (pizza, steak, sushi)
|
7 |
-
seed:int=42):
|
8 |
-
# 1, 2, 3 Create EffNetB2 pretrained weights, transforms and model
|
9 |
-
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
10 |
-
transforms = weights.transforms()
|
11 |
-
model = torchvision.models.efficientnet_b2(weights=weights)
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
nn.Dropout(p=0.3, inplace=True),
|
21 |
-
nn.Linear(in_features=1408, out_features=num_classes)
|
22 |
-
)
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
from torch import nn
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
def create_effnetb2_model(num_classes:int=3,
|
8 |
+
seed:int=42):
|
9 |
+
"""Creates an EfficientNetB2 feature extractor model and transforms.
|
10 |
+
Args:
|
11 |
+
num_classes (int, optional): number of classes in the classifier head.
|
12 |
+
Defaults to 3.
|
13 |
+
seed (int, optional): random seed value. Defaults to 42.
|
14 |
+
Returns:
|
15 |
+
model (torch.nn.Module): EffNetB2 feature extractor model.
|
16 |
+
transforms (torchvision.transforms): EffNetB2 image transforms.
|
17 |
+
"""
|
18 |
+
# Create EffNetB2 pretrained weights, transforms and model
|
19 |
+
weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
|
20 |
+
transforms = weights.transforms()
|
21 |
+
model = torchvision.models.efficientnet_b2(weights=weights)
|
22 |
|
23 |
+
# Freeze all layers in base model
|
24 |
+
for param in model.parameters():
|
25 |
+
param.requires_grad = False
|
|
|
|
|
|
|
26 |
|
27 |
+
# Change classifier head with random seed for reproducibility
|
28 |
+
torch.manual_seed(seed)
|
29 |
+
model.classifier = nn.Sequential(
|
30 |
+
nn.Dropout(p=0.3, inplace=True),
|
31 |
+
nn.Linear(in_features=1408, out_features=num_classes),
|
32 |
+
)
|
33 |
+
|
34 |
+
return model, transforms
|