File size: 3,199 Bytes
b5fe48b
 
 
26db02c
b5fe48b
f68f46e
 
 
 
 
b5fe48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4be3406
b5fe48b
 
 
 
 
 
4be3406
b5fe48b
 
 
 
 
 
 
 
 
8e81ff2
b5fe48b
 
8e81ff2
b5fe48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4be3406
b5fe48b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras import layers, models
import tensorflow as tf

labels = ['Actinic Keratosis', 'Atopic Dermatitis', 'Benign Keratosis', 
          'Dermatofibroma', 'Melanocytic nevus', 'Melanoma', 
          'Squamous cell carcinoma', 'Tinea Ringworm candidiasis', 
          'Vascular lesion']

# Image data generator with augmentation
train_datagen = ImageDataGenerator(
    rescale=1.0/255,
    rotation_range=20,
    width_shift_range=0.2,
    height_shift_range=0.2,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True,
    fill_mode='nearest'
)

test_datagen = ImageDataGenerator(rescale=1.0/255)

train_generator = train_datagen.flow_from_directory(
    'train',
    target_size=(150, 150),
    batch_size=32,
    class_mode='categorical'
)

validation_generator = test_datagen.flow_from_directory(
    'val',
    target_size=(150, 150),
    batch_size=32,
    class_mode='categorical'
)

# Model architecture
base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
base_model.trainable = False  # Freeze the base model

model = tf.keras.models.Sequential([
    base_model,
    layers.GlobalAveragePooling2D(),
    layers.Dense(1024, activation='relu'),
    layers.Dense(len(labels), activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])

# Training
history = model.fit(
    train_generator,
    steps_per_epoch=train_generator.samples // 32,
    validation_data=validation_generator,
    validation_steps=validation_generator.samples // 32,
    epochs=20
)

# Save the model
model.save('skin_disease_model.h5')

import gradio as gr
import numpy as np
import tensorflow as tf

# Load your trained model
model = tf.keras.models.load_model('skin_disease_model.h5')  # Update with your model path

# Class labels
labels = ['Actinic Keratosis', 'Atopic Dermatitis', 'Benign Keratosis', 
          'Dermatofibroma', 'Melanocytic nevus', 'Melanoma', 
          'Squamous cell carcinoma', 'Tinea Ringworm candidiasis', 
          'Vascular lesion']

# Gradio function for prediction
def predict_skin_disease(img):
    try:
        # Preprocess the image
        img = np.array(img) / 255.0  # Normalize
        img = tf.image.resize(img, (150, 150))  # Resize to match model input
        img = np.expand_dims(img, axis=0)  # Add batch dimension

        # Make prediction using the model
        prediction = model.predict(img)

        # Convert prediction to a dictionary of class labels and probabilities
        return {labels[i]: float(prediction[0][i]) for i in range(len(labels))}

    except Exception as e:
        return f"Error: {str(e)}"

# Gradio Interface
gr_interface = gr.Interface(
    fn=predict_skin_disease,
    inputs=gr.Image(type="numpy", label="Upload an Image of Skin Lesion"),
    outputs=gr.Label(num_top_classes=3),  # Display top 3 predictions
    title="Skin Disease Classification",
    description="Upload an image of a skin lesion to classify it into various skin diseases."
)

# Launch the Gradio interface
gr_interface.launch()