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.
Parameters
pathstrPackage to load.
Which processors Core ML may schedule on. Reaching the Neural
Engine also needs the package to be float16 — check with
CoreMLModel.compute_plan.Returns
CoreMLModelLoaded and ready to predict.
Raises
RuntimeErrorCore ML could not compile or load the package; its own message
names the offending layer.
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)