The problem
Tuberculosis still ranks among the top infectious killers, and the chest X-ray is the frontline screening tool where sputum testing is slow or unavailable. The radiographic signs — apical nodules, cavitation, fibrotic streaking — are recognizable but reading thousands of screening films needs trained radiologists who are scarce in high-burden regions. Two public datasets exist precisely for this research, released by the U.S. National Library of Medicine: the Shenzhen set (662 posterior-anterior chest X-rays, 336 TB and 326 normal) and the Montgomery County set (138 films, 58 TB and 80 normal). This project fine-tunes a DenseNet121 on these films, but its real distinguishing feature is methodological: instead of reporting only in-distribution scores, it trains on one institution and tests on the other, confronting the domain-shift problem that decides whether any screening model works in practice. The demo makes this visible with a dedicated cross-dataset comparison view, and the documentation carries the medical-prototype disclaimer throughout.
How it works
- Chest X-rays from the Shenzhen and Montgomery sets are loaded and passed through a U-Net lung segmenter; each film is cropped to its lung ROI.
- ROIs are contrast-enhanced with CLAHE, resized to 512x512 and normalized with ImageNet statistics.
- Splits are made at the patient level; the cross-institution protocol additionally holds out one entire dataset for testing.
- A DenseNet121 backbone (ImageNet weights) with a dropout + sigmoid head trains with class-weighted binary cross-entropy under the AdamW optimizer, head first, then deeper blocks unfrozen.
- Augmentation — small rotations, horizontal flips, elastic distortion — is applied during training; every epoch logs validation metrics and the best checkpoint is kept.
- Evaluation reports AUC, sensitivity at 90% specificity and F1 both in-distribution and cross-institution.
- The demo app serves predictions with attention overlays, the pattern checklist, and the side-by-side dataset comparison view.
Tech stack:
- Python 3.10, PyTorch with torchvision DenseNet121
- OpenCV (loading, CLAHE, ROI cropping), NumPy, pandas
- scikit-learn (patient-level splits, metrics)
- Matplotlib, Seaborn (training curves, ROC curves)
- Flask demo app (classifier, attention, dataset-comparison views)
- Shenzhen TB X-ray set (662 CXRs) + Montgomery County set (138 CXRs), via NLM
| Parameter | Value |
|---|---|
| Model | DenseNet121 (ImageNet backbone), fine-tuned + dropout/sigmoid head |
| Task | Binary classification: TB manifestation vs normal |
| Input | PA chest X-ray, 512x512 after lung-ROI crop |
| Preprocessing | U-Net lung segmentation, CLAHE, ImageNet normalization |
| Datasets | Shenzhen 662 (336 TB / 326 normal); Montgomery 138 (58 TB / 80 normal) |
| Loss / optimizer | Class-weighted binary cross-entropy; AdamW, two-phase unfreezing |
| Output | P(TB) + attention overlay + pattern checklist |
| Evaluation | AUC, sensitivity at 90% specificity, F1 — in-distribution and cross-institution, computed during the build |
| Design target | Approximately 0.85+ AUC in-distribution (target, not a claimed result) |
| Demo | Flask app with 3 views |
Project features
- [DenseNet121 transfer learning] ImageNet-pretrained DenseNet121 fine-tuned with a two-phase schedule (head first, then deeper dense blocks via AdamW) for TB-vs-normal classification
- [Lung-segmentation pre-step] U-Net lung masks crop each film to the lung ROI before classification, so the model learns from lung tissue rather than background and annotations
- [Cross-institution evaluation] Train-on-Shenzhen/test-on-Montgomery protocol (and reverse) with AUC, sensitivity-at-90%-specificity and F1 reported per direction — the honest generalization test
- [Attention overlays] Grad-CAM-style maps from the final dense block highlight the regions (typically upper lobes) driving each decision
- [Radiographic pattern checklist] Demo overlay annotating nodules, cavitation, fibrotic streaking, effusion and miliary pattern for teaching purposes
- [Three-view demo app] Classifier view, attention-map toggle, and a dataset-comparison view showing both collections side by side
- [512x512 high-resolution pipeline] CLAHE enhancement and ImageNet normalization tuned for the digitized film quality of these sets
- [Viva-ready documentation] Report with dataset analysis, domain-shift discussion, error analysis and the medical-prototype disclaimer stated explicitly
What is included
- Complete source code (segmentation, training, evaluation, demo app)
- Fine-tuned model weights exported from the included training run
- Jupyter training and evaluation notebook (buyer-run procedure: train, cross-test, evaluate)
- Project report PDF (background, dataset analysis, domain-shift discussion, evaluation, error analysis, medical-prototype disclaimer)
- PPT presentation for final review
- Viva Q&A preparation document (DenseNet, transfer learning, domain shift, metrics)
- Setup guide (environment, dataset download, training, running the demo)
Limitations & prerequisites
- This is an academic research prototype, not a medical device: TB diagnosis requires sputum testing (e.g. CBNAAT) and a radiologist; this system never replaces that pathway.
- No AUC or sensitivity figure is promised: cross-institution scores are expected to be lower than in-distribution ones, and the report documents the measured gap honestly.
- The two datasets are small by deep-learning standards (800 films combined) and come from specific screening programs; performance on other populations is unvalidated.
- The model outputs a film-level score, not a localized diagnosis; it does not type TB manifestations (cavitation vs effusion) in the base build.
- Digitized-film artifacts and pediatric cases in the Shenzhen set add noise that bounds achievable performance.
Frequently Asked Questions
Which datasets are used?
Two public NLM collections: the Shenzhen set (662 PA chest X-rays — 336 TB, 326 normal, from Shenzhen No.3 People's Hospital) and the Montgomery County set (138 films — 58 TB, 80 normal, from the Maryland TB-control screening program). Both are freely available for research.
What is cross-institution evaluation, and why does it matter?
Training on Shenzhen and testing on Montgomery (and vice versa) measures whether the model learned TB patterns or just one hospital's scanner characteristics. Scores almost always drop across institutions; documenting that drop honestly is the central methodological contribution of this build.
Is the accuracy guaranteed?
No. The design target is approximately 0.85+ AUC in-distribution, but every figure — AUC, sensitivity at 90% specificity, F1, per-direction scores — is computed by the training notebook during the build and reported with the gap between in-distribution and cross-institution results.
Is this a medical device? Can it diagnose TB?
No. This is an educational research prototype. TB diagnosis needs microbiological confirmation and a qualified radiologist; this system produces an assistive score only and is never a substitute for clinical diagnosis.
Why the lung-segmentation step?
Raw films contain shoulders, text annotations and collimation edges that a classifier can latch onto as shortcuts. Cropping to the U-Net lung ROI forces the model to decide from lung tissue, which the report demonstrates with ablation-style discussion.
Can it distinguish TB from pneumonia or COVID patterns?
Not in the base build — it is a TB-vs-normal classifier on these two datasets. Multi-condition classification is documented future scope and would need additional labeled data. Suitable for B.E./B.Tech final-year projects in Computer Science, AI/ML and Biomedical streams.
Components & software requirements
- Python 3.10, PyTorch with torchvision DenseNet121
- OpenCV (loading, CLAHE, ROI cropping), NumPy, pandas
- scikit-learn (patient-level splits, metrics)
- Matplotlib, Seaborn (training curves, ROC curves)
- Flask demo app (classifier, attention, dataset-comparison views)
- Shenzhen TB X-ray set (662 CXRs) + Montgomery County set (138 CXRs), via NLM
Delivery information
Built-to-order project. Delivery timeline is shared after order confirmation based on current queue.
Support terms
Complete documentation, setup guide, and viva preparation included. Support for setup and explanation provided.