Model β’ Training β’ API β’ System β’ Setup β’ Results
DermaVision is an end-to-end AI-powered web application that leverages deep learning for real-time skin lesion classification. The platform combines a custom-trained CNN model with Spatial Transformer Networks (STN) and transfer learning from MobileNetV2 to deliver accurate, accessible dermatological screening.
Key Highlights:
- π§ MobileNetV2 with Spatial Transformer Network achieving 97.45% training accuracy and 87.27% validation accuracy
- π¬ Transfer learning with strategic fine-tuning of top 50 layers
- π Comprehensive class imbalance handling with computed class weights
- π Multi-format deployment: TensorFlow SavedModel, TFLite, and TensorFlow. js
- π₯ Complete telemedicine platform with appointment booking and role-based dashboards
- π Designed to democratize dermatological care access across Africa
π View the Complete Training Notebook - Full implementation with code, outputs, and training logs
The classification model is built on MobileNetV2 architecture enhanced with a Spatial Transformer Network (STN) to handle variations in image capture (rotation, scaling, translation). MobileNetV2 was selected for its efficiency and suitability for mobile and embedded vision applications, loaded with ImageNet weights (alpha=0.75) for transfer learning.
Input Image (224x224x3)
β
βββββββββββββββββββββββββββββββββββββββ
β MobileNetV2 Preprocessing β
β β’ tf.keras.applications. mobilenet β
β . preprocess_input β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β MobileNetV2 Base (ImageNet) β
β β’ alpha=0.75 (optimized size) β
β β’ Top 50 layers unfrozen β
β β’ Transfer learning enabled β
β β’ 1,915,501 total parameters β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Spatial Transformer Network β
β β’ Localization Network (Conv+Pool)β
β β’ Grid Generator (Affine params) β
β β’ Bilinear Sampler β
β β’ L2 Regularization (0.001) β
βββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββββββββββββββββββ
β Classification Head β
β β’ GlobalAveragePooling2D β
β β’ BatchNormalization β
β β’ Dropout(0.4) β
β β’ Dense(7) + L2(0.01) + Softmax β
βββββββββββββββββββββββββββββββββββββββ
β
Prediction (7 classes)
| Component | Parameters | Trainable |
|---|---|---|
| Total | 1,915,501 (7. 31 MB) | - |
| Trainable | 1,662,397 (6.34 MB) | β |
| Non-trainable | 253,104 (988.69 KB) | β |
The STN dynamically transforms input feature maps, enabling the network to learn spatial invarianceβcritical for medical imaging where precise alignment impacts diagnostic accuracy.
# STN Architecture (from compressed_Model_Training_Notebook.ipynb)
def spatial_transformer_network(inputs):
# Localization Network
localization = Conv2D(16, (5,5), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(0.001))(inputs)
localization = MaxPooling2D((2,2))(localization)
localization = BatchNormalization()(localization)
localization = Conv2D(32, (3,3), activation='relu', padding='same',
kernel_regularizer=regularizers.l2(0.001))(localization)
localization = MaxPooling2D((2,2))(localization)
localization = BatchNormalization()(localization)
localization = Flatten()(localization)
localization = Dense(64, activation='relu',
kernel_regularizer=regularizers.l2(0.001))(localization)
localization = Dropout(0.3)(localization)
# Affine transformation parameters (identity initialization)
theta = Dense(6, weights=[np.zeros((64, 6)),
np.array([1, 0, 0, 0, 1, 0])])(localization)
# Grid Generator + Bilinear Sampler
output = Lambda(lambda x: stn(x))([theta, inputs])
return output| Component | Function |
|---|---|
| Localization Network | Predicts 6 affine transformation parameters using Conv + Pooling layers |
| Grid Generator | Creates sampling grid based on transformation parameters |
| Bilinear Sampler | Warps input feature maps using differentiable interpolation |
Primary Dataset: HAM10000 (Human Against Machine with 10,000 training images)
- Total Images: 10,015 dermatoscopic images
- Training Set: 8,012 images (80%)
- Validation Set: 2,003 images (20%)
- Split Strategy: Stratified by diagnosis class (
random_state=42)
| Class | Condition | Description |
|---|---|---|
| AKIEC | Actinic Keratoses | Pre-cancerous lesions from sun damage (Bowen's disease) |
| BCC | Basal Cell Carcinoma | Common skin cancer with low metastasis |
| BKL | Benign Keratosis | Non-cancerous growths (keratosis-like lesions) |
| DF | Dermatofibroma | Benign skin tumors |
| NV | Melanocytic Nevi | Common moles |
| MEL | Melanoma | Aggressive form of skin cancer |
| VASC | Vascular Lesions | Blood vessel-associated lesions |
π Full training implementation:
compressed_Model_Training_Notebook.ipynb
Aggressive data augmentation was applied using ImageDataGenerator to enhance generalization and mitigate overfitting by simulating real-world imaging conditions:
# From compressed_Model_Training_Notebook.ipynb - Cell 4
datagen = ImageDataGenerator(
preprocessing_function=tf.keras.applications. mobilenet.preprocess_input,
rotation_range=20, # Random rotations Β±20Β°
width_shift_range=0.2, # Horizontal shifts Β±20%
height_shift_range=0.2, # Vertical shifts Β±20%
shear_range=0.2, # Shear transformations
zoom_range=0.2, # Random zoom
horizontal_flip=True, # Horizontal flipping
vertical_flip=True, # Vertical flipping
fill_mode='nearest' # Pixel fill strategy
)| Parameter | Value | Rationale |
|---|---|---|
| Optimizer | Adam | Adaptive learning rate optimization |
| Initial Learning Rate | 0.001 β 1e-5 (fine-tuning) | Two-phase training strategy |
| Loss Function | Categorical Cross-Entropy | Multi-class classification |
| Epochs | 50 | Sufficient convergence with early stopping |
| Batch Size | 64 | Optimized for GPU memory |
| Class Weights | Computed via sklearn.utils.class_weight |
Addresses HAM10000 class imbalance |
| L2 Regularization | 0.01 (dense), 0.001 (STN) | Prevents overfitting |
| Dropout Rate | 0.4 (classification), 0.3 (STN) | Additional regularization |
# From compressed_Model_Training_Notebook. ipynb - Cell 7
callbacks = [
# Save best model based on validation top-3 accuracy
ModelCheckpoint(
filepath='/content/drive/My Drive/MODEL_CORRECTION/model_unquant.keras',
monitor='val_top_3_accuracy',
verbose=1,
save_best_only=True,
mode='max'
),
# Dynamic learning rate reduction
ReduceLROnPlateau(
monitor='val_top_3_accuracy',
factor=0.5,
patience=2,
verbose=1,
mode='max',
min_lr=1e-9
),
# TensorBoard for visualization
TensorBoard(log_dir='./logs')
]
# Class weight computation for imbalanced dataset
class_weights = compute_class_weight(
class_weight="balanced",
classes=np.unique(train_generator.classes),
y=train_generator.classes
)| Metric | Purpose |
|---|---|
| Categorical Accuracy | Primary classification accuracy |
| Top-3 Accuracy | Clinically relevantβcorrect diagnosis in top 3 predictions |
| Per-class Precision | Accuracy of positive predictions per condition |
| Per-class Recall | Sensitivity for each skin condition |
| F1-Score | Harmonic mean of precision and recall |
| Metric | Score |
|---|---|
| Training Accuracy | 97.45% |
| Validation Accuracy | 87.27% |
The model was rigorously evaluated using:
- Classification Report: Per-class precision, recall, F1-score, and support metrics
- Confusion Matrix: Visual analysis of prediction patterns and misclassification tendencies
The model demonstrates strong generalization from training data, making it reliable for preliminary skin disease diagnosis while maintaining clinical relevance through top-3 accuracy monitoring.
The trained model was exported in three formats to maximize deployment flexibility:
| Format | Use Case | Path |
|---|---|---|
| TensorFlow SavedModel | Server-side inference, TensorFlow Serving | model_unquant_savedmodel/ |
| TensorFlow Lite (. tflite) | Mobile/embedded devices | model_unquant. tflite |
| TensorFlow.js | Browser-based inference | tfjs_model/ |
# From compressed_Model_Training_Notebook.ipynb - Cell 7
# Save in SavedModel format
saved_model_path = '/content/drive/My Drive/MODEL_CORRECTION/model_unquant'
tf.saved_model.save(model, saved_model_path)
# TFLite conversion (optimized for mobile)
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_path)
tflite_model = converter.convert()
# TensorFlow.js conversion (for web deployment)
# tfjs. converters.save_keras_model(model, 'tfjs_model')The TensorFlow Lite model is deployed as a RESTful API using Flask on Google Cloud Run:
- β‘ Auto-scaling: Handles variable request volumes
- π Zero-downtime updates: Seamless model version updates
- π Global CDN: Low-latency responses worldwide
- π° Cost-efficient: Pay-per-use serverless architecture
skin-disease-api/
βββ app.py # Flask API server
βββ model_unquant.tflite # TFLite model (~10MB)
βββ Dockerfile # Container configuration
βββ requirements. txt # Python dependencies
βββ . gcloudignore # Cloud deployment ignore file
POST /predict
Content-Type: multipart/form-dataRequest:
curl -X POST \
-F "image=@skin_lesion. jpg" \
https://your-api-url/predictResponse:
{
"predictions": [
{"class": "MEL", "confidence": 0.85, "label": "Melanoma"},
{"class": "NV", "confidence": 0.10, "label": "Melanocytic Nevi"},
{"class": "BKL", "confidence": 0.05, "label": "Benign Keratosis"}
],
"top_prediction": "Melanoma",
"severity": "High"
}| Layer | Technology |
|---|---|
| Frontend | Next.js 13, React, TypeScript |
| Styling | Tailwind CSS, Framer Motion |
| Backend | Firebase (Auth, Firestore) |
| ML Training | TensorFlow/Keras, Google Colab |
| ML Inference | TensorFlow Lite, TensorFlow.js |
| API Framework | Python Flask |
| Deployment | Google Cloud Run, Firebase Hosting |
DERMAVISION/
βββ src/
β βββ app/ # Next.js 13 app router
β βββ components/ # Reusable React components
β βββ contexts/ # React context providers
β βββ Firebase/ # Firebase configuration
β βββ services/ # API service layer
β βββ types/ # TypeScript type definitions
β βββ utils/ # Utility functions
βββ skin-disease-api/ # Python ML API
β βββ app.py # Flask server
β βββ model_unquant.tflite # Trained model
β βββ Dockerfile # Container config
βββ compressed_Model_Training_Notebook.ipynb # π ML Training Notebook
βββ public/ # Static assets
βββ firebase.json # Firebase configuration
| Role | Features |
|---|---|
| Patient | Upload images, view analysis history, book appointments |
| Doctor | Manage appointments, view patient records, set availability |
| Admin | Verify doctors, manage users, view system statistics |
- Node.js v18+
- Python 3.8+
- Firebase account
- Google Cloud account (for API deployment)
1. Clone the repository
git clone https://github.com/devilsfave/Dermavision_AI.git
cd Dermavision_AI2. Install frontend dependencies
npm install
# or
yarn install3. Configure environment variables
cp .env. example .env. localAdd your Firebase configuration:
NEXT_PUBLIC_FIREBASE_API_KEY=your_api_key
NEXT_PUBLIC_FIREBASE_AUTH_DOMAIN=your_auth_domain
NEXT_PUBLIC_FIREBASE_PROJECT_ID=your_project_id
NEXT_PUBLIC_FIREBASE_STORAGE_BUCKET=your_storage_bucket
NEXT_PUBLIC_FIREBASE_MESSAGING_SENDER_ID=your_sender_id
NEXT_PUBLIC_FIREBASE_APP_ID=your_app_id4. Start the development server
npm run dev5. Run the ML API (separate terminal)
cd skin-disease-api
pip install -r requirements. txt
python app. pyTo retrain or fine-tune the model, open the training notebook:
# Open in Google Colab or Jupyter
jupyter notebook compressed_Model_Training_Notebook.ipynbcd skin-disease-api
gcloud builds submit --tag gcr.io/PROJECT_ID/skin-disease-api
gcloud run deploy --image gcr.io/PROJECT_ID/skin-disease-api --platform managed- π Authentication: Firebase Auth with role-based access control
- π‘οΈ Data Protection: Secure data storage with Firestore security rules
- π Privacy: HIPAA-compliant design principles
- π API Security: HTTPS encryption, CORS configuration
DermaVision AI was developed to address the critical shortage of dermatologists across the African continent. By leveraging AI for preliminary skin cancer screening, this platform aims to:
- Democratize Access: Provide quality dermatological screening in underserved regions
- Enable Early Detection: Identify potentially malignant lesions before they progress
- Support Healthcare Workers: Assist non-specialist clinicians with AI-powered second opinions
- Reduce Diagnostic Delays: Offer immediate preliminary assessments via mobile devices
- Geolocation for finding nearby dermatologists
- EHR (Electronic Health Records) integration
- Multilingual support (French, Swahili, Arabic)
- Offline capabilities with on-device TFLite inference
- Explainable AI (XAI) with Grad-CAM visualizations
- Federated learning for privacy-preserving model improvements
| Innovation | Implementation |
|---|---|
| Transfer Learning | MobileNetV2 with ImageNet weights, selective fine-tuning of top 50 layers |
| Spatial Invariance | Custom STN layer with bilinear interpolation for robust feature extraction |
| Class Imbalance | Dynamic class weight computation via scikit-learn |
| Multi-platform Deployment | SavedModel, TFLite, TensorFlow.js exports |
| Production-ready API | Containerized Flask on Google Cloud Run |
All training code, hyperparameters, and model weights are available for full reproducibility:
| Resource | Location |
|---|---|
| Training Notebook | compressed_Model_Training_Notebook.ipynb |
| Dataset | HAM10000 on Kaggle |
| Model Weights | Available upon request |
| API Code | skin-disease-api/ directory |
- Dataset: [ISIC HAM10000](https://dataverse.harvard. edu/dataset.xhtml?persistentId=doi:10.7910/DVN/DBW86T)
- University: University of Energy and Natural Resources, Sunyani, Ghana
- Frameworks: TensorFlow, Keras, Next.js, Firebase
Developer: devilsfave
Email: devilsfave39@gmail.com
Repository: github.com/devilsfave/Dermavision_AI
π Leveraging AI to democratize access to dermatological care across Africa π
Built with passion for healthcare equity and computational innovation
