hixoop commited on
Commit
4d9f978
·
verified ·
1 Parent(s): 78a8186

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -34
app.py CHANGED
@@ -3,59 +3,54 @@ from tensorflow import keras
3
  import numpy as np
4
  from PIL import Image
5
  import cv2
 
6
 
7
- # Load model
8
  model = keras.models.load_model('my_model (2).h5')
9
 
10
- # Class labels
11
  CLASS_NAMES = ['Necrotic Tumor', 'Non-Tumor', 'Viable Tumor']
12
 
13
- def make_gradcam_heatmap(img_array, model, last_conv_layer_name="last_conv_layer"):
14
- grad_model = keras.models.Model(
15
- model.inputs,
16
- [model.get_layer(last_conv_layer_name).output, model.output]
 
17
  )
18
 
19
- with keras.backend.GradientTape() as tape:
20
  conv_outputs, predictions = grad_model(img_array)
21
- pred_index = np.argmax(predictions[0])
22
  class_channel = predictions[:, pred_index]
23
 
24
  grads = tape.gradient(class_channel, conv_outputs)
25
- pooled_grads = np.mean(grads, axis=(0, 1, 2))
26
 
27
  conv_outputs = conv_outputs[0]
28
- heatmap = conv_outputs @ pooled_grads[..., np.newaxis]
29
- heatmap = np.squeeze(heatmap)
30
- heatmap = np.maximum(heatmap, 0) / (np.max(heatmap) + 1e-8)
31
 
32
- return heatmap
33
 
34
  def predict(input_image):
35
- # Preprocess
36
  img = Image.fromarray(input_image).convert('RGB')
37
  img = img.resize((224, 224))
38
  img_array = np.array(img) / 255.0
39
  img_array = np.expand_dims(img_array, axis=0)
40
 
41
- # Predict
42
  predictions = model.predict(img_array)
43
  pred_class = CLASS_NAMES[np.argmax(predictions[0])]
44
  confidence = float(np.max(predictions[0])) * 100
45
 
46
- # Create result text
47
  result_text = f"Prediction: {pred_class}\nConfidence: {confidence:.2f}%\n\n"
48
  result_text += "All Probabilities:\n"
49
  for i, name in enumerate(CLASS_NAMES):
50
  result_text += f" {name}: {predictions[0][i]*100:.2f}%\n"
51
 
52
- # Generate Grad-CAM
53
  try:
54
  heatmap = make_gradcam_heatmap(img_array, model)
55
  heatmap = cv2.resize(heatmap, (224, 224))
56
  heatmap = np.uint8(255 * heatmap)
57
  heatmap_colored = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
58
-
59
  original = np.array(img)
60
  superimposed = cv2.addWeighted(original, 0.6, heatmap_colored, 0.4, 0)
61
  output_image = superimposed
@@ -76,19 +71,4 @@ demo = gr.Interface(
76
  description="Upload an H&E stained histopathology image to classify as Non-Tumor, Viable Tumor, or Necrotic Tumor."
77
  )
78
 
79
- demo.launch()
80
- ```
81
-
82
- 4. Click **Commit new file to main**
83
-
84
- ---
85
-
86
- ## Also update `requirements.txt`
87
-
88
- You need to add `opencv-python`. Edit your `requirements.txt` to:
89
- ```
90
- tensorflow
91
- gradio
92
- numpy
93
- opencv-python-headless
94
- pillow
 
3
  import numpy as np
4
  from PIL import Image
5
  import cv2
6
+ import tensorflow as tf
7
 
 
8
  model = keras.models.load_model('my_model (2).h5')
9
 
 
10
  CLASS_NAMES = ['Necrotic Tumor', 'Non-Tumor', 'Viable Tumor']
11
 
12
+ def make_gradcam_heatmap(img_array, model):
13
+ last_conv_layer = model.get_layer('last_conv_layer')
14
+ grad_model = tf.keras.models.Model(
15
+ [model.inputs],
16
+ [last_conv_layer.output, model.output]
17
  )
18
 
19
+ with tf.GradientTape() as tape:
20
  conv_outputs, predictions = grad_model(img_array)
21
+ pred_index = tf.argmax(predictions[0])
22
  class_channel = predictions[:, pred_index]
23
 
24
  grads = tape.gradient(class_channel, conv_outputs)
25
+ pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
26
 
27
  conv_outputs = conv_outputs[0]
28
+ heatmap = conv_outputs @ pooled_grads[..., tf.newaxis]
29
+ heatmap = tf.squeeze(heatmap)
30
+ heatmap = tf.maximum(heatmap, 0) / (tf.math.reduce_max(heatmap) + 1e-8)
31
 
32
+ return heatmap.numpy()
33
 
34
  def predict(input_image):
 
35
  img = Image.fromarray(input_image).convert('RGB')
36
  img = img.resize((224, 224))
37
  img_array = np.array(img) / 255.0
38
  img_array = np.expand_dims(img_array, axis=0)
39
 
 
40
  predictions = model.predict(img_array)
41
  pred_class = CLASS_NAMES[np.argmax(predictions[0])]
42
  confidence = float(np.max(predictions[0])) * 100
43
 
 
44
  result_text = f"Prediction: {pred_class}\nConfidence: {confidence:.2f}%\n\n"
45
  result_text += "All Probabilities:\n"
46
  for i, name in enumerate(CLASS_NAMES):
47
  result_text += f" {name}: {predictions[0][i]*100:.2f}%\n"
48
 
 
49
  try:
50
  heatmap = make_gradcam_heatmap(img_array, model)
51
  heatmap = cv2.resize(heatmap, (224, 224))
52
  heatmap = np.uint8(255 * heatmap)
53
  heatmap_colored = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
 
54
  original = np.array(img)
55
  superimposed = cv2.addWeighted(original, 0.6, heatmap_colored, 0.4, 0)
56
  output_image = superimposed
 
71
  description="Upload an H&E stained histopathology image to classify as Non-Tumor, Viable Tumor, or Necrotic Tumor."
72
  )
73
 
74
+ demo.launch()