Spaces:
Sleeping
Sleeping
Commit ·
bce1e8d
1
Parent(s): 1b533e4
large file
Browse files- app.py +82 -0
- completemodel1 (1).json +0 -0
- completeweights.weights.h5 +3 -0
- model.py +85 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pickle
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import cv2
|
| 6 |
+
|
| 7 |
+
# Load the trained model
|
| 8 |
+
with open('linear_regression_model.pkl', 'rb') as f:
|
| 9 |
+
model = pickle.load(f)
|
| 10 |
+
|
| 11 |
+
# Define the class labels for HAM10000 dataset
|
| 12 |
+
CLASS_NAMES = [
|
| 13 |
+
'Melanoma',
|
| 14 |
+
'Melanocytic nevus',
|
| 15 |
+
'Basal cell carcinoma',
|
| 16 |
+
'Actinic keratosis / Bowen\'s disease',
|
| 17 |
+
'Benign keratosis',
|
| 18 |
+
'Dermatofibroma',
|
| 19 |
+
'Vascular lesion'
|
| 20 |
+
]
|
| 21 |
+
|
| 22 |
+
def preprocess_image(image):
|
| 23 |
+
"""
|
| 24 |
+
Preprocess the image for prediction.
|
| 25 |
+
|
| 26 |
+
Args:
|
| 27 |
+
image: Input image (PIL Image or numpy array)
|
| 28 |
+
|
| 29 |
+
Returns:
|
| 30 |
+
numpy.ndarray: Preprocessed image features
|
| 31 |
+
"""
|
| 32 |
+
# Convert image to numpy array if it's PIL Image
|
| 33 |
+
if isinstance(image, Image.Image):
|
| 34 |
+
image = np.array(image)
|
| 35 |
+
|
| 36 |
+
# Resize image
|
| 37 |
+
image = cv2.resize(image, (224, 224))
|
| 38 |
+
|
| 39 |
+
# Flatten the image
|
| 40 |
+
features = image.flatten()
|
| 41 |
+
|
| 42 |
+
# Normalize features
|
| 43 |
+
features = features / 255.0
|
| 44 |
+
|
| 45 |
+
return features
|
| 46 |
+
|
| 47 |
+
def predict(image):
|
| 48 |
+
"""
|
| 49 |
+
Predict the skin lesion class from the input image.
|
| 50 |
+
|
| 51 |
+
Args:
|
| 52 |
+
image: Input image (PIL Image or numpy array)
|
| 53 |
+
|
| 54 |
+
Returns:
|
| 55 |
+
dict: Dictionary containing class probabilities
|
| 56 |
+
"""
|
| 57 |
+
# Preprocess the image
|
| 58 |
+
features = preprocess_image(image)
|
| 59 |
+
|
| 60 |
+
# Get prediction
|
| 61 |
+
prediction = model.predict_proba([features])[0]
|
| 62 |
+
|
| 63 |
+
# Convert to dictionary
|
| 64 |
+
result = {CLASS_NAMES[i]: float(prediction[i]) for i in range(len(CLASS_NAMES))}
|
| 65 |
+
return result
|
| 66 |
+
|
| 67 |
+
# Create Gradio interface
|
| 68 |
+
iface = gr.Interface(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=gr.Image(type="pil", label="Upload Skin Lesion Image"),
|
| 71 |
+
outputs=gr.Label(num_top_classes=3, label="Prediction Results"),
|
| 72 |
+
title="Skin Cancer Classification",
|
| 73 |
+
description="Upload an image of a skin lesion to classify it using our trained model.",
|
| 74 |
+
examples=[
|
| 75 |
+
["example1.jpg"],
|
| 76 |
+
["example2.jpg"]
|
| 77 |
+
],
|
| 78 |
+
theme=gr.themes.Soft()
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
if __name__ == "__main__":
|
| 82 |
+
iface.launch(share=True)
|
completemodel1 (1).json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
completeweights.weights.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:293aa1cb580389ce561314315324fb026952c28c13d511d3ac2e208087e86f43
|
| 3 |
+
size 278723968
|
model.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import timm
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
class SkinCancerModel(nn.Module):
|
| 7 |
+
def __init__(
|
| 8 |
+
self,
|
| 9 |
+
model_name: str = "efficientnet_b0",
|
| 10 |
+
num_classes: int = 7,
|
| 11 |
+
pretrained: bool = True,
|
| 12 |
+
dropout_rate: float = 0.2
|
| 13 |
+
):
|
| 14 |
+
"""
|
| 15 |
+
Initialize the skin cancer classification model using EfficientNet.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
model_name (str): Name of the EfficientNet variant to use
|
| 19 |
+
num_classes (int): Number of output classes (7 for HAM10000)
|
| 20 |
+
pretrained (bool): Whether to use pretrained weights
|
| 21 |
+
dropout_rate (float): Dropout rate for regularization
|
| 22 |
+
"""
|
| 23 |
+
super().__init__()
|
| 24 |
+
|
| 25 |
+
# Load the base EfficientNet model
|
| 26 |
+
self.base_model = timm.create_model(
|
| 27 |
+
model_name,
|
| 28 |
+
pretrained=pretrained,
|
| 29 |
+
num_classes=0 # Remove the default classification head
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Get the number of features from the base model
|
| 33 |
+
num_features = self.base_model.num_features
|
| 34 |
+
|
| 35 |
+
# Create custom classification head
|
| 36 |
+
self.classifier = nn.Sequential(
|
| 37 |
+
nn.Dropout(dropout_rate),
|
| 38 |
+
nn.Linear(num_features, 512),
|
| 39 |
+
nn.ReLU(),
|
| 40 |
+
nn.Dropout(dropout_rate),
|
| 41 |
+
nn.Linear(512, num_classes)
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
| 45 |
+
"""
|
| 46 |
+
Forward pass of the model.
|
| 47 |
+
|
| 48 |
+
Args:
|
| 49 |
+
x (torch.Tensor): Input tensor of shape (batch_size, channels, height, width)
|
| 50 |
+
|
| 51 |
+
Returns:
|
| 52 |
+
torch.Tensor: Output tensor of shape (batch_size, num_classes)
|
| 53 |
+
"""
|
| 54 |
+
# Get features from the base model
|
| 55 |
+
features = self.base_model(x)
|
| 56 |
+
|
| 57 |
+
# Pass through the classifier
|
| 58 |
+
output = self.classifier(features)
|
| 59 |
+
|
| 60 |
+
return output
|
| 61 |
+
|
| 62 |
+
def get_model(
|
| 63 |
+
model_name: str = "efficientnet_b0",
|
| 64 |
+
num_classes: int = 7,
|
| 65 |
+
pretrained: bool = True,
|
| 66 |
+
dropout_rate: float = 0.2
|
| 67 |
+
) -> SkinCancerModel:
|
| 68 |
+
"""
|
| 69 |
+
Factory function to create and return the model.
|
| 70 |
+
|
| 71 |
+
Args:
|
| 72 |
+
model_name (str): Name of the EfficientNet variant to use
|
| 73 |
+
num_classes (int): Number of output classes
|
| 74 |
+
pretrained (bool): Whether to use pretrained weights
|
| 75 |
+
dropout_rate (float): Dropout rate for regularization
|
| 76 |
+
|
| 77 |
+
Returns:
|
| 78 |
+
SkinCancerModel: Initialized model instance
|
| 79 |
+
"""
|
| 80 |
+
return SkinCancerModel(
|
| 81 |
+
model_name=model_name,
|
| 82 |
+
num_classes=num_classes,
|
| 83 |
+
pretrained=pretrained,
|
| 84 |
+
dropout_rate=dropout_rate
|
| 85 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
numpy>=1.21.0
|
| 2 |
+
Pillow>=9.0.0
|
| 3 |
+
opencv-python>=4.5.0
|
| 4 |
+
gradio>=4.0.0
|
| 5 |
+
scikit-learn>=1.0.0
|