Skip to content

Commit d49357c

Browse files
Fixed failing python tests.
1 parent 565568c commit d49357c

18 files changed

+93
-55
lines changed

docs/Python-LLAPI-Documentation.md

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* [random\_action](#mlagents_envs.base_env.ActionSpec.random_action)
2222
* [create\_continuous](#mlagents_envs.base_env.ActionSpec.create_continuous)
2323
* [create\_discrete](#mlagents_envs.base_env.ActionSpec.create_discrete)
24+
* [create\_hybrid](#mlagents_envs.base_env.ActionSpec.create_hybrid)
2425
* [DimensionProperty](#mlagents_envs.base_env.DimensionProperty)
2526
* [UNSPECIFIED](#mlagents_envs.base_env.DimensionProperty.UNSPECIFIED)
2627
* [NONE](#mlagents_envs.base_env.DimensionProperty.NONE)
@@ -412,6 +413,16 @@ Creates an ActionSpec that is homogenously continuous
412413

413414
Creates an ActionSpec that is homogenously discrete
414415

416+
<a name="mlagents_envs.base_env.ActionSpec.create_hybrid"></a>
417+
#### create\_hybrid
418+
419+
```python
420+
| @staticmethod
421+
| create_hybrid(continuous_size: int, discrete_branches: Tuple[int]) -> "ActionSpec"
422+
```
423+
424+
Creates a hybrid ActionSpace
425+
415426
<a name="mlagents_envs.base_env.DimensionProperty"></a>
416427
## DimensionProperty Objects
417428

ml-agents-envs/mlagents_envs/base_env.py

+9
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,15 @@ def create_discrete(discrete_branches: Tuple[int]) -> "ActionSpec":
446446
"""
447447
return ActionSpec(0, discrete_branches)
448448

449+
@staticmethod
450+
def create_hybrid(
451+
continuous_size: int, discrete_branches: Tuple[int]
452+
) -> "ActionSpec":
453+
"""
454+
Creates a hybrid ActionSpace
455+
"""
456+
return ActionSpec(continuous_size, discrete_branches)
457+
449458

450459
class DimensionProperty(IntFlag):
451460
"""

ml-agents-envs/mlagents_envs/rpc_utils.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,9 @@ def process_pixels(
122122
image = Image.open(image_fp)
123123
# Normally Image loads lazily, load() forces it to do loading in the timer scope.
124124
image.load()
125-
image_arrays.append(np.moveaxis(np.array(image, dtype=np.float32) / 255.0, -1, 0))
125+
image_arrays.append(
126+
np.moveaxis(np.array(image, dtype=np.float32) / 255.0, -1, 0)
127+
)
126128

127129
# Look for the next header, starting from the current stream location
128130
try:

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,24 @@ def setup_test_behavior_specs(
212212
action_spec = ActionSpec.create_discrete(tuple(vector_action_space))
213213
else:
214214
action_spec = ActionSpec.create_continuous(vector_action_space)
215-
observation_shapes = [(84, 84, 3)] * int(use_visual) + [(vector_obs_space,)]
215+
observation_shapes = [(3, 84, 84)] * int(use_visual) + [(vector_obs_space,)]
216+
obs_spec = create_observation_specs_with_shapes(observation_shapes)
217+
behavior_spec = BehaviorSpec(obs_spec, action_spec)
218+
return behavior_spec
219+
220+
221+
def setup_test_hybrid_behavior_specs(
222+
use_visual=False,
223+
continuous_action_space=3,
224+
discrete_action_space=None,
225+
vector_obs_space=1,
226+
):
227+
if discrete_action_space is None:
228+
discrete_action_space = [2]
229+
action_spec = ActionSpec.create_hybrid(
230+
continuous_action_space, tuple(discrete_action_space)
231+
)
232+
observation_shapes = [(3, 84, 84)] * int(use_visual) + [(vector_obs_space,)]
216233
obs_spec = create_observation_specs_with_shapes(observation_shapes)
217234
behavior_spec = BehaviorSpec(obs_spec, action_spec)
218235
return behavior_spec
@@ -234,3 +251,9 @@ def create_mock_banana_behavior_specs():
234251
return setup_test_behavior_specs(
235252
True, True, vector_action_space=[3, 3, 3, 2], vector_obs_space=0
236253
)
254+
255+
256+
def create_visual_food_collector_specs():
257+
return setup_test_hybrid_behavior_specs(
258+
True, continuous_action_space=3, discrete_action_space=[2], vector_obs_space=1
259+
)
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
{
2-
"param_1": {
3-
"lesson_num": 2
4-
},
5-
"param_2": {
6-
"lesson_num": 0
7-
},
8-
"param_3": {
9-
"lesson_num": 0
10-
},
112
"metadata": {
123
"stats_format_version": "0.3.0",
13-
"mlagents_version": "0.29.0",
14-
"torch_version": "1.8.1"
4+
"mlagents_version": "0.31.0.dev0",
5+
"torch_version": "1.11.0+cu102"
156
}
16-
}
7+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from mlagents.trainers.tests.dummy_config import create_observation_specs_with_shapes
2121

2222
OBS_SIZE = 1
23-
VIS_OBS_SIZE = (20, 20, 3)
23+
VIS_OBS_SIZE = (3, 20, 20)
2424
VAR_LEN_SIZE = (10, 5)
2525
STEP_SIZE = 0.2
2626

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def generate_compressed_data(in_array: np.ndarray) -> bytes:
6969
num_images = (num_channels + 2) // 3
7070
# Split the input image into batches of 3 channels.
7171
for i in range(num_images):
72-
sub_image = image_arr[3 * i: 3 * i + 3, ...]
72+
sub_image = image_arr[3 * i : 3 * i + 3, ...]
7373
if (i == num_images - 1) and (num_channels % 3) != 0:
7474
# Pad zeros
7575
zero_shape = list(in_array.shape)

ml-agents/mlagents/trainers/tests/torch_entities/test_bcmodule.py

+12-10
Original file line numberDiff line numberDiff line change
@@ -123,26 +123,28 @@ def test_bcmodule_rnn_update(is_sac):
123123
assert_stats_are_float(stats)
124124

125125

126-
# Test with discrete control and visual observations
126+
# Test with hybrid control and visual observations
127127
@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"])
128-
def test_bcmodule_dc_visual_update(is_sac):
129-
mock_specs = mb.create_mock_banana_behavior_specs()
128+
def test_bcmodule_hybrid_visual_updates(is_sac):
129+
mock_specs = mb.create_visual_food_collector_specs()
130130
bc_settings = BehavioralCloningSettings(
131-
demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "testdcvis.demo"
131+
demo_path=os.path.dirname(os.path.abspath(__file__))
132+
+ "/"
133+
+ "testhybridvis.demo"
132134
)
133135
bc_module = create_bc_module(mock_specs, bc_settings, False, is_sac)
134136
stats = bc_module.update()
135137
assert_stats_are_float(stats)
136138

137139

138-
# Test with discrete control, visual observations and RNN
139-
140-
140+
# Test with hybrid control, visual observations and rnn
141141
@pytest.mark.parametrize("is_sac", [True, False], ids=["sac", "ppo"])
142-
def test_bcmodule_rnn_dc_update(is_sac):
143-
mock_specs = mb.create_mock_banana_behavior_specs()
142+
def test_bcmodule_rnn_hybrid_update(is_sac):
143+
mock_specs = mb.create_visual_food_collector_specs()
144144
bc_settings = BehavioralCloningSettings(
145-
demo_path=os.path.dirname(os.path.abspath(__file__)) + "/" + "testdcvis.demo"
145+
demo_path=os.path.dirname(os.path.abspath(__file__))
146+
+ "/"
147+
+ "testhybridvis.demo"
146148
)
147149
bc_module = create_bc_module(mock_specs, bc_settings, True, is_sac)
148150
stats = bc_module.update()

ml-agents/mlagents/trainers/tests/torch_entities/test_encoders.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def test_vector_encoder(mock_normalizer):
7373
mock_normalizer_inst.copy_from.assert_called_with(mock_normalizer_inst)
7474

7575

76-
@pytest.mark.parametrize("image_size", [(36, 36, 3), (84, 84, 4), (256, 256, 5)])
76+
@pytest.mark.parametrize("image_size", [(3, 36, 36), (4, 84, 84), (5, 256, 256)])
7777
@pytest.mark.parametrize(
7878
"vis_class",
7979
[
@@ -86,7 +86,7 @@ def test_vector_encoder(mock_normalizer):
8686
)
8787
def test_visual_encoder(vis_class, image_size):
8888
num_outputs = 128
89-
enc = vis_class(image_size[0], image_size[1], image_size[2], num_outputs)
89+
enc = vis_class(image_size[1], image_size[2], image_size[0], num_outputs)
9090
# Note: NCHW not NHWC
9191
sample_input = torch.ones((1, image_size[0], image_size[1], image_size[2]))
9292
encoding = enc(sample_input)
@@ -106,17 +106,17 @@ def test_visual_encoder(vis_class, image_size):
106106
@pytest.mark.slow
107107
def test_visual_encoder_trains(vis_class, size):
108108
torch.manual_seed(0)
109-
image_size = (size, size, 1)
109+
image_size = (1, size, size)
110110
batch = 100
111111

112112
inputs = torch.cat(
113113
[torch.zeros((batch,) + image_size), torch.ones((batch,) + image_size)], dim=0
114114
)
115115
target = torch.cat([torch.zeros((batch,)), torch.ones((batch,))], dim=0)
116-
enc = vis_class(image_size[0], image_size[1], image_size[2], 1)
116+
enc = vis_class(image_size[1], image_size[2], image_size[0], 1)
117117
optimizer = torch.optim.Adam(enc.parameters(), lr=0.001)
118118

119-
for _ in range(15):
119+
for _ in range(25):
120120
prediction = enc(inputs)[:, 0]
121121
loss = torch.mean((target - prediction) ** 2)
122122
optimizer.zero_grad()

ml-agents/mlagents/trainers/tests/torch_entities/test_hybrid.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def test_hybrid_ppo(action_size):
3333
PPO_TORCH_CONFIG,
3434
hyperparameters=new_hyperparams,
3535
network_settings=new_network_settings,
36-
max_steps=10000,
36+
max_steps=20000,
3737
)
3838
check_environment_trains(env, {BRAIN_NAME: config}, success_threshold=0.9)
3939

@@ -90,7 +90,7 @@ def test_hybrid_sac(action_size):
9090
buffer_init_steps=0,
9191
)
9292
config = attr.evolve(
93-
SAC_TORCH_CONFIG, hyperparameters=new_hyperparams, max_steps=4000
93+
SAC_TORCH_CONFIG, hyperparameters=new_hyperparams, max_steps=8000
9494
)
9595
check_environment_trains(env, {BRAIN_NAME: config}, success_threshold=0.9)
9696

ml-agents/mlagents/trainers/tests/torch_entities/test_networks.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ def test_networkbody_lstm():
7474
def test_networkbody_visual():
7575
torch.manual_seed(1)
7676
vec_obs_size = 4
77-
obs_size = (84, 84, 3)
77+
obs_size = (3, 84, 84)
7878
network_settings = NetworkSettings()
7979
obs_shapes = [(vec_obs_size,), obs_size]
8080

8181
networkbody = NetworkBody(
8282
create_observation_specs_with_shapes(obs_shapes), network_settings
8383
)
8484
optimizer = torch.optim.Adam(networkbody.parameters(), lr=3e-3)
85-
sample_obs = 0.1 * torch.ones((1, 84, 84, 3), dtype=torch.float32)
85+
sample_obs = 0.1 * torch.ones((1, 3, 84, 84), dtype=torch.float32)
8686
sample_vec_obs = torch.ones((1, vec_obs_size), dtype=torch.float32)
8787
obs = [sample_vec_obs] + [sample_obs]
8888
loss = 1
@@ -200,7 +200,7 @@ def test_multinetworkbody_visual(with_actions):
200200
act_size = 2
201201
n_agents = 3
202202
obs_size = 4
203-
vis_obs_size = (84, 84, 3)
203+
vis_obs_size = (3, 84, 84)
204204
network_settings = NetworkSettings()
205205
obs_shapes = [(obs_size,), vis_obs_size]
206206
action_spec = ActionSpec(act_size, tuple(act_size for _ in range(act_size)))
@@ -209,7 +209,7 @@ def test_multinetworkbody_visual(with_actions):
209209
)
210210
optimizer = torch.optim.Adam(networkbody.parameters(), lr=3e-3)
211211
sample_obs = [
212-
[0.1 * torch.ones((1, obs_size))] + [0.1 * torch.ones((1, 84, 84, 3))]
212+
[0.1 * torch.ones((1, obs_size))] + [0.1 * torch.ones((1, 3, 84, 84))]
213213
for _ in range(n_agents)
214214
]
215215
# simulate baseline in POCA
@@ -273,7 +273,7 @@ def test_valuenetwork():
273273
@pytest.mark.parametrize("lstm", [True, False])
274274
def test_actor_critic(lstm, shared):
275275
obs_size = 4
276-
vis_obs_size = (84, 84, 3)
276+
vis_obs_size = (3, 84, 84)
277277
network_settings = NetworkSettings(
278278
memory=NetworkSettings.MemorySettings() if lstm else None, normalize=True
279279
)
@@ -291,14 +291,14 @@ def test_actor_critic(lstm, shared):
291291
critic = ValueNetwork(stream_names, obs_spec, network_settings)
292292
if lstm:
293293
sample_vis_obs = torch.ones(
294-
(network_settings.memory.sequence_length, 84, 84, 3), dtype=torch.float32
294+
(network_settings.memory.sequence_length, 3, 84, 84), dtype=torch.float32
295295
)
296296
sample_obs = torch.ones((network_settings.memory.sequence_length, obs_size))
297297
memories = torch.ones(
298298
(1, network_settings.memory.sequence_length, actor.memory_size)
299299
)
300300
else:
301-
sample_vis_obs = 0.1 * torch.ones((1, 84, 84, 3), dtype=torch.float32)
301+
sample_vis_obs = 0.1 * torch.ones((1, 3, 84, 84), dtype=torch.float32)
302302
sample_obs = torch.ones((1, obs_size))
303303
memories = torch.tensor([])
304304
# memories isn't always set to None, the network should be able to

ml-agents/mlagents/trainers/tests/torch_entities/test_reward_providers/test_curiosity.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ def test_construction(behavior_spec: BehaviorSpec) -> None:
4747
create_observation_specs_with_shapes([(10,)]), ACTIONSPEC_CONTINUOUS
4848
),
4949
BehaviorSpec(
50-
create_observation_specs_with_shapes([(10,), (64, 66, 3), (84, 86, 1)]),
50+
create_observation_specs_with_shapes([(10,), (3, 64, 66), (1, 84, 86)]),
5151
ACTIONSPEC_CONTINUOUS,
5252
),
5353
BehaviorSpec(
54-
create_observation_specs_with_shapes([(10,), (64, 66, 1)]),
54+
create_observation_specs_with_shapes([(10,), (1, 64, 66)]),
5555
ACTIONSPEC_TWODISCRETE,
5656
),
5757
BehaviorSpec(
@@ -72,7 +72,7 @@ def test_factory(behavior_spec: BehaviorSpec) -> None:
7272
"behavior_spec",
7373
[
7474
BehaviorSpec(
75-
create_observation_specs_with_shapes([(10,), (64, 66, 3), (24, 26, 1)]),
75+
create_observation_specs_with_shapes([(10,), (3, 64, 66), (1, 24, 26)]),
7676
ACTIONSPEC_CONTINUOUS,
7777
),
7878
BehaviorSpec(
@@ -125,7 +125,7 @@ def test_continuous_action_prediction(behavior_spec: BehaviorSpec, seed: int) ->
125125
"behavior_spec",
126126
[
127127
BehaviorSpec(
128-
create_observation_specs_with_shapes([(10,), (64, 66, 3), (24, 26, 1)]),
128+
create_observation_specs_with_shapes([(10,), (3, 64, 66), (1, 24, 26)]),
129129
ACTIONSPEC_CONTINUOUS,
130130
),
131131
BehaviorSpec(

ml-agents/mlagents/trainers/tests/torch_entities/test_reward_providers/test_gail.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def test_factory(behavior_spec: BehaviorSpec) -> None:
6161
"behavior_spec",
6262
[
6363
BehaviorSpec(
64-
create_observation_specs_with_shapes([(8,), (24, 26, 1)]),
64+
create_observation_specs_with_shapes([(8,), (1, 24, 26)]),
6565
ACTIONSPEC_CONTINUOUS,
6666
),
6767
BehaviorSpec(
@@ -116,7 +116,7 @@ def test_reward_decreases(
116116
"behavior_spec",
117117
[
118118
BehaviorSpec(
119-
create_observation_specs_with_shapes([(8,), (24, 26, 1)]),
119+
create_observation_specs_with_shapes([(8,), (1, 24, 26)]),
120120
ACTIONSPEC_CONTINUOUS,
121121
),
122122
BehaviorSpec(

ml-agents/mlagents/trainers/tests/torch_entities/test_reward_providers/test_rnd.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ def test_construction(behavior_spec: BehaviorSpec) -> None:
4646
create_observation_specs_with_shapes([(10,)]), ACTIONSPEC_CONTINUOUS
4747
),
4848
BehaviorSpec(
49-
create_observation_specs_with_shapes([(10,), (64, 66, 3), (84, 86, 1)]),
49+
create_observation_specs_with_shapes([(10,), (3, 64, 66), (1, 84, 86)]),
5050
ACTIONSPEC_CONTINUOUS,
5151
),
5252
BehaviorSpec(
53-
create_observation_specs_with_shapes([(10,), (64, 66, 1)]),
53+
create_observation_specs_with_shapes([(10,), (1, 64, 66)]),
5454
ACTIONSPEC_TWODISCRETE,
5555
),
5656
BehaviorSpec(
@@ -71,7 +71,7 @@ def test_factory(behavior_spec: BehaviorSpec) -> None:
7171
"behavior_spec",
7272
[
7373
BehaviorSpec(
74-
create_observation_specs_with_shapes([(10,), (64, 66, 3), (24, 26, 1)]),
74+
create_observation_specs_with_shapes([(10,), (3, 64, 66), (1, 24, 26)]),
7575
ACTIONSPEC_CONTINUOUS,
7676
),
7777
BehaviorSpec(

ml-agents/mlagents/trainers/tests/torch_entities/test_simple_rl.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def test_var_len_obs_and_goal_poca(num_vis, num_vector, num_var_len, conditionin
9393
POCA_TORCH_CONFIG,
9494
hyperparameters=new_hyperparams,
9595
network_settings=new_network,
96-
max_steps=5000,
96+
max_steps=20000,
9797
)
9898
check_environment_trains(env, {BRAIN_NAME: config})
9999

@@ -207,7 +207,7 @@ def test_visual_advanced_ppo(vis_encode_type, num_visual):
207207
num_visual=num_visual,
208208
num_vector=0,
209209
step_size=0.5,
210-
vis_obs_size=(5, 5, 5) if vis_encode_type == "match3" else (36, 36, 3),
210+
vis_obs_size=(5, 5, 5) if vis_encode_type == "match3" else (3, 36, 36),
211211
)
212212
new_networksettings = attr.evolve(
213213
SAC_TORCH_CONFIG.network_settings, vis_encode_type=EncoderType(vis_encode_type)
@@ -311,7 +311,7 @@ def test_visual_advanced_sac(vis_encode_type, num_visual):
311311
num_visual=num_visual,
312312
num_vector=0,
313313
step_size=0.5,
314-
vis_obs_size=(5, 5, 5) if vis_encode_type == "match3" else (36, 36, 3),
314+
vis_obs_size=(5, 5, 5) if vis_encode_type == "match3" else (3, 36, 36),
315315
)
316316
new_networksettings = attr.evolve(
317317
SAC_TORCH_CONFIG.network_settings, vis_encode_type=EncoderType(vis_encode_type)

0 commit comments

Comments
 (0)