blot-mini / build.py
pmarquees's picture
Publish Blot Mini XS, S and M with full CredData comparison
83afe32 verified
Raw History Blame Contribute Delete
799 Bytes
"""Re-export the saved FP32 tensors without changing weights.npz."""
from pathlib import Path
import numpy as np
import coremltools as ct
from coremltools.converters.mil import Builder as mb
ROOT=Path(__file__).resolve().parent
with np.load(ROOT/'weights.npz',allow_pickle=False) as z:
W,b,o=z['W'],z['b'],z['o']
@mb.program(input_specs=[mb.TensorSpec(shape=(128,W.shape[1]))],opset_version=ct.target.iOS18)
def network(features):
h=mb.relu(x=mb.linear(x=features,weight=W,bias=b))
return mb.linear(x=h,weight=o[None,:],bias=np.array([-0.5],np.float32),name='scores')
m=ct.convert(network,minimum_deployment_target=ct.target.iOS18,
compute_precision=ct.precision.FLOAT32,skip_model_load=True)
(ROOT/'bundle').mkdir(exist_ok=True)
m.save(str(ROOT/'bundle/BlotHandwritten.mlpackage'))