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
Nano: fix keras onnx model output shape #7138
Nano: fix keras onnx model output shape #7138
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
others LGTM
python/nano/src/bigdl/nano/deps/onnxruntime/tensorflow/tensorflow_onnxruntime_model.py
Show resolved
Hide resolved
@@ -78,7 +86,10 @@ def __call__(self, *args, **kwargs): | |||
kwargs[name] = value | |||
for param in self._call_fn_args_backup[len(inputs):len(self._forward_args)]: | |||
inputs.append(kwargs[param]) | |||
return self.call(*inputs) | |||
outputs = self.call(*inputs) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand right, here returns original onnx output(a list) ? so maybe it contains _nesting_level at first (not start from 0) ?
It's just a bit confusing whether this way of adding lists works in all cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, here outputs
is a single Tensor, not onnx output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.call
will call tf.py_function(func, ..., Tout=tf.float32)
, which will convert the output of func
to a single Tensor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
Fix the output shape of keras onnx model
1. Why the change?
tf.py_function(func, ..., Tout=tf.float32)
will convert the output offunc
to a single Tensor, that means[t]
or[[t]]
(t
is atf.Tensor
) all will be converted tot
.Besides, if the original model return a single tensor
t
, then after tracing, onnxruntime model will return[t]
.These two rules will destroy the output shape when the original output is a single tensor, or a list of tensor with only one element, or a list of list of tensor with only one element, ...
Before this PR, we simply check whether the output is a list and has only one element, if is, then return its first element. We use this method to
However, this method cannot distinguish whether the original model return
t
or[t]
(onnxruntime model will return[t]
in both case). And we don't handle the influence oftf.py_function
.This PR fix both, it save the right output shape first, and convert the final output to this right shape.
2. User API changes
N/A
3. Summary of the change
N/A
4. How to test?