浏览代码

unit tests for RenderTextureSensor+component (#3569)

/bug-failed-api-check
GitHub 5 年前
当前提交
11ff1553
共有 5 个文件被更改,包括 114 次插入2 次删除
  1. 18
      com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs
  2. 34
      com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSenorTests.cs
  3. 11
      com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSenorTests.cs.meta
  4. 42
      com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSensorComponentTests.cs
  5. 11
      com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSensorComponentTests.cs.meta

18
com.unity.ml-agents/Runtime/Sensors/RenderTextureSensor.cs


var texture = ObservationToTexture(m_RenderTexture);
// TODO support more types here, e.g. JPG
var compressed = texture.EncodeToPNG();
UnityEngine.Object.Destroy(texture);
DestroyTexture(texture);
return compressed;
}
}

{
var texture = ObservationToTexture(m_RenderTexture);
var numWritten = Utilities.TextureToTensorProxy(texture, adapter, m_Grayscale);
UnityEngine.Object.Destroy(texture);
DestroyTexture(texture);
return numWritten;
}
}

texture2D.Apply();
RenderTexture.active = prevActiveRt;
return texture2D;
}
static void DestroyTexture(Texture2D texture)
{
if (Application.isEditor)
{
// Edit Mode tests complain if we use Destroy()
// TODO move to extension methods for UnityEngine.Object?
UnityEngine.Object.DestroyImmediate(texture);
}
else
{
UnityEngine.Object.Destroy(texture);
}
}
}
}

34
com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSenorTests.cs


using System;
using NUnit.Framework;
using UnityEngine;
using MLAgents.Sensors;
namespace MLAgents.Tests
{
[TestFixture]
public class RenderTextureSensorTest
{
[Test]
public void TestRenderTextureSensor()
{
foreach (var grayscale in new[] { true, false })
{
foreach (SensorCompressionType compression in Enum.GetValues(typeof(SensorCompressionType)))
{
var width = 24;
var height = 16;
var texture = new RenderTexture(width, height, 0);
var sensor = new RenderTextureSensor(texture, grayscale, "TestCameraSensor", compression);
var writeAdapter = new WriteAdapter();
var obs = sensor.GetObservationProto(writeAdapter);
Assert.AreEqual((int) compression, (int) obs.CompressionType);
var expectedShape = new[] { height, width, grayscale ? 1 : 3 };
Assert.AreEqual(expectedShape, obs.Shape);
}
}
}
}
}

11
com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSenorTests.cs.meta


fileFormatVersion: 2
guid: be9f7d8ce17d8407e92d46fbee2ab809
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

42
com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSensorComponentTests.cs


using System;
using NUnit.Framework;
using UnityEngine;
using MLAgents.Sensors;
namespace MLAgents.Tests
{
[TestFixture]
public class RenderTextureSensorComponentTest
{
[Test]
public void TestRenderTextureSensorComponent()
{
foreach (var grayscale in new[] { true, false })
{
foreach (SensorCompressionType compression in Enum.GetValues(typeof(SensorCompressionType)))
{
var width = 24;
var height = 16;
var texture = new RenderTexture(width, height, 0);
var agentGameObj = new GameObject("agent");
var renderTexComponent = agentGameObj.AddComponent<RenderTextureSensorComponent>();
renderTexComponent.renderTexture = texture;
renderTexComponent.grayscale = grayscale;
renderTexComponent.compression = compression;
var expectedShape = new[] { height, width, grayscale ? 1 : 3 };
Assert.AreEqual(expectedShape, renderTexComponent.GetObservationShape());
Assert.IsTrue(renderTexComponent.IsVisual());
Assert.IsFalse(renderTexComponent.IsVector());
var sensor = renderTexComponent.CreateSensor();
Assert.AreEqual(expectedShape, sensor.GetObservationShape());
Assert.AreEqual(typeof(RenderTextureSensor), sensor.GetType());
}
}
}
}
}

11
com.unity.ml-agents/Tests/Editor/Sensor/RenderTextureSensorComponentTests.cs.meta


fileFormatVersion: 2
guid: 6be53c3cd01244f179a58c96560c54cf
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存