/// <summary> /// Initializes the sensor. /// </summary> /// <param name="wrapped">The wrapped sensor.</param> /// <param name="numStackedObservations">Number of stacked observations to keep.</param> public StackingSensor(ISensor wrapped, int numStackedObservations) { // TODO ensure numStackedObservations > 1 m_WrappedSensor = wrapped; m_NumStackedObservations = numStackedObservations; m_Name = $"StackingSensor_size{numStackedObservations}_{wrapped.GetName()}"; if (wrapped.GetCompressionType() != SensorCompressionType.None) { throw new UnityAgentsException("StackingSensor doesn't support compressed observations.'"); } var shape = wrapped.GetObservationShape(); if (shape.Length != 1) { throw new UnityAgentsException("Only 1-D observations are supported by StackingSensor"); } m_Shape = new int[shape.Length]; m_UnstackedObservationSize = wrapped.ObservationSize(); for (int d = 0; d < shape.Length; d++) { m_Shape[d] = shape[d]; } // TODO support arbitrary stacking dimension m_Shape[0] *= numStackedObservations; m_StackedObservations = new float[numStackedObservations][]; for (var i = 0; i < numStackedObservations; i++) { m_StackedObservations[i] = new float[m_UnstackedObservationSize]; } }
/// <inheritdoc/> public void Reset() { m_WrappedSensor.Reset(); // Zero out the buffer. for (var i = 0; i < m_NumStackedObservations; i++) { Array.Clear(m_StackedObservations[i], 0, m_StackedObservations[i].Length); } if (m_WrappedSensor.GetCompressionType() != SensorCompressionType.None) { for (var i = 0; i < m_NumStackedObservations; i++) { m_StackedCompressedObservations[i] = m_EmptyCompressedObservation; } } }
/// <summary> /// Generate an ObservationProto for the sensor using the provided WriteAdapter. /// This is equivalent to producing an Observation and calling Observation.ToProto(), /// but avoid some intermediate memory allocations. /// </summary> /// <param name="sensor"></param> /// <param name="writeAdapter"></param> /// <returns></returns> public static ObservationProto GetObservationProto(this ISensor sensor, WriteAdapter writeAdapter) { var shape = sensor.GetObservationShape(); ObservationProto observationProto = null; if (sensor.GetCompressionType() == SensorCompressionType.None) { var numFloats = sensor.ObservationSize(); var floatDataProto = new ObservationProto.Types.FloatData(); // Resize the float array // TODO upgrade protobuf versions so that we can set the Capacity directly - see https://github.com/protocolbuffers/protobuf/pull/6530 for (var i = 0; i < numFloats; i++) { floatDataProto.Data.Add(0.0f); } writeAdapter.SetTarget(floatDataProto.Data, sensor.GetObservationShape(), 0); sensor.Write(writeAdapter); observationProto = new ObservationProto { FloatData = floatDataProto, CompressionType = (CompressionTypeProto)SensorCompressionType.None, }; } else { var compressedObs = sensor.GetCompressedObservation(); if (compressedObs == null) { throw new UnityAgentsException( $"GetCompressedObservation() returned null data for sensor named {sensor.GetName()}. " + "You must return a byte[]. If you don't want to use compressed observations, " + "return SensorCompressionType.None from GetCompressionType()." ); } observationProto = new ObservationProto { CompressedData = ByteString.CopyFrom(compressedObs), CompressionType = (CompressionTypeProto)sensor.GetCompressionType(), }; } observationProto.Shape.AddRange(shape); return(observationProto); }
/// <summary> /// Generate an ObservationProto for the sensor using the provided WriteAdapter. /// This is equivalent to producing an Observation and calling Observation.ToProto(), /// but avoid some intermediate memory allocations. /// </summary> /// <param name="sensor"></param> /// <param name="writeAdapter"></param> /// <returns></returns> public static ObservationProto GetObservationProto(this ISensor sensor, WriteAdapter writeAdapter) { var shape = sensor.GetObservationShape(); ObservationProto observationProto = null; if (sensor.GetCompressionType() == SensorCompressionType.None) { var numFloats = sensor.ObservationSize(); var floatDataProto = new ObservationProto.Types.FloatData(); // Resize the float array // TODO upgrade protobuf versions so that we can set the Capacity directly - see https://github.com/protocolbuffers/protobuf/pull/6530 for (var i = 0; i < numFloats; i++) { floatDataProto.Data.Add(0.0f); } writeAdapter.SetTarget(floatDataProto.Data, sensor.GetObservationShape(), 0); sensor.Write(writeAdapter); observationProto = new ObservationProto { FloatData = floatDataProto, CompressionType = (CompressionTypeProto)SensorCompressionType.None, }; } else { observationProto = new ObservationProto { CompressedData = ByteString.CopyFrom(sensor.GetCompressedObservation()), CompressionType = (CompressionTypeProto)sensor.GetCompressionType(), }; } observationProto.Shape.AddRange(shape); return(observationProto); }
/// <summary> /// Initializes the sensor. /// </summary> /// <param name="wrapped">The wrapped sensor.</param> /// <param name="numStackedObservations">Number of stacked observations to keep.</param> public StackingSensor(ISensor wrapped, int numStackedObservations) { // TODO ensure numStackedObservations > 1 m_WrappedSensor = wrapped; m_NumStackedObservations = numStackedObservations; m_Name = $"StackingSensor_size{numStackedObservations}_{wrapped.GetName()}"; m_WrappedSpec = wrapped.GetObservationSpec(); m_UnstackedObservationSize = wrapped.ObservationSize(); // Set up the cached observation spec for the StackingSensor var newShape = m_WrappedSpec.Shape; // TODO support arbitrary stacking dimension newShape[newShape.Length - 1] *= numStackedObservations; m_ObservationSpec = new ObservationSpec( newShape, m_WrappedSpec.DimensionProperties, m_WrappedSpec.ObservationType ); // Initialize uncompressed buffer anyway in case python trainer does not // support the compression mapping and has to fall back to uncompressed obs. m_StackedObservations = new float[numStackedObservations][]; for (var i = 0; i < numStackedObservations; i++) { m_StackedObservations[i] = new float[m_UnstackedObservationSize]; } if (m_WrappedSensor.GetCompressionType() != SensorCompressionType.None) { m_StackedCompressedObservations = new byte[numStackedObservations][]; m_EmptyCompressedObservation = CreateEmptyPNG(); for (var i = 0; i < numStackedObservations; i++) { m_StackedCompressedObservations[i] = m_EmptyCompressedObservation; } m_CompressionMapping = ConstructStackedCompressedChannelMapping(wrapped); } if (m_WrappedSpec.Rank != 1) { var wrappedShape = m_WrappedSpec.Shape; m_tensorShape = new TensorShape(0, wrappedShape[0], wrappedShape[1], wrappedShape[2]); } }
public static EventObservationSpec FromSensor(ISensor sensor) { var shape = sensor.GetObservationShape(); var dimInfos = new EventObservationDimensionInfo[shape.Length]; for (var i = 0; i < shape.Length; i++) { dimInfos[i].Size = shape[i]; // TODO copy flags when we have them } return(new EventObservationSpec { SensorName = sensor.GetName(), CompressionType = sensor.GetCompressionType().ToString(), DimensionInfos = dimInfos, }); }
/// <summary> /// Initializes the sensor. /// </summary> /// <param name="wrapped">The wrapped sensor.</param> /// <param name="numStackedObservations">Number of stacked observations to keep.</param> public StackingSensor(ISensor wrapped, int numStackedObservations) { // TODO ensure numStackedObservations > 1 m_WrappedSensor = wrapped; m_NumStackedObservations = numStackedObservations; m_Name = $"StackingSensor_size{numStackedObservations}_{wrapped.GetName()}"; m_WrappedShape = wrapped.GetObservationShape(); m_Shape = new int[m_WrappedShape.Length]; m_UnstackedObservationSize = wrapped.ObservationSize(); for (int d = 0; d < m_WrappedShape.Length; d++) { m_Shape[d] = m_WrappedShape[d]; } // TODO support arbitrary stacking dimension m_Shape[m_Shape.Length - 1] *= numStackedObservations; // Initialize uncompressed buffer anyway in case python trainer does not // support the compression mapping and has to fall back to uncompressed obs. m_StackedObservations = new float[numStackedObservations][]; for (var i = 0; i < numStackedObservations; i++) { m_StackedObservations[i] = new float[m_UnstackedObservationSize]; } if (m_WrappedSensor.GetCompressionType() != SensorCompressionType.None) { m_StackedCompressedObservations = new byte[numStackedObservations][]; m_EmptyCompressedObservation = CreateEmptyPNG(); for (var i = 0; i < numStackedObservations; i++) { m_StackedCompressedObservations[i] = m_EmptyCompressedObservation; } m_CompressionMapping = ConstructStackedCompressedChannelMapping(wrapped); } if (m_Shape.Length != 1) { m_tensorShape = new TensorShape(0, m_WrappedShape[0], m_WrappedShape[1], m_WrappedShape[2]); } }
public static EventObservationSpec FromSensor(ISensor sensor) { var shape = sensor.GetObservationShape(); var dimInfos = new EventObservationDimensionInfo[shape.Length]; for (var i = 0; i < shape.Length; i++) { dimInfos[i].Size = shape[i]; // TODO copy flags when we have them } var builtInSensorType = (sensor as IBuiltInSensor)?.GetBuiltInSensorType() ?? Sensors.BuiltInSensorType.Unknown; return(new EventObservationSpec { SensorName = sensor.GetName(), CompressionType = sensor.GetCompressionType().ToString(), BuiltInSensorType = (int)builtInSensorType, DimensionInfos = dimInfos, }); }
public static EventObservationSpec FromSensor(ISensor sensor) { var shape = sensor.GetObservationShape(); var dimProps = (sensor as IDimensionPropertiesSensor)?.GetDimensionProperties(); var dimInfos = new EventObservationDimensionInfo[shape.Length]; for (var i = 0; i < shape.Length; i++) { dimInfos[i].Size = shape[i]; dimInfos[i].Flags = dimProps != null ? (int)dimProps[i] : 0; } var builtInSensorType = (sensor as IBuiltInSensor)?.GetBuiltInSensorType() ?? Sensors.BuiltInSensorType.Unknown; return(new EventObservationSpec { SensorName = sensor.GetName(), CompressionType = sensor.GetCompressionType().ToString(), BuiltInSensorType = (int)builtInSensorType, DimensionInfos = dimInfos, }); }
/// <summary> /// Generate an ObservationProto for the sensor using the provided ObservationWriter. /// This is equivalent to producing an Observation and calling Observation.ToProto(), /// but avoid some intermediate memory allocations. /// </summary> /// <param name="sensor"></param> /// <param name="observationWriter"></param> /// <returns></returns> public static ObservationProto GetObservationProto(this ISensor sensor, ObservationWriter observationWriter) { var shape = sensor.GetObservationShape(); ObservationProto observationProto = null; var compressionType = sensor.GetCompressionType(); // Check capabilities if we need to concatenate PNGs if (compressionType == SensorCompressionType.PNG && shape.Length == 3 && shape[2] > 3) { var trainerCanHandle = Academy.Instance.TrainerCapabilities == null || Academy.Instance.TrainerCapabilities.ConcatenatedPngObservations; if (!trainerCanHandle) { if (!s_HaveWarnedTrainerCapabilitiesMultiPng) { Debug.LogWarning( $"Attached trainer doesn't support multiple PNGs. Switching to uncompressed observations for sensor {sensor.GetName()}. " + "Please find the versions that work best together from our release page: " + "https://github.com/Unity-Technologies/ml-agents/releases" ); s_HaveWarnedTrainerCapabilitiesMultiPng = true; } compressionType = SensorCompressionType.None; } } // Check capabilities if we need mapping for compressed observations if (compressionType != SensorCompressionType.None && shape.Length == 3 && shape[2] > 3) { var trainerCanHandleMapping = Academy.Instance.TrainerCapabilities == null || Academy.Instance.TrainerCapabilities.CompressedChannelMapping; var isTrivialMapping = IsTrivialMapping(sensor); if (!trainerCanHandleMapping && !isTrivialMapping) { if (!s_HaveWarnedTrainerCapabilitiesMapping) { Debug.LogWarning( $"The sensor {sensor.GetName()} is using non-trivial mapping and " + "the attached trainer doesn't support compression mapping. " + "Switching to uncompressed observations. " + "Please find the versions that work best together from our release page: " + "https://github.com/Unity-Technologies/ml-agents/releases" ); s_HaveWarnedTrainerCapabilitiesMapping = true; } compressionType = SensorCompressionType.None; } } if (compressionType == SensorCompressionType.None) { var numFloats = sensor.ObservationSize(); var floatDataProto = new ObservationProto.Types.FloatData(); // Resize the float array // TODO upgrade protobuf versions so that we can set the Capacity directly - see https://github.com/protocolbuffers/protobuf/pull/6530 for (var i = 0; i < numFloats; i++) { floatDataProto.Data.Add(0.0f); } observationWriter.SetTarget(floatDataProto.Data, sensor.GetObservationShape(), 0); sensor.Write(observationWriter); observationProto = new ObservationProto { FloatData = floatDataProto, CompressionType = (CompressionTypeProto)SensorCompressionType.None, }; } else { var compressedObs = sensor.GetCompressedObservation(); if (compressedObs == null) { throw new UnityAgentsException( $"GetCompressedObservation() returned null data for sensor named {sensor.GetName()}. " + "You must return a byte[]. If you don't want to use compressed observations, " + "return SensorCompressionType.None from GetCompressionType()." ); } observationProto = new ObservationProto { CompressedData = ByteString.CopyFrom(compressedObs), CompressionType = (CompressionTypeProto)sensor.GetCompressionType(), }; var compressibleSensor = sensor as ISparseChannelSensor; if (compressibleSensor != null) { observationProto.CompressedChannelMapping.AddRange(compressibleSensor.GetCompressedChannelMapping()); } } // Add the dimension properties if any to the observationProto var dimensionPropertySensor = sensor as IDimensionPropertiesSensor; if (dimensionPropertySensor != null) { var dimensionProperties = dimensionPropertySensor.GetDimensionProperties(); int[] intDimensionProperties = new int[dimensionProperties.Length]; for (int i = 0; i < dimensionProperties.Length; i++) { observationProto.DimensionProperties.Add((int)dimensionProperties[i]); } // Checking trainer compatibility with variable length observations if (dimensionProperties.Length == 2) { if (dimensionProperties[0] == DimensionProperty.VariableSize && dimensionProperties[1] == DimensionProperty.None) { var trainerCanHandleVarLenObs = Academy.Instance.TrainerCapabilities == null || Academy.Instance.TrainerCapabilities.VariableLengthObservation; if (!trainerCanHandleVarLenObs) { throw new UnityAgentsException("Variable Length Observations are not supported by the trainer"); } } } } observationProto.Shape.AddRange(shape); // Add the observation type, if any, to the observationProto var typeSensor = sensor as ITypedSensor; if (typeSensor != null) { observationProto.ObservationType = (ObservationTypeProto)typeSensor.GetObservationType(); } else { observationProto.ObservationType = ObservationTypeProto.Default; } return(observationProto); }
/// <summary> /// Generate an ObservationProto for the sensor using the provided ObservationWriter. /// This is equivalent to producing an Observation and calling Observation.ToProto(), /// but avoid some intermediate memory allocations. /// </summary> /// <param name="sensor"></param> /// <param name="observationWriter"></param> /// <returns></returns> public static ObservationProto GetObservationProto(this ISensor sensor, ObservationWriter observationWriter) { var shape = sensor.GetObservationShape(); ObservationProto observationProto = null; var compressionType = sensor.GetCompressionType(); // Check capabilities if we need to concatenate PNGs if (compressionType == SensorCompressionType.PNG && shape.Length == 3 && shape[2] > 3) { var trainerCanHandle = Academy.Instance.TrainerCapabilities == null || Academy.Instance.TrainerCapabilities.ConcatenatedPngObservations; if (!trainerCanHandle) { if (!s_HaveWarnedAboutTrainerCapabilities) { Debug.LogWarning($"Attached trainer doesn't support multiple PNGs. Switching to uncompressed observations for sensor {sensor.GetName()}."); s_HaveWarnedAboutTrainerCapabilities = true; } compressionType = SensorCompressionType.None; } } if (compressionType == SensorCompressionType.None) { var numFloats = sensor.ObservationSize(); var floatDataProto = new ObservationProto.Types.FloatData(); // Resize the float array // TODO upgrade protobuf versions so that we can set the Capacity directly - see https://github.com/protocolbuffers/protobuf/pull/6530 for (var i = 0; i < numFloats; i++) { floatDataProto.Data.Add(0.0f); } observationWriter.SetTarget(floatDataProto.Data, sensor.GetObservationShape(), 0); sensor.Write(observationWriter); observationProto = new ObservationProto { FloatData = floatDataProto, CompressionType = (CompressionTypeProto)SensorCompressionType.None, }; } else { var compressedObs = sensor.GetCompressedObservation(); if (compressedObs == null) { throw new UnityAgentsException( $"GetCompressedObservation() returned null data for sensor named {sensor.GetName()}. " + "You must return a byte[]. If you don't want to use compressed observations, " + "return SensorCompressionType.None from GetCompressionType()." ); } observationProto = new ObservationProto { CompressedData = ByteString.CopyFrom(compressedObs), CompressionType = (CompressionTypeProto)sensor.GetCompressionType(), }; } observationProto.Shape.AddRange(shape); return(observationProto); }