Web demo hỗ trợ phân loại bệnh Alzheimer từ ảnh MRI 3D và dữ liệu lâm sàng, đồng thời trực quan hóa các vùng ảnh MRI ảnh hưởng đến dự đoán bằng XAI (Explainable Artificial Intelligence - trí tuệ nhân tạo có thể giải thích).
Hệ thống phục vụ demo khóa luận với hai luồng chính:
- Classification (phân loại): phân loại ba lớp
CN,MCI,ADbằng best multimodal model (mô hình đa phương thức tốt nhất). - XAI Visualization (trực quan hóa XAI): tạo heatmap (bản đồ nhiệt) cho các MRI-only models (mô hình chỉ dùng MRI) bằng
Grad-CAM,Grad-CAM++,Ablation-CAM.
Ứng dụng cho phép người dùng upload file MRI tensor .pt. Backend tự suy ra subject_id từ tên file theo mẫu ADNI, ví dụ:
005_S_1224.pt
005_S_1224_mri.pt
Sau khi có subject_id, backend tự động:
- Tìm dữ liệu lâm sàng trong file CSV.
- Tìm file NIfTI trong thư mục nội bộ.
- Kiểm tra hippocampus mask (mặt nạ hồi hải mã) đã tồn tại hay chưa.
- Nếu cần, tạo hippocampus mask từ NIfTI và Harvard-Oxford atlas.
Kết quả hiển thị trên web gồm:
- Nhãn dự đoán
CN/MCI/AD. - Confidence (độ tin cậy) và xác suất từng lớp.
- Ảnh XAI summary (ảnh tổng quan) theo phong cách notebook Kaggle.
- Ảnh axial, coronal, sagittal gồm original (ảnh gốc), heatmap và overlay (ảnh chồng lớp).
Backend:
- FastAPI
- PyTorch
- MONAI
- scikit-learn
- nibabel, nilearn
- matplotlib, Pillow
Frontend:
- Next.js
- React
- TypeScript
- Tailwind CSS
- shadcn-style local UI components
- lucide-react icons
Alzheimer-Web-GUI/
├── backend/
│ ├── app/
│ │ ├── model_definitions/
│ │ ├── routers/
│ │ ├── services/
│ │ └── xai/
│ ├── data/
│ ├── models/
│ ├── outputs/
│ ├── tests/
│ ├── config.py
│ ├── main.py
│ └── requirements.txt
├── frontend/
│ ├── src/
│ │ ├── app/
│ │ ├── components/
│ │ └── lib/
│ ├── package.json
│ └── tailwind.config.ts
├── notebook/
├── .gitignore
└── README.md
Do model checkpoint, MRI, NIfTI và dữ liệu bệnh nhân có kích thước lớn hoặc nhạy cảm, các file này không nên commit lên Git.
Best classification model (mô hình phân loại tốt nhất):
backend/models/classification/best_model.pt
Checkpoint XAI MRI-only:
backend/models/xai/densenet121.pt
backend/models/xai/efficientnet_b0.pt
backend/models/xai/seresnet50.pt
backend/models/xai/swin-transformer.pt
backend/models/xai/hybridnet.pt
CSV clinical mặc định:
backend/data/patient_clinical_data.csv
CSV cần có các cột clinical:
Age, MMSE, FAQ_Score, Global_CDR, APOE4, Gender, Education, ADAS11, ADAS13
Các cột ID được hỗ trợ:
subject_id, Subject, PTID, RID
Thư mục NIfTI mặc định:
D:/KhoaLuanTotNghiep/processed_dataset/bias_corrected
Có thể thay đổi bằng biến môi trường:
$env:NIFTI_DATA_DIR="D:/path/to/nifti"
$env:CLINICAL_CSV_PATH="D:/path/to/patient_clinical_data.csv"Pattern NIfTI được backend tìm:
{subject_id}_mni_bias_corrected.nii
{subject_id}_mni_bias_corrected.nii.gz
{subject_id}*.nii
{subject_id}*.nii.gz
cd backend
python -m pip install -r requirements.txtChạy backend:
python -m uvicorn main:app --host 127.0.0.1 --port 8000 --reloadKiểm tra health check:
Invoke-RestMethod http://127.0.0.1:8000/api/healthcd frontend
npm install
npm run dev -- --hostname 127.0.0.1 --port 3000Mở web:
http://127.0.0.1:3000
Health check:
GET /api/health
Thông tin model:
GET /api/models
Danh sách bệnh nhân từ CSV:
GET /api/patients
GET /api/patients/{subject_id}
Metadata sau khi upload MRI:
POST /api/xai/mri-metadata
Response gồm subject, MRI shape, clinical status, NIfTI status và mask status.
Phân loại bằng best model:
POST /api/predict-best
XAI:
POST /api/xai/explain
POST /api/xai/gradcam
/api/xai/explain hỗ trợ:
method=gradcammethod=gradcam_plusmethod=ablationcam
- Chọn file MRI
.pt. - Hệ thống tự suy ra
subject_id. - Backend tự tìm clinical data trong CSV.
- Backend tự tìm NIfTI trong
NIFTI_DATA_DIR. - Bấm Chạy phân loại để xem kết quả
CN/MCI/AD. - Bấm Tạo heatmap để sinh ảnh XAI.
Nếu không tìm thấy clinical data, giao diện mới hiển thị form nhập clinical_vector thủ công.
Nếu không tìm thấy NIfTI, XAI vẫn chạy nhưng không có contour hồi hải mã.
Backend:
python -m compileall backend
python backend/tests/test_xai_mri_metadata.py
python backend/tests/test_xai_gradcam.py
python backend/tests/test_xai_gradcam_plus.py
python backend/tests/test_xai_ablationcam.py
python backend/tests/test_xai_kaggle_style.pyFrontend:
cd frontend
npm run lint
npm run build.gitignore đã loại các file lớn và dữ liệu sinh ra:
backend/models/backend/outputs/backend/data/patient_clinical_data.csvbackend/data/nilearn_data/backend/data/hippocampus_masks/*.pt,*.pth,*.nii,*.nii.gzfrontend/node_modules/,frontend/.next/
Không commit checkpoint model, dữ liệu bệnh nhân, output XAI hoặc MRI gốc lên repository công khai.
Hệ thống này là web demo phục vụ nghiên cứu/khóa luận. Kết quả phân loại và XAI không thay thế chẩn đoán y khoa.
This web demo supports Alzheimer classification from 3D MRI scans and clinical data, while also visualizing MRI regions that influence the prediction using XAI (Explainable Artificial Intelligence).
The system is designed for a thesis demo and includes two main workflows:
- Classification: classifies three labels
CN,MCI, andADusing the best multimodal model. - XAI Visualization: generates heatmaps for MRI-only models using
Grad-CAM,Grad-CAM++, andAblation-CAM.
The application allows users to upload an MRI tensor file .pt. The backend automatically infers the subject_id from the filename using the ADNI format, for example:
005_S_1224.pt
005_S_1224_mri.pt
After obtaining the subject_id, the backend automatically:
- Searches for the corresponding clinical data in the CSV file.
- Searches for the matching NIfTI file in the internal data directory.
- Checks whether the hippocampus mask already exists.
- If needed, generates a hippocampus mask from the NIfTI file and the Harvard-Oxford atlas.
The web result includes:
- Predicted label
CN/MCI/AD. - Confidence score and class probabilities.
- Kaggle-notebook-style XAI summary image.
- Axial, coronal, and sagittal images, including original image, heatmap, and overlay.
Backend:
- FastAPI
- PyTorch
- MONAI
- scikit-learn
- nibabel, nilearn
- matplotlib, Pillow
Frontend:
- Next.js
- React
- TypeScript
- Tailwind CSS
- shadcn-style local UI components
- lucide-react icons
Alzheimer-Web-GUI/
├── backend/
│ ├── app/
│ │ ├── model_definitions/
│ │ ├── routers/
│ │ ├── services/
│ │ └── xai/
│ ├── data/
│ ├── models/
│ ├── outputs/
│ ├── tests/
│ ├── config.py
│ ├── main.py
│ └── requirements.txt
├── frontend/
│ ├── src/
│ │ ├── app/
│ │ ├── components/
│ │ └── lib/
│ ├── package.json
│ └── tailwind.config.ts
├── notebook/
├── .gitignore
└── README.md
Model checkpoints, MRI files, NIfTI files, and patient data can be large or sensitive, so they should not be committed to Git.
Best classification model:
backend/models/classification/best_model.pt
MRI-only XAI checkpoints:
backend/models/xai/densenet121.pt
backend/models/xai/efficientnet_b0.pt
backend/models/xai/seresnet50.pt
backend/models/xai/swin-transformer.pt
backend/models/xai/hybridnet.pt
Default clinical CSV:
backend/data/patient_clinical_data.csv
Required clinical columns:
Age, MMSE, FAQ_Score, Global_CDR, APOE4, Gender, Education, ADAS11, ADAS13
Supported ID columns:
subject_id, Subject, PTID, RID
Default NIfTI directory:
D:/KhoaLuanTotNghiep/processed_dataset/bias_corrected
You can override the paths using environment variables:
$env:NIFTI_DATA_DIR="D:/path/to/nifti"
$env:CLINICAL_CSV_PATH="D:/path/to/patient_clinical_data.csv"NIfTI filename patterns searched by the backend:
{subject_id}_mni_bias_corrected.nii
{subject_id}_mni_bias_corrected.nii.gz
{subject_id}*.nii
{subject_id}*.nii.gz
cd backend
python -m pip install -r requirements.txtRun backend:
python -m uvicorn main:app --host 127.0.0.1 --port 8000 --reloadHealth check:
Invoke-RestMethod http://127.0.0.1:8000/api/healthcd frontend
npm install
npm run dev -- --hostname 127.0.0.1 --port 3000Open the web app:
http://127.0.0.1:3000
Health check:
GET /api/health
Model information:
GET /api/models
Patient list and patient detail from CSV:
GET /api/patients
GET /api/patients/{subject_id}
MRI metadata after upload:
POST /api/xai/mri-metadata
The response includes subject information, MRI shape, clinical status, NIfTI status, and mask status.
Best model classification:
POST /api/predict-best
XAI:
POST /api/xai/explain
POST /api/xai/gradcam
/api/xai/explain supports:
method=gradcammethod=gradcam_plusmethod=ablationcam
- Select an MRI
.ptfile. - The system automatically infers the
subject_id. - The backend automatically searches for clinical data in the CSV file.
- The backend automatically searches for the NIfTI file in
NIFTI_DATA_DIR. - Click Chạy phân loại to view the
CN/MCI/ADprediction. - Click Tạo heatmap to generate XAI images.
If clinical data is not found, the interface will display a manual clinical_vector input form.
If the NIfTI file is not found, XAI can still run, but the hippocampus contour will not be displayed.
Backend:
python -m compileall backend
python backend/tests/test_xai_mri_metadata.py
python backend/tests/test_xai_gradcam.py
python backend/tests/test_xai_gradcam_plus.py
python backend/tests/test_xai_ablationcam.py
python backend/tests/test_xai_kaggle_style.pyFrontend:
cd frontend
npm run lint
npm run build.gitignore excludes large files and generated data:
backend/models/backend/outputs/backend/data/patient_clinical_data.csvbackend/data/nilearn_data/backend/data/hippocampus_masks/*.pt,*.pth,*.nii,*.nii.gzfrontend/node_modules/,frontend/.next/
Do not commit model checkpoints, patient data, XAI outputs, or original MRI files to a public repository.
This system is a research/thesis demo. Classification and XAI results must not be used as a replacement for medical diagnosis.