浏览代码

Fix for vis obs memory leak in docker (#2274)

* Fix for vis obs memory leak in docker

* Remove reversions from code
/develop-generalizationTraining-TrainerController
GitHub 5 年前
当前提交
49f20394
共有 1 个文件被更改,包括 18 次插入30 次删除
  1. 48
      UnitySDK/Assets/ML-Agents/Scripts/Agent.cs

48
UnitySDK/Assets/ML-Agents/Scripts/Agent.cs


using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Google.Protobuf;
using MLAgents.CommunicatorObjects;

agentInfoProto.VisualObservations.Add(
ByteString.CopyFrom(obs.EncodeToPNG())
);
Object.Destroy(obs);
visualObservations.Clear();
return agentInfoProto;
}
}

/// Keeps track of the actions that are masked at each step.
private ActionMasker actionMasker;
/// Array of Texture2D used to render to from render buffer before
/// transforming into float tensor.
Texture2D[] textureArray;
/// <summary>
/// Demonstration recorder.
/// </summary>

/// becomes enabled or active.
void OnEnable()
{
var textureCount = agentParameters.agentCameras.Count+agentParameters.agentRenderTextures.Count;
textureArray = new Texture2D[textureCount];
for (int i = 0; i < textureCount; i++)
{
textureArray[i] = new Texture2D(1, 1, TextureFormat.RGB24, false);
}
id = gameObject.GetInstanceID();
Academy academy = Object.FindObjectOfType<Academy>() as Academy;
OnEnableHelper(academy);

//First add all cameras
for (int i = 0; i < agentParameters.agentCameras.Count; i++)
{
ObservationToTexture(
var obsTexture = ObservationToTexture(
param.cameraResolutions[i].height,
ref textureArray[i]);
info.visualObservations.Add(textureArray[i]);
param.cameraResolutions[i].height);
info.visualObservations.Add(obsTexture);
}
//Then add all renderTextures

ObservationToTexture(
var obsTexture = ObservationToTexture(
param.cameraResolutions[camCount+i].height,
ref textureArray[i]);
info.visualObservations.Add(textureArray[i]);
param.cameraResolutions[camCount+i].height);
info.visualObservations.Add(obsTexture);
}
info.reward = reward;

{
action.memories.AddRange(memories);
}
public List<float> GetMemoriesAction()
{
return action.memories;

/// <param name="width">Width of resulting 2D texture.</param>
/// <param name="height">Height of resulting 2D texture.</param>
/// <param name="texture2D">Texture2D to render to.</param>
public static void ObservationToTexture(Camera obsCamera, int width, int height, ref Texture2D texture2D)
public static Texture2D ObservationToTexture(Camera obsCamera, int width, int height)
var texture2D = new Texture2D(width, height, TextureFormat.RGB24, false);
Rect oldRec = obsCamera.rect;
obsCamera.rect = new Rect(0f, 0f, 1f, 1f);
var depth = 24;

var tempRT =
RenderTexture.GetTemporary(width, height, depth, format, readWrite);
if (width != texture2D.width || height != texture2D.height)
{
texture2D.Resize(width, height);
}
var prevActiveRT = RenderTexture.active;
var prevCameraRT = obsCamera.targetTexture;

obsCamera.Render();
texture2D.ReadPixels(new Rect(0, 0, texture2D.width, texture2D.height), 0, 0);
texture2D.Apply();
return texture2D;
}
/// <summary>

/// <param name="width">Width of resulting 2D texture.</param>
/// <param name="height">Height of resulting 2D texture.</param>
/// <param name="texture2D">Texture2D to render to.</param>
public static void ObservationToTexture(RenderTexture obsTexture, int width, int height, ref Texture2D texture2D)
public static Texture2D ObservationToTexture(RenderTexture obsTexture, int width, int height)
var texture2D = new Texture2D(width, height, TextureFormat.RGB24, false);
if (width != texture2D.width || height != texture2D.height)
{
texture2D.Resize(width, height);

texture2D.ReadPixels(new Rect(0, 0, texture2D.width, texture2D.height), 0, 0);
texture2D.Apply();
RenderTexture.active = prevActiveRT;
return texture2D;
}
/// <summary>

正在加载...
取消
保存