Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- app.py +72 -0
- examples/5612.jpg +0 -0
- examples/5903.jpg +0 -0
- examples/6561.jpg +0 -0
- examples/6652.jpg +0 -0
- examples/6800.jpg +0 -0
- melanoma_model1.pth +3 -0
- model.py +19 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from timeit import default_timer as timer
|
| 6 |
+
|
| 7 |
+
from model import create_model
|
| 8 |
+
from typing import Tuple, Dict
|
| 9 |
+
|
| 10 |
+
class_names = ['Benign', 'Malignant']
|
| 11 |
+
|
| 12 |
+
model, transform = create_model()
|
| 13 |
+
|
| 14 |
+
# Load saved weights
|
| 15 |
+
model.load_state_dict(
|
| 16 |
+
torch.load(
|
| 17 |
+
f="melanoma_model1.pth",
|
| 18 |
+
map_location=torch.device("cpu"), # load to CPU
|
| 19 |
+
)
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
### 3. Predict function ###
|
| 24 |
+
|
| 25 |
+
# Create predict function
|
| 26 |
+
|
| 27 |
+
def predict(img) -> Tuple[Dict, float]:
|
| 28 |
+
"""Transforms and performs a prediction on img and returns prediction and time taken.
|
| 29 |
+
"""
|
| 30 |
+
# Start the timer
|
| 31 |
+
start_time = timer()
|
| 32 |
+
|
| 33 |
+
# Apply transformations to the image
|
| 34 |
+
img_tensor = transform(img).unsqueeze(0).to(next(model.parameters()).device)
|
| 35 |
+
|
| 36 |
+
# Put model into evaluation mode
|
| 37 |
+
model.eval()
|
| 38 |
+
|
| 39 |
+
# Pass the image through the model
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
y_logits = model(img_tensor).squeeze()
|
| 42 |
+
y_pred_probs = torch.sigmoid(y_logits)
|
| 43 |
+
|
| 44 |
+
# Round the prediction probabilities to get binary predictions
|
| 45 |
+
y_pred_binary = torch.round(y_pred_probs).item()
|
| 46 |
+
|
| 47 |
+
# Create a dictionary with the class label and the corresponding prediction probability
|
| 48 |
+
pred_label = class_names[int(y_pred_binary)]
|
| 49 |
+
|
| 50 |
+
# Calculate the prediction time
|
| 51 |
+
pred_time = round(timer() - start_time, 5)
|
| 52 |
+
|
| 53 |
+
# Return the prediction dictionary and prediction time
|
| 54 |
+
return {pred_label: float(y_pred_probs)}, pred_time
|
| 55 |
+
|
| 56 |
+
# Create title, description and article strings
|
| 57 |
+
title = "Melanoma Cancer Detection"
|
| 58 |
+
description = "An Vision Tranformer feature extractor computer vision model to classify images of MELANOMA CANCER.."
|
| 59 |
+
article = " model is built by Shukurullo Meliboev using Kaggle's Melanoma disease datasets."
|
| 60 |
+
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 61 |
+
# Create the Gradio demo
|
| 62 |
+
demo = gr.Interface(fn=predict, # mapping function from input to output
|
| 63 |
+
inputs=gr.Image(type="pil"), # what are the inputs?
|
| 64 |
+
outputs=[gr.Label(num_top_classes=1, label="Predictions"), # what are the outputs?
|
| 65 |
+
gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs
|
| 66 |
+
examples=example_list,
|
| 67 |
+
title=title,
|
| 68 |
+
description=description,
|
| 69 |
+
article=article)
|
| 70 |
+
|
| 71 |
+
# Launch the demo!
|
| 72 |
+
demo.launch(False) # generate a publically shareable URL?
|
examples/5612.jpg
ADDED
|
examples/5903.jpg
ADDED
|
examples/6561.jpg
ADDED
|
examples/6652.jpg
ADDED
|
examples/6800.jpg
ADDED
|
melanoma_model1.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cefce437c7b45b48513536454fcf2be41049995597180bb818a4a6b2ed0ae8d4
|
| 3 |
+
size 343259298
|
model.py
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torchvision
|
| 3 |
+
from torch import nn
|
| 4 |
+
def create_model():
|
| 5 |
+
torch.manual_seed(42)
|
| 6 |
+
torch.cuda.manual_seed(42)
|
| 7 |
+
|
| 8 |
+
weights = torchvision.models.ViT_B_16_Weights.DEFAULT
|
| 9 |
+
transform = weights.transforms()
|
| 10 |
+
model = torchvision.models.vit_b_16(weights=weights)
|
| 11 |
+
|
| 12 |
+
# 4. Freeze all layers in base model
|
| 13 |
+
for param in model.parameters():
|
| 14 |
+
param.requires_grad = False
|
| 15 |
+
|
| 16 |
+
model.heads = nn.Sequential(
|
| 17 |
+
nn.Linear(768,1)
|
| 18 |
+
)
|
| 19 |
+
return model, transform
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch==2.2.1
|
| 2 |
+
torchvision==0.17.1
|
| 3 |
+
gradio==4.22.0
|