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
 
 
 
 
bf9db6a
96dce1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
"""

LaborView AI - MedSigLIP Edge Export

Export trained MedSigLIP model to ONNX/CoreML/TFLite for mobile deployment

"""

# /// script
# dependencies = [
#   "torch>=2.0.0",
#   "transformers>=4.50.0",
#   "onnx>=1.14.0",
#   "onnxscript>=0.1.0",
#   "onnxruntime>=1.16.0",
#   "huggingface_hub>=0.20.0",
#   "numpy>=1.24.0",
#   "pillow>=10.0.0",
#   "coremltools>=7.0",
# ]
# ///

import os
import sys
import json
import argparse
from pathlib import Path
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple

import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from PIL import Image


@dataclass
class Config:
    # Model
    encoder_pretrained: str = "google/medsiglip-448"
    encoder_hidden_dim: int = 1152
    projection_dim: int = 256
    num_plane_classes: int = 2
    num_seg_classes: int = 3
    image_size: int = 448

    # Export
    hub_model_id: str = "samwell/laborview-medsiglip"
    output_dir: Path = Path("./exports")
    opset_version: int = 17


class SegmentationDecoder(nn.Module):
    """Decoder for upsampling vision features to segmentation mask"""

    def __init__(self, input_dim: int, num_classes: int, decoder_channels=[512, 256, 128, 64]):
        super().__init__()

        self.input_proj = nn.Conv2d(input_dim, decoder_channels[0], 1)

        self.up_blocks = nn.ModuleList()
        in_ch = decoder_channels[0]
        for out_ch in decoder_channels[1:]:
            self.up_blocks.append(nn.Sequential(
                nn.ConvTranspose2d(in_ch, out_ch, 4, stride=2, padding=1),
                nn.BatchNorm2d(out_ch),
                nn.GELU()
            ))
            in_ch = out_ch

        self.final_up = nn.Sequential(
            nn.ConvTranspose2d(decoder_channels[-1], 32, 4, stride=2, padding=1),
            nn.BatchNorm2d(32),
            nn.GELU(),
            nn.ConvTranspose2d(32, 32, 4, stride=2, padding=1),
            nn.BatchNorm2d(32),
            nn.GELU(),
        )

        self.classifier = nn.Conv2d(32, num_classes, 1)

    def forward(self, x, target_size=None):
        B = x.shape[0]

        if x.dim() == 3:
            num_patches = x.shape[1]
            H = W = int(num_patches ** 0.5)
            x = x.transpose(1, 2).reshape(B, -1, H, W)

        x = self.input_proj(x)

        for block in self.up_blocks:
            x = block(x)

        x = self.final_up(x)
        x = self.classifier(x)

        if target_size:
            x = F.interpolate(x, size=target_size, mode='bilinear', align_corners=False)

        return x


class LaborViewMedSigLIP(nn.Module):
    """LaborView model with MedSigLIP vision encoder"""

    def __init__(self, config: Config):
        super().__init__()
        self.config = config

        from transformers import AutoModel

        print(f"Loading MedSigLIP from {config.encoder_pretrained}...")
        self.encoder = AutoModel.from_pretrained(
            config.encoder_pretrained,
            trust_remote_code=True
        )

        if hasattr(self.encoder, 'vision_model'):
            self.vision_encoder = self.encoder.vision_model
        else:
            self.vision_encoder = self.encoder

        if hasattr(self.vision_encoder.config, 'hidden_size'):
            hidden_dim = self.vision_encoder.config.hidden_size
        else:
            hidden_dim = config.encoder_hidden_dim

        self.projector = nn.Sequential(
            nn.Linear(hidden_dim, config.projection_dim),
            nn.LayerNorm(config.projection_dim),
            nn.GELU(),
            nn.Linear(config.projection_dim, config.projection_dim)
        )

        self.cls_head = nn.Linear(config.projection_dim, config.num_plane_classes)
        self.seg_decoder = SegmentationDecoder(hidden_dim, config.num_seg_classes)

    def forward(self, pixel_values):
        if hasattr(self, 'vision_encoder'):
            outputs = self.vision_encoder(pixel_values)
        else:
            outputs = self.encoder.get_image_features(pixel_values, return_dict=True)

        if hasattr(outputs, 'last_hidden_state'):
            hidden = outputs.last_hidden_state
        elif hasattr(outputs, 'pooler_output'):
            hidden = outputs.pooler_output
        else:
            hidden = outputs

        if hidden.dim() == 2:
            pooled = hidden
            B, D = hidden.shape
            seq = hidden.unsqueeze(1).expand(B, 32*32, D)
        elif hidden.dim() == 3:
            pooled = hidden.mean(dim=1)
            seq = hidden
        else:
            B, D, H, W = hidden.shape
            pooled = hidden.mean(dim=[2, 3])
            seq = hidden.flatten(2).transpose(1, 2)

        projected = self.projector(pooled)
        plane_logits = self.cls_head(projected)
        seg_masks = self.seg_decoder(seq, target_size=pixel_values.shape[-2:])

        return plane_logits, seg_masks


class LaborViewExportWrapper(nn.Module):
    """Wrapper for ONNX export - returns dict-like outputs"""

    def __init__(self, model):
        super().__init__()
        self.model = model

    def forward(self, pixel_values):
        plane_logits, seg_masks = self.model(pixel_values)
        # Return segmentation probabilities and class prediction
        seg_probs = F.softmax(seg_masks, dim=1)
        plane_pred = plane_logits.argmax(dim=1)
        return seg_probs, plane_pred


def load_trained_model(config: Config):
    """Load trained model from HuggingFace Hub"""
    from huggingface_hub import hf_hub_download

    print(f"Downloading model from {config.hub_model_id}...")

    # Download checkpoint
    checkpoint_path = hf_hub_download(
        repo_id=config.hub_model_id,
        filename="best.pt"
    )

    print(f"Loading checkpoint from {checkpoint_path}...")
    checkpoint = torch.load(checkpoint_path, map_location="cpu", weights_only=False)

    # Create model
    model = LaborViewMedSigLIP(config)

    # Load state dict
    model.load_state_dict(checkpoint["model_state_dict"])

    print(f"Model loaded successfully!")
    if "val_loss" in checkpoint:
        print(f"  Val Loss: {checkpoint['val_loss']:.4f}")
    if "val_iou" in checkpoint:
        print(f"  Val IoU: {checkpoint['val_iou']:.4f}")

    return model


def export_to_onnx(model, config: Config, quantize: bool = True):
    """Export model to ONNX format using TorchScript tracing"""
    import onnx
    import os
    os.environ["TORCH_ONNX_USE_DYNAMO"] = "0"  # Force legacy exporter

    config.output_dir.mkdir(parents=True, exist_ok=True)

    model.eval()
    wrapper = LaborViewExportWrapper(model)
    wrapper.eval()

    # Create dummy input on CPU
    dummy_input = torch.randn(1, 3, config.image_size, config.image_size)

    # Move model to CPU for export
    wrapper = wrapper.cpu()

    # Export paths
    onnx_path = config.output_dir / "laborview_medsiglip.onnx"
    onnx_quant_path = config.output_dir / "laborview_medsiglip_int8.onnx"

    print(f"Exporting to ONNX: {onnx_path}")

    # Export directly without tracing (for new PyTorch exporter compatibility)
    with torch.no_grad():
        torch.onnx.export(
            wrapper,
            (dummy_input,),
            str(onnx_path),
            export_params=True,
            opset_version=14,
            do_constant_folding=True,
            input_names=['pixel_values'],
            output_names=['seg_probs', 'plane_pred']
        )

    # Verify ONNX model
    onnx_model = onnx.load(str(onnx_path))
    onnx.checker.check_model(onnx_model)
    print(f"ONNX model verified successfully!")

    # Get model size
    onnx_size = onnx_path.stat().st_size / (1024 * 1024 * 1024)
    print(f"ONNX model size: {onnx_size:.2f} GB")

    # Quantize to INT8 (with error handling)
    onnx_quant_path_result = None
    if quantize and onnx_size > 0.001:
        try:
            from onnxruntime.quantization import quantize_dynamic, QuantType
            print(f"Quantizing to INT8: {onnx_quant_path}")
            quantize_dynamic(
                str(onnx_path),
                str(onnx_quant_path),
                weight_type=QuantType.QInt8
            )

            quant_size = onnx_quant_path.stat().st_size / (1024 * 1024 * 1024)
            print(f"Quantized model size: {quant_size:.2f} GB")
            print(f"Size reduction: {(1 - quant_size/onnx_size) * 100:.1f}%")
            onnx_quant_path_result = onnx_quant_path
        except Exception as e:
            print(f"Quantization failed: {e}")
            print("Continuing with FP32 model only")

    return onnx_path, onnx_quant_path_result


def export_to_coreml(model, config: Config):
    """Export model to CoreML format for iOS with Neural Engine optimization"""
    try:
        import coremltools as ct
        from coremltools.models.neural_network import quantization_utils
    except ImportError:
        print("coremltools not installed. Skipping CoreML export.")
        print("Install with: pip install coremltools")
        return None

    config.output_dir.mkdir(parents=True, exist_ok=True)

    model.eval()
    model = model.cpu()  # Move to CPU for export
    wrapper = LaborViewExportWrapper(model)
    wrapper.eval()

    # Trace the model
    print("Tracing model for CoreML...")
    dummy_input = torch.randn(1, 3, config.image_size, config.image_size)

    with torch.no_grad():
        traced_model = torch.jit.trace(wrapper, dummy_input)

    coreml_path = config.output_dir / "laborview_medsiglip.mlpackage"
    coreml_fp16_path = config.output_dir / "laborview_medsiglip_fp16.mlpackage"

    print(f"Converting to CoreML: {coreml_path}")

    try:
        # Convert to CoreML with FP32 first
        mlmodel = ct.convert(
            traced_model,
            inputs=[
                ct.TensorType(
                    name="pixel_values",
                    shape=(1, 3, config.image_size, config.image_size),
                    dtype=np.float32
                )
            ],
            outputs=[
                ct.TensorType(name="seg_probs"),
                ct.TensorType(name="plane_pred")
            ],
            minimum_deployment_target=ct.target.iOS16,
            compute_units=ct.ComputeUnit.ALL  # Use Neural Engine + GPU + CPU
        )

        # Add metadata
        mlmodel.author = "LaborView AI"
        mlmodel.short_description = "MedSigLIP ultrasound segmentation for labor monitoring"
        mlmodel.version = "1.0"

        # Save FP32 version
        mlmodel.save(str(coreml_path))
        print(f"CoreML FP32 model saved: {coreml_path}")

        # Create FP16 version for smaller size and faster inference
        print("Creating FP16 quantized version...")
        mlmodel_fp16 = ct.convert(
            traced_model,
            inputs=[
                ct.TensorType(
                    name="pixel_values",
                    shape=(1, 3, config.image_size, config.image_size),
                    dtype=np.float32
                )
            ],
            outputs=[
                ct.TensorType(name="seg_probs"),
                ct.TensorType(name="plane_pred")
            ],
            minimum_deployment_target=ct.target.iOS16,
            compute_units=ct.ComputeUnit.ALL,
            compute_precision=ct.precision.FLOAT16
        )

        mlmodel_fp16.author = "LaborView AI"
        mlmodel_fp16.short_description = "MedSigLIP ultrasound segmentation (FP16)"
        mlmodel_fp16.version = "1.0"
        mlmodel_fp16.save(str(coreml_fp16_path))
        print(f"CoreML FP16 model saved: {coreml_fp16_path}")

        return coreml_path

    except Exception as e:
        print(f"CoreML export failed: {e}")
        import traceback
        traceback.print_exc()
        return None


def verify_onnx_model(onnx_path: Path, config: Config):
    """Verify ONNX model with sample inference"""
    import onnxruntime as ort

    print(f"\nVerifying ONNX model: {onnx_path}")

    # Create session
    session = ort.InferenceSession(str(onnx_path))

    # Get input/output info
    input_info = session.get_inputs()[0]
    print(f"Input: {input_info.name}, shape: {input_info.shape}, type: {input_info.type}")

    for output in session.get_outputs():
        print(f"Output: {output.name}, shape: {output.shape}, type: {output.type}")

    # Run inference
    dummy_input = np.random.randn(1, 3, config.image_size, config.image_size).astype(np.float32)

    outputs = session.run(None, {"pixel_values": dummy_input})

    seg_probs, plane_pred = outputs
    print(f"\nTest inference:")
    print(f"  Segmentation output shape: {seg_probs.shape}")
    print(f"  Plane prediction: {plane_pred}")

    # Measure inference time
    import time
    times = []
    for _ in range(10):
        start = time.time()
        session.run(None, {"pixel_values": dummy_input})
        times.append(time.time() - start)

    avg_time = np.mean(times) * 1000
    print(f"  Average inference time: {avg_time:.1f} ms")

    return True


def main():
    parser = argparse.ArgumentParser(description="Export MedSigLIP for edge deployment")
    parser.add_argument("--output-dir", type=str, default="./exports", help="Output directory")
    parser.add_argument("--quantize", action="store_true", default=True, help="Quantize to INT8")
    parser.add_argument("--coreml", action="store_true", default=True, help="Export to CoreML")
    parser.add_argument("--verify", action="store_true", default=True, help="Verify exported model")
    args = parser.parse_args()

    config = Config()
    config.output_dir = Path(args.output_dir)

    # Load trained model
    model = load_trained_model(config)
    model.eval()

    # Export to ONNX
    onnx_path, onnx_quant_path = export_to_onnx(model, config, quantize=args.quantize)

    # Verify
    if args.verify and onnx_path:
        verify_onnx_model(onnx_path, config)
        if onnx_quant_path:
            verify_onnx_model(onnx_quant_path, config)

    # Export to CoreML
    if args.coreml:
        export_to_coreml(model, config)

    print("\n" + "="*50)
    print("Export Summary:")
    print("="*50)

    for f in config.output_dir.glob("*"):
        size_mb = f.stat().st_size / (1024 * 1024)
        if size_mb > 1024:
            print(f"  {f.name}: {size_mb/1024:.2f} GB")
        else:
            print(f"  {f.name}: {size_mb:.1f} MB")

    print("\nDone!")


if __name__ == "__main__":
    main()