From 477cb03729c0ffe5f96def0aa05c4426cbbb0886 Mon Sep 17 00:00:00 2001 From: Arthur Juliani Date: Thu, 28 Jun 2018 11:59:47 -0700 Subject: [PATCH 1/3] Fix for memory leak --- .../Assets/ML-Agents/Scripts/Agent.cs | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/unity-environment/Assets/ML-Agents/Scripts/Agent.cs b/unity-environment/Assets/ML-Agents/Scripts/Agent.cs index d28ecac270..936c233b23 100755 --- a/unity-environment/Assets/ML-Agents/Scripts/Agent.cs +++ b/unity-environment/Assets/ML-Agents/Scripts/Agent.cs @@ -236,22 +236,32 @@ public abstract class Agent : MonoBehaviour /// their own experience. int stepCount; - // Flag to signify that an agent has been reset but the fact that it is - // done has not been communicated (required for On Demand Decisions). + /// Flag to signify that an agent has been reset but the fact that it is + /// done has not been communicated (required for On Demand Decisions). bool hasAlreadyReset; - // Flag to signify that an agent is done and should not reset until - // the fact that it is done has been communicated. + /// Flag to signify that an agent is done and should not reset until + /// the fact that it is done has been communicated. bool terminate; /// Unique identifier each agent receives at initialization. It is used /// to separate between different agents in the environment. int id; + /// Array of Texture2D used to render to from render buffer before + /// transforming into float tensor. + Texture2D[] textureArray; + /// Monobehavior function that is called when the attached GameObject /// becomes enabled or active. void OnEnable() { + textureArray = new Texture2D[agentParameters.agentCameras.Count]; + for (int i = 0; i < brain.brainParameters.cameraResolutions.Length; i++) + { + textureArray[i] = new Texture2D(brain.brainParameters.cameraResolutions[i].width, + brain.brainParameters.cameraResolutions[i].height, TextureFormat.RGB24, false); + } id = gameObject.GetInstanceID(); Academy academy = Object.FindObjectOfType() as Academy; OnEnableHelper(academy); @@ -564,7 +574,8 @@ void SendInfoToBrain() info.visualObservations.Add(ObservationToTexture( agentParameters.agentCameras[i], param.cameraResolutions[i].width, - param.cameraResolutions[i].height)); + param.cameraResolutions[i].height, + textureArray[i])); } info.reward = reward; @@ -926,37 +937,42 @@ void MakeRequests(int academyStepCounter) /// Converts a camera and correspinding resolution to a 2D texture. /// /// The 2D texture. - /// Camera. + /// Camera. /// Width of resulting 2D texture. /// Height of resulting 2D texture. - public static Texture2D ObservationToTexture(Camera camera, int width, int height) + /// Texture2D to render to. + public static Texture2D ObservationToTexture(Camera obsCamera, int width, int height, Texture2D texture2D) { - Rect oldRec = camera.rect; - camera.rect = new Rect(0f, 0f, 1f, 1f); + Rect oldRec = obsCamera.rect; + obsCamera.rect = new Rect(0f, 0f, 1f, 1f); var depth = 24; var format = RenderTextureFormat.Default; var readWrite = RenderTextureReadWrite.Default; var tempRT = RenderTexture.GetTemporary(width, height, depth, format, readWrite); - var tex = new Texture2D(width, height, TextureFormat.RGB24, false); + + if (width != texture2D.width || height != texture2D.height) + { + texture2D = new Texture2D(width, height, TextureFormat.RGB24, false); + } var prevActiveRT = RenderTexture.active; - var prevCameraRT = camera.targetTexture; + var prevCameraRT = obsCamera.targetTexture; // render to offscreen texture (readonly from CPU side) RenderTexture.active = tempRT; - camera.targetTexture = tempRT; + obsCamera.targetTexture = tempRT; - camera.Render(); + obsCamera.Render(); - tex.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0); - tex.Apply(); - camera.targetTexture = prevCameraRT; - camera.rect = oldRec; + texture2D.ReadPixels(new Rect(0, 0, texture2D.width, texture2D.height), 0, 0); + texture2D.Apply(); + obsCamera.targetTexture = prevCameraRT; + obsCamera.rect = oldRec; RenderTexture.active = prevActiveRT; RenderTexture.ReleaseTemporary(tempRT); - return tex; + return texture2D; } } } From e527bf42c23a2c1f3ebf24dbad6c4d0b0a707a90 Mon Sep 17 00:00:00 2001 From: Arthur Juliani Date: Thu, 28 Jun 2018 12:14:22 -0700 Subject: [PATCH 2/3] Replace return w/ reference --- unity-environment/Assets/ML-Agents/Scripts/Agent.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/unity-environment/Assets/ML-Agents/Scripts/Agent.cs b/unity-environment/Assets/ML-Agents/Scripts/Agent.cs index 936c233b23..0e53c4e15a 100755 --- a/unity-environment/Assets/ML-Agents/Scripts/Agent.cs +++ b/unity-environment/Assets/ML-Agents/Scripts/Agent.cs @@ -571,11 +571,12 @@ void SendInfoToBrain() for (int i = 0; i < brain.brainParameters.cameraResolutions.Length; i++) { - info.visualObservations.Add(ObservationToTexture( + ObservationToTexture( agentParameters.agentCameras[i], param.cameraResolutions[i].width, param.cameraResolutions[i].height, - textureArray[i])); + ref textureArray[i]); + info.visualObservations.Add(textureArray[i]); } info.reward = reward; @@ -941,7 +942,7 @@ void MakeRequests(int academyStepCounter) /// Width of resulting 2D texture. /// Height of resulting 2D texture. /// Texture2D to render to. - public static Texture2D ObservationToTexture(Camera obsCamera, int width, int height, Texture2D texture2D) + public static void ObservationToTexture(Camera obsCamera, int width, int height, ref Texture2D texture2D) { Rect oldRec = obsCamera.rect; obsCamera.rect = new Rect(0f, 0f, 1f, 1f); @@ -972,7 +973,6 @@ public static Texture2D ObservationToTexture(Camera obsCamera, int width, int he obsCamera.rect = oldRec; RenderTexture.active = prevActiveRT; RenderTexture.ReleaseTemporary(tempRT); - return texture2D; } } } From 298a46446296cd244dbda73719f5f9ce24b64189 Mon Sep 17 00:00:00 2001 From: Arthur Juliani Date: Thu, 28 Jun 2018 13:43:04 -0700 Subject: [PATCH 3/3] Use resize instead of new --- unity-environment/Assets/ML-Agents/Scripts/Agent.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unity-environment/Assets/ML-Agents/Scripts/Agent.cs b/unity-environment/Assets/ML-Agents/Scripts/Agent.cs index 0e53c4e15a..12226ab654 100755 --- a/unity-environment/Assets/ML-Agents/Scripts/Agent.cs +++ b/unity-environment/Assets/ML-Agents/Scripts/Agent.cs @@ -955,7 +955,7 @@ public static void ObservationToTexture(Camera obsCamera, int width, int height, if (width != texture2D.width || height != texture2D.height) { - texture2D = new Texture2D(width, height, TextureFormat.RGB24, false); + texture2D.Resize(width, height); } var prevActiveRT = RenderTexture.active;