نظام ذكي لتحليل صور الأشعة السينية واكتشاف الالتهاب الرئوي باستخدام التعلم العميق
An intelligent system for analyzing chest X-ray images and detecting pneumonia using deep learning
اضغط هنا لتجربة نظام كشف الالتهاب الرئوي
Click here to try the pneumonia detection system
بناء نموذج ذكاء اصطناعي يقدر يكتشف الالتهاب الرئوي من صور الأشعة السينية للصدر بدقة عالية تساعد الأطباء في التشخيص السريع.
Build an AI model that can detect pneumonia from chest X-ray images with high accuracy to assist doctors in rapid diagnosis.
استخدمنا 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) عشان نحصل على أفضل دقة ممكنة - كل موديل بيساهم في القرار النهائي.
We combined multiple models together (Ensemble) to achieve the best possible accuracy - each model contributes to the final decision.
جهزنا 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.
التدريب كان بيقع بخطأ 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.
حولنا التدريب لـ 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).
لقينا صور 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.
عملنا سكريبت 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.
الموديل كان بيفوّت حالات مرضية كتير (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!
خفضنا الـ 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.
واجهنا أخطاء في بناء 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
صلحنا الـ Dockerfile وأضفنا الملفات الناقصة وحدّثنا أسماء الـ packages. كده الـ build اشتغل صح على Railway.
Fixed the Dockerfile, added missing files, and updated package names. The build then worked correctly on Railway.
ظهرت أخطاء ModuleNotFoundError: No module named 'tensorflow.python'
بسبب مشاكل في الـ installation.
Got errors ModuleNotFoundError: No module named 'tensorflow.python'
due to installation issues.
عملنا reinstall لـ TensorFlow مع الإصدار الصح (tensorflow-cpu للـ
deployment) وأصلحنا الـ imports.
Reinstalled TensorFlow with the correct version (tensorflow-cpu for
deployment) and fixed imports.
الصور جاية بأحجام مختلفة والموديل محتاج حجم ثابت 224×224. ده كان بيسبب errors في الـ evaluation.
Images came in different sizes while the model needed a fixed 224×224 size. This was causing errors in evaluation.
أضفنا preprocessing layer في الموديل يعمل resize تلقائي لكل الصور، ووحّدنا الـ config في كل الملفات.
Added a preprocessing layer in the model for automatic resize of all images, and unified the config across all files.
اكتشفنا إن الموديل كان بيقبل أي صورة (حتى صور الأشخاص أو المناظر الطبيعية)
وبيدي نتيجة تشخيص! ده خطير جداً لأن الموديل مفروض يستقبل صور أشعة الصدر فقط.
أمثلة على صور تم قبولها بالخطأ:
• 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
المحاولة الأولى (فشلت): استخدام 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
🆕 جديد: خطوة X-ray Validation بتتأكد إن الصورة المرفوعة هي أشعة صدر فعلاً قبل التشخيص
🆕 New: X-ray Validation step ensures the uploaded image is actually a chest X-ray before diagnosis
تجهيز Python environment، تنزيل الـ dataset، ومعالجة الصور التالفة.
Setting up Python environment, downloading dataset, and handling corrupted images.
تجربة موديلات مختلفة (B0, B3, B4) مع تقنيات augmentation متنوعة.
Testing different models (B0, B3, B4) with various augmentation techniques.
استخدام Ensemble learning وتحسين الـ threshold للوصول لأفضل نتائج.
Using Ensemble learning and optimizing threshold for best results.
تطوير FastAPI backend وStreamlit frontend للاستخدام السهل.
Developing FastAPI backend and Streamlit frontend for easy use.
إعداد Docker وملفات التكوين للنشر على منصات سحابية مختلفة.
Setting up Docker and configuration files for deployment on various cloud platforms.
الملف الرئيسي للتدريب - يحتوي على بناء الموديل، الـ training loop، والـ callbacks.
Main training file - contains model building, training loop, and callbacks.
FastAPI server للـ REST API - يستقبل الصور ويرجع التشخيص.
FastAPI server for REST API - receives images and returns diagnosis.
واجهة المستخدم الرسومية - سهلة الاستخدام للأطباء.
Graphical user interface - easy to use for doctors.
سكريبت تقييم الموديل وإنتاج التقارير والرسومات البيانية.
Model evaluation script that produces reports and charts.
دمج عدة موديلات للحصول على تنبؤات أكثر دقة.
Combines multiple models for more accurate predictions.
ملف Docker للـ containerization والنشر السحابي.
Docker file for containerization and cloud deployment.
نظام التحقق من صور الأشعة - يتأكد إن الصورة المرفوعة هي أشعة صدر فعلاً قبل التشخيص.
X-ray image validation system - ensures uploaded image is actually a chest X-ray before diagnosis.
Lead AI Engineer
(Model Training & Development)
AI Developer
Data Scientist
المشروع ده مثال عملي على استخدام الذكاء الاصطناعي في المجال الطبي.
واجهنا تحديات كتير من مشاكل الـ 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.