DHEIVER commited on
Commit
ae2057d
1 Parent(s): 5f07f33

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -52
app.py CHANGED
@@ -1,53 +1,36 @@
 
 
1
  import gradio as gr
2
- import torch
3
- from torchvision import transforms
4
-
5
- # Load the model
6
- model = torch.hub.load('pytorch/vision:v0.9.0', 'resnet101', pretrained=True)
7
- model.eval()
8
-
9
- # Define class names
10
- class_names = ["normal", "pneumonia"]
11
-
12
- # Define preprocessing function
13
- preprocess = transforms.Compose([
14
- transforms.Resize(256),
15
- transforms.CenterCrop(224),
16
- transforms.ToTensor(),
17
- transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
18
- ])
19
-
20
- # Define predict function
21
- def predict(img):
22
- # Preprocess the image
23
- img = preprocess(img)
24
-
25
- # Add batch dimension
26
- img = img.unsqueeze(0)
27
-
28
- # Make prediction
29
- with torch.no_grad():
30
- outputs = model(img)
31
- _, predicted_idx = torch.max(outputs, 1)
32
- confidence = torch.nn.functional.softmax(outputs, dim=1)[0]
33
-
34
- # Format prediction results
35
- prediction = {
36
- class_names[0]: float(confidence[0]),
37
- class_names[1]: float(confidence[1])
38
- }
39
-
40
- return prediction
41
-
42
- # Create the Gradio interface
43
- iface = gr.Interface(
44
- fn=predict,
45
- inputs=gr.inputs.Image(type="pil", label="Input Image"),
46
- outputs=gr.outputs.Label(num_top_classes=2, label="Predicted Class"),
47
- title="Pneumonia Detector 馃憗",
48
- description="A ResNet101 computer vision model to detect pneumonia",
49
- article="Please add a chest X-Ray image"
50
- )
51
-
52
- # Launch the interface
53
- iface.launch()
 
1
+ import tensorflow as tf
2
+ import keras
3
  import gradio as gr
4
+ import numpy as np
5
+ import cv2
6
+ import os
7
+
8
+ model=tf.keras.models.load_model('model.h5')
9
+
10
+ def predict_pneumonia(image):
11
+ resized_img = cv2.resize(image, (180, 180))
12
+ img_array = np.array(resized_img).reshape((1, 180, 180, 3))
13
+
14
+ prediction = model.predict(img_array)[0][0]
15
+ pneumonia_percent = prediction*1
16
+ normal_percent = (1 - prediction)*1
17
+ return {"Pneumonia ": pneumonia_percent, "Normal ": normal_percent}
18
+
19
+ inputs = gr.inputs.Image(shape=(180, 180))
20
+ outputs = gr.outputs.Label(num_top_classes=2)
21
+ gradio_interface = gr.Interface(fn=predict_pneumonia, inputs=inputs, outputs=outputs,
22
+ title="Classification of pneumonia in chest X-ray",
23
+ #description = "A simple app to classify chest X-ray images into normal and pneumonia and show the percentage of each",
24
+ examples = ["person1946_bacteria_4875.jpeg", "person1952_bacteria_4883.jpeg", "NORMAL2-IM-1427-0001.jpeg", "NORMAL2-IM-1431-0001.jpeg"],
25
+ article = "<p style='text-align: center'>Lior Cohen & Arad Peleg | Final Project 2023</p>"
26
+ "<p style='text-align: center'>Supervisor: Dr. Dima Alberg</p>",
27
+ theme = gr.themes.Monochrome(),)
28
+
29
+ # gr.themes.Soft() 讻讞讜诇
30
+ # gr.themes.Monochrome() 砖讞讜专
31
+ # gr.themes.Glass() 讗驻讜专
32
+
33
+ gradio_interface.launch()
34
+ # share=True
35
+ # live=True
36
+ # enable_queue=True