Aziizzz commited on
Commit
adfe243
·
1 Parent(s): 9426fec
Files changed (2) hide show
  1. app.py +36 -1
  2. model.py +0 -36
app.py CHANGED
@@ -3,9 +3,44 @@ import gradio as gr
3
  import os
4
  import torch
5
 
6
- from model import create_effnetb2_model
7
  from timeit import default_timer as timer
8
  from typing import Tuple, Dict
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  # Setup class names
11
  class_names = ["Normal", "Pneumonia"]
 
3
  import os
4
  import torch
5
 
 
6
  from timeit import default_timer as timer
7
  from typing import Tuple, Dict
8
+ import torchvision
9
+
10
+ from torch import nn
11
+
12
+
13
+ def create_effnetb2_model(num_classes: int = 1,
14
+ seed: int = 42):
15
+ """Creates an EfficientNetB2 feature extractor model and transforms.
16
+
17
+ Args:
18
+ num_classes (int, optional): number of classes in the classifier head.
19
+ Defaults to 3.
20
+ seed (int, optional): random seed value. Defaults to 42.
21
+
22
+ Returns:
23
+ model (torch.nn.Module): EffNetB2 feature extractor model.
24
+ transforms (torchvision.transforms): EffNetB2 image transforms.
25
+ """
26
+ # Create EffNetB2 pretrained weights, transforms and model
27
+ weights = torchvision.models.AlexNet_Weights.DEFAULT
28
+ transforms = weights.transforms()
29
+ model = torchvision.models.alexnet(weights=weights)
30
+
31
+ # Freeze all layers in base model
32
+ for param in model.parameters():
33
+ param.requires_grad = False
34
+
35
+ # Change classifier head with random seed for reproducibility
36
+ torch.manual_seed(seed)
37
+ model.classifier = nn.Sequential(
38
+ nn.Dropout(p=0.2,),
39
+ nn.Linear(in_features=9216, out_features=1),
40
+ )
41
+
42
+ return model, transforms
43
+
44
 
45
  # Setup class names
46
  class_names = ["Normal", "Pneumonia"]
model.py DELETED
@@ -1,36 +0,0 @@
1
- import torch
2
- import torchvision
3
-
4
- from torch import nn
5
-
6
-
7
- def create_effnetb2_model(num_classes: int = 1,
8
- seed: int = 42):
9
- """Creates an EfficientNetB2 feature extractor model and transforms.
10
-
11
- Args:
12
- num_classes (int, optional): number of classes in the classifier head.
13
- Defaults to 3.
14
- seed (int, optional): random seed value. Defaults to 42.
15
-
16
- Returns:
17
- model (torch.nn.Module): EffNetB2 feature extractor model.
18
- transforms (torchvision.transforms): EffNetB2 image transforms.
19
- """
20
- # Create EffNetB2 pretrained weights, transforms and model
21
- weights = torchvision.models.AlexNet_Weights.DEFAULT
22
- transforms = weights.transforms()
23
- model = torchvision.models.alexNet(weights=weights)
24
-
25
- # Freeze all layers in base model
26
- for param in model.parameters():
27
- param.requires_grad = False
28
-
29
- # Change classifier head with random seed for reproducibility
30
- torch.manual_seed(seed)
31
- model.classifier = nn.Sequential(
32
- nn.Dropout(p=0.2,),
33
- nn.Linear(in_features=9216, out_features=1),
34
- )
35
-
36
- return model, transforms