Update app.py
Browse files
app.py
CHANGED
@@ -8,10 +8,6 @@ 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")
|
17 |
|
@@ -34,25 +30,6 @@ transform = transforms.Compose([
|
|
34 |
[0.229, 0.224, 0.225])
|
35 |
])
|
36 |
|
37 |
-
# Logging setup
|
38 |
-
log_path = "prediction_logs.csv"
|
39 |
-
|
40 |
-
# Create the CSV file with headers if it doesn't exist
|
41 |
-
if not os.path.exists(log_path):
|
42 |
-
with open(log_path, mode='w', newline='') as file:
|
43 |
-
writer = csv.writer(file)
|
44 |
-
writer.writerow(["timestamp", "image_name", "prediction", "confidence"])
|
45 |
-
|
46 |
-
def log_prediction(filename, prediction, confidence):
|
47 |
-
timestamp = datetime.datetime.now().isoformat()
|
48 |
-
row = [timestamp, filename, prediction, f"{confidence:.4f}"]
|
49 |
-
|
50 |
-
print("⏺ Logging prediction:", row)
|
51 |
-
|
52 |
-
with open(log_path, mode='a', newline='') as file:
|
53 |
-
writer = csv.writer(file)
|
54 |
-
writer.writerow(row)
|
55 |
-
|
56 |
# Prediction function
|
57 |
def predict_retinopathy(image):
|
58 |
img = image.convert("RGB").resize((224, 224))
|
@@ -72,10 +49,6 @@ def predict_retinopathy(image):
|
|
72 |
grayscale_cam = cam(input_tensor=img_tensor, targets=[ClassifierOutputTarget(pred)])[0]
|
73 |
cam_image = show_cam_on_image(rgb_img_np, grayscale_cam, use_rgb=True)
|
74 |
|
75 |
-
# Logging
|
76 |
-
filename = getattr(image, "filename", "uploaded_image")
|
77 |
-
log_prediction(filename, label, confidence)
|
78 |
-
|
79 |
cam_pil = Image.fromarray(cam_image)
|
80 |
return cam_pil, f"{label} (Confidence: {confidence:.2f})"
|
81 |
|
@@ -88,5 +61,5 @@ gr.Interface(
|
|
88 |
gr.Text(label="Prediction")
|
89 |
],
|
90 |
title="Diabetic Retinopathy Detection",
|
91 |
-
description="Upload a retinal image to classify DR and view Grad-CAM heatmap.
|
92 |
).launch()
|
|
|
8 |
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
|
9 |
from pytorch_grad_cam.utils.image import show_cam_on_image
|
10 |
|
|
|
|
|
|
|
|
|
11 |
# Set device
|
12 |
device = torch.device("cpu")
|
13 |
|
|
|
30 |
[0.229, 0.224, 0.225])
|
31 |
])
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
# Prediction function
|
34 |
def predict_retinopathy(image):
|
35 |
img = image.convert("RGB").resize((224, 224))
|
|
|
49 |
grayscale_cam = cam(input_tensor=img_tensor, targets=[ClassifierOutputTarget(pred)])[0]
|
50 |
cam_image = show_cam_on_image(rgb_img_np, grayscale_cam, use_rgb=True)
|
51 |
|
|
|
|
|
|
|
|
|
52 |
cam_pil = Image.fromarray(cam_image)
|
53 |
return cam_pil, f"{label} (Confidence: {confidence:.2f})"
|
54 |
|
|
|
61 |
gr.Text(label="Prediction")
|
62 |
],
|
63 |
title="Diabetic Retinopathy Detection",
|
64 |
+
description="Upload a retinal image to classify DR and view Grad-CAM heatmap."
|
65 |
).launch()
|