GitHub
4 年前
当前提交
3bcb029b
共有 38 个文件被更改,包括 466 次插入 和 819 次删除
-
2ml-agents/mlagents/trainers/agent_processor.py
-
7ml-agents/mlagents/trainers/behavior_id_utils.py
-
2ml-agents/mlagents/trainers/components/bc/model.py
-
8ml-agents/mlagents/trainers/components/bc/module.py
-
35ml-agents/mlagents/trainers/components/reward_signals/curiosity/model.py
-
32ml-agents/mlagents/trainers/components/reward_signals/gail/model.py
-
2ml-agents/mlagents/trainers/components/reward_signals/gail/signal.py
-
74ml-agents/mlagents/trainers/demo_loader.py
-
5ml-agents/mlagents/trainers/env_manager.py
-
8ml-agents/mlagents/trainers/ghost/trainer.py
-
54ml-agents/mlagents/trainers/models.py
-
7ml-agents/mlagents/trainers/policy/nn_policy.py
-
42ml-agents/mlagents/trainers/policy/tf_policy.py
-
8ml-agents/mlagents/trainers/ppo/trainer.py
-
5ml-agents/mlagents/trainers/sac/network.py
-
6ml-agents/mlagents/trainers/sac/trainer.py
-
13ml-agents/mlagents/trainers/simple_env_manager.py
-
22ml-agents/mlagents/trainers/subprocess_env_manager.py
-
187ml-agents/mlagents/trainers/tests/mock_brain.py
-
38ml-agents/mlagents/trainers/tests/test_agent_processor.py
-
4ml-agents/mlagents/trainers/tests/test_barracuda_converter.py
-
37ml-agents/mlagents/trainers/tests/test_bcmodule.py
-
70ml-agents/mlagents/trainers/tests/test_demo_loader.py
-
100ml-agents/mlagents/trainers/tests/test_ghost.py
-
49ml-agents/mlagents/trainers/tests/test_nn_policy.py
-
138ml-agents/mlagents/trainers/tests/test_ppo.py
-
17ml-agents/mlagents/trainers/tests/test_reward_signals.py
-
19ml-agents/mlagents/trainers/tests/test_rl_trainer.py
-
86ml-agents/mlagents/trainers/tests/test_sac.py
-
6ml-agents/mlagents/trainers/tests/test_subprocess_env_manager.py
-
4ml-agents/mlagents/trainers/tests/test_trainer_controller.py
-
29ml-agents/mlagents/trainers/tests/test_trainer_util.py
-
4ml-agents/mlagents/trainers/tests/test_trajectory.py
-
6ml-agents/mlagents/trainers/trainer/trainer.py
-
4ml-agents/mlagents/trainers/trainer_controller.py
-
36ml-agents/mlagents/trainers/tests/test_models.py
-
88ml-agents/mlagents/trainers/brain.py
-
31ml-agents/mlagents/trainers/brain_conversion_utils.py
|
|||
import pytest |
|||
|
|||
from mlagents.trainers.models import ModelUtils |
|||
from mlagents.tf_utils import tf |
|||
from mlagents_envs.base_env import BehaviorSpec, ActionType |
|||
|
|||
|
|||
def create_behavior_spec(num_visual, num_vector, vector_size): |
|||
behavior_spec = BehaviorSpec( |
|||
[(84, 84, 3)] * int(num_visual) + [(vector_size,)] * int(num_vector), |
|||
ActionType.DISCRETE, |
|||
(1,), |
|||
) |
|||
return behavior_spec |
|||
|
|||
|
|||
@pytest.mark.parametrize("num_visual", [1, 2, 4]) |
|||
@pytest.mark.parametrize("num_vector", [1, 2, 4]) |
|||
def test_create_input_placeholders(num_vector, num_visual): |
|||
vec_size = 8 |
|||
name_prefix = "test123" |
|||
bspec = create_behavior_spec(num_visual, num_vector, vec_size) |
|||
vec_in, vis_in = ModelUtils.create_input_placeholders( |
|||
bspec.observation_shapes, name_prefix=name_prefix |
|||
) |
|||
|
|||
assert isinstance(vis_in, list) |
|||
assert len(vis_in) == num_visual |
|||
assert isinstance(vec_in, tf.Tensor) |
|||
assert vec_in.get_shape().as_list()[1] == num_vector * 8 |
|||
|
|||
# Check names contain prefix and vis shapes are correct |
|||
for _vis in vis_in: |
|||
assert _vis.get_shape().as_list() == [None, 84, 84, 3] |
|||
assert _vis.name.startswith(name_prefix) |
|||
assert vec_in.name.startswith(name_prefix) |
|
|||
from mlagents_envs.communicator_objects.agent_info_pb2 import AgentInfoProto |
|||
from mlagents_envs.communicator_objects.brain_parameters_pb2 import BrainParametersProto |
|||
from typing import List, NamedTuple |
|||
|
|||
|
|||
class CameraResolution(NamedTuple): |
|||
height: int |
|||
width: int |
|||
num_channels: int |
|||
|
|||
@property |
|||
def gray_scale(self) -> bool: |
|||
return self.num_channels == 1 |
|||
|
|||
def __str__(self): |
|||
return f"CameraResolution({self.height}, {self.width}, {self.num_channels})" |
|||
|
|||
|
|||
class BrainParameters: |
|||
def __init__( |
|||
self, |
|||
brain_name: str, |
|||
vector_observation_space_size: int, |
|||
camera_resolutions: List[CameraResolution], |
|||
vector_action_space_size: List[int], |
|||
vector_action_descriptions: List[str], |
|||
vector_action_space_type: int, |
|||
): |
|||
""" |
|||
Contains all brain-specific parameters. |
|||
""" |
|||
self.brain_name = brain_name |
|||
self.vector_observation_space_size = vector_observation_space_size |
|||
self.number_visual_observations = len(camera_resolutions) |
|||
self.camera_resolutions = camera_resolutions |
|||
self.vector_action_space_size = vector_action_space_size |
|||
self.vector_action_descriptions = vector_action_descriptions |
|||
self.vector_action_space_type = ["discrete", "continuous"][ |
|||
vector_action_space_type |
|||
] |
|||
|
|||
def __str__(self): |
|||
return """Unity brain name: {} |
|||
Number of Visual Observations (per agent): {} |
|||
Camera Resolutions: {} |
|||
Vector Observation space size (per agent): {} |
|||
Vector Action space type: {} |
|||
Vector Action space size (per agent): {} |
|||
Vector Action descriptions: {}""".format( |
|||
self.brain_name, |
|||
str(self.number_visual_observations), |
|||
str([str(cr) for cr in self.camera_resolutions]), |
|||
str(self.vector_observation_space_size), |
|||
self.vector_action_space_type, |
|||
str(self.vector_action_space_size), |
|||
", ".join(self.vector_action_descriptions), |
|||
) |
|||
|
|||
@staticmethod |
|||
def from_proto( |
|||
brain_param_proto: BrainParametersProto, agent_info: AgentInfoProto |
|||
) -> "BrainParameters": |
|||
""" |
|||
Converts brain parameter proto to BrainParameter object. |
|||
:param brain_param_proto: protobuf object. |
|||
:return: BrainParameter object. |
|||
""" |
|||
resolutions = [ |
|||
CameraResolution(obs.shape[0], obs.shape[1], obs.shape[2]) |
|||
for obs in agent_info.observations |
|||
if len(obs.shape) >= 3 |
|||
] |
|||
|
|||
total_vector_obs = sum( |
|||
obs.shape[0] for obs in agent_info.observations if len(obs.shape) == 1 |
|||
) |
|||
|
|||
brain_params = BrainParameters( |
|||
brain_name=brain_param_proto.brain_name, |
|||
vector_observation_space_size=total_vector_obs, |
|||
camera_resolutions=resolutions, |
|||
vector_action_space_size=list(brain_param_proto.vector_action_size), |
|||
vector_action_descriptions=list( |
|||
brain_param_proto.vector_action_descriptions |
|||
), |
|||
vector_action_space_type=brain_param_proto.vector_action_space_type, |
|||
) |
|||
return brain_params |
|
|||
from mlagents.trainers.brain import BrainParameters, CameraResolution |
|||
from mlagents_envs.base_env import BehaviorSpec |
|||
import numpy as np |
|||
from typing import List |
|||
|
|||
|
|||
def behavior_spec_to_brain_parameters( |
|||
name: str, behavior_spec: BehaviorSpec |
|||
) -> BrainParameters: |
|||
vec_size = np.sum( |
|||
[shape[0] for shape in behavior_spec.observation_shapes if len(shape) == 1] |
|||
) |
|||
vis_sizes = [shape for shape in behavior_spec.observation_shapes if len(shape) == 3] |
|||
cam_res = [CameraResolution(s[0], s[1], s[2]) for s in vis_sizes] |
|||
a_size: List[int] = [] |
|||
if behavior_spec.is_action_discrete(): |
|||
a_size += list(behavior_spec.discrete_action_branches) |
|||
vector_action_space_type = 0 |
|||
else: |
|||
a_size += [behavior_spec.action_size] |
|||
vector_action_space_type = 1 |
|||
return BrainParameters( |
|||
name, int(vec_size), cam_res, a_size, [], vector_action_space_type |
|||
) |
|||
|
|||
|
|||
def get_global_agent_id(worker_id: int, agent_id: int) -> str: |
|||
""" |
|||
Create an agent id that is unique across environment workers using the worker_id. |
|||
""" |
|||
return f"${worker_id}-{agent_id}" |
撰写
预览
正在加载...
取消
保存
Reference in new issue