File size: 4,736 Bytes
1146c25 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import timm
from huggingface_hub import hf_hub_download
# ββ Config ββ
IMG_SIZE = 300
DEVICE = torch.device('cpu') # Streamlit runs on CPU
HF_REPO = 'Salonideshmukh/dr-detection-model'
MODEL_FILE = 'best_dr_model.pth'
# ββ Model definition (exact same as training) ββ
class DRClassifier(nn.Module):
def __init__(self, num_classes=2):
super().__init__()
self.backbone = timm.create_model(
'efficientnet_b4', pretrained=False, num_classes=0, global_pool=''
)
feat_dim = self.backbone.num_features
self.pool = nn.AdaptiveAvgPool2d(1)
self.head = nn.Sequential(
nn.Flatten(),
nn.Dropout(0.4),
nn.Linear(feat_dim, 512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, num_classes)
)
self.gradients = None
self.activations = None
def save_gradient(self, grad):
self.gradients = grad
def forward(self, x):
feats = self.backbone(x)
if feats.requires_grad:
feats.register_hook(self.save_gradient)
self.activations = feats
return self.head(self.pool(feats))
@torch.no_grad()
def load_model():
"""Download model from HuggingFace and load weights."""
model_path = hf_hub_download(repo_id=HF_REPO, filename=MODEL_FILE)
model = DRClassifier(num_classes=2).to(DEVICE)
model.load_state_dict(torch.load(model_path, map_location=DEVICE))
model.eval()
return model
# ββ Preprocessing (identical to training) ββ
def crop_black_borders(img, threshold=10):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
_, binary = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY)
contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
if not contours:
return img
x, y, w, h = cv2.boundingRect(max(contours, key=cv2.contourArea))
return img[y:y+h, x:x+w]
def ben_graham_normalization(img, sigmaX=10):
return cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), sigmaX), -4, 128)
def apply_clahe(img):
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
lab[:, :, 0] = clahe.apply(lab[:, :, 0])
return cv2.cvtColor(lab, cv2.COLOR_LAB2BGR)
def preprocess_image(img_array):
"""
Input : numpy array in RGB (from Streamlit uploader)
Output : preprocessed numpy array in RGB
This is the key fix β we convert RGBβBGR for OpenCV processing,
then convert back to RGB for display. Same result as training.
"""
img = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR) # RGB β BGR for OpenCV
img = crop_black_borders(img)
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
img = apply_clahe(img)
img = ben_graham_normalization(img)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # BGR β RGB for display
return img
def to_tensor(img_rgb):
"""Normalize and convert to tensor β identical to val_transform in training."""
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
img = img_rgb.astype(np.float32) / 255.0
img = (img - mean) / std
img = torch.from_numpy(img).permute(2, 0, 1).float() # (3, H, W)
return img
def predict(model, img_array, threshold=0.35):
"""
Full inference pipeline.
img_array : numpy RGB array from Streamlit
Returns : preprocessed image, grad-cam, prediction, probability
"""
# Preprocess
img_rgb = preprocess_image(img_array)
img_tensor = to_tensor(img_rgb)
# ββ Grad-CAM ββ
model.eval()
model.backbone.set_grad_checkpointing(enable=False)
inp = img_tensor.unsqueeze(0).to(DEVICE)
features = model.backbone(inp)
features.retain_grad()
pooled = model.pool(features)
output = model.head(pooled)
pred_class = output.argmax(dim=1).item()
prob = F.softmax(output, dim=1)[0, 1].item()
model.zero_grad()
output[0, pred_class].backward()
grads = features.grad
weights = grads.mean(dim=(2, 3), keepdim=True)
cam = F.relu((weights * features).sum(dim=1, keepdim=True))
cam = F.interpolate(cam, (IMG_SIZE, IMG_SIZE), mode='bilinear', align_corners=False)
cam = cam.squeeze().detach().numpy()
cam = (cam - cam.min()) / (cam.max() - cam.min() + 1e-8)
prediction = 'DR' if prob >= threshold else 'No DR'
return img_rgb, cam, prediction, prob |