Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator
|
2 |
+
from tensorflow.keras.applications import MobileNetV2
|
3 |
+
from tensorflow.keras import layers, models
|
4 |
+
|
5 |
+
# Image data generator with augmentation
|
6 |
+
train_datagen = ImageDataGenerator(
|
7 |
+
rescale=1.0/255,
|
8 |
+
rotation_range=20,
|
9 |
+
width_shift_range=0.2,
|
10 |
+
height_shift_range=0.2,
|
11 |
+
shear_range=0.2,
|
12 |
+
zoom_range=0.2,
|
13 |
+
horizontal_flip=True,
|
14 |
+
fill_mode='nearest'
|
15 |
+
)
|
16 |
+
|
17 |
+
test_datagen = ImageDataGenerator(rescale=1.0/255)
|
18 |
+
|
19 |
+
train_generator = train_datagen.flow_from_directory(
|
20 |
+
'/content/drive/MyDrive/Skin Disease Data/Split_smol/train',
|
21 |
+
target_size=(150, 150),
|
22 |
+
batch_size=32,
|
23 |
+
class_mode='categorical'
|
24 |
+
)
|
25 |
+
|
26 |
+
validation_generator = test_datagen.flow_from_directory(
|
27 |
+
'/content/drive/MyDrive/Skin Disease Data/Split_smol/val',
|
28 |
+
target_size=(150, 150),
|
29 |
+
batch_size=32,
|
30 |
+
class_mode='categorical'
|
31 |
+
)
|
32 |
+
|
33 |
+
# Model architecture
|
34 |
+
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
|
35 |
+
base_model.trainable = False # Freeze the base model
|
36 |
+
|
37 |
+
model = models.Sequential([
|
38 |
+
base_model,
|
39 |
+
layers.GlobalAveragePooling2D(),
|
40 |
+
layers.Dense(128, activation='relu'),
|
41 |
+
layers.Dense(len(labels), activation='softmax')
|
42 |
+
])
|
43 |
+
|
44 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
45 |
+
|
46 |
+
# Training
|
47 |
+
history = model.fit(
|
48 |
+
train_generator,
|
49 |
+
steps_per_epoch=train_generator.samples // 32,
|
50 |
+
validation_data=validation_generator,
|
51 |
+
validation_steps=validation_generator.samples // 32,
|
52 |
+
epochs=20
|
53 |
+
)
|
54 |
+
|
55 |
+
# Save the model
|
56 |
+
model.save('skin_disease_model.h5')
|
57 |
+
|
58 |
+
import gradio as gr
|
59 |
+
import numpy as np
|
60 |
+
import tensorflow as tf
|
61 |
+
|
62 |
+
# Load your trained model
|
63 |
+
model = tf.keras.models.load_model('/content/skin_disease_model.h5') # Update with your model path
|
64 |
+
|
65 |
+
# Class labels
|
66 |
+
labels = ['Actinic Keratosis', 'Atopic Dermatitis', 'Benign Keratosis',
|
67 |
+
'Dermatofibroma', 'Melanocytic nevus', 'Melanoma',
|
68 |
+
'Squamous cell carcinoma', 'Tinea Ringworm candidiasis',
|
69 |
+
'Vascular lesion']
|
70 |
+
|
71 |
+
# Gradio function for prediction
|
72 |
+
def predict_skin_disease(img):
|
73 |
+
try:
|
74 |
+
# Preprocess the image
|
75 |
+
img = np.array(img) / 255.0 # Normalize
|
76 |
+
img = tf.image.resize(img, (150, 150)) # Resize to match model input
|
77 |
+
img = np.expand_dims(img, axis=0) # Add batch dimension
|
78 |
+
|
79 |
+
# Make prediction using the model
|
80 |
+
prediction = model.predict(img)
|
81 |
+
|
82 |
+
# Convert prediction to a dictionary of class labels and probabilities
|
83 |
+
return {labels[i]: float(prediction[0][i]) for i in range(len(labels))}
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
return f"Error: {str(e)}"
|
87 |
+
|
88 |
+
# Gradio Interface
|
89 |
+
gr_interface = gr.Interface(
|
90 |
+
fn=predict_skin_disease,
|
91 |
+
inputs=gr.Image(type="numpy", label="Upload an Image of Skin Lesion"),
|
92 |
+
outputs=gr.Label(num_top_classes=3), # Display top 3 predictions
|
93 |
+
title="Skin Disease Classification",
|
94 |
+
description="Upload an image of a skin lesion to classify it into various skin diseases."
|
95 |
+
)
|
96 |
+
|
97 |
+
# Launch the Gradio interface
|
98 |
+
gr_interface.launch()
|