use of com.oracle.labs.mlrg.olcut.provenance.io.ProvenanceSerializationException in project tribuo by oracle.
the class ONNXExternalModel method getTribuoProvenance.
/**
* Returns the model provenance from the ONNX model if that
* model was trained in Tribuo.
* <p>
* Tribuo's ONNX export functionality stores the model provenance inside the
* ONNX file in the metadata field {@link ONNXExportable#PROVENANCE_METADATA_FIELD},
* and this method provides the access point for it.
* <p>
* Note it is different from the {@link Model#getProvenance()} call which
* returns information about the ONNX file itself, and when the {@code ONNXExternalModel}
* was created. It does not replace that provenance because instantiating this provenance
* may require classes which are not present on the classpath at deployment time.
*
* @return The model provenance from the original Tribuo training run, if it exists, and
* returns {@link Optional#empty()} otherwise.
*/
public Optional<ModelProvenance> getTribuoProvenance() {
try {
OnnxModelMetadata metadata = session.getMetadata();
Optional<String> value = metadata.getCustomMetadataValue(ONNXExportable.PROVENANCE_METADATA_FIELD);
if (value.isPresent()) {
Provenance prov = ONNXExportable.SERIALIZER.deserializeAndUnmarshal(value.get());
if (prov instanceof ModelProvenance) {
return Optional.of((ModelProvenance) prov);
} else {
logger.log(Level.WARNING, "Found invalid provenance object, " + prov.toString());
return Optional.empty();
}
} else {
return Optional.empty();
}
} catch (OrtException e) {
logger.log(Level.WARNING, "ORTException when reading session metadata", e);
return Optional.empty();
} catch (ProvenanceSerializationException e) {
logger.log(Level.WARNING, "Failed to parse provenance from value.", e);
return Optional.empty();
}
}
Aggregations