File size: 10,517 Bytes
84a0314 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | """
inference.py β ConvNeXt Dual-Modal Skin Lesion Classifier
ISIC 2025 / MILK10k | CC BY-NC 4.0
Classifies skin lesions from paired dermoscopic + clinical images into 11 categories.
Used as a tool called by MedGemma in the Skin AI application.
"""
import os
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import timm
from PIL import Image
import torchvision.transforms as transforms
from pathlib import Path
from typing import Union
# βββββββββββββββββββββββββββββββββββββββββββββ
# Constants
# βββββββββββββββββββββββββββββββββββββββββββββ
CLASS_NAMES = ['AKIEC', 'BCC', 'BEN_OTH', 'BKL', 'DF',
'INF', 'MAL_OTH', 'MEL', 'NV', 'SCCKA', 'VASC']
CLASS_DESCRIPTIONS = {
'AKIEC': 'Actinic keratosis / intraepithelial carcinoma',
'BCC': 'Basal cell carcinoma',
'BEN_OTH': 'Other benign lesion',
'BKL': 'Benign keratosis',
'DF': 'Dermatofibroma',
'INF': 'Inflammatory / infectious',
'MAL_OTH': 'Other malignant lesion',
'MEL': 'Melanoma',
'NV': 'Melanocytic nevus',
'SCCKA': 'Squamous cell carcinoma / keratoacanthoma',
'VASC': 'Vascular lesion',
}
IMG_SIZE = 384
TRANSFORM = transforms.Compose([
transforms.Resize((IMG_SIZE, IMG_SIZE)),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
# βββββββββββββββββββββββββββββββββββββββββββββ
# Architecture
# βββββββββββββββββββββββββββββββββββββββββββββ
class DualConvNeXt(nn.Module):
"""
Dual-input ConvNeXt-Base for paired dermoscopic + clinical image classification.
Both encoders share the same architecture but are trained independently.
"""
def __init__(self, num_classes: int = 11, model_name: str = 'convnext_base'):
super().__init__()
self.clinical_encoder = timm.create_model(
model_name, pretrained=False, num_classes=0
)
self.derm_encoder = timm.create_model(
model_name, pretrained=False, num_classes=0
)
feat_dim = self.clinical_encoder.num_features # 1024 for convnext_base
self.classifier = nn.Sequential(
nn.Linear(feat_dim * 2, 512),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(512, num_classes)
)
def forward(self, clinical: torch.Tensor, derm: torch.Tensor) -> torch.Tensor:
c = self.clinical_encoder(clinical)
d = self.derm_encoder(derm)
return self.classifier(torch.cat([c, d], dim=1))
# βββββββββββββββββββββββββββββββββββββββββββββ
# Model loading
# βββββββββββββββββββββββββββββββββββββββββββββ
def load_model(
weights_path: Union[str, Path],
device: torch.device = None
) -> DualConvNeXt:
"""
Load a trained DualConvNeXt model from a checkpoint file.
Args:
weights_path: Path to .pth checkpoint (expects dict with 'model_state_dict')
device: torch.device β defaults to CUDA if available
Returns:
Loaded model in eval mode
"""
if device is None:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = DualConvNeXt(num_classes=len(CLASS_NAMES))
checkpoint = torch.load(weights_path, map_location=device)
# Handle both raw state dict and wrapped checkpoints
state = checkpoint.get('model_state_dict', checkpoint)
model.load_state_dict(state)
model.eval().to(device)
return model
def load_ensemble(
weights_dir: Union[str, Path],
device: torch.device = None
) -> list:
"""
Load all fold models from a directory for ensemble inference.
Args:
weights_dir: Directory containing convnext_fold*.pth files
device: torch.device
Returns:
List of loaded models
"""
if device is None:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
weights_dir = Path(weights_dir)
model_paths = sorted(weights_dir.glob('convnext_fold*.pth'))
if not model_paths:
raise FileNotFoundError(f"No fold checkpoints found in {weights_dir}")
models = [load_model(p, device) for p in model_paths]
print(f"Loaded {len(models)} fold models from {weights_dir}")
return models
# βββββββββββββββββββββββββββββββββββββββββββββ
# Preprocessing
# βββββββββββββββββββββββββββββββββββββββββββββ
def preprocess_image(image_path: Union[str, Path]) -> torch.Tensor:
"""Load and preprocess a single image to model input format."""
img = Image.open(image_path).convert('RGB')
return TRANSFORM(img)
# βββββββββββββββββββββββββββββββββββββββββββββ
# Inference
# βββββββββββββββββββββββββββββββββββββββββββββ
def predict_single(
model: DualConvNeXt,
clinical_path: Union[str, Path],
derm_path: Union[str, Path],
device: torch.device = None
) -> dict:
"""
Run inference with a single model.
Args:
model: Loaded DualConvNeXt model
clinical_path: Path to clinical close-up image
derm_path: Path to dermoscopic image
device: torch.device
Returns:
dict with prediction, confidence, and per-class probabilities
"""
if device is None:
device = next(model.parameters()).device
clinical = preprocess_image(clinical_path).unsqueeze(0).to(device)
derm = preprocess_image(derm_path).unsqueeze(0).to(device)
with torch.no_grad():
logits = model(clinical, derm)
probs = F.softmax(logits, dim=1).squeeze().cpu().numpy()
pred_idx = int(probs.argmax())
return {
'prediction': CLASS_NAMES[pred_idx],
'description': CLASS_DESCRIPTIONS[CLASS_NAMES[pred_idx]],
'confidence': float(probs[pred_idx]),
'probabilities': {c: float(p) for c, p in zip(CLASS_NAMES, probs)}
}
def predict_ensemble(
models: list,
clinical_path: Union[str, Path],
derm_path: Union[str, Path],
device: torch.device = None
) -> dict:
"""
Run ensemble inference by averaging softmax probabilities across fold models.
Args:
models: List of loaded DualConvNeXt models
clinical_path: Path to clinical close-up image
derm_path: Path to dermoscopic image
device: torch.device
Returns:
dict with ensemble prediction, confidence, per-class probabilities,
and per-model probability breakdown
"""
if device is None:
device = next(models[0].parameters()).device
clinical = preprocess_image(clinical_path).unsqueeze(0).to(device)
derm = preprocess_image(derm_path).unsqueeze(0).to(device)
all_probs = []
with torch.no_grad():
for model in models:
logits = model(clinical, derm)
probs = F.softmax(logits, dim=1).squeeze().cpu().numpy()
all_probs.append(probs)
ensemble_probs = np.mean(all_probs, axis=0)
pred_idx = int(ensemble_probs.argmax())
return {
'prediction': CLASS_NAMES[pred_idx],
'description': CLASS_DESCRIPTIONS[CLASS_NAMES[pred_idx]],
'confidence': float(ensemble_probs[pred_idx]),
'probabilities': {c: float(p) for c, p in zip(CLASS_NAMES, ensemble_probs)},
'n_models': len(models)
}
# βββββββββββββββββββββββββββββββββββββββββββββ
# Batch inference
# βββββββββββββββββββββββββββββββββββββββββββββ
def predict_batch(
models: list,
pairs: list,
device: torch.device = None
) -> list:
"""
Run ensemble inference over a batch of image pairs.
Args:
models: List of loaded DualConvNeXt models
pairs: List of (clinical_path, derm_path) tuples
device: torch.device
Returns:
List of result dicts (same format as predict_ensemble)
"""
return [predict_ensemble(models, c, d, device) for c, d in pairs]
# βββββββββββββββββββββββββββββββββββββββββββββ
# CLI / Quick test
# βββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Skin lesion classifier inference')
parser.add_argument('--clinical', required=True, help='Path to clinical image')
parser.add_argument('--derm', required=True, help='Path to dermoscopic image')
parser.add_argument('--weights', required=True,
help='Path to .pth checkpoint or directory of fold checkpoints')
args = parser.parse_args()
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"Using device: {device}")
weights_path = Path(args.weights)
if weights_path.is_dir():
models = load_ensemble(weights_path, device)
result = predict_ensemble(models, args.clinical, args.derm, device)
print(f"\nEnsemble ({result['n_models']} models)")
else:
model = load_model(weights_path, device)
result = predict_single(model, args.clinical, args.derm, device)
print(f"Prediction: {result['prediction']} β {result['description']}")
print(f"Confidence: {result['confidence']:.1%}")
print("\nAll class probabilities:")
for cls, prob in sorted(result['probabilities'].items(),
key=lambda x: x[1], reverse=True):
bar = 'β' * int(prob * 30)
print(f" {cls:8s} {prob:.3f} {bar}")
|