Skip to content
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

Inference Layer by Layer or feature extraction #6025

Open
IzanCatalan opened this issue Mar 20, 2024 · 2 comments
Open

Inference Layer by Layer or feature extraction #6025

IzanCatalan opened this issue Mar 20, 2024 · 2 comments
Labels
question Questions about ONNX

Comments

@IzanCatalan
Copy link

IzanCatalan commented Mar 20, 2024

Hi everyone, I would like to know if performing a layer-per-layer inference on a pre-trained model (in fp32 or int8 datatypes) is possible using GPU with cuda 11.2.

My idea is to use several fp32 and int8-quantized models from ONNX Model Zoo Repo and then do the inference layer by layer to achieve a feature extraction. After this, I would modify the outputs from each layer and use them as a new input for the following layers, with the last layer's output equal to the output of the original model.

The approximate code would be something similar to this one:

model_path = "model.onnx"
ort_session = ort.InferenceSession(model_path)

input_data = np.random.randn(1, 3, 32, 32).astype(np.float32)

conv1_output = ort_session.run(None, {'input1': input_data})[0]

conv2_output = ort_session.run(None, {'input2': conv1_output})[0]
# Now, I can work with intermediate outputs, modify them and use them as new inputs

However, I tried to reproduce this code with a resnet50 pre-trained model from ONNX Model Zoo Repo, but it seems this model, like the rest of pre-trained models, only has one input and one output (no way of accessing to intermediate outputs).

So, is there any way I could do this? I have seen Evaluation Step by Step documentation. However, I am unsure if this "ReferenceEvaluator" function also works for pre-trained/quantised models and, more importantly, obtaining accuracy from a dataset like ImageNet.

Thank you!

@IzanCatalan IzanCatalan added the question Questions about ONNX label Mar 20, 2024
@gramalingam
Copy link
Contributor

What is your intended use-case? Is this to debug/understand what's happening (where performance does not matter), or is it for production use? For debugging/understanding, IIRC @xadupre has written utilities that will execute models node by node. He might be able to point you to it and answer your question about reference evaluator also.

@IzanCatalan
Copy link
Author

Hi @gramalingam, yes my use case is to use the models with a feature extraction as research to understand its behaviour. Any information would help.

github-merge-queue bot pushed a commit that referenced this issue Apr 4, 2024
### Description

Intermediate results can only be printed right now. With this PR, they
can be returned as well.

### Motivation and Context
See #6025.

---------

Signed-off-by: Xavier Dupre <xadupre@microsoft.com>
gramalingam pushed a commit to gramalingam/onnx that referenced this issue Apr 12, 2024
### Description

Intermediate results can only be printed right now. With this PR, they
can be returned as well.

### Motivation and Context
See onnx#6025.

---------

Signed-off-by: Xavier Dupre <xadupre@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Questions about ONNX
Projects
None yet
Development

No branches or pull requests

2 participants