kkhushisaid commited on
Commit
1d4913b
·
verified ·
1 Parent(s): e694d56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -147
app.py CHANGED
@@ -1,148 +1,36 @@
1
- import torch
2
- import torch.nn as nn
3
- from torchvision import transforms as T
4
- import os
5
  import gradio as gr
6
-
7
- #################################
8
- # Define problem parameters
9
- #################################
10
-
11
- class config:
12
-
13
- img_size = 224
14
-
15
- pn_mean = [0.4752, 0.4752, 0.4752] # Pneumonia dataset mean
16
- pn_std = [0.2234, 0.2234, 0.2234] # Pneumonia dataset std
17
-
18
- class_names = ["Normal", "Pneumonia"]
19
-
20
-
21
- device = torch.device('cpu')
22
-
23
- print(f"device: {device}")
24
-
25
- #######################################
26
- # Define image transformation pipeline
27
- #######################################
28
-
29
- class Gray2RGB:
30
- def __call__(self, image):
31
- if image.shape[0] == 3:
32
- return image
33
- else:
34
- return image.repeat(3, 1, 1) # Repeat the single channel across 3 channels to convert to RGB
35
-
36
- test_transform_custom = T.Compose([
37
- T.Resize(size=(config.img_size, config.img_size)),
38
- T.ToTensor(),
39
- Gray2RGB(),
40
- T.Normalize(config.pn_mean, config.pn_std),
41
- ])
42
-
43
-
44
- #################################
45
- # Define model architecture
46
- #################################
47
-
48
- class ConvolutionalNetwork(nn.Module):
49
- def __init__(self):
50
- super().__init__()
51
-
52
- self.conv1 = nn.Sequential(
53
- nn.Conv2d(3, 8, 3, stride=1, padding=1),
54
- nn.ReLU(inplace=True),
55
- nn.BatchNorm2d(8),
56
- nn.MaxPool2d(2,2))
57
-
58
- self.conv2 = nn.Sequential(
59
- nn.Conv2d(8, 16, 3, stride=1, padding=1),
60
- nn.ReLU(inplace=True),
61
- nn.BatchNorm2d(16),
62
- nn.MaxPool2d(2,2))
63
-
64
- self.conv3 = nn.Sequential(
65
- nn.Conv2d(16, 32, 3, stride=1, padding=1),
66
- nn.ReLU(inplace=True),
67
- nn.BatchNorm2d(32),
68
- nn.MaxPool2d(2,2))
69
-
70
- self.conv4 = nn.Sequential(
71
- nn.Conv2d(32, 64, 3, stride=1, padding=1),
72
- nn.ReLU(inplace=True),
73
- nn.BatchNorm2d(64),
74
- nn.MaxPool2d(2,2))
75
-
76
- self.conv5 = nn.Sequential(
77
- nn.Conv2d(64, 128, 3, stride=1, padding=1),
78
- nn.ReLU(inplace=True),
79
- nn.BatchNorm2d(128),
80
- nn.MaxPool2d(2,2))
81
-
82
- self.fc = nn.Sequential(
83
- nn.Linear(128*7*7, 512),
84
- nn.ReLU(inplace=True),
85
- nn.BatchNorm1d(512),
86
- nn.Dropout(0.5),
87
- nn.Linear(512, 2))
88
-
89
- def forward(self, x):
90
- x = self.conv1(x)
91
- x = self.conv2(x)
92
- x = self.conv3(x)
93
- x = self.conv4(x)
94
- x = self.conv5(x)
95
- x = x.view(x.shape[0], -1)
96
- x = self.fc(x)
97
- return x
98
-
99
-
100
- cnn_model = ConvolutionalNetwork()
101
-
102
- cnn_model.to(device)
103
-
104
- status = cnn_model.load_state_dict(torch.load('pneumonia_cnn_model.pt', map_location=device, weights_only=True))
105
- print(f"Status: {status}")
106
-
107
- #################################
108
- # Define the prediction fucntion
109
- #################################
110
-
111
- def predict(image):
112
- """Transforms and performs a prediction on an image and returns the prediction dictionary."""
113
-
114
- image = test_transform_custom(image).unsqueeze(0)
115
-
116
- cnn_model.eval()
117
- with torch.no_grad():
118
- pred_probs = torch.softmax(cnn_model(image), dim=1)
119
-
120
- # Create a prediction probability dictionary for each prediction class
121
- pred_dict = {config.class_names[i]: float(pred_probs[0][i]) for i in range(len(config.class_names))}
122
-
123
- # Return the prediction dictionary
124
- return pred_dict
125
-
126
- ##########################
127
- # Create the Gradio demo
128
- ##########################
129
-
130
- title = "Pneumonia Detection"
131
-
132
- description = """This is a pneumonia detection model that uses a custom convolutional neural network to predict whether an image contains pneumonia or not. \
133
- GitHub project can be accessed [here](https://github.com/mma666/Pneumonia-Detection-Computer-Vision).
134
- """
135
-
136
- # Create examples list from "examples/" directory
137
- example_list = [["examples/" + example] for example in os.listdir("examples")]
138
-
139
- # Create the Gradio demo
140
- demo = gr.Interface(fn=predict,
141
- inputs=[gr.Image(label="Upload image", type="pil", height=320, width=320)],
142
- outputs=[gr.Label(num_top_classes=2, label="Predictions")],
143
- examples=example_list,
144
- title=title,
145
- description=description,
146
- cache_examples=False)
147
-
148
- demo.launch()
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import tensorflow as tf
4
+ from tensorflow.keras.models import load_model
5
+ from tensorflow.keras.preprocessing import image
6
+
7
+ # Load the trained model
8
+ model = load_model("pneumonia_cnn_model.h5", custom_objects={
9
+ 'f1': lambda y_true, y_pred: 2*((precision(y_true, y_pred)*recall(y_true, y_pred)) / (precision(y_true, y_pred)+recall(y_true, y_pred)+tf.keras.backend.epsilon())),
10
+ 'precision': precision,
11
+ 'recall': recall
12
+ })
13
+
14
+ # Preprocessing function
15
+ def predict_pneumonia(img):
16
+ img = img.convert('L') # convert to grayscale
17
+ img = img.resize((299, 299))
18
+ img_array = image.img_to_array(img)
19
+ img_array = img_array / 255.0
20
+ img_array = np.expand_dims(img_array, axis=0) # batch dimension
21
+ prediction = model.predict(img_array)[0][0]
22
+
23
+ if prediction >= 0.5:
24
+ return "PNEUMONIA 🫁 (Confidence: {:.2f}%)".format(prediction * 100)
25
+ else:
26
+ return "NORMAL ✅ (Confidence: {:.2f}%)".format((1 - prediction) * 100)
27
+
28
+ # Gradio interface
29
+ interface = gr.Interface(fn=predict_pneumonia,
30
+ inputs=gr.Image(type="pil"),
31
+ outputs="text",
32
+ title="Pneumonia Detection from Chest X-rays",
33
+ description="Upload a chest X-ray image to detect Pneumonia using a CNN model.")
34
+
35
+ if __name__ == "__main__":
36
+ interface.launch()