Spaces:
Sleeping
Sleeping
File size: 6,758 Bytes
5bd1dc2 | 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 | """
Model Loading Utilities
Loads all three trained models (Fundus, OCT, Multimodal Fusion)
"""
import torch
import torch.nn as nn
from torchvision import models
from pathlib import Path
import os
class FundusModel(nn.Module):
"""ResNet50-based CNN for Fundus image classification"""
def __init__(self, num_classes=2):
super(FundusModel, self).__init__()
resnet = models.resnet50(weights=None)
self.features = nn.Sequential(*list(resnet.children())[:-1])
num_features = resnet.fc.in_features
self.classifier = nn.Sequential(
nn.Dropout(0.3),
nn.Linear(num_features, 256),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(256, num_classes)
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
x = self.classifier(x)
return x
def extract_features(self, x):
x = self.features(x)
x = x.view(x.size(0), -1)
return x
class OCTCNN_Optimized(nn.Module):
"""ResNet50-based CNN for OCT image classification"""
def __init__(self, num_classes=7):
super(OCTCNN_Optimized, self).__init__()
self.resnet = models.resnet50(weights=None)
in_features = self.resnet.fc.in_features
self.resnet.fc = nn.Sequential(
nn.Dropout(0.4),
nn.Linear(in_features, 512),
nn.ReLU(),
nn.BatchNorm1d(512),
nn.Dropout(0.3),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.2),
nn.Linear(256, num_classes)
)
def forward(self, x):
return self.resnet(x)
def extract_features(self, x):
x = self.resnet.conv1(x)
x = self.resnet.bn1(x)
x = self.resnet.relu(x)
x = self.resnet.maxpool(x)
x = self.resnet.layer1(x)
x = self.resnet.layer2(x)
x = self.resnet.layer3(x)
x = self.resnet.layer4(x)
x = self.resnet.avgpool(x)
x = torch.flatten(x, 1)
return x
class MultimodalFusionModel(nn.Module):
"""Multimodal fusion model combining Fundus and OCT"""
def __init__(self, fundus_model, oct_model, num_classes=2):
super(MultimodalFusionModel, self).__init__()
self.fundus_model = fundus_model
self.oct_model = oct_model
fusion_input_dim = 2048 + 2048 + 2 # Fundus + OCT + Clinical
self.fusion_head = nn.Sequential(
nn.Linear(fusion_input_dim, 512),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(512, 256),
nn.ReLU(),
nn.Dropout(0.3),
nn.Linear(256, num_classes)
)
def forward(self, fundus_img, oct_img, clinical_features):
f_feat = self.fundus_model.extract_features(fundus_img)
o_feat = self.oct_model.extract_features(oct_img)
combined = torch.cat((f_feat, o_feat, clinical_features), dim=1)
return self.fusion_head(combined)
class ModelLoader:
"""Loads and manages all models"""
def __init__(self):
self.device = torch.device('cpu') # Force CPU for deployment availability
print(f"Using device: {self.device}")
# Paths to model checkpoints (Modified for deployment structure)
# Assumes models are in the SAME directory as this script
self.models_dir = Path(__file__).parent
self.fundus_path = self.models_dir / 'fundus_cnn_best.pth'
self.oct_path = self.models_dir / 'oct_resnet50_best.pth'
self.fusion_path = self.models_dir / 'multimodal_fusion_best.pth'
# Load models
self.fundus_model = self._load_fundus_model()
self.oct_model = self._load_oct_model()
self.fusion_model = self._load_fusion_model()
# Class names
self.fundus_classes = ['GON-', 'GON+']
self.oct_classes = ['AMD', 'DME', 'ERM', 'NO', 'RAO', 'RVO', 'VID']
self.fusion_classes = ['NORMAL', 'GLAUCOMA']
def _load_fundus_model(self):
"""Load Fundus model"""
print(f"Loading Fundus model from {self.fundus_path}")
model = FundusModel(num_classes=2)
if self.fundus_path.exists():
checkpoint = torch.load(self.fundus_path, map_location=self.device, weights_only=False)
if 'model_state_dict' in checkpoint:
model.load_state_dict(checkpoint['model_state_dict'], strict=False)
else:
model.load_state_dict(checkpoint, strict=False)
print("✓ Fundus model loaded")
else:
print(f"⚠ Warning: Fundus model not found at {self.fundus_path}")
model = model.to(self.device)
model.eval()
return model
def _load_oct_model(self):
"""Load OCT model"""
print(f"Loading OCT model from {self.oct_path}")
model = OCTCNN_Optimized(num_classes=7)
if self.oct_path.exists():
checkpoint = torch.load(self.oct_path, map_location=self.device, weights_only=False)
if 'model_state_dict' in checkpoint:
model.load_state_dict(checkpoint['model_state_dict'], strict=False)
else:
model.load_state_dict(checkpoint, strict=False)
print("✓ OCT model loaded")
else:
print(f"⚠ Warning: OCT model not found at {self.oct_path}")
model = model.to(self.device)
model.eval()
return model
def _load_fusion_model(self):
"""Load Multimodal Fusion model"""
print(f"Loading Fusion model from {self.fusion_path}")
# Create base models for fusion
fundus_base = FundusModel(num_classes=2)
oct_base = OCTCNN_Optimized(num_classes=7)
model = MultimodalFusionModel(fundus_base, oct_base, num_classes=2)
if self.fusion_path.exists():
checkpoint = torch.load(self.fusion_path, map_location=self.device, weights_only=False)
if isinstance(checkpoint, dict) and 'model_state_dict' in checkpoint:
model.load_state_dict(checkpoint['model_state_dict'], strict=False)
else:
model.load_state_dict(checkpoint, strict=False)
print("✓ Fusion model loaded")
else:
print(f"⚠ Warning: Fusion model not found at {self.fusion_path}")
model = model.to(self.device)
model.eval()
return model
|