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

[MLA-1809] catch mismatched observation sizes #5030

Merged
merged 8 commits into from
Mar 8, 2021

Conversation

chriselion
Copy link
Contributor

@chriselion chriselion commented Mar 4, 2021

Proposed change(s)

Even though we check and assert on the Unity side, it's possible to get observations of different size send over. This will cause a numpy exception when trying to pack them into an array:

2021-03-01 20:07:58 ERROR [subprocess_env_manager.py:226] UnityEnvironment worker 0: environment raised an unexpected exception.
TypeError: float() argument must be a string or a number, not 'google.protobuf.pyext._message.RepeatedScalarContainer'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/chris.elion/code/ml-agents/ml-agents/mlagents/trainers/subprocess_env_manager.py", line 208, in worker
    env.reset()
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/environment.py", line 309, in reset
    self._update_state(rl_output)
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/environment.py", line 290, in _update_state
    self._env_state[brain_name] = steps_from_proto(
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/timers.py", line 305, in wrapped
    return func(*args, **kwargs)
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/rpc_utils.py", line 304, in steps_from_proto
    _process_rank_one_or_two_observation(
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/timers.py", line 305, in wrapped
    return func(*args, **kwargs)
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/rpc_utils.py", line 265, in _process_rank_one_or_two_observation
    np_obs = np.array(
ValueError: setting an array element with a sequence.

This change adds an extra check that the observations all have the shape of the observation spec:

2021-03-03 17:42:32 ERROR [subprocess_env_manager.py:226] UnityEnvironment worker 0: environment raised an unexpected exception.
Traceback (most recent call last):
  File "/Users/chris.elion/code/ml-agents/ml-agents/mlagents/trainers/subprocess_env_manager.py", line 208, in worker
    env.reset()
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/environment.py", line 309, in reset
    self._update_state(rl_output)
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/environment.py", line 290, in _update_state
    self._env_state[brain_name] = steps_from_proto(
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/timers.py", line 305, in wrapped
    return func(*args, **kwargs)
  File "/Users/chris.elion/code/ml-agents/ml-agents-envs/mlagents_envs/rpc_utils.py", line 295, in steps_from_proto
    raise ValueError(
ValueError: Observation at index=0 for agent with id=7 didn't match the ObservationSpec. Expeceted shape (8,) but got (9,).

Useful links (Github issues, JIRA tickets, ML-Agents forum threads etc.)

https://forum.unity.com/threads/walker-demo-adding-blockcontact-observation-valueerror-setting-an-array-element-with-a-sequence.1065587/#post-6895055

Types of change(s)

  • Bug fix

Checklist

  • Added tests that prove my fix is effective or that my feature works
  • Updated the changelog (if applicable)

@@ -258,17 +285,25 @@ def _raise_on_nan_and_inf(data: np.array, source: str) -> np.array:

@timed
def _process_rank_one_or_two_observation(
obs_index: int, shape: Tuple[int, ...], agent_info_list: Collection[AgentInfoProto]
obs_index: int,
observation_spec: ObservationSpec,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is better now.

@chriselion chriselion merged commit d37a2af into main Mar 8, 2021
@delete-merged-branch delete-merged-branch bot deleted the MLA-1809-catch-mismatch-obs branch March 8, 2021 17:47
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants