Spaces:
Sleeping
Sleeping
Commit ·
bdd75a0
1
Parent(s): b332e79
Fix Grad-CAM gradient issue and add debug logging
Browse files
app.py
CHANGED
|
@@ -150,6 +150,8 @@ def predict(file_obj):
|
|
| 150 |
# Convert to Tensor [Batch, Channel, Depth, Height, Width]
|
| 151 |
inp = torch.from_numpy(img_data_normalized).float().unsqueeze(0).unsqueeze(0)
|
| 152 |
|
|
|
|
|
|
|
| 153 |
# Run Inference
|
| 154 |
with torch.no_grad():
|
| 155 |
outputs = model(inp)
|
|
@@ -162,9 +164,13 @@ def predict(file_obj):
|
|
| 162 |
results.append([label, float(probs[i])])
|
| 163 |
|
| 164 |
print(f"Prediction result: {results}")
|
|
|
|
| 165 |
|
| 166 |
# Generate Grad-CAM heatmap for the predicted class
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
# Resize heatmap back to original scan size
|
| 170 |
heatmap_resized = resize(heatmap, original_shape, mode='constant', anti_aliasing=True).astype(np.float32)
|
|
|
|
| 150 |
# Convert to Tensor [Batch, Channel, Depth, Height, Width]
|
| 151 |
inp = torch.from_numpy(img_data_normalized).float().unsqueeze(0).unsqueeze(0)
|
| 152 |
|
| 153 |
+
print(f"Input tensor shape: {inp.shape}, min: {inp.min()}, max: {inp.max()}")
|
| 154 |
+
|
| 155 |
# Run Inference
|
| 156 |
with torch.no_grad():
|
| 157 |
outputs = model(inp)
|
|
|
|
| 164 |
results.append([label, float(probs[i])])
|
| 165 |
|
| 166 |
print(f"Prediction result: {results}")
|
| 167 |
+
print(f"Predicted class: {predicted_class} ({CLASS_LABELS[predicted_class]})")
|
| 168 |
|
| 169 |
# Generate Grad-CAM heatmap for the predicted class
|
| 170 |
+
# Need to create a new tensor that requires gradients
|
| 171 |
+
inp_grad = torch.from_numpy(img_data_normalized).float().unsqueeze(0).unsqueeze(0)
|
| 172 |
+
inp_grad.requires_grad = True
|
| 173 |
+
heatmap = grad_cam.generate_cam(inp_grad, target_class=predicted_class)
|
| 174 |
|
| 175 |
# Resize heatmap back to original scan size
|
| 176 |
heatmap_resized = resize(heatmap, original_shape, mode='constant', anti_aliasing=True).astype(np.float32)
|