Skip to content

[js] Add API for accessing metadata of a model's input/output #23937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 24, 2025

Conversation

fs-eire
Copy link
Contributor

@fs-eire fs-eire commented Mar 7, 2025

Description

Add API for accessing metadata of a model's input/output.

Currently, The implementation is only applied to web assembly backend and nodejs binding. For webgl, there is so far no plan to implement this API; for react-native, the implementation will be done later and is not included in this PR.

Example usage:

const mySession = await ort.InferenceSession.create( ... );

console.log(`there are ${mySession.inputMetadata.length} inputs:`);
for (let i = 0; i < mySession.inputMetadata.length; i++) {
  let info;
  if (mySession.inputMetadata[i].isTensor) {
    info = `tensor: ${mySession.inputMetadata[i].type}, shape: ${mySession.inputMetadata[i].shape}`;
  } else {
    info = `non-tensor`;
  }
  console.log(`input ${i}: ${mySession.inputMetadata[i].name}: ${info}`);
}

possible output:

there are 1 inputs:
input 0: input: tensor: float32, shape: [batch, 3, 224, 224]

Resolves:

@fs-eire fs-eire marked this pull request as ready for review March 9, 2025 20:25
@fs-eire fs-eire requested a review from guschmue March 21, 2025 17:00
@fs-eire fs-eire merged commit 618aef7 into main Mar 24, 2025
90 of 92 checks passed
@fs-eire fs-eire deleted the fs-eire/api-io-metadata branch March 24, 2025 23:11
@xenova
Copy link
Contributor

xenova commented Mar 25, 2025

Was looking forward to this for a long time! Fantastic!

quic-zhaoxul pushed a commit to CodeLinaro/onnxruntime that referenced this pull request Apr 17, 2025
…oft#23937)

### Description

Add API for accessing metadata of a model's input/output.

Currently, The implementation is only applied to web assembly backend
and nodejs binding. For webgl, there is so far no plan to implement this
API; for react-native, the implementation will be done later and is not
included in this PR.

#### Example usage:

```js
const mySession = await ort.InferenceSession.create( ... );

console.log(`there are ${mySession.inputMetadata.length} inputs:`);
for (let i = 0; i < mySession.inputMetadata.length; i++) {
  let info;
  if (mySession.inputMetadata[i].isTensor) {
    info = `tensor: ${mySession.inputMetadata[i].type}, shape: ${mySession.inputMetadata[i].shape}`;
  } else {
    info = `non-tensor`;
  }
  console.log(`input ${i}: ${mySession.inputMetadata[i].name}: ${info}`);
}

```

possible output:
```
there are 1 inputs:
input 0: input: tensor: float32, shape: [batch, 3, 224, 224]

```

Resolves:
- microsoft#22682
- microsoft#22949
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants