Skip to content

Commit df80759

Browse files
Chris ElionErvin Tvincentpierresurfnerd
authored
Release 2 cherry pick (#3971)
* [bug-fix] Fix issue with initialize not resetting step count (#3962) * Develop better error message for #3953 (#3963) * Making the error for wrong number of agents raise consistently * Better error message for inputs of wrong dimensions * Fix #3932, stop the editor from going into a loop when a prefab is selected. (#3949) * Minor doc updates to release * add unit tests and fix exceptions (#3930) Co-authored-by: Ervin T <ervin@unity3d.com> Co-authored-by: Vincent-Pierre BERGES <vincentpierre@unity3d.com> Co-authored-by: Chris Goy <christopherg@unity3d.com>
1 parent 39d5394 commit df80759

File tree

11 files changed

+92
-31
lines changed

11 files changed

+92
-31
lines changed

com.unity.ml-agents/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to
88

99
## [1.0.1-preview] - 2020-05-19
1010
### Bug Fixes
11+
- An issue was fixed where using `--initialize-from` would resume from the past step count. (#3962)
1112
#### com.unity.ml-agents (C#)
1213
#### ml-agents / ml-agents-envs / gym-unity (Python)
1314

com.unity.ml-agents/Editor/BrainParametersDrawer.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,14 @@ static void DrawVectorAction(Rect position, SerializedProperty property)
124124
static void DrawContinuousVectorAction(Rect position, SerializedProperty property)
125125
{
126126
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
127-
vecActionSize.arraySize = 1;
127+
128+
// This check is here due to:
129+
// https://fogbugz.unity3d.com/f/cases/1246524/
130+
// If this case has been resolved, please remove this if condition.
131+
if (vecActionSize.arraySize != 1)
132+
{
133+
vecActionSize.arraySize = 1;
134+
}
128135
var continuousActionSize =
129136
vecActionSize.GetArrayElementAtIndex(0);
130137
EditorGUI.PropertyField(
@@ -142,8 +149,17 @@ static void DrawContinuousVectorAction(Rect position, SerializedProperty propert
142149
static void DrawDiscreteVectorAction(Rect position, SerializedProperty property)
143150
{
144151
var vecActionSize = property.FindPropertyRelative(k_ActionSizePropName);
145-
vecActionSize.arraySize = EditorGUI.IntField(
152+
var newSize = EditorGUI.IntField(
146153
position, "Branches Size", vecActionSize.arraySize);
154+
155+
// This check is here due to:
156+
// https://fogbugz.unity3d.com/f/cases/1246524/
157+
// If this case has been resolved, please remove this if condition.
158+
if (newSize != vecActionSize.arraySize)
159+
{
160+
vecActionSize.arraySize = newSize;
161+
}
162+
147163
position.y += k_LineHeight;
148164
position.x += 20;
149165
position.width -= 20;

com.unity.ml-agents/Runtime/Communicator/GrpcExtensions.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ public static AgentInfoActionPairProto ToInfoActionPairProto(this AgentInfo ai)
2626
{
2727
var agentInfoProto = ai.ToAgentInfoProto();
2828

29-
var agentActionProto = new AgentActionProto
29+
var agentActionProto = new AgentActionProto();
30+
if(ai.storedVectorActions != null)
3031
{
31-
VectorActions = { ai.storedVectorActions }
32-
};
32+
agentActionProto.VectorActions.AddRange(ai.storedVectorActions);
33+
}
3334

3435
return new AgentInfoActionPairProto
3536
{
@@ -95,12 +96,14 @@ public static BrainParametersProto ToProto(this BrainParameters bp, string name,
9596
var brainParametersProto = new BrainParametersProto
9697
{
9798
VectorActionSize = { bp.VectorActionSize },
98-
VectorActionSpaceType =
99-
(SpaceTypeProto)bp.VectorActionSpaceType,
99+
VectorActionSpaceType = (SpaceTypeProto) bp.VectorActionSpaceType,
100100
BrainName = name,
101101
IsTraining = isTraining
102102
};
103-
brainParametersProto.VectorActionDescriptions.AddRange(bp.VectorActionDescriptions);
103+
if(bp.VectorActionDescriptions != null)
104+
{
105+
brainParametersProto.VectorActionDescriptions.AddRange(bp.VectorActionDescriptions);
106+
}
104107
return brainParametersProto;
105108
}
106109

@@ -128,13 +131,14 @@ public static BrainParameters ToBrainParameters(this BrainParametersProto bpp)
128131
/// </summary>
129132
public static DemonstrationMetaProto ToProto(this DemonstrationMetaData dm)
130133
{
134+
var demonstrationName = dm.demonstrationName ?? "";
131135
var demoProto = new DemonstrationMetaProto
132136
{
133137
ApiVersion = DemonstrationMetaData.ApiVersion,
134138
MeanReward = dm.meanReward,
135139
NumberSteps = dm.numberSteps,
136140
NumberEpisodes = dm.numberEpisodes,
137-
DemonstrationName = dm.demonstrationName
141+
DemonstrationName = demonstrationName
138142
};
139143
return demoProto;
140144
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using NUnit.Framework;
2+
using UnityEngine;
3+
using Unity.MLAgents.Policies;
4+
using Unity.MLAgents.Demonstrations;
5+
using Unity.MLAgents.Sensors;
6+
7+
namespace Unity.MLAgents.Tests
8+
{
9+
[TestFixture]
10+
public class GrpcExtensionsTests
11+
{
12+
[Test]
13+
public void TestDefaultBrainParametersToProto()
14+
{
15+
// Should be able to convert a default instance to proto.
16+
var brain = new BrainParameters();
17+
brain.ToProto("foo", false);
18+
}
19+
20+
[Test]
21+
public void TestDefaultAgentInfoToProto()
22+
{
23+
// Should be able to convert a default instance to proto.
24+
var agentInfo = new AgentInfo();
25+
agentInfo.ToInfoActionPairProto();
26+
agentInfo.ToAgentInfoProto();
27+
}
28+
29+
[Test]
30+
public void TestDefaultDemonstrationMetaDataToProto()
31+
{
32+
// Should be able to convert a default instance to proto.
33+
var demoMetaData = new DemonstrationMetaData();
34+
demoMetaData.ToProto();
35+
}
36+
}
37+
}

com.unity.ml-agents/Tests/Editor/MLAgentsEditModeTest.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ internal class TestPolicy : IPolicy
1313
{
1414
public Action OnRequestDecision;
1515
ObservationWriter m_ObsWriter = new ObservationWriter();
16-
public void RequestDecision(AgentInfo info, List<ISensor> sensors) {
17-
foreach(var sensor in sensors){
16+
public void RequestDecision(AgentInfo info, List<ISensor> sensors)
17+
{
18+
foreach (var sensor in sensors)
19+
{
1820
sensor.GetObservationProto(m_ObsWriter);
1921
}
2022
OnRequestDecision?.Invoke();
@@ -517,8 +519,10 @@ public void AssertStackingReset()
517519
agent1.SetPolicy(policy);
518520

519521
StackingSensor sensor = null;
520-
foreach(ISensor s in agent1.sensors){
521-
if (s is StackingSensor){
522+
foreach (ISensor s in agent1.sensors)
523+
{
524+
if (s is StackingSensor)
525+
{
522526
sensor = s as StackingSensor;
523527
}
524528
}
@@ -529,7 +533,6 @@ public void AssertStackingReset()
529533
{
530534
agent1.RequestDecision();
531535
aca.EnvironmentStep();
532-
533536
}
534537

535538
policy.OnRequestDecision = () => SensorTestHelper.CompareObservation(sensor, new[] {18f, 19f, 21f});

docs/Training-ML-Agents.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,8 +445,8 @@ Below is a list of included `sampler-type` as part of the toolkit.
445445
`interval_2_max`], ...]
446446
- **sub-arguments** - `intervals`
447447

448-
The implementation of the samplers can be found at
449-
`ml-agents-envs/mlagents_envs/sampler_class.py`.
448+
The implementation of the samplers can be found in the
449+
[sampler_class.py file](../ml-agents/mlagents/trainers/sampler_class.py).
450450

451451
#### Defining a New Sampler Type
452452

docs/Using-Tensorboard.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ session running on port 6006 a new session can be launched on an open port using
2121
the --port option.
2222

2323
**Note:** If you don't assign a `run-id` identifier, `mlagents-learn` uses the
24-
default string, "ppo". All the statistics will be saved to the same sub-folder
25-
and displayed as one session in TensorBoard. After a few runs, the displays can
26-
become difficult to interpret in this situation. You can delete the folders
27-
under the `summaries` directory to clear out old statistics.
24+
default string, "ppo". You can delete the folders under the `results` directory
25+
to clear out old statistics.
2826

2927
On the left side of the TensorBoard window, you can select which of the training
3028
runs you want to display. You can select multiple run-ids to compare statistics.

gym-unity/gym_unity/envs/__init__.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def __init__(
5353
self._env.step()
5454

5555
self.visual_obs = None
56-
self._n_agents = -1
5756

5857
# Save the step result from the last time all Agents requested decisions.
5958
self._previous_decision_step: DecisionSteps = None
@@ -172,6 +171,7 @@ def step(self, action: List[Any]) -> GymStepResult:
172171

173172
self._env.step()
174173
decision_step, terminal_step = self._env.get_steps(self.name)
174+
self._check_agents(max(len(decision_step), len(terminal_step)))
175175
if len(terminal_step) != 0:
176176
# The agent is done
177177
self.game_over = True
@@ -264,10 +264,11 @@ def seed(self, seed: Any = None) -> None:
264264
logger.warning("Could not seed environment %s", self.name)
265265
return
266266

267-
def _check_agents(self, n_agents: int) -> None:
268-
if self._n_agents > 1:
267+
@staticmethod
268+
def _check_agents(n_agents: int) -> None:
269+
if n_agents > 1:
269270
raise UnityGymException(
270-
"There can only be one Agent in the environment but {n_agents} were detected."
271+
f"There can only be one Agent in the environment but {n_agents} were detected."
271272
)
272273

273274
@property
@@ -290,10 +291,6 @@ def action_space(self):
290291
def observation_space(self):
291292
return self._observation_space
292293

293-
@property
294-
def number_agents(self):
295-
return self._n_agents
296-
297294

298295
class ActionFlattener:
299296
"""

ml-agents-envs/mlagents_envs/environment.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ def set_actions(self, behavior_name: BehaviorName, action: np.ndarray) -> None:
415415
expected_shape = (len(self._env_state[behavior_name][0]), spec.action_size)
416416
if action.shape != expected_shape:
417417
raise UnityActionException(
418-
"The behavior {0} needs an input of dimension {1} but received input of dimension {2}".format(
419-
behavior_name, expected_shape, action.shape
420-
)
418+
"The behavior {0} needs an input of dimension {1} for "
419+
"(<number of agents>, <action size>) but received input of "
420+
"dimension {2}".format(behavior_name, expected_shape, action.shape)
421421
)
422422
if action.dtype != expected_type:
423423
action = action.astype(expected_type)

ml-agents/mlagents/trainers/policy/tf_policy.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def _load_graph(self, model_path: str, reset_global_steps: bool = False) -> None
137137
)
138138
)
139139
if reset_global_steps:
140+
self._set_step(0)
140141
logger.info(
141142
"Starting training from step 0 and saving to {}.".format(
142143
self.model_path

ml-agents/mlagents/trainers/tests/test_nn_policy.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def test_load_save(dummy_config, tmp_path):
8686
trainer_params["model_path"] = path1
8787
policy = create_policy_mock(trainer_params)
8888
policy.initialize_or_load()
89+
policy._set_step(2000)
8990
policy.save_model(2000)
9091

9192
assert len(os.listdir(tmp_path)) > 0
@@ -94,6 +95,7 @@ def test_load_save(dummy_config, tmp_path):
9495
policy2 = create_policy_mock(trainer_params, load=True, seed=1)
9596
policy2.initialize_or_load()
9697
_compare_two_policies(policy, policy2)
98+
assert policy2.get_current_step() == 2000
9799

98100
# Try initialize from path 1
99101
trainer_params["model_path"] = path2
@@ -102,6 +104,8 @@ def test_load_save(dummy_config, tmp_path):
102104
policy3.initialize_or_load()
103105

104106
_compare_two_policies(policy2, policy3)
107+
# Assert that the steps are 0.
108+
assert policy3.get_current_step() == 0
105109

106110

107111
def _compare_two_policies(policy1: NNPolicy, policy2: NNPolicy) -> None:

0 commit comments

Comments
 (0)