processoptimisationsystem commited on
Commit
30b5c8e
·
verified ·
1 Parent(s): 7ef6fe5

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +16 -8
src/streamlit_app.py CHANGED
@@ -7,6 +7,12 @@ forums](https://discuss.streamlit.io).
7
 
8
  In the meantime, below is an example of what you can do with just a few lines of code:
9
  """
 
 
 
 
 
 
10
 
11
  import streamlit as st
12
  import torch
@@ -24,9 +30,11 @@ from lime.lime_image import LimeImageExplainer
24
  from skimage.segmentation import mark_boundaries
25
  import shap
26
  from shap import GradientExplainer
 
27
  device = "cuda" if torch.cuda.is_available() else "cpu"
28
  num_classes = 4
29
  image_size = (224, 224)
 
30
  # Define CNN Model
31
  class MyModel(nn.Module):
32
  def __init__(self, num_classes=4):
@@ -36,27 +44,22 @@ class MyModel(nn.Module):
36
  nn.BatchNorm2d(64),
37
  nn.ReLU(inplace=True),
38
  nn.MaxPool2d(kernel_size=2, stride=2),
39
-
40
  nn.Conv2d(64, 128, kernel_size=3, padding=1),
41
  nn.BatchNorm2d(128),
42
  nn.ReLU(inplace=True),
43
  nn.MaxPool2d(kernel_size=2, stride=2),
44
-
45
  nn.Conv2d(128, 128, kernel_size=3, padding=1),
46
  nn.BatchNorm2d(128),
47
  nn.ReLU(inplace=True),
48
  nn.MaxPool2d(kernel_size=2, stride=2),
49
-
50
  nn.Conv2d(128, 256, kernel_size=3, padding=1),
51
  nn.BatchNorm2d(256),
52
  nn.ReLU(inplace=True),
53
  nn.MaxPool2d(kernel_size=2, stride=2),
54
-
55
  nn.Conv2d(256, 256, kernel_size=3, padding=1),
56
  nn.BatchNorm2d(256),
57
  nn.ReLU(inplace=True),
58
  nn.MaxPool2d(kernel_size=2, stride=2),
59
-
60
  nn.Conv2d(256, 512, kernel_size=3, padding=1),
61
  nn.BatchNorm2d(512),
62
  nn.ReLU(inplace=True),
@@ -67,21 +70,26 @@ class MyModel(nn.Module):
67
  nn.Linear(512 * 3 * 3, 1024),
68
  nn.ReLU(inplace=True),
69
  nn.Dropout(0.25),
70
-
71
  nn.Linear(1024, 512),
72
  nn.ReLU(inplace=True),
73
  nn.Dropout(0.25),
74
-
75
  nn.Linear(512, num_classes)
76
  )
77
  def forward(self, x):
78
  x = self.features(x)
79
  x = self.classifier(x)
80
  return x
 
81
  # Load model
82
  model = MyModel(num_classes=num_classes).to(device)
83
- model.load_state_dict(torch.load("src/brainCNNpytorch_model", map_location=torch.device('cpu')))
 
 
 
 
 
84
  model.eval()
 
85
  # Label dictionary
86
  label_dict = {0: "Meningioma", 1: "Glioma", 2: "No Tumor", 3: "Pituitary"}
87
  # Preprocessing
 
7
 
8
  In the meantime, below is an example of what you can do with just a few lines of code:
9
  """
10
+ import os
11
+
12
+ # Fix for Hugging Face permission error and torch watcher bug
13
+ os.environ["STREAMLIT_HOME"] = "./.streamlit"
14
+ os.environ["STREAMLIT_WATCH_DISABLE"] = "true"
15
+ os.makedirs("./.streamlit", exist_ok=True)
16
 
17
  import streamlit as st
18
  import torch
 
30
  from skimage.segmentation import mark_boundaries
31
  import shap
32
  from shap import GradientExplainer
33
+
34
  device = "cuda" if torch.cuda.is_available() else "cpu"
35
  num_classes = 4
36
  image_size = (224, 224)
37
+
38
  # Define CNN Model
39
  class MyModel(nn.Module):
40
  def __init__(self, num_classes=4):
 
44
  nn.BatchNorm2d(64),
45
  nn.ReLU(inplace=True),
46
  nn.MaxPool2d(kernel_size=2, stride=2),
 
47
  nn.Conv2d(64, 128, kernel_size=3, padding=1),
48
  nn.BatchNorm2d(128),
49
  nn.ReLU(inplace=True),
50
  nn.MaxPool2d(kernel_size=2, stride=2),
 
51
  nn.Conv2d(128, 128, kernel_size=3, padding=1),
52
  nn.BatchNorm2d(128),
53
  nn.ReLU(inplace=True),
54
  nn.MaxPool2d(kernel_size=2, stride=2),
 
55
  nn.Conv2d(128, 256, kernel_size=3, padding=1),
56
  nn.BatchNorm2d(256),
57
  nn.ReLU(inplace=True),
58
  nn.MaxPool2d(kernel_size=2, stride=2),
 
59
  nn.Conv2d(256, 256, kernel_size=3, padding=1),
60
  nn.BatchNorm2d(256),
61
  nn.ReLU(inplace=True),
62
  nn.MaxPool2d(kernel_size=2, stride=2),
 
63
  nn.Conv2d(256, 512, kernel_size=3, padding=1),
64
  nn.BatchNorm2d(512),
65
  nn.ReLU(inplace=True),
 
70
  nn.Linear(512 * 3 * 3, 1024),
71
  nn.ReLU(inplace=True),
72
  nn.Dropout(0.25),
 
73
  nn.Linear(1024, 512),
74
  nn.ReLU(inplace=True),
75
  nn.Dropout(0.25),
 
76
  nn.Linear(512, num_classes)
77
  )
78
  def forward(self, x):
79
  x = self.features(x)
80
  x = self.classifier(x)
81
  return x
82
+
83
  # Load model
84
  model = MyModel(num_classes=num_classes).to(device)
85
+ try:
86
+ model.load_state_dict(torch.load("brainCNNpytorch_model", map_location=torch.device('cpu')))
87
+ except FileNotFoundError:
88
+ st.error("Model file 'brainCNNpytorch_model' not found. Please upload the file correctly.")
89
+ st.stop()
90
+
91
  model.eval()
92
+
93
  # Label dictionary
94
  label_dict = {0: "Meningioma", 1: "Glioma", 2: "No Tumor", 3: "Pituitary"}
95
  # Preprocessing