Spaces:
Runtime error
Runtime error
Upload 3 files
Browse files- app.py +36 -0
- best_model_augmented.pth +3 -0
- model.py +34 -0
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from model import load_model
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
+
# Load your model
|
| 8 |
+
model = load_model('best_model_augmented.pth')
|
| 9 |
+
|
| 10 |
+
# Define the transformations for input images
|
| 11 |
+
transform = transforms.Compose([
|
| 12 |
+
transforms.Resize((224, 224)),
|
| 13 |
+
transforms.ToTensor(),
|
| 14 |
+
])
|
| 15 |
+
|
| 16 |
+
# Define the class labels
|
| 17 |
+
class_names = ['Normal', 'Monkeypox', 'Chickenpox', 'Measles']
|
| 18 |
+
|
| 19 |
+
def predict(image):
|
| 20 |
+
# Preprocess the image
|
| 21 |
+
image = transform(image).unsqueeze(0) # Add batch dimension
|
| 22 |
+
with torch.no_grad():
|
| 23 |
+
outputs = model(image)
|
| 24 |
+
_, predicted = torch.max(outputs, 1)
|
| 25 |
+
return class_names[predicted.item()]
|
| 26 |
+
|
| 27 |
+
# Create Gradio interface
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict,
|
| 30 |
+
inputs=gr.inputs.Image(type="pil", label="Upload an Image"),
|
| 31 |
+
outputs=gr.outputs.Label(num_top_classes=4, label="Prediction"),
|
| 32 |
+
live=True
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the interface
|
| 36 |
+
iface.launch()
|
best_model_augmented.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5ac0ac82eec1d104340ac30eea19ecf22ff94d283a8fa0631298567d7a738bca
|
| 3 |
+
size 84782380
|
model.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from torchvision import models
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
class CustomModel(nn.Module):
|
| 7 |
+
def __init__(self, num_classes=4):
|
| 8 |
+
super(CustomModel, self).__init__()
|
| 9 |
+
self.efficientnet = models.efficientnet_v2_s(weights=models.EfficientNet_V2_S_Weights.IMAGENET1K_V1)
|
| 10 |
+
num_features = self.efficientnet.classifier[1].in_features
|
| 11 |
+
self.efficientnet = nn.Sequential(*list(self.efficientnet.children())[:-1])
|
| 12 |
+
self.gap = nn.AdaptiveAvgPool2d(1)
|
| 13 |
+
self.fc1 = nn.Linear(num_features, 512)
|
| 14 |
+
self.dropout1 = nn.Dropout(0.5)
|
| 15 |
+
self.fc2 = nn.Linear(512, 256)
|
| 16 |
+
self.dropout2 = nn.Dropout(0.3)
|
| 17 |
+
self.fc3 = nn.Linear(256, num_classes)
|
| 18 |
+
|
| 19 |
+
def forward(self, x):
|
| 20 |
+
x = self.efficientnet(x)
|
| 21 |
+
x = self.gap(x)
|
| 22 |
+
x = torch.flatten(x, 1)
|
| 23 |
+
x = F.relu(self.fc1(x))
|
| 24 |
+
x = self.dropout1(x)
|
| 25 |
+
x = F.relu(self.fc2(x))
|
| 26 |
+
x = self.dropout2(x)
|
| 27 |
+
x = self.fc3(x)
|
| 28 |
+
return x
|
| 29 |
+
|
| 30 |
+
def load_model(model_path):
|
| 31 |
+
model = CustomModel()
|
| 32 |
+
model.load_state_dict(torch.load(model_path))
|
| 33 |
+
model.eval()
|
| 34 |
+
return model
|