yalue / onnxruntime_go

A Go (golang) library wrapping microsoft/onnxruntime.
MIT License
171 stars 31 forks source link

model metadata #35

Closed oldma3095 closed 7 months ago

oldma3095 commented 7 months ago

Hi. I noticed that the API has methods for Ort::ModelMetadata. Includes: input.name, input.tensor, output.name, output.tensor, format, imgsz, names ... image

This is very useful. Hope to add a function to get this information. Thanks!

yalue commented 7 months ago

I just added support for GetModelMetadata as of commit d8bb20d9a9ee4cecc5d5f (tagged version v1.8.0). You can get the metadata using AdvancedSession.GetModelMetadata() or onnxruntime_go.GetModelMetadata("path/to/file.onnx"):

metadata, err := mySession.GetModelMetadata()
// Alternatively:
// metadata, err := onnxruntime_go.GetModelMetadata("path/to/file.onnx")
if err != nil {
    panic("Error getting metadata")
}

// ModelMetadata instances must always be cleaned up when no longer needed.
defer metadata.Destroy()

modelProducerName, err := metadata.GetProducerName()
if err != nil {
    panic("Error getting model producer name from metadata")
}

fmt.Printf("Model producer name: %s\n", modelProducerName)

// etc.

You mentioned input and output names and sizes in your comment, but those aren't actually provided by the OrtModelMetadata API. They're actually part of a completely different set of APIs, that this library already supports. See this comment in the relevant issue for how to get input and output names.

I'll also note that many .onnx files don't include much interesting metadata by default. For example, the information about image classes in your screenshot is not a fundamental part of many .onnx files, and will only be there if the creator of the .onnx file was considerate enough to add it manually. For most networks, the GetInputOutputInfo functions will be more likely to contain the useful information you want.