পাঠ ৩৪ · ৩৫-এর মধ্যে · মডিউল ৪
Home / AI Courses / Computer Vision / Project: medical segmentation

প্রজেক্ট: medical image segmentation

Project: chest X-ray lung segmentation with U-Net
১৫ মিনিট পড়া উচ্চ · Advanced Project

এই project-এ যা করবেন

  • Medical dataset preprocess
  • U-Net training with Dice loss
  • Augmentation, evaluation
  • Bangladesh hospital deployment consideration

১ · Project scope

  • Chest X-ray → both lung mask।
  • Application: TB screening, COVID lesion area, pneumonia।
  • Bangladesh need — TB high prevalence, radiologist shortage।
Why medical CV matters in Bangladesh

ICDDR,B, BIRDEM, BSMMU — research আছে। 1 radiologist per ~50,000 people। AI-assisted screening — rural area-এ life-saving। Responsible deployment essential।

২ · Dataset

  • Montgomery Country (MC): 138 chest X-ray, lung mask annotated। Public NIH।
  • Shenzhen: 662 chest X-ray (Bangladesh-relevant, TB cases)।
  • JSRT: 247 X-ray, lung + heart mask।
  • NIH ChestX-ray14: 100K X-ray (no mask)।
  • Bangladesh data: ICDDR,B partnership essential for local validation।

৩ · Preprocessing

Python · OpenCV
import cv2
import numpy as np

def preprocess_xray(img_path, target_size=512):
    img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)

    # Histogram equalization — contrast enhance
    img = cv2.equalizeHist(img)

    # Or CLAHE — adaptive
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    img = clahe.apply(img)

    # Resize maintaining aspect ratio
    h, w = img.shape
    scale = target_size / max(h, w)
    new_h, new_w = int(h*scale), int(w*scale)
    img = cv2.resize(img, (new_w, new_h))

    # Pad to square
    pad_h = target_size - new_h
    pad_w = target_size - new_w
    img = cv2.copyMakeBorder(img, pad_h//2, pad_h-pad_h//2,
                              pad_w//2, pad_w-pad_w//2,
                              cv2.BORDER_CONSTANT, value=0)

    # Normalize
    img = img.astype(np.float32) / 255.0
    return img

    

৪ · U-Net with ResNet encoder

Python · PyTorch + segmentation_models
# pip install segmentation-models-pytorch
import segmentation_models_pytorch as smp
import torch

model = smp.Unet(
    encoder_name="resnet50",        # ImageNet pretrained
    encoder_weights="imagenet",
    in_channels=1,                   # grayscale X-ray
    classes=1,                       # binary mask (lung vs background)
    activation=None                  # apply sigmoid externally
)

# Test
x = torch.randn(1, 1, 512, 512)
y = model(x)
print("Output:", y.shape)            # (1, 1, 512, 512)

    

৫ · Loss function

Python · PyTorch
import torch
import torch.nn as nn

class DiceBCELoss(nn.Module):
    def __init__(self, dice_weight=0.5):
        super().__init__()
        self.bce = nn.BCEWithLogitsLoss()
        self.dice_weight = dice_weight

    def forward(self, pred, target):
        bce = self.bce(pred, target)

        pred_sig = torch.sigmoid(pred)
        smooth = 1.0
        intersection = (pred_sig * target).sum()
        dice = 1 - (2*intersection + smooth) / (pred_sig.sum() + target.sum() + smooth)

        return bce + self.dice_weight * dice

criterion = DiceBCELoss(dice_weight=0.5)

    

Dice — class imbalance robust। BCE — pixel confidence। Combined — best of both।

৬ · Training loop

Python · PyTorch
from torch.utils.data import DataLoader
from torch.optim import AdamW
from torch.optim.lr_scheduler import CosineAnnealingLR

# Train setup
model = model.to('cuda')
optimizer = AdamW(model.parameters(), lr=1e-4, weight_decay=1e-4)
scheduler = CosineAnnealingLR(optimizer, T_max=50)

train_loader = DataLoader(train_dataset, batch_size=8, shuffle=True, num_workers=4)
val_loader = DataLoader(val_dataset, batch_size=8)

best_dice = 0
for epoch in range(50):
    model.train()
    train_loss = 0
    for images, masks in train_loader:
        images, masks = images.to('cuda'), masks.to('cuda')
        optimizer.zero_grad()
        pred = model(images)
        loss = criterion(pred, masks)
        loss.backward()
        optimizer.step()
        train_loss += loss.item()

    # Validation
    model.eval()
    val_dice = 0
    with torch.no_grad():
        for images, masks in val_loader:
            images, masks = images.to('cuda'), masks.to('cuda')
            pred = torch.sigmoid(model(images))
            pred_bin = (pred > 0.5).float()
            dice = (2 * (pred_bin * masks).sum() + 1) / (pred_bin.sum() + masks.sum() + 1)
            val_dice += dice.item()

    val_dice /= len(val_loader)
    scheduler.step()

    print(f"Epoch {epoch+1}: train_loss={train_loss/len(train_loader):.4f}, val_dice={val_dice:.4f}")

    if val_dice > best_dice:
        best_dice = val_dice
        torch.save(model.state_dict(), 'best_lung_unet.pth')

    

৭ · Augmentation

import albumentations as A

train_transform = A.Compose([
    A.Resize(512, 512),
    A.HorizontalFlip(p=0.5),                  # safe (chest symmetry-ish)
    A.Rotate(limit=10, p=0.5),                # small rotation
    A.RandomBrightnessContrast(p=0.3),
    A.GaussNoise(var_limit=0.01, p=0.3),
    A.ElasticTransform(alpha=1, sigma=50, p=0.2),  # tissue elasticity
    A.Normalize(mean=[0.5], std=[0.25]),
])

Avoid: vertical flip (heart asymmetry), large rotation (>30°)।

৮ · Evaluation metrics

  • Dice coefficient: 2|A∩B| / (|A|+|B|) — primary।
  • IoU: Jaccard index।
  • Sensitivity (recall): TP / (TP + FN) — miss critical।
  • Specificity: TN / (TN + FP) — false alarm।
  • Hausdorff distance: boundary precision।

Medical priority: high sensitivity (don't miss disease) over specificity (false alarm OK)।

Medical AI = "second opinion to radiologist" — replacement nয়। Dice 0.95 doesn't mean clinical accuracy। Real-world validation, FDA pathway, clinician trust — all required।
Medical CV — research to clinic 📁 Dataset Montgomery 🔧 Preprocess CLAHE, normalize 🎯 Train U-Net + Dice 📊 Validate 5-fold CV 🏥 Pilot hospital trial 📜 Approval DGDA/FDA Bangladesh — DGDA approval pathway, hospital partnership Research → pilot → multi-center → deployment 2-3 year typical timeline; ethics review essential
Medical AI project — research-grade থেকে clinical-grade-এ পথ। Bangladesh DGDA regulatory framework।

৯ · DICOM handling

Hospital-এ X-ray DICOM format — 12-16 bit, metadata। Conversion essential।

# pip install pydicom
import pydicom
import numpy as np

def load_dicom(path):
    ds = pydicom.dcmread(path)
    img = ds.pixel_array

    # 16-bit → 8-bit windowing
    # Use DICOM window center/width if present
    wc = ds.get('WindowCenter', np.median(img))
    ww = ds.get('WindowWidth', img.max() - img.min())

    img_min = wc - ww/2
    img_max = wc + ww/2
    img = np.clip(img, img_min, img_max)
    img = ((img - img_min) / (img_max - img_min) * 255).astype(np.uint8)

    return img, ds  # also return metadata

১০ · Inference + visualize

def predict_lung_mask(img_path, model_path='best_lung_unet.pth'):
    model = smp.Unet(encoder_name="resnet50", in_channels=1, classes=1)
    model.load_state_dict(torch.load(model_path))
    model.eval().cuda()

    img = preprocess_xray(img_path)
    img_tensor = torch.tensor(img).unsqueeze(0).unsqueeze(0).cuda()

    with torch.no_grad():
        pred = torch.sigmoid(model(img_tensor))
        mask = (pred > 0.5).cpu().numpy()[0, 0]

    # Calculate lung area (cm² estimate)
    lung_pixel_count = mask.sum()
    return mask, lung_pixel_count

১১ · Bangladesh deployment consideration

Regulatory:

  • DGDA approval: Bangladesh Directorate General of Drug Administration।
  • Software as Medical Device — clarify level।
  • Multi-site validation।

Hospital integration:

  • PACS (Picture Archiving) integration।
  • HIS (Hospital Information System) interface।
  • HL7/DICOM compliance।
  • Workflow seamless।

Clinical validation:

  • 3+ radiologist concordance।
  • Bangladeshi patient demographic data।
  • Edge case analysis।
  • Failure mode documentation।

Ethics:

  • BMRC (Bangladesh Medical Research Council) approval।
  • Patient consent।
  • Data anonymization।
  • Bias audit।

১২ · Bangladesh-specific challenges

  • Image quality: rural hospital — older X-ray equipment, more noise।
  • Demographic: Bangladesh chest morphology — need local data validation।
  • TB prevalence: high — model bias toward TB-positive may overconfide।
  • Compute infrastructure: rural — offline inference essential।
  • Internet: intermittent — local server needed।

১৩ · Beyond lung — extensions

  • TB lesion detect: next layer।
  • COVID lesion: pneumonia distinguishing।
  • Pneumothorax: emergency।
  • Cardiomegaly: heart enlargement।
  • Multi-class: all in one model।
Medical AI deploy responsible — not just accurate। Misdiagnosis liability, regulatory hurdle, ethics critical। Bangladesh ICDDR,B, BSMMU partnership essential।

ভাবনার প্রশ্ন

প্র ০১ Medical AI accuracy 95%+ হলেও hospital deploy অনেক years lag-এ। কেন?

"AI hype" vs "clinical reality" gap — major barriers।

Validation gap:

  • Lab dataset ≠ hospital reality।
  • Prospective trial — months to years।
  • Multi-site validation।

Regulatory complexity:

  • FDA/CE/DGDA approval — 2-5 year typical।
  • Documentation extensive।
  • Software-as-medical-device classification।
  • Update protocol challenge (modeling drift)।

Clinical workflow:

  • Doctor habit — change resistance।
  • EMR integration — IT cost।
  • Liability — who responsible if error?
  • Reimbursement code — insurance।

Bias risk:

  • Training demographic — deployment population mismatch।
  • Equipment variation।
  • Edge case undocumented।

Trust building:

  • Explainability — Grad-CAM, attention map।
  • Calibrated confidence — when uncertain, say so।
  • Failure mode transparent।

Bangladesh specific:

  • DGDA framework gradually maturing।
  • Local validation cohort essential।
  • Pilot program (BIRDEM, NCC) longer।
  • Engineer + clinician collaboration critical।

Cost-benefit reality:

  • Bangladesh — radiologist scarcity makes AI valuable।
  • Rural deployment particular need।
  • Cost-effective vs urban radiologist visit।

মূল উপলব্ধি: Medical AI — high accuracy necessary not sufficient। Trust, workflow, regulatory — clinical adoption-এর co-equal challenges।

প্র ০২ Dice loss vs cross-entropy — medical-এ কেন Dice prefer? Limitations?

Loss function selection — medical CV-র subtle but critical decision।

Cross-entropy issues:

  • Per-pixel — class imbalance dominate background।
  • Lung segment 30% pixel typical — borderline OK।
  • Tumor segment 1% pixel — fail।

Dice advantage:

  • Ratio-based — class size invariant।
  • Direct optimize evaluation metric।
  • Foreground-focused।

Dice limitations:

  • Discontinuous gradient at zero overlap।
  • Boundary refinement-এ weak gradient।
  • Sensitivity to small object।

Combined Dice + BCE:

  • BCE — pixel-level confidence signal।
  • Dice — overlap-level optimize।
  • Best of both।

Other medical loss:

  • Tversky: $\alpha, \beta$ — false positive/negative weight।
  • Focal Tversky: hard case focus।
  • Hausdorff loss: boundary precision।
  • Boundary loss: edge-aware।

Per-task selection:

  • Lung segment (30% pixel): BCE + Dice OK।
  • Tumor (1% pixel): Dice + Focal।
  • Boundary critical: + boundary loss।

Hyperparameter:

  • Dice weight 0.3-0.7 typical।
  • Trade-off pixel accuracy vs overlap।

মূল উপলব্ধি: Loss = task encode mathematical। Dice = overlap-aware। Medical task structure-aware loss critical।

প্র ০৩ Bangladesh hospital pilot — 100 patient। Promising। Scale up — কী challenge?

Pilot to scale — most ML projects fail here।

Pilot success factors:

  • Curated dataset।
  • Experienced team।
  • Best equipment।
  • Single hospital culture।

Scale challenges:

  • Equipment variability: different X-ray machine — calibration ভিন্ন।
  • Patient diversity: rural vs urban, demographic, comorbidity।
  • Workflow variation: protocol differ across hospital।
  • Operator skill: X-ray technician training varies।
  • IT infrastructure: some no PACS, internet poor।

Specific issues:

  • Domain shift — accuracy drops 10-20%।
  • Edge cases unanticipated।
  • Confidence calibration off।
  • UI/UX assumptions wrong।

Mitigation:

  • Multi-site dataset: 5-10 hospital cohort।
  • Continuous learning: failure case retrain।
  • Calibration layer: per-site confidence adjust।
  • Human-in-the-loop: low confidence radiologist review।
  • Monitoring dashboard: performance per site track।

Bangladesh roadmap:

  1. Pilot: 100 patient single hospital (BIRDEM)।
  2. Validation: 1000 patient, 5 hospital।
  3. Multi-site cohort: 10K patient।
  4. DGDA submission।
  5. Scale-up: nationwide।

Stakeholders:

  • Government (Health Ministry)।
  • Hospital administration।
  • Doctor, technician training।
  • Patient education।
  • Insurance company।

Cost reality:

  • Pilot: $50K-100K।
  • Validation: $500K-1M।
  • Scale: $5M+।
  • Funding: USAID, GAVI, Gates Foundation common।

মূল উপলব্ধি: "Pilot success ≠ scale success"। Bangladesh medical AI — multi-stakeholder, multi-year journey। Engineering 30%, rest sociology, regulation, business।

প্র ০৪ "Explainable AI" medical-এ critical। Grad-CAM, attention map কীভাবে use? Limitation?

Medical AI trust — transparency essential।

Grad-CAM (২০১৬):

  • "Where did model look?" heatmap।
  • Gradient-based attribution।
  • Last conv layer activation × gradient।
  • Coarse but interpretable।

Implementation:

from pytorch_grad_cam import GradCAM

target_layer = model.encoder.layer4
cam = GradCAM(model=model, target_layers=[target_layer])
grayscale_cam = cam(input_tensor=img, targets=None)[0, :]
# Overlay on original image

Use cases:

  • Doctor verify — model focusing on relevant region।
  • Failure case analyze — model confused by what।
  • Trust building।

Other techniques:

  • SHAP: per-pixel contribution।
  • LIME: local linear approximation।
  • Attention map (transformer): direct attention weight।
  • Counterfactual: "what would change prediction?"।

Limitations:

  • Coarse — pixel-level attribution rough।
  • Method-dependent — different XAI different output।
  • Confirmation bias — radiologist looks where model points।
  • Pseudo-explanation — model may attend wrong region but predict right।

Best practice:

  • Multi-method comparison।
  • Sanity check — random model XAI compare।
  • Quantitative metric (Pointing Game)।
  • Doctor + AI joint decision।

Calibration:

  • Confidence score reliable চাই।
  • Temperature scaling।
  • Conformal prediction।
  • "95% confident" actually 95% accurate।

Regulatory:

  • EU AI Act — explainability requirement।
  • FDA — increasing focus।
  • DGDA — emerging framework।

মূল উপলব্ধি: Black-box ≠ acceptable in medicine। XAI imperfect but essential। Engineering responsibility — interpretable system build।

অনুশীলন

  1. Setup: Montgomery dataset download, U-Net train baseline।

    Code section ৪-৬। Free Colab GPU 2-3 hour। Final Dice ~0.95 expected।

  2. Visualize: Test image-এ predict + Grad-CAM overlay।

    pytorch-grad-cam library use। Lung region attention concentrate confirm।

  3. ভাবুন: ICDDR,B-এ TB screening AI deploy — কী additional steps medical-grade হতে?

    (1) Multi-site Bangladesh data collect। (2) BMRC ethics approval। (3) Radiologist concordance study। (4) Workflow integration। (5) DGDA submission। (6) Continuous monitoring। 2-3 year timeline minimum।

আরও পড়ুন · ABCL TECH-এ আপনার পরবর্তী পদক্ষেপ

কোড রানার কাজ না করলে? Google Colab use করুন।
পূর্ববর্তী পাঠ
পাঠ ৩৩ · Project: realtime detection