浏览代码

Add visual version of task and simply encoders

/goal-conditioning/new
Arthur Juliani 4 年前
当前提交
a180dbf7
共有 21 个文件被更改,包括 2342 次插入93 次删除
  1. 6
      Project/Assets/ML-Agents/Examples/GoalNav/Prefabs/Area.prefab
  2. 27
      Project/Assets/ML-Agents/Examples/GoalNav/Scripts/GoalNavAgent.cs
  3. 19
      com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs
  4. 15
      com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs
  5. 8
      com.unity.ml-agents/Runtime/Sensors/ITypedSensor.cs
  6. 2
      com.unity.ml-agents/Tests/Editor/Sensor/CameraSensorTest.cs
  7. 2
      com.unity.ml-agents/Tests/Editor/Sensor/StackingSensorTests.cs
  8. 77
      ml-agents/mlagents/trainers/torch/layers.py
  9. 28
      ml-agents/mlagents/trainers/torch/networks.py
  10. 8
      Project/Assets/ML-Agents/Examples/GoalNav/Materials.meta
  11. 1001
      Project/Assets/ML-Agents/Examples/GoalNav/Prefabs/VisualArea.prefab
  12. 7
      Project/Assets/ML-Agents/Examples/GoalNav/Prefabs/VisualArea.prefab.meta
  13. 83
      Project/Assets/ML-Agents/Examples/GoalNav/RainbowFloor.mat
  14. 8
      Project/Assets/ML-Agents/Examples/GoalNav/RainbowFloor.mat.meta
  15. 1001
      Project/Assets/ML-Agents/Examples/GoalNav/Scenes/VisualGoalNav.unity
  16. 7
      Project/Assets/ML-Agents/Examples/GoalNav/Scenes/VisualGoalNav.unity.meta
  17. 7
      Project/Assets/ML-Agents/Examples/GoalNav/rainbow.png
  18. 88
      Project/Assets/ML-Agents/Examples/GoalNav/rainbow.png.meta
  19. 33
      Project/Assets/ML-Agents/Examples/GoalNav/Materials/rainbow.mat
  20. 8
      Project/Assets/ML-Agents/Examples/GoalNav/Materials/rainbow.mat.meta

6
Project/Assets/ML-Agents/Examples/GoalNav/Prefabs/Area.prefab


NumStackedVectorObservations: 2
m_ActionSpec:
m_NumContinuousActions: 0
BranchSizes: 07000000
VectorActionSize: 07000000
BranchSizes: 05000000
VectorActionSize: 05000000
VectorActionDescriptions: []
VectorActionSpaceType: 0
hasUpgradedBrainParametersWithActionSpec: 1

- landmark
- wall
m_RaysPerDirection: 5
m_MaxRayDegrees: 90
m_MaxRayDegrees: 180
m_SphereCastRadius: 0.5
m_RayLength: 15
m_RayLayerMask:

27
Project/Assets/ML-Agents/Examples/GoalNav/Scripts/GoalNavAgent.cs


public override void CollectObservations(VectorSensor sensor)
{
goalSensor = this.GetComponent<GoalSensorComponent>();
goalSensor.AddGoal(goalLoc / 10f);
goalSensor.AddGoal(obstacleLoc/ 10f);
if (useVectorObs)
{
goalSensor.AddGoal(goalLoc / 10f);
}
goalSensor.AddGoal(obstacleLoc / 10f);
}

dirToGo = transform.forward * -1f;
break;
case 3:
rotateDir = transform.up * 1f;
dirToGo = transform.right * -0.75f;
rotateDir = transform.up * -1f;
break;
case 5:
dirToGo = transform.right * -0.75f;
break;
case 6:
transform.Rotate(rotateDir, Time.fixedDeltaTime * 200f);
//transform.Rotate(rotateDir, Time.fixedDeltaTime * 200f);
m_AgentRb.AddForce(dirToGo * m_PushBlockSettings.agentRunSpeed,
ForceMode.VelocityChange);
}

discreteActionsOut[0] = 0;
if (Input.GetKey(KeyCode.D))
{
discreteActionsOut[0] = 3;
discreteActionsOut[0] = 4;
}
else if (Input.GetKey(KeyCode.W))
{

{
discreteActionsOut[0] = 4;
discreteActionsOut[0] = 3;
}
else if (Input.GetKey(KeyCode.S))
{

/// </summary>
public override void OnEpisodeBegin()
{
var rotation = Random.Range(0, 4);
var rotationAngle = rotation * 90f;
area.transform.Rotate(new Vector3(0f, rotationAngle, 0f));
//var rotation = Random.Range(0, 4);
//var rotationAngle = rotation * 90f;
//area.transform.Rotate(new Vector3(0f, rotationAngle, 0f));
transform.position = GetRandomSpawnPos();
m_AgentRb.velocity = Vector3.zero;

19
com.unity.ml-agents/Runtime/Sensors/CameraSensor.cs


/// <summary>
/// A sensor that wraps a Camera object to generate visual observations for an agent.
/// </summary>
public class CameraSensor : ISensor
public class CameraSensor : ISensor, ITypedSensor
{
Camera m_Camera;
int m_Width;

int[] m_Shape;
SensorCompressionType m_CompressionType;
ObservationType m_ObservationType;
/// <summary>
/// The Camera used for rendering the sensor observations.

set { m_CompressionType = value; }
}
public ObservationType ObservationType
{
get { return m_ObservationType; }
set { m_ObservationType = value; }
}
/// <summary>
/// Creates and returns the camera sensor.

/// <param name="name">The name of the camera sensor.</param>
/// <param name="compression">The compression to apply to the generated image.</param>
public CameraSensor(
Camera camera, int width, int height, bool grayscale, string name, SensorCompressionType compression)
Camera camera, int width, int height, bool grayscale, string name, SensorCompressionType compression,
ObservationType observationType)
{
m_Camera = camera;
m_Width = width;

m_Shape = GenerateShape(width, height, grayscale);
m_CompressionType = compression;
m_ObservationType = observationType;
}
/// <summary>

public string GetName()
{
return m_Name;
}
/// <inheritdoc/>
public virtual ObservationType GetObservationType()
{
return m_ObservationType;
}
/// <summary>

15
com.unity.ml-agents/Runtime/Sensors/CameraSensorComponent.cs


set { m_SensorName = value; }
}
[HideInInspector, SerializeField, FormerlySerializedAs("observationType")]
ObservationType m_ObservationType = ObservationType.Goal;
[HideInInspector, SerializeField, FormerlySerializedAs("width")]
int m_Width = 84;

}
/// <summary>
/// The type of the observation.
/// </summary>
public ObservationType SensorObservationType
{
get { return m_ObservationType; }
set { m_ObservationType = value; UpdateSensor(); }
}
/// <summary>
m_Sensor = new CameraSensor(m_Camera, m_Width, m_Height, Grayscale, m_SensorName, m_Compression);
m_Sensor = new CameraSensor(m_Camera, m_Width, m_Height, Grayscale, m_SensorName, m_Compression, m_ObservationType);
if (ObservationStacks != 1)
{

{
m_Sensor.Camera = m_Camera;
m_Sensor.CompressionType = m_Compression;
m_Sensor.ObservationType = m_ObservationType;
}
}
}

8
com.unity.ml-agents/Runtime/Sensors/ITypedSensor.cs


public enum ObservationType
{
// Collected observations are generic.
Default = 0,
Default,
Goal = 1,
Goal,
Reward = 2,
Reward,
Message = 3,
Message,
}

2
com.unity.ml-agents/Tests/Editor/Sensor/CameraSensorTest.cs


var width = 24;
var height = 16;
var camera = Camera.main;
var sensor = new CameraSensor(camera, width, height, grayscale, "TestCameraSensor", compression);
var sensor = new CameraSensor(camera, width, height, grayscale, "TestCameraSensor", compression, ObservationType.Default);
var obsWriter = new ObservationWriter();
var obs = sensor.GetObservationProto(obsWriter);

2
com.unity.ml-agents/Tests/Editor/Sensor/StackingSensorTests.cs


{
// Test grayscale stacked mapping with CameraSensor
var cameraSensor = new CameraSensor(new Camera(), 64, 64,
true, "grayscaleCamera", SensorCompressionType.PNG);
true, "grayscaleCamera", SensorCompressionType.PNG, ObservationType.Default);
var stackedCameraSensor = new StackingSensor(cameraSensor, 2);
Assert.AreEqual(stackedCameraSensor.GetCompressedChannelMapping(), new[] { 0, 0, 0, 1, 1, 1 });

77
ml-agents/mlagents/trainers/torch/layers.py


from enum import Enum
class ConditioningMode(Enum):
DEFAULT = 0
HYPER = 1
SOFT_MUL = 2
SOFT_SUM = 3
class Swish(torch.nn.Module):
def forward(self, data: torch.Tensor) -> torch.Tensor:
return torch.mul(data, torch.sigmoid(data))

return (layer_activations - mean) / (torch.sqrt(var + 1e-5))
class ConditionalEncoder(torch.nn.Module):
"""
Linear layers.
"""
class ConditionalLayer(torch.nn.Module):
def __init__(
self,
input_size: int,

kernel_init: Initialization = Initialization.KaimingHeNormal,
kernel_gain: float = 1.0,
condition_type: ConditioningMode,
self.layers = []
self.goal_encoders = []
prev_size = input_size + goal_size
for _ in range(num_layers):
self.layers.append(
linear_layer(
prev_size,
hidden_size,
kernel_init=kernel_init,
kernel_gain=kernel_gain,
)
self.condition_type = condition_type
if self.condition_type != ConditioningMode.HYPER:
self.goal_encoder = LinearEncoder(
goal_size, num_layers, hidden_size, final_activation=True
self.goal_encoders.append(
LinearEncoder(goal_size, 2, hidden_size, final_activation=True)
self.input_encoder = linear_layer(input_size, hidden_size)
else:
self.input_encoder = HyperNetwork(
input_size, hidden_size, goal_size, num_layers, hidden_size
self.layers.append(Swish())
prev_size = hidden_size
self.layers = torch.nn.ModuleList(self.layers)
self.goal_encoders = torch.nn.ModuleList(self.goal_encoders)
activation = torch.cat([input_tensor, goal_tensor], dim=-1)
for idx, layer in enumerate(self.layers):
if isinstance(layer, Swish):
activation = layer(activation)
else:
activation = layer(activation) * self.goal_encoders[idx // 2](
goal_tensor
)
return activation
if self.condition_type == ConditioningMode.SOFT_MUL:
return self.input_encoder(input_tensor) * self.goal_encoder(goal_tensor)
elif self.condition_type == ConditioningMode.SOFT_SUM:
return self.input_encoder(input_tensor) + self.goal_encoder(goal_tensor)
else:
return self.input_encoder(input_tensor, goal_tensor)
class HyperEncoder(torch.nn.Module):
class ConditionalEncoder(torch.nn.Module):
"""
Linear layers.
"""

goal_size: int,
num_layers: int,
hidden_size: int,
condition_type: ConditioningMode,
conditional_layers: int,
num_hyper_layers: int = 1,
if i < num_layers - num_hyper_layers:
if num_layers - i <= conditional_layers:
self.layers.append(
ConditionalLayer(
prev_size, goal_size, 2, hidden_size, condition_type
)
)
else:
self.layers.append(
linear_layer(
prev_size,

)
)
else:
self.layers.append(
HyperNetwork(prev_size, hidden_size, goal_size, 2, hidden_size)
)
self.layers = torch.nn.ModuleList(self.layers)
self.layers = torch.nn.ModuleList(self.layers)
def forward(
self, input_tensor: torch.Tensor, goal_tensor: torch.Tensor

if isinstance(layer, HyperNetwork):
if isinstance(layer, ConditionalLayer):
activation = layer(activation, goal_tensor)
else:
activation = layer(activation)

28
ml-agents/mlagents/trainers/torch/networks.py


from mlagents.trainers.settings import NetworkSettings
from mlagents.trainers.torch.utils import ModelUtils
from mlagents.trainers.torch.decoders import ValueHeads
from mlagents.trainers.torch.layers import (
LSTM,
LinearEncoder,
ConditionalEncoder,
HyperEncoder,
)
from mlagents.trainers.torch.layers import LSTM, LinearEncoder, ConditionalEncoder, ConditioningMode
from mlagents.trainers.torch.encoders import VectorInput
from mlagents.trainers.buffer import AgentBuffer
from mlagents.trainers.trajectory import ObsUtil

]
EPSILON = 1e-7
class ConditioningMode(Enum):
DEFAULT = 0
HYPER = 1
SOFT = 3
class NetworkBody(nn.Module):

if (
ObservationType.GOAL in self.obs_types
and self.conditioning_mode == ConditioningMode.HYPER
):
self.linear_encoder = HyperEncoder(
total_enc_size,
total_goal_size,
network_settings.num_layers,
self.h_size,
num_hyper_layers=1,
)
elif (
ObservationType.GOAL in self.obs_types
and self.conditioning_mode == ConditioningMode.SOFT
and self.conditioning_mode != ConditioningMode.DEFAULT
):
self.linear_encoder = ConditionalEncoder(
total_enc_size,

condition_type=self.conditioning_mode,
conditional_layers=1,
)
else:
self.linear_encoder = LinearEncoder(

8
Project/Assets/ML-Agents/Examples/GoalNav/Materials.meta


fileFormatVersion: 2
guid: 21cd0cd17ab48441885e4ef86c073bfd
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

1001
Project/Assets/ML-Agents/Examples/GoalNav/Prefabs/VisualArea.prefab
文件差异内容过多而无法显示
查看文件

7
Project/Assets/ML-Agents/Examples/GoalNav/Prefabs/VisualArea.prefab.meta


fileFormatVersion: 2
guid: 5026853f7c20546528e81ea5a650b802
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

83
Project/Assets/ML-Agents/Examples/GoalNav/RainbowFloor.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: RainbowFloor
m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0}
m_ShaderKeywords: _EMISSION _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
m_LightmapFlags: 1
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _BumpMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailAlbedoMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailMask:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _DetailNormalMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _EmissionMap:
m_Texture: {fileID: 2800000, guid: d2d6b5af144604b0f8e88760ae2319b4, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MainTex:
m_Texture: {fileID: 2800000, guid: d2d6b5af144604b0f8e88760ae2319b4, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _MetallicGlossMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _OcclusionMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
- _ParallaxMap:
m_Texture: {fileID: 0}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _BumpScale: 1
- _Cutoff: 0.5
- _DetailNormalMapScale: 1
- _DrawU: 1
- _DrawV: 1
- _DstBlend: 0
- _GlossMapScale: 1
- _Glossiness: 0.5
- _GlossyReflections: 1
- _GridSize: 6
- _LineSize: 0.009
- _Metallic: 0
- _Mode: 0
- _OcclusionStrength: 1
- _Parallax: 0.02
- _SmoothnessTextureChannel: 1
- _SpecularHighlights: 1
- _SrcBlend: 1
- _UVSec: 0
- _ZWrite: 1
m_Colors:
- _CellColor: {r: 0, g: 0, b: 0, a: 0}
- _Color: {r: 1, g: 1, b: 1, a: 1}
- _EmissionColor: {r: 0.2264151, g: 0.2264151, b: 0.2264151, a: 1}
- _LineColor: {r: 1, g: 1, b: 1, a: 1}

8
Project/Assets/ML-Agents/Examples/GoalNav/RainbowFloor.mat.meta


fileFormatVersion: 2
guid: 6c458fa586a0c425fa1a4f9bd3ad7f53
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:

1001
Project/Assets/ML-Agents/Examples/GoalNav/Scenes/VisualGoalNav.unity
文件差异内容过多而无法显示
查看文件

7
Project/Assets/ML-Agents/Examples/GoalNav/Scenes/VisualGoalNav.unity.meta


fileFormatVersion: 2
guid: a2d5173a85d7145f0a1d74be403bd08f
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

7
Project/Assets/ML-Agents/Examples/GoalNav/rainbow.png
文件差异内容过多而无法显示
查看文件

88
Project/Assets/ML-Agents/Examples/GoalNav/rainbow.png.meta


fileFormatVersion: 2
guid: d2d6b5af144604b0f8e88760ae2319b4
TextureImporter:
fileIDToRecycleName: {}
externalObjects: {}
serializedVersion: 9
mipmaps:
mipMapMode: 0
enableMipMap: 1
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: -1
aniso: -1
mipBias: -100
wrapU: -1
wrapV: -1
wrapW: -1
nPOTScale: 1
lightmap: 0
compressionQuality: 50
spriteMode: 0
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 0
spriteTessellationDetail: -1
textureType: 0
textureShape: 1
singleChannelComponent: 0
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
platformSettings:
- serializedVersion: 2
buildTarget: DefaultTexturePlatform
maxTextureSize: 2048
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID:
vertices: []
indices:
edges: []
weights: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

33
Project/Assets/ML-Agents/Examples/GoalNav/Materials/rainbow.mat


%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!21 &2100000
Material:
serializedVersion: 6
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: rainbow
m_Shader: {fileID: 4800000, guid: bbecf177c38d84ff08d7c683c116affd, type: 3}
m_ShaderKeywords:
m_LightmapFlags: 4
m_EnableInstancingVariants: 0
m_DoubleSidedGI: 0
m_CustomRenderQueue: -1
stringTagMap: {}
disabledShaderPasses: []
m_SavedProperties:
serializedVersion: 3
m_TexEnvs:
- _MainTex:
m_Texture: {fileID: 2800000, guid: d2d6b5af144604b0f8e88760ae2319b4, type: 3}
m_Scale: {x: 1, y: 1}
m_Offset: {x: 0, y: 0}
m_Floats:
- _DrawU: 1
- _DrawV: 1
- _GridSize: 10
- _LineSize: 0.15
m_Colors:
- _CellColor: {r: 0, g: 0, b: 0, a: 0}
- _LineColor: {r: 1, g: 1, b: 1, a: 1}

8
Project/Assets/ML-Agents/Examples/GoalNav/Materials/rainbow.mat.meta


fileFormatVersion: 2
guid: 8e6c6e2706b134878ae1d56bb435cb20
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 2100000
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存