GerryRaz commited on
Commit
3dd8ed4
·
verified ·
1 Parent(s): 5cf318f

first commit

Browse files
Files changed (4) hide show
  1. app.py +74 -0
  2. efficientnet_mri_model.pth +3 -0
  3. model.py +22 -0
  4. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ import os
4
+ import torch
5
+
6
+ from demos.model import create_model
7
+ from timeit import default_timer as timer
8
+
9
+
10
+ # Setup Class Names
11
+ class_names = [
12
+ "glioma",
13
+ "meningioma",
14
+ "notumor",
15
+ "pituitary"
16
+ ]
17
+
18
+ # Create efficientnet model
19
+ efficient_model, transforms = create_model(num_classes=len(class_names))
20
+
21
+ # Load saved weights for MRI classifier
22
+ state_dict = torch.load(
23
+ f="/content/demos/efficientnet_mri_model.pth",
24
+ weights_only=False,
25
+ map_location="cpu"
26
+ )
27
+
28
+ efficient_model.load_state_dict(state_dict())
29
+
30
+ def predict_img(img):
31
+
32
+ # Start Timer
33
+ start_time = timer()
34
+
35
+ # Transform image and add extra dimension
36
+ img = transforms(img).unsqueeze(0)
37
+
38
+ # Put the model on eval and inference mode
39
+ efficient_model.eval()
40
+ with torch.inference_mode():
41
+
42
+ # Pass the transformed image through the model.
43
+ pred_probs = torch.softmax(efficient_model(img), dim=1)
44
+
45
+ # Pred Labels and Probabilities required in gradio format
46
+ pred_labels_and_probs = {class_names[i] : float(pred_probs[0][i]) for i in range(len(class_names))}
47
+
48
+ # Calculate the total time
49
+ pred_time = round(timer() - start_time, 5)
50
+
51
+ return pred_labels_and_probs, pred_time
52
+
53
+ # Ddfine the title, desc and article
54
+ title = "Dr. Gerry MRI result classified"
55
+ desc = "An Efficientnet model feature extractor to classify MRI images"
56
+ article = "Created @ Mauaque Resettlement Center Gonzales Compound"
57
+
58
+ # Create example list
59
+ path_list = '/content/demos/examples'
60
+ example_list = os.listdir(path_list)
61
+
62
+ # Initialized Gradio Demo
63
+ demo = gr.Interface(
64
+ fn=predict_img,
65
+ inputs=gr.Image(type="pil"),
66
+ outputs=[gr.Label(num_top_classes=4,label="Predictions"),
67
+ gr.Number(label="Prediction's time")],
68
+ examples=example_list,
69
+ title=title,
70
+ description=desc,
71
+ article=article
72
+ )
73
+
74
+ demo.launch()
efficientnet_mri_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0fbe8aef55563e04f1af41ee2e66ea3d13b7f3ad108823c73a35310392c2722b
3
+ size 31365477
model.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torchvision
3
+ import torch
4
+
5
+ def create_model(
6
+ num_classes: int=4,
7
+ seed: int=42):
8
+
9
+
10
+ weights = torchvision.models.EfficientNet_B2_Weights.DEFAULT
11
+ transform = weights.transforms()
12
+ model = torchvision.models.efficientnet_b2(weights=weights).to("cpu")
13
+
14
+ for params in model.parameters():
15
+ params.requires_grad = False
16
+
17
+ model.classifier = torch.nn.Sequential(
18
+ torch.nn.Dropout(p=0.2,inplace=True),
19
+ torch.nn.Linear(1408,num_classes)
20
+ )
21
+
22
+ return model, transform
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==2.10.0
2
+ torchvision==0.25.0
3
+ gradio==5.50.0