ericjedha commited on
Commit
6d34f9d
·
verified ·
1 Parent(s): abaa69b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -3
app.py CHANGED
@@ -77,10 +77,11 @@ def make_gradcam(img_path, model, last_conv_layer_name="conv5_block32_concat", c
77
 
78
  with tf.GradientTape() as tape:
79
  conv_outputs, predictions = grad_model(input_array)
 
80
  loss = predictions[:, class_index]
81
 
82
- grads = tape.gradient(loss, conv_outputs)[0] # shape: (H, W, C)
83
- pooled_grads = tf.reduce_mean(grads, axis=(0, 1)) # shape: (C,)
84
 
85
  conv_outputs = conv_outputs[0] # (H, W, C)
86
  heatmap = tf.reduce_sum(conv_outputs * pooled_grads, axis=-1) # (H, W)
@@ -91,11 +92,12 @@ def make_gradcam(img_path, model, last_conv_layer_name="conv5_block32_concat", c
91
  heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
92
 
93
  superimposed_img = cv2.addWeighted(
94
- cv2.cvtColor(img_array.astype("uint8"), cv2.COLOR_RGB2BGR),
95
  0.6, heatmap, 0.4, 0
96
  )
97
  return cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB)
98
 
 
99
  # ---- Fonction Gradio ----
100
  def gradio_predict(image_file):
101
  probs = predict_single(image_file)
 
77
 
78
  with tf.GradientTape() as tape:
79
  conv_outputs, predictions = grad_model(input_array)
80
+ predictions = tf.convert_to_tensor(predictions) # 👈 correction
81
  loss = predictions[:, class_index]
82
 
83
+ grads = tape.gradient(loss, conv_outputs)[0] # (H, W, C)
84
+ pooled_grads = tf.reduce_mean(grads, axis=(0, 1)) # (C,)
85
 
86
  conv_outputs = conv_outputs[0] # (H, W, C)
87
  heatmap = tf.reduce_sum(conv_outputs * pooled_grads, axis=-1) # (H, W)
 
92
  heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
93
 
94
  superimposed_img = cv2.addWeighted(
95
+ cv2.cvtColor(img_array.astype("uint8"), cv2.COLOR_RGB2BGR),
96
  0.6, heatmap, 0.4, 0
97
  )
98
  return cv2.cvtColor(superimposed_img, cv2.COLOR_BGR2RGB)
99
 
100
+
101
  # ---- Fonction Gradio ----
102
  def gradio_predict(image_file):
103
  probs = predict_single(image_file)