Enferlain commited on
Commit
5843fdb
·
verified ·
1 Parent(s): 14db026

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +286 -0
app.py ADDED
@@ -0,0 +1,286 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import traceback
4
+ from typing import Optional, Tuple, Union, List
5
+
6
+ import torch
7
+ import torch.nn as nn
8
+ import torch.nn.functional as F
9
+ from PIL import Image, PngImagePlugin
10
+ from safetensors.torch import load_file
11
+ from huggingface_hub import hf_hub_download
12
+ from transformers import AutoProcessor, AutoModel, AutoImageProcessor
13
+ import gradio as gr
14
+ import math # Added math
15
+
16
+ # --- Device Setup ---
17
+ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
18
+ # Use float16 for vision model on CUDA for speed/memory, but head expects float32
19
+ VISION_DTYPE = torch.float16 if DEVICE == "cuda" else torch.float32
20
+ HEAD_DTYPE = torch.float32 # Head usually trained/stable in float32
21
+
22
+ print(f"Using device: {DEVICE}")
23
+ print(f"Vision model dtype: {VISION_DTYPE}")
24
+ print(f"Head model dtype: {HEAD_DTYPE}")
25
+
26
+
27
+ # --- Model Definitions (Copied from hybrid_model.py) ---
28
+
29
+ class RMSNorm(nn.Module):
30
+ def __init__(self, dim: int, eps: float = 1e-6):
31
+ super().__init__()
32
+ self.weight = nn.Parameter(torch.ones(dim))
33
+ self.eps = eps
34
+ def _norm(self, x: torch.Tensor) -> torch.Tensor:
35
+ return x * torch.rsqrt(x.pow(2).mean(-1, keepdim=True) + self.eps)
36
+ def forward(self, x: torch.Tensor) -> torch.Tensor:
37
+ output = self._norm(x.float()).type_as(x)
38
+ return output * self.weight
39
+ def extra_repr(self) -> str:
40
+ return f"{tuple(self.weight.shape)}, eps={self.eps}"
41
+
42
+ class SwiGLUFFN(nn.Module):
43
+ def __init__(self, in_features: int, hidden_features: int = None, out_features: int = None, act_layer: nn.Module = nn.SiLU, dropout: float = 0.):
44
+ super().__init__()
45
+ out_features = out_features or in_features
46
+ hidden_features = hidden_features or int(in_features * 8 / 3 / 2 * 2 )
47
+ hidden_features = (hidden_features + 1) // 2 * 2
48
+ self.w12 = nn.Linear(in_features, hidden_features * 2, bias=False)
49
+ self.act = act_layer()
50
+ self.dropout1 = nn.Dropout(dropout)
51
+ self.w3 = nn.Linear(hidden_features, out_features, bias=False)
52
+ self.dropout2 = nn.Dropout(dropout)
53
+ def forward(self, x):
54
+ gate_val, up_val = self.w12(x).chunk(2, dim=-1)
55
+ x = self.dropout1(self.act(gate_val) * up_val)
56
+ x = self.dropout2(self.w3(x))
57
+ return x
58
+
59
+ class ResBlockRMS(nn.Module):
60
+ def __init__(self, ch: int, dropout: float = 0.0, rms_norm_eps: float = 1e-6):
61
+ super().__init__()
62
+ self.norm = RMSNorm(ch, eps=rms_norm_eps)
63
+ self.ffn = SwiGLUFFN(in_features=ch, dropout=dropout)
64
+ def forward(self, x):
65
+ return x + self.ffn(self.norm(x))
66
+
67
+ class HybridHeadModel(nn.Module):
68
+ def __init__(self, features: int, hidden_dim: int = 1280, num_classes: int = 2, use_attention: bool = True,
69
+ num_attn_heads: int = 16, attn_dropout: float = 0.1, num_res_blocks: int = 3,
70
+ dropout_rate: float = 0.1, rms_norm_eps: float = 1e-6, output_mode: str = 'linear'):
71
+ super().__init__()
72
+ self.features = features; self.hidden_dim = hidden_dim; self.num_classes = num_classes
73
+ self.use_attention = use_attention; self.output_mode = output_mode.lower()
74
+ # --- Optional Self-Attention Layer ---
75
+ self.attention = None; self.norm_attn = None
76
+ if self.use_attention:
77
+ actual_num_heads = num_attn_heads # Adjust head logic needed here if features != 1152
78
+ # Simple head adjustment:
79
+ if features % num_attn_heads != 0:
80
+ possible_heads = [h for h in [1, 2, 4, 8, 16] if features % h == 0]
81
+ if not possible_heads: actual_num_heads = 1 # Fallback to 1 head if no divisors found
82
+ else: actual_num_heads = min(possible_heads, key=lambda x: abs(x-num_attn_heads))
83
+ if actual_num_heads != num_attn_heads: print(f"HybridHead Warning: Adjusting heads {num_attn_heads}->{actual_num_heads}")
84
+
85
+ self.attention = nn.MultiheadAttention(features, actual_num_heads, dropout=attn_dropout, batch_first=True, bias=True)
86
+ self.norm_attn = RMSNorm(features, eps=rms_norm_eps)
87
+ # --- MLP Head ---
88
+ mlp_layers = []
89
+ mlp_layers.append(nn.Linear(features, hidden_dim)); mlp_layers.append(RMSNorm(hidden_dim, eps=rms_norm_eps))
90
+ for _ in range(num_res_blocks): mlp_layers.append(ResBlockRMS(hidden_dim, dropout=dropout_rate, rms_norm_eps=rms_norm_eps))
91
+ mlp_layers.append(RMSNorm(hidden_dim, eps=rms_norm_eps))
92
+ down_proj_hidden = hidden_dim // 2
93
+ mlp_layers.append(SwiGLUFFN(hidden_dim, hidden_features=down_proj_hidden, out_features=down_proj_hidden, dropout=dropout_rate))
94
+ mlp_layers.append(RMSNorm(down_proj_hidden, eps=rms_norm_eps))
95
+ mlp_layers.append(nn.Linear(down_proj_hidden, num_classes))
96
+ self.mlp_head = nn.Sequential(*mlp_layers)
97
+ # --- Validate Output Mode ---
98
+ # (Warnings can be added here if desired, but functionality handled in forward)
99
+
100
+ def forward(self, x: torch.Tensor):
101
+ if self.use_attention and self.attention is not None:
102
+ x_seq = x.unsqueeze(1); attn_output, _ = self.attention(x_seq, x_seq, x_seq); x = self.norm_attn(x + attn_output.squeeze(1))
103
+ logits = self.mlp_head(x.to(HEAD_DTYPE)) # Ensure input to MLP has correct dtype
104
+ # --- Apply Final Activation ---
105
+ output = None
106
+ if self.output_mode == 'linear': output = logits
107
+ elif self.output_mode == 'sigmoid': output = torch.sigmoid(logits)
108
+ elif self.output_mode == 'softmax': output = F.softmax(logits, dim=-1)
109
+ elif self.output_mode == 'tanh_scaled': output = (torch.tanh(logits) + 1.0) / 2.0
110
+ else: raise RuntimeError(f"Invalid output_mode '{self.output_mode}'.")
111
+ if self.num_classes == 1 and output.ndim == 2 and output.shape[1] == 1: output = output.squeeze(-1)
112
+ return output
113
+
114
+ # --- Constants and Model Loading ---
115
+
116
+ # Option 1: Files are in the Space repo (e.g., in a 'model' folder)
117
+ # MODEL_DIR = "model"
118
+ # HEAD_MODEL_FILENAME = "AnatomyFlaws-v11.3_adabelief_fl_naflex_3000_s9K.safetensors"
119
+ # CONFIG_FILENAME = "AnatomyFlaws-v11.3_adabelief_fl_naflex_3000.config.json" # Assuming config matches base name
120
+ # HEAD_MODEL_PATH = os.path.join(MODEL_DIR, HEAD_MODEL_FILENAME)
121
+ # CONFIG_PATH = os.path.join(MODEL_DIR, CONFIG_FILENAME)
122
+
123
+ # Option 2: Download from Hub
124
+ # Replace with your HF username and repo name
125
+ HUB_REPO_ID = "Enferlain/lumi-classifier" # Or wherever you uploaded the model
126
+ # Use the specific checkpoint you want (e.g., s9k or the best_val one)
127
+ HEAD_MODEL_FILENAME = "AnatomyFlaws-v11.3_adabelief_fl_naflex_3000_s9K.safetensors"
128
+ # Usually config corresponds to the base run name, not a specific step
129
+ CONFIG_FILENAME = "AnatomyFlaws-v11.3_adabelief_fl_naflex_3000.config.json"
130
+
131
+ print("Downloading model files if necessary...")
132
+ try:
133
+ HEAD_MODEL_PATH = hf_hub_download(repo_id=HUB_REPO_ID, filename=HEAD_MODEL_FILENAME)
134
+ CONFIG_PATH = hf_hub_download(repo_id=HUB_REPO_ID, filename=CONFIG_FILENAME)
135
+ print("Files downloaded/found successfully.")
136
+ except Exception as e:
137
+ print(f"ERROR downloading files from {HUB_REPO_ID}: {e}")
138
+ print("Please ensure the files exist on the Hub or place them in a local 'model' folder.")
139
+ # Optionally exit or fallback
140
+ exit(1) # Exit if essential files aren't available
141
+
142
+
143
+ # --- Load Config ---
144
+ print(f"Loading config from: {CONFIG_PATH}")
145
+ config = {}
146
+ try:
147
+ with open(CONFIG_PATH, 'r', encoding='utf-8') as f:
148
+ config = json.load(f)
149
+ except Exception as e:
150
+ print(f"ERROR loading config file: {e}"); exit(1)
151
+
152
+ # --- Load Vision Model ---
153
+ BASE_VISION_MODEL_NAME = config.get("base_vision_model", "google/siglip2-so400m-patch16-naflex")
154
+ print(f"Loading vision model: {BASE_VISION_MODEL_NAME}")
155
+ try:
156
+ hf_processor = AutoProcessor.from_pretrained(BASE_VISION_MODEL_NAME)
157
+ vision_model = AutoModel.from_pretrained(
158
+ BASE_VISION_MODEL_NAME, torch_dtype=VISION_DTYPE
159
+ ).to(DEVICE).eval()
160
+ print("Vision model loaded.")
161
+ except Exception as e:
162
+ print(f"ERROR loading vision model: {e}"); exit(1)
163
+
164
+ # --- Load HybridHeadModel ---
165
+ print(f"Loading head model: {HEAD_MODEL_PATH}")
166
+ head_model = None
167
+ try:
168
+ state_dict = load_file(HEAD_MODEL_PATH, device='cpu')
169
+ # Infer details from config - use defaults matching the successful run
170
+ features = config.get("features", 1152)
171
+ num_classes = config.get("num_classes", 2) # Should be 2 for focal loss run
172
+ output_mode = config.get("output_mode", "linear") # Should be linear
173
+ hidden_dim = config.get("hidden_dim", 1280)
174
+ num_res_blocks = config.get("num_res_blocks", 3)
175
+ dropout_rate = config.get("dropout_rate", 0.3) # Use the high dropout from best run
176
+ use_attention = config.get("use_attention", True) # Use attention was likely True
177
+ num_attn_heads = config.get("num_attn_heads", 16)
178
+ attn_dropout = config.get("attn_dropout", 0.3) # Use the high dropout
179
+ rms_norm_eps= config.get("rms_norm_eps", 1e-6)
180
+
181
+ head_model = HybridHeadModel(
182
+ features=features, hidden_dim=hidden_dim, num_classes=num_classes,
183
+ use_attention=use_attention, num_attn_heads=num_attn_heads, attn_dropout=attn_dropout,
184
+ num_res_blocks=num_res_blocks, dropout_rate=dropout_rate, rms_norm_eps=rms_norm_eps,
185
+ output_mode=output_mode
186
+ )
187
+ missing, unexpected = head_model.load_state_dict(state_dict, strict=False)
188
+ if missing: print(f"Warning: Missing keys loading head: {missing}")
189
+ if unexpected: print(f"Warning: Unexpected keys loading head: {unexpected}")
190
+ head_model.to(DEVICE).eval()
191
+ print("Head model loaded.")
192
+ except Exception as e:
193
+ print(f"ERROR loading head model: {e}"); exit(1)
194
+
195
+ # --- Label Mapping ---
196
+ # Assume labels are '0': Bad, '1': Good from config or default
197
+ LABELS = config.get("labels", {'0': 'Bad Anatomy', '1': 'Good Anatomy'})
198
+ LABEL_NAMES = {
199
+ 0: LABELS.get('0', 'Class 0'),
200
+ 1: LABELS.get('1', 'Class 1')
201
+ }
202
+ print(f"Using Labels: {LABEL_NAMES}")
203
+
204
+ # --- Prediction Function ---
205
+ def predict_anatomy(image: Image.Image):
206
+ """Takes PIL Image, returns dict of class probabilities."""
207
+ if image is None: return {"Error": "No image provided"}
208
+ try:
209
+ pil_image = image.convert("RGB")
210
+
211
+ # 1. Extract SigLIP NaFlex Embedding
212
+ with torch.no_grad():
213
+ inputs = hf_processor(images=[pil_image], return_tensors="pt", max_num_patches=1024)
214
+ pixel_values = inputs.get("pixel_values").to(device=DEVICE, dtype=VISION_DTYPE)
215
+ attention_mask = inputs.get("pixel_attention_mask").to(device=DEVICE)
216
+ spatial_shapes = inputs.get("spatial_shapes")
217
+ model_call_kwargs = {"pixel_values": pixel_values, "attention_mask": attention_mask,
218
+ "spatial_shapes": torch.tensor(spatial_shapes, dtype=torch.long).to(DEVICE)}
219
+
220
+ vision_model_component = getattr(vision_model, 'vision_model', vision_model) # Handle potential nesting
221
+ emb = vision_model_component(**model_call_kwargs).pooler_output
222
+ if emb is None: raise ValueError("Failed to get embedding.")
223
+
224
+ # L2 Norm
225
+ norm = torch.linalg.norm(emb.float(), dim=-1, keepdim=True).clamp(min=1e-8)
226
+ emb_normalized = emb / norm.to(emb.dtype)
227
+
228
+ # 2. Obtain Prediction from HybridHeadModel Head
229
+ with torch.no_grad():
230
+ prediction = head_model(emb_normalized.to(DEVICE, dtype=HEAD_DTYPE))
231
+
232
+ # 3. Format Output Probabilities
233
+ output_probs = {}
234
+ output_mode = getattr(head_model, 'output_mode', 'linear')
235
+
236
+ if head_model.num_classes == 1:
237
+ logit = prediction.squeeze().item()
238
+ prob_good = torch.sigmoid(torch.tensor(logit)).item() if output_mode == 'linear' else logit
239
+ output_probs[LABEL_NAMES[0]] = 1.0 - prob_good
240
+ output_probs[LABEL_NAMES[1]] = prob_good
241
+ elif head_model.num_classes == 2:
242
+ if output_mode == 'linear':
243
+ probs = F.softmax(prediction.squeeze().float(), dim=-1) # Use float for softmax stability
244
+ else: # Assume sigmoid or already softmax
245
+ probs = prediction.squeeze().float()
246
+ output_probs[LABEL_NAMES[0]] = probs[0].item()
247
+ output_probs[LABEL_NAMES[1]] = probs[1].item()
248
+ else:
249
+ output_probs["Error"] = f"Unsupported num_classes: {head_model.num_classes}"
250
+
251
+ # Convert to percentage strings for gr.Label maybe? Or keep floats? Keep floats.
252
+ # output_formatted = {k: f"{v:.1%}" for k, v in output_probs.items()}
253
+ return output_probs
254
+
255
+ except Exception as e:
256
+ print(f"Error during prediction: {e}\n{traceback.format_exc()}")
257
+ return {"Error": str(e)}
258
+
259
+ # --- Gradio Interface ---
260
+ DESCRIPTION = """
261
+ ## Anatomy Flaw Classifier Demo ✨ (Based on SigLIP Naflex + Hybrid Head)
262
+ Upload an image to classify its anatomy as 'Good' or 'Bad'.
263
+ This model uses embeddings from **google/siglip2-so400m-patch16-naflex**
264
+ and a custom **HybridHeadModel** fine-tuned for anatomy classification.
265
+ Model Checkpoint: **AnatomyFlaws-v11.3_..._s9K** (or specify which one).
266
+ """
267
+
268
+ # Add example images if you have some in an 'examples' folder in the Space repo
269
+ EXAMPLE_DIR = "examples"
270
+ examples = []
271
+ if os.path.isdir(EXAMPLE_DIR):
272
+ examples = [os.path.join(EXAMPLE_DIR, fname) for fname in sorted(os.listdir(EXAMPLE_DIR)) if fname.lower().endswith(('.png', '.jpg', '.jpeg', '.webp'))]
273
+
274
+ interface = gr.Interface(
275
+ fn=predict_anatomy,
276
+ inputs=gr.Image(type="pil", label="Input Image"),
277
+ outputs=gr.Label(label="Class Probabilities", num_top_classes=2), # Show top 2 classes
278
+ title="Lumi's Anatomy Classifier Demo",
279
+ description=DESCRIPTION,
280
+ examples=examples if examples else None,
281
+ allow_flagging="never",
282
+ cache_examples=False # Disable caching if examples change or loading is fast
283
+ )
284
+
285
+ if __name__ == "__main__":
286
+ interface.launch()