data
Classifier
Classifier(labels: tuple[str, ...], label_name: str = 'classLabel', probabilities_name: str = 'classLabel_probs')Turn the exported scores into labels Core ML knows how to name.
Without this a package returns a score array and the app does its own
argmax and label lookup. Vision's VNCoreMLRequest does not even
get that far: it reads the package's predictedFeatureName, and an
unset one means it returns nothing.
The two feature names default to what the reference tooling emits, because that is what Xcode's preview and most sample code look for.
classify does not normalise: whatever the model produces is
what the map contains, so a network ending in a linear layer yields
raw scores under a name that says "probabilities". Add a softmax to
the model if the values need to be probabilities — Core ML will not
do it, and the name will not tell you it did not.
Attributes
labelstuple[str, ...]One label per score, in the order the model produces them.
label_namestrFeature the winning label is returned under.
probabilities_namestrFeature the label-to-probability map is returned under.
Examples
A linear layer that passes its input through, so the scores are the
input itself:
>>> import shutil, tempfile
>>> import lucid, lucid.nn as nn, lucid.coreml as cml
>>> model = nn.Linear(2, 2).eval()
>>> _ = nn.init.eye_(model.weight), nn.init.zeros_(model.bias)
>>> x, room = lucid.tensor([[3.0, 1.0]]), tempfile.mkdtemp()
>>> package = cml.export(
... model, x, f"{room}/cls.mlpackage",
... classifier=cml.Classifier(labels=("cat", "dog")),
... )
>>> label, probabilities = package.classify(x)
>>> label
'cat'
>>> sorted(probabilities.items()) # the raw scores, not normalised
[('cat', 3.0), ('dog', 1.0)]
>>> package.close()
>>> shutil.rmtree(room)