Spaces:
Sleeping
Sleeping
Create amlm.py
Browse files- core/amlm.py +106 -0
core/amlm.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# ==========================================
|
| 6 |
+
# 1. MODEL CONFIGURATION (V7 Standard)
|
| 7 |
+
# ==========================================
|
| 8 |
+
# Fixed latent dimension matching your trained "amlm_v5.pth"
|
| 9 |
+
LATENT_DIM = 64
|
| 10 |
+
|
| 11 |
+
class AMLM_3D(nn.Module):
|
| 12 |
+
def __init__(self):
|
| 13 |
+
super(AMLM_3D, self).__init__()
|
| 14 |
+
|
| 15 |
+
# --- ENCODER ---
|
| 16 |
+
# Input: (Batch, 6, 64, 9)
|
| 17 |
+
# 6 Channels = 3 Value Channels (Data) + 3 Visibility Channels (Mask)
|
| 18 |
+
self.encoder = nn.Sequential(
|
| 19 |
+
# Layer 1: 64x9 -> 32x9
|
| 20 |
+
nn.Conv2d(6, 32, kernel_size=3, padding=1),
|
| 21 |
+
nn.ReLU(),
|
| 22 |
+
nn.MaxPool2d((2, 1)),
|
| 23 |
+
|
| 24 |
+
# Layer 2: 32x9 -> 16x9
|
| 25 |
+
nn.Conv2d(32, 64, kernel_size=3, padding=1),
|
| 26 |
+
nn.ReLU(),
|
| 27 |
+
nn.MaxPool2d((2, 1)),
|
| 28 |
+
|
| 29 |
+
# Flatten: 64 channels * 16 height * 9 width = 9216
|
| 30 |
+
nn.Flatten()
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
# --- BOTTLENECK ---
|
| 34 |
+
# Compresses the 9216 features into the 64-dim "Clinical Syndrome" vector
|
| 35 |
+
self.bottleneck = nn.Sequential(
|
| 36 |
+
nn.Linear(9216, 256),
|
| 37 |
+
nn.ReLU(),
|
| 38 |
+
nn.Linear(256, LATENT_DIM)
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# --- DECODER INPUT ---
|
| 42 |
+
# Expands 64-dim syndrome back to spatial features
|
| 43 |
+
self.decoder_input = nn.Sequential(
|
| 44 |
+
nn.Linear(LATENT_DIM, 256),
|
| 45 |
+
nn.ReLU(),
|
| 46 |
+
nn.Linear(256, 9216),
|
| 47 |
+
nn.ReLU()
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# --- DECODER ---
|
| 51 |
+
# Reconstructs the full probability map
|
| 52 |
+
self.decoder = nn.Sequential(
|
| 53 |
+
nn.ConvTranspose2d(64, 32, kernel_size=3, padding=1),
|
| 54 |
+
nn.Upsample(scale_factor=(2, 1)),
|
| 55 |
+
nn.ReLU(),
|
| 56 |
+
|
| 57 |
+
nn.ConvTranspose2d(32, 16, kernel_size=3, padding=1),
|
| 58 |
+
nn.Upsample(scale_factor=(2, 1)),
|
| 59 |
+
nn.ReLU(),
|
| 60 |
+
|
| 61 |
+
# Output: 3 Channels (Initial, Medial, Final)
|
| 62 |
+
nn.Conv2d(16, 3, kernel_size=3, padding=1),
|
| 63 |
+
nn.Sigmoid() # Squish output to 0.0 - 1.0 probability
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
def forward(self, x):
|
| 67 |
+
"""
|
| 68 |
+
Forward pass for inference.
|
| 69 |
+
x shape must be: (Batch_Size, 6, 64, 9)
|
| 70 |
+
"""
|
| 71 |
+
features = self.encoder(x)
|
| 72 |
+
latent = self.bottleneck(features)
|
| 73 |
+
|
| 74 |
+
x_recon = self.decoder_input(latent)
|
| 75 |
+
# Reshape back to spatial dimensions (Batch, Channels, Height, Width)
|
| 76 |
+
x_recon = x_recon.view(-1, 64, 16, 9)
|
| 77 |
+
|
| 78 |
+
# Output map (Batch, 3, 64, 9)
|
| 79 |
+
output = self.decoder(x_recon)
|
| 80 |
+
|
| 81 |
+
return output, latent
|
| 82 |
+
|
| 83 |
+
# ==========================================
|
| 84 |
+
# 2. LOADER UTILITY
|
| 85 |
+
# ==========================================
|
| 86 |
+
def load_amlm_model(model_path, device):
|
| 87 |
+
"""
|
| 88 |
+
Initializes the model and loads weights safely.
|
| 89 |
+
"""
|
| 90 |
+
print(f"--- Loading AMLM V7 from {model_path} ---")
|
| 91 |
+
model = AMLM_3D().to(device)
|
| 92 |
+
|
| 93 |
+
if os.path.exists(model_path):
|
| 94 |
+
try:
|
| 95 |
+
state_dict = torch.load(model_path, map_location=device)
|
| 96 |
+
model.load_state_dict(state_dict)
|
| 97 |
+
model.eval() # Set to inference mode (freezes Dropout/BatchNorm)
|
| 98 |
+
print("✅ AMLM Weights Loaded Successfully")
|
| 99 |
+
except Exception as e:
|
| 100 |
+
print(f"❌ CRITICAL: Failed to load AMLM weights: {e}")
|
| 101 |
+
# We assume the app should crash or handle this if weights are bad
|
| 102 |
+
raise e
|
| 103 |
+
else:
|
| 104 |
+
print(f"⚠️ WARNING: Model file not found at {model_path}. Running with random weights (Debugging Mode).")
|
| 105 |
+
|
| 106 |
+
return model
|