Delete raven/onnx_export.py
Browse files- raven/onnx_export.py +0 -39
raven/onnx_export.py
DELETED
|
@@ -1,39 +0,0 @@
|
|
| 1 |
-
from __future__ import annotations
|
| 2 |
-
from pathlib import Path
|
| 3 |
-
import torch
|
| 4 |
-
import torch.nn as nn
|
| 5 |
-
from .model import RavenFusion
|
| 6 |
-
from .foundation import FoundationEncoder
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class FusionEnsembleWrapper(nn.Module):
|
| 10 |
-
KEYS=("semantic","camera","spectral","patch","specialist")
|
| 11 |
-
def __init__(self,models): super().__init__(); self.models=nn.ModuleList(models)
|
| 12 |
-
def forward(self,semantic,camera,spectral,patch,specialist,quality,anchors,luma):
|
| 13 |
-
f={"semantic":semantic,"camera":camera,"spectral":spectral,"patch":patch,"specialist":specialist}; outs=[m(f,quality,anchors,luma,0.0,False) for m in self.models]
|
| 14 |
-
logits=torch.stack([o["logit"] for o in outs],1); branches=torch.stack([o["branch_logits"] for o in outs],1); gates=torch.stack([o["gates"] for o in outs],1)
|
| 15 |
-
return logits.mean(1),logits.std(1,unbiased=False),branches.mean(1),gates.mean(1)
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
class FoundationWrapper(nn.Module):
|
| 19 |
-
def __init__(self,enc): super().__init__(); self.enc=enc
|
| 20 |
-
def forward(self,x): return self.enc(x)
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def export_fusion(model_path:str,out_path:str):
|
| 24 |
-
ck=torch.load(model_path,map_location="cpu",weights_only=False); models=[]
|
| 25 |
-
for st in ck["ensemble_states"]:
|
| 26 |
-
m=RavenFusion(ck["dims"],len(ck.get("ai_source_map",{})),len(ck.get("real_source_map",{}))); m.load_state_dict(st); m.eval(); models.append(m)
|
| 27 |
-
w=FusionEnsembleWrapper(models).eval(); d=ck["dims"]; inputs=[torch.randn(1,d[k]) for k in ("semantic","camera","spectral","patch","specialist")]+[torch.randn(1,d["quality"]),torch.randn(1,5),torch.tensor([.3])]
|
| 28 |
-
Path(out_path).parent.mkdir(parents=True,exist_ok=True)
|
| 29 |
-
torch.onnx.export(w,tuple(inputs),out_path,input_names=["semantic","camera","spectral","patch","specialist","quality","anchors","luma"],output_names=["logit_mean","logit_std","branch_mean","gates_mean"],dynamic_axes={n:{0:"batch"} for n in ["semantic","camera","spectral","patch","specialist","quality","anchors","luma","logit_mean","logit_std","branch_mean","gates_mean"]},opset_version=18)
|
| 30 |
-
return out_path
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
def export_foundation(model_path:str,out_path:str):
|
| 34 |
-
ck=torch.load(model_path,map_location="cpu",weights_only=False); enc=FoundationEncoder(ck["foundation_backend"],allow_fallback=False,fallback_backend=ck["foundation_backend"],pretrained=ck.get("foundation_state") is None).eval();
|
| 35 |
-
if ck.get("foundation_state") is not None: enc.model.load_state_dict(ck["foundation_state"]["state_dict"])
|
| 36 |
-
w=FoundationWrapper(enc).eval(); x=torch.randn(1,3,224,224)
|
| 37 |
-
Path(out_path).parent.mkdir(parents=True,exist_ok=True)
|
| 38 |
-
torch.onnx.export(w,x,out_path,input_names=["image"],output_names=["embedding"],dynamic_axes={"image":{0:"batch"},"embedding":{0:"batch"}},opset_version=18)
|
| 39 |
-
return out_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|