Download app.py from bimlendr/MedEyeNet: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/spaces/bimlendr/MedEyeNet/resolve/main/app.py
- Command line
-
hf download hf://spaces/bimlendr/MedEyeNet/app.py
-
curl -L -o app.py https://huggingface.co/spaces/bimlendr/MedEyeNet/resolve/main/app.py
1.98 kB
| 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() | |