File size: 1,984 Bytes
03c9d46 | 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 | import gradio as gr
import torch
import torchvision.transforms as transforms
import torch.nn.functional as F
from PIL import Image
class ImprovedCNNModel(torch.nn.Module):
def __init__(self):
super(ImprovedCNNModel, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 32, kernel_size=3, padding=1)
self.bn1 = torch.nn.BatchNorm2d(32)
self.pool1 = torch.nn.MaxPool2d(2, 2)
self.conv2 = torch.nn.Conv2d(32, 64, kernel_size=3, padding=1)
self.bn2 = torch.nn.BatchNorm2d(64)
self.pool2 = torch.nn.MaxPool2d(2, 2)
self.fc1 = torch.nn.Linear(64 * 7 * 7, 128)
self.fc2 = torch.nn.Linear(128, 64)
self.fc3 = torch.nn.Linear(64, 4)
self.dropout = torch.nn.Dropout(0.3)
def forward(self, x):
x = self.pool1(F.relu(self.bn1(self.conv1(x))))
x = self.pool2(F.relu(self.bn2(self.conv2(x))))
x = x.view(-1, 64 * 7 * 7)
x = F.relu(self.fc1(x))
x = self.dropout(x)
x = F.relu(self.fc2(x))
x = self.dropout(x)
x = self.fc3(x)
return x
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = ImprovedCNNModel().to(device)
model.load_state_dict(torch.load("best_model.pth", map_location=device))
model.eval()
transform = transforms.Compose([
transforms.Grayscale(),
transforms.Resize((28, 28)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.5], std=[0.5])
])
class_labels = {0: "CNV", 1: "DME", 2: "DRUSEN", 3: "NORMAL"}
def predict(image):
image = transform(image).unsqueeze(0).to(device)
with torch.no_grad():
outputs = model(image)
probs = F.softmax(outputs, dim=1).cpu().numpy()[0]
return {class_labels[i]: float(probs[i]) for i in range(4)}
gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs=gr.Label(num_top_classes=4),
title="Retinal Disease Classification",
description="Upload an OCT image to classify it."
).launch()
|