Update app.py
Browse files
app.py
CHANGED
|
@@ -8,9 +8,9 @@ from pytorch_grad_cam import GradCAM
|
|
| 8 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
| 9 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 10 |
|
|
|
|
| 11 |
import csv
|
| 12 |
import datetime
|
| 13 |
-
import os
|
| 14 |
|
| 15 |
# Set device
|
| 16 |
device = torch.device("cpu")
|
|
@@ -26,7 +26,7 @@ model.eval()
|
|
| 26 |
target_layer = model.layer4[-1]
|
| 27 |
cam = GradCAM(model=model, target_layers=[target_layer])
|
| 28 |
|
| 29 |
-
#
|
| 30 |
transform = transforms.Compose([
|
| 31 |
transforms.Resize((224, 224)),
|
| 32 |
transforms.ToTensor(),
|
|
@@ -34,18 +34,15 @@ transform = transforms.Compose([
|
|
| 34 |
[0.229, 0.224, 0.225])
|
| 35 |
])
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
|
| 39 |
|
|
|
|
| 40 |
def log_prediction(filename, prediction, confidence):
|
| 41 |
timestamp = datetime.datetime.now().isoformat()
|
| 42 |
row = [timestamp, filename, prediction, f"{confidence:.4f}"]
|
| 43 |
-
|
| 44 |
-
print("⏺
|
| 45 |
-
|
| 46 |
-
with open(log_path, mode='a', newline='') as file:
|
| 47 |
-
writer = csv.writer(file)
|
| 48 |
-
writer.writerow(row)
|
| 49 |
|
| 50 |
# Prediction function
|
| 51 |
def predict_retinopathy(image):
|
|
@@ -66,21 +63,46 @@ def predict_retinopathy(image):
|
|
| 66 |
grayscale_cam = cam(input_tensor=img_tensor, targets=[ClassifierOutputTarget(pred)])[0]
|
| 67 |
cam_image = show_cam_on_image(rgb_img_np, grayscale_cam, use_rgb=True)
|
| 68 |
|
| 69 |
-
#
|
| 70 |
filename = getattr(image, "filename", "uploaded_image")
|
| 71 |
log_prediction(filename, label, confidence)
|
| 72 |
|
| 73 |
cam_pil = Image.fromarray(cam_image)
|
| 74 |
return cam_pil, f"{label} (Confidence: {confidence:.2f})"
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
| 9 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
| 10 |
|
| 11 |
+
import io
|
| 12 |
import csv
|
| 13 |
import datetime
|
|
|
|
| 14 |
|
| 15 |
# Set device
|
| 16 |
device = torch.device("cpu")
|
|
|
|
| 26 |
target_layer = model.layer4[-1]
|
| 27 |
cam = GradCAM(model=model, target_layers=[target_layer])
|
| 28 |
|
| 29 |
+
# Preprocessing
|
| 30 |
transform = transforms.Compose([
|
| 31 |
transforms.Resize((224, 224)),
|
| 32 |
transforms.ToTensor(),
|
|
|
|
| 34 |
[0.229, 0.224, 0.225])
|
| 35 |
])
|
| 36 |
|
| 37 |
+
# In-memory log list
|
| 38 |
+
prediction_log = [["timestamp", "image_name", "prediction", "confidence"]]
|
| 39 |
|
| 40 |
+
# Logging function
|
| 41 |
def log_prediction(filename, prediction, confidence):
|
| 42 |
timestamp = datetime.datetime.now().isoformat()
|
| 43 |
row = [timestamp, filename, prediction, f"{confidence:.4f}"]
|
| 44 |
+
prediction_log.append(row)
|
| 45 |
+
print("⏺ Logged:", row)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
# Prediction function
|
| 48 |
def predict_retinopathy(image):
|
|
|
|
| 63 |
grayscale_cam = cam(input_tensor=img_tensor, targets=[ClassifierOutputTarget(pred)])[0]
|
| 64 |
cam_image = show_cam_on_image(rgb_img_np, grayscale_cam, use_rgb=True)
|
| 65 |
|
| 66 |
+
# Log it
|
| 67 |
filename = getattr(image, "filename", "uploaded_image")
|
| 68 |
log_prediction(filename, label, confidence)
|
| 69 |
|
| 70 |
cam_pil = Image.fromarray(cam_image)
|
| 71 |
return cam_pil, f"{label} (Confidence: {confidence:.2f})"
|
| 72 |
|
| 73 |
+
# CSV download function
|
| 74 |
+
def download_logs():
|
| 75 |
+
output = io.StringIO()
|
| 76 |
+
writer = csv.writer(output)
|
| 77 |
+
writer.writerows(prediction_log)
|
| 78 |
+
output.seek(0)
|
| 79 |
+
return gr.File.update(value=io.BytesIO(output.getvalue().encode()), filename="prediction_logs.csv")
|
| 80 |
+
|
| 81 |
+
# Build the UI with Gradio Blocks
|
| 82 |
+
with gr.Blocks() as demo:
|
| 83 |
+
gr.Markdown("## 🧠 Diabetic Retinopathy Detection with Grad-CAM & Logging")
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
image_input = gr.Image(type="pil", label="Upload Retinal Image")
|
| 87 |
+
cam_output = gr.Image(type="pil", label="Grad-CAM")
|
| 88 |
+
|
| 89 |
+
prediction_output = gr.Text(label="Prediction")
|
| 90 |
+
|
| 91 |
+
with gr.Row():
|
| 92 |
+
run_button = gr.Button("Submit")
|
| 93 |
+
download_button = gr.Button("📥 Download Logs")
|
| 94 |
+
download_file = gr.File(label="Your Log File", interactive=False)
|
| 95 |
+
|
| 96 |
+
run_button.click(
|
| 97 |
+
fn=predict_retinopathy,
|
| 98 |
+
inputs=image_input,
|
| 99 |
+
outputs=[cam_output, prediction_output]
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
download_button.click(
|
| 103 |
+
fn=download_logs,
|
| 104 |
+
inputs=[],
|
| 105 |
+
outputs=download_file
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
demo.launch()
|