MedMagik commited on
Commit
7cbb030
·
verified ·
1 Parent(s): fb0ca3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -7
app.py CHANGED
@@ -5,9 +5,45 @@ import gradio as gr
5
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
6
  from tensorflow.keras.applications.densenet import preprocess_input, decode_predictions
7
  import numpy as np
 
8
  model = load_model('Densenet.h5')
9
  model.load_weights("pretrained_model.h5")
10
  class_names = ['Cardiomegaly', 'Emphysema', 'Effusion', 'Hernia', 'Infiltration', 'Mass', 'Nodule', 'Atelectasis', 'Pneumothorax', 'Pleural_Thickening', 'Pneumonia', 'Fibrosis', 'Edema', 'Consolidation']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  def custom_decode_predictions(predictions, class_labels):
12
 
13
  decoded_predictions = []
@@ -23,18 +59,22 @@ def classify_image(img):
23
  img_array = img_to_array(img)
24
  img_array = np.expand_dims(img_array, axis=0)
25
  img_array = preprocess_input(img_array)
26
- predictions = model.predict(img_array)
27
- decoded_predictions = custom_decode_predictions(predictions, class_names)
28
- return decoded_predictions
29
 
 
 
 
 
 
 
 
30
  # Gradio interface
31
  iface = gr.Interface(
32
  fn=classify_image,
33
  inputs="image",
34
- outputs="text",
35
- title="Xray Classification - KIMS",
36
- description="Classify cxr into one of 20 classes - Atelectasis, Cardiomegaly, Consolidation, Edema, Effusion, Emphysema, Fibrosis, Hernia, Infiltration, Mass, Nodule, Pleural Thickening, Pneumonia, Pneumothorax, Pneumoperitoneum, Pneumomediastinum, Subcutaneous Emphysema, Tortuous Aorta, Calcification of the Aorta, No Finding. Built by Dr Sai and Dr Ajavindu",
37
- theme = "dark")
38
 
39
  # Launch the interface
40
  iface.launch(inline = False)
 
5
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
6
  from tensorflow.keras.applications.densenet import preprocess_input, decode_predictions
7
  import numpy as np
8
+ from PIL import Image
9
  model = load_model('Densenet.h5')
10
  model.load_weights("pretrained_model.h5")
11
  class_names = ['Cardiomegaly', 'Emphysema', 'Effusion', 'Hernia', 'Infiltration', 'Mass', 'Nodule', 'Atelectasis', 'Pneumothorax', 'Pleural_Thickening', 'Pneumonia', 'Fibrosis', 'Edema', 'Consolidation']
12
+ def get_gradcam(model, img, layer_name):
13
+
14
+ img_array = img_to_array(img)
15
+ img_array = np.expand_dims(img_array, axis=0)
16
+ img_array = preprocess_input(img_array)
17
+
18
+ grad_model = Model(inputs=model.inputs, outputs=[model.get_layer(layer_name).output, model.output])
19
+
20
+ with tf.GradientTape() as tape:
21
+ conv_outputs, predictions = grad_model(img_array)
22
+ class_idx = tf.argmax(predictions[0])
23
+
24
+ output = conv_outputs[0]
25
+ grads = tape.gradient(predictions, conv_outputs)[0]
26
+ guided_grads = tf.cast(output > 0, 'float32') * tf.cast(grads > 0, 'float32') * grads
27
+
28
+ weights = tf.reduce_mean(guided_grads, axis=(0, 1))
29
+ cam = tf.reduce_sum(tf.multiply(weights, output), axis=-1)
30
+ heatmap = np.maximum(cam, 0)
31
+ heatmap /= tf.reduce_max(heatmap)
32
+ heatmap_img = plt.cm.jet(heatmap)[..., :3]
33
+
34
+ # Load the original image
35
+ original_img = Image.fromarray(img)
36
+
37
+ # Resize the heatmap to match the original image size
38
+ heatmap_img = Image.fromarray((heatmap_img * 255).astype(np.uint8))
39
+ heatmap_img = heatmap_img.resize(original_img.size)
40
+
41
+ # Overlay the heatmap on the original image
42
+ overlay_img = Image.blend(original_img, heatmap_img, 0.5)
43
+
44
+ # Return the overlayed image
45
+ return overlay_img
46
+
47
  def custom_decode_predictions(predictions, class_labels):
48
 
49
  decoded_predictions = []
 
59
  img_array = img_to_array(img)
60
  img_array = np.expand_dims(img_array, axis=0)
61
  img_array = preprocess_input(img_array)
 
 
 
62
 
63
+
64
+ predictions1 = model.predict(img_array)
65
+ decoded_predictions = custom_decode_predictions(predictions1, class_names)
66
+ overlay_img = get_gradcam(model, img, layer_name)
67
+
68
+ # Return the decoded predictions and the overlayed image
69
+ return decoded_predictions, overlay_img
70
  # Gradio interface
71
  iface = gr.Interface(
72
  fn=classify_image,
73
  inputs="image",
74
+ outputs=["text", "image"], # Add an "image" output for the overlayed image
75
+ title="Image Classification",
76
+ description="Classify images using your pre-trained model."
77
+ )
78
 
79
  # Launch the interface
80
  iface.launch(inline = False)