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

Fix inference when action names are not alphabetic #159

Conversation

Ivan-267
Copy link
Collaborator

@Ivan-267 Ivan-267 commented Dec 3, 2023

Fixes onnx inference when the action names are not sorted, together with this PR on the plugin side:
edbeeching/godot_rl_agents_plugin#27

Related issue: edbeeching/godot_rl_agents_plugin#25

One way to test this is to try e.g. current version of FlyBy (may need to update plugin before testing). For me, when I try the onnx file, the result doesn't work unless I swap the order of the two actions.

After the changes, here is a quick test with the onnx working properly after retraining the agent for ~50_000 steps using the SB3 example:

flyby_onnx_test.mp4

(The extra actions and unnecessary dimensions were added just for debugging)

As for what causes the issue, during conversion to spaces.Dict, the items of the dictionary were getting sorted while converted to OrderedDict internally:

self._action_space = spaces.Dict(action_spaces)

The is fine for training and Python inference since it sends a dictionary containing action names and values back to Godot, but during onnx inference the only information about action order available is from the action space as defined in gdscript. In envs where that was already sorted, there weren't any issues with onnx, but as I tried training an env with action names ordered differently, I noticed the onnx wasn't working correctly.

This should fix those cases, changing to OrderedDict seems to remove the conversion and sorting, so the order sent from Godot is preserved and should be what onnx inference expects.

@Ivan-267 Ivan-267 added the bugfix label Dec 3, 2023
@Ivan-267 Ivan-267 requested a review from edbeeching December 3, 2023 11:55
Copy link
Owner

@edbeeching edbeeching left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Ivan-267 Ivan-267 merged commit 8227583 into main Dec 4, 2023
12 checks passed
@Ivan-267 Ivan-267 deleted the fix-action-names-must-be-alphabetically-ordered-for-onnx-inference-to-work branch December 4, 2023 06:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants