🫁

مشروع كشف الالتهاب الرئوي بالذكاء الاصطناعي

AI-Powered Pneumonia Detection Project

نظام ذكي لتحليل صور الأشعة السينية واكتشاف الالتهاب الرئوي باستخدام التعلم العميق

An intelligent system for analyzing chest X-ray images and detecting pneumonia using deep learning

🚀 جرب التطبيق مباشرة 🚀 Try the Live Demo

اضغط هنا لتجربة نظام كشف الالتهاب الرئوي

Click here to try the pneumonia detection system

📋 نظرة عامة على المشروع

📋 Project Overview

🎯

الهدف

Goal

بناء نموذج ذكاء اصطناعي يقدر يكتشف الالتهاب الرئوي من صور الأشعة السينية للصدر بدقة عالية تساعد الأطباء في التشخيص السريع.

Build an AI model that can detect pneumonia from chest X-ray images with high accuracy to assist doctors in rapid diagnosis.

🧠

التقنية المستخدمة

Technology Used

استخدمنا Transfer Learning مع EfficientNet (B0, B3, B4) عشان نستفيد من النماذج المدربة مسبقاً ونضيف طبقات مخصصة للمشكلة بتاعتنا.

We used Transfer Learning with EfficientNet (B0, B3, B4) to leverage pre-trained models and add custom layers for our specific problem.

🔄

Ensemble Learning

جمعنا أكتر من موديل مع بعض (Ensemble) عشان نحصل على أفضل دقة ممكنة - كل موديل بيساهم في القرار النهائي.

We combined multiple models together (Ensemble) to achieve the best possible accuracy - each model contributes to the final decision.

🌐

Deployment

جهزنا API بـ FastAPI وواجهة مستخدم بـ Streamlit، مع دعم Docker للنشر على Railway, Render, Azure وغيرهم.

We built an API with FastAPI and a user interface with Streamlit, with Docker support for deployment on Railway, Render, Azure and more.

📊 نتائج الموديل

📊 Model Results

89.3%
الدقة الإجمالية (Accuracy)
Overall Accuracy
95.2%
ROC-AUC Score
88.7%
الحساسية (Sensitivity)
Sensitivity
90.2%
النوعية (Specificity)
Specificity

⚠️ أهم المشكلات اللي واجهتنا والحلول

⚠️ Key Challenges We Faced & Solutions

1 مشكلة GPU و cuDNN GPU & cuDNN Issue

التدريب كان بيقع بخطأ UNKNOWN: <unknown cudnn status: 5003>. المشكلة كانت عدم توافق بين TensorFlow 2.20.0 و cuDNN drivers مع كارت NVIDIA GeForce GTX 1070.

Training crashed with error UNKNOWN: <unknown cudnn status: 5003>. The issue was incompatibility between TensorFlow 2.20.0 and cuDNN drivers with NVIDIA GeForce GTX 1070.

✅ الحل
✅ Solution

حولنا التدريب لـ CPU فقط باستخدام CUDA_VISIBLE_DEVICES="". التدريب اشتغل تمام بس أبطأ شوية (2-3 ساعات بدل دقائق).

Switched to CPU-only training using CUDA_VISIBLE_DEVICES="". Training worked fine but slower (2-3 hours instead of minutes).

2 صور تالفة في Dataset Corrupted Images in Dataset

لقينا صور corrupted في الـ dataset كانت بتوقف التدريب وبتعمل errors. الصور دي كانت بتسبب مشاكل في الـ data pipeline.

Found corrupted images in the dataset that were stopping training and causing errors. These images were causing issues in the data pipeline.

✅ الحل
✅ Solution

عملنا سكريبت fix_corrupted_images.py يكتشف الصور التالفة ويحركها لفولدر backup. كده الـ training اشتغل من غير مشاكل.

Created fix_corrupted_images.py script to detect corrupted images and move them to a backup folder. Training then worked without issues.

3 Sensitivity منخفضة Low Sensitivity

الموديل كان بيفوّت حالات مرضية كتير (False Negatives). في البداية الـ Sensitivity كانت 66% بس - يعني 33% من المرضى ممكن يتفوتوا!

Model was missing many sick cases (False Negatives). Initially Sensitivity was only 66% - meaning 33% of patients could be missed!

✅ الحل
✅ Solution

خفضنا الـ Threshold من 0.5 لـ 0.4، واستخدمنا Ensemble من عدة موديلات، وزودنا الـ augmentation. وصلنا لـ 88.7% sensitivity.

Reduced Threshold from 0.5 to 0.4, used Ensemble of multiple models, and increased augmentation. Reached 88.7% sensitivity.

4 مشاكل Docker و Railway Docker & Railway Issues

واجهنا أخطاء في بناء Docker image على Railway:
- ملف requirements_api.txt مش موجود
- أسماء packages قديمة ومش متوافقة
- FastAPI module not found

Encountered errors building Docker image on Railway:
- File requirements_api.txt not found
- Outdated and incompatible package names
- FastAPI module not found

✅ الحل
✅ Solution

صلحنا الـ Dockerfile وأضفنا الملفات الناقصة وحدّثنا أسماء الـ packages. كده الـ build اشتغل صح على Railway.

Fixed the Dockerfile, added missing files, and updated package names. The build then worked correctly on Railway.

5 TensorFlow Module Errors

ظهرت أخطاء ModuleNotFoundError: No module named 'tensorflow.python' بسبب مشاكل في الـ installation.

Got errors ModuleNotFoundError: No module named 'tensorflow.python' due to installation issues.

✅ الحل
✅ Solution

عملنا reinstall لـ TensorFlow مع الإصدار الصح (tensorflow-cpu للـ deployment) وأصلحنا الـ imports.

Reinstalled TensorFlow with the correct version (tensorflow-cpu for deployment) and fixed imports.

6 حجم الصور مش متطابق Image Size Mismatch

الصور جاية بأحجام مختلفة والموديل محتاج حجم ثابت 224×224. ده كان بيسبب errors في الـ evaluation.

Images came in different sizes while the model needed a fixed 224×224 size. This was causing errors in evaluation.

✅ الحل
✅ Solution

أضفنا preprocessing layer في الموديل يعمل resize تلقائي لكل الصور، ووحّدنا الـ config في كل الملفات.

Added a preprocessing layer in the model for automatic resize of all images, and unified the config across all files.

7 🆕 الموديل بيقبل أي صورة! 🆕 Model Accepts Any Image!

اكتشفنا إن الموديل كان بيقبل أي صورة (حتى صور الأشخاص أو المناظر الطبيعية) وبيدي نتيجة تشخيص! ده خطير جداً لأن الموديل مفروض يستقبل صور أشعة الصدر فقط.

أمثلة على صور تم قبولها بالخطأ:
• Screenshots من سطح المكتب
• صور Motherboards وقطع الكمبيوتر
• صور الفريق (Team Photos)
• شرائح العروض التقديمية (Business Slides)

Discovered that the model was accepting any image (even selfies or landscapes) and giving diagnosis results! Very dangerous since the model should only accept chest X-rays.

Examples of incorrectly accepted images:
• Desktop screenshots
• Motherboard and computer parts photos
• Team photos
• Business presentation slides

✅ الحل: تدريب Classifier مخصص للتعرف على صور الأشعة
✅ Solution: Train a Custom X-ray Classifier

المحاولة الأولى (فشلت): استخدام Grayscale Threshold
• جربنا رفض الصور اللي الـ grayscale score بتاعها أقل من 0.93
• المشكلة: صور كتير (screenshots، slides) عندها grayscale score عالي برضو!

الحل النهائي: تدريب Binary Classifier
• درّبنا موديل MobileNetV2 مخصص للتفريق بين X-rays وغيرها
• استخدمنا ~700 صورة X-ray + ~300 صورة non-X-ray للتدريب
• الموديل بيدي score من 0 لـ 1 (أعلى من 0.5 = X-ray)

النتائج:
• ✅ Dataset X-ray: score = 1.000 (مقبول)
• ✅ User X-ray: score = 0.998 (مقبول)
• ❌ Motherboard: score = 0.004 (مرفوض)
• ❌ Screenshot: score = 0.003 (مرفوض)
• ❌ Team Photo: score = 0.001 (مرفوض)

الملف: xray_validator.py + results/models/xray_detector.keras

First Attempt (Failed): Using Grayscale Threshold
• Tried rejecting images with grayscale score below 0.93
• Problem: Many images (screenshots, slides) also have high grayscale scores!

Final Solution: Train Binary Classifier
• Trained a custom MobileNetV2 model to distinguish X-rays from non-X-rays
• Used ~700 X-ray images + ~300 non-X-ray images for training
• Model gives score from 0 to 1 (above 0.5 = X-ray)

Results:
• ✅ Dataset X-ray: score = 1.000 (accepted)
• ✅ User X-ray: score = 0.998 (accepted)
• ❌ Motherboard: score = 0.004 (rejected)
• ❌ Screenshot: score = 0.003 (rejected)
• ❌ Team Photo: score = 0.001 (rejected)

Files: xray_validator.py + results/models/xray_detector.keras

🛠️ التقنيات المستخدمة

🛠️ Technologies Used

🐍
Python 3.11
🧠
TensorFlow
FastAPI
🎈
Streamlit
🐳
Docker
📊
EfficientNet

🏗️ بنية المشروع

🏗️ Project Architecture

📷
رفع صورة
Upload Image
🔍
X-ray Validation
🔄
Preprocessing
🧠
EfficientNet
📊
Ensemble
التشخيص
Diagnosis

🆕 جديد: خطوة X-ray Validation بتتأكد إن الصورة المرفوعة هي أشعة صدر فعلاً قبل التشخيص

🆕 New: X-ray Validation step ensures the uploaded image is actually a chest X-ray before diagnosis

📅 مراحل المشروع

📅 Project Phases

1️⃣ إعداد البيئة والبيانات
1️⃣ Environment & Data Setup

تجهيز Python environment، تنزيل الـ dataset، ومعالجة الصور التالفة.

Setting up Python environment, downloading dataset, and handling corrupted images.

2️⃣ بناء وتدريب الموديل
2️⃣ Model Building & Training

تجربة موديلات مختلفة (B0, B3, B4) مع تقنيات augmentation متنوعة.

Testing different models (B0, B3, B4) with various augmentation techniques.

3️⃣ تحسين الأداء
3️⃣ Performance Optimization

استخدام Ensemble learning وتحسين الـ threshold للوصول لأفضل نتائج.

Using Ensemble learning and optimizing threshold for best results.

4️⃣ بناء الـ API والواجهة
4️⃣ API & Interface Development

تطوير FastAPI backend وStreamlit frontend للاستخدام السهل.

Developing FastAPI backend and Streamlit frontend for easy use.

5️⃣ الـ Deployment
5️⃣ Deployment

إعداد Docker وملفات التكوين للنشر على منصات سحابية مختلفة.

Setting up Docker and configuration files for deployment on various cloud platforms.

📁 ملفات المشروع الرئيسية

📁 Main Project Files

🧠 model.py

الملف الرئيسي للتدريب - يحتوي على بناء الموديل، الـ training loop، والـ callbacks.

Main training file - contains model building, training loop, and callbacks.

⚡ api.py

FastAPI server للـ REST API - يستقبل الصور ويرجع التشخيص.

FastAPI server for REST API - receives images and returns diagnosis.

🎈 streamlit_app.py

واجهة المستخدم الرسومية - سهلة الاستخدام للأطباء.

Graphical user interface - easy to use for doctors.

📊 evaluate_model.py

سكريبت تقييم الموديل وإنتاج التقارير والرسومات البيانية.

Model evaluation script that produces reports and charts.

🔄 ensemble_predict.py

دمج عدة موديلات للحصول على تنبؤات أكثر دقة.

Combines multiple models for more accurate predictions.

🐳 Dockerfile

ملف Docker للـ containerization والنشر السحابي.

Docker file for containerization and cloud deployment.

🔍 xray_validator.py جديدNEW

نظام التحقق من صور الأشعة - يتأكد إن الصورة المرفوعة هي أشعة صدر فعلاً قبل التشخيص.

X-ray image validation system - ensures uploaded image is actually a chest X-ray before diagnosis.

🧠 فريق الذكاء الاصطناعي

🧠 AI Team

Eng. Abdelwahab Radi

Eng. Abdelwahab Radi

Lead AI Engineer

(Model Training & Development)

Eng. Mohamed Ali

Eng. Mohamed Ali

AI Developer

Eng. Abdelrahman Saleh

Eng. Abdelrahman Saleh

Data Scientist

🎯 الخلاصة

🎯 Conclusion

المشروع ده مثال عملي على استخدام الذكاء الاصطناعي في المجال الطبي. واجهنا تحديات كتير من مشاكل الـ GPU لحد مشاكل الـ deployment، بس في النهاية وصلنا لنموذج بدقة 89.3% وROC-AUC 95.2%.

النموذج ممكن يستخدم كأداة مساعدة للأطباء في التشخيص السريع، مع التأكيد إنه مش بديل عن الخبرة الطبية.

⚠️ تنويه: المشروع ده للأغراض التعليمية ومش للاستخدام الطبي الفعلي بدون إشراف طبي.

This project is a practical example of using AI in the medical field. We faced many challenges from GPU issues to deployment problems, but ultimately achieved a model with 89.3% accuracy and 95.2% ROC-AUC.

The model can be used as a supporting tool for doctors in rapid diagnosis, while emphasizing that it is not a replacement for medical expertise.

⚠️ Disclaimer: This project is for educational purposes and not for actual medical use without medical supervision.