load(path: str, compute_units: ComputeUnits = ComputeUnits.ALL)Load an existing .mlpackage.
Feature names are read from the package, so this works on packages
Lucid did not write. So is the precision a Lucid export recorded —
the body's and the interface's — which a package from elsewhere, or
from a release that did not record it, reports as "UNKNOWN".
The package is compiled once per content and the compiled model kept
in ~/.cache/lucid/coreml (LUCID_COREML_CACHE_DIR, or
$LUCID_HOME/coreml), so loading it again — in this process or the
next — skips the compile and lets Core ML reuse the bundle it
specialised the model into, instead of writing a new copy of the
weights into its own cache each time. The cache keeps to
LUCID_COREML_CACHE_LIMIT bytes (default 4G, least recently used
first; 0 turns it off) and empty_cache clears it.
Parameters
pathstrCoreMLModel.compute_plan.Returns
CoreMLModelLoaded and ready to predict.
Raises
RuntimeErrorOSErrorENOSPC: a volume the load writes to — the temporary space
Core ML compiles into, the cache, Core ML's own caches — has less
free than the package's size plus a margin. Core ML ends the
process when it runs out of disk part way, so the load refuses
before it starts.Examples
>>> import shutil, tempfile
>>> import lucid, lucid.nn as nn, lucid.coreml as cml
>>> model = nn.Sequential(
... nn.Conv2d(3, 16, 3, padding=1), nn.ReLU(),
... nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(16, 10),
... ).eval()
>>> x, room = lucid.randn(1, 3, 32, 32), tempfile.mkdtemp()
>>> cml.export(model, x, f"{room}/model.mlpackage").close()
>>> package = cml.load(f"{room}/model.mlpackage")
>>> package.predict(x).shape
(1, 10)
>>> package.close()
The handle recovers what the export knew — that an input is a
picture, that the outputs are labels, which inputs stand in for a
draw — because the package declares all of it:
>>> brightest = nn.Sequential(nn.AdaptiveAvgPool2d(1), nn.Flatten()).eval()
>>> red = lucid.zeros(1, 3, 8, 8)
>>> red[:, 0] = 255.0
>>> cml.export(brightest, red, f"{room}/cls.mlpackage",
... image_input=cml.ImageInput(scale=1 / 255.0),
... classifier=cml.Classifier(labels=("red", "green", "blue")),
... ).close()
>>> reopened = cml.load(f"{room}/cls.mlpackage")
>>> reopened.classify(red)[0]
'red'
>>> reopened.close()
>>> shutil.rmtree(room)