Skip to content

Commit 4329fbe

Browse files
authored
Merge pull request #940 from tcmxx/tcmxx/master
Optimize BatchVisualObservations
2 parents ab8d6e8 + 162443e commit 4329fbe

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

unity-environment/Assets/ML-Agents/Scripts/CoreBrainInternal.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -562,36 +562,37 @@ public void OnInspector()
562562
else
563563
pixels = 3;
564564
float[,,,] result = new float[batchSize, height, width, pixels];
565+
float[] resultTemp = new float[batchSize * height * width * pixels];
566+
int hwp = height * width * pixels;
567+
int wp = width * pixels;
565568

566569
for (int b = 0; b < batchSize; b++)
567570
{
568571
Color32[] cc = textures[b].GetPixels32();
569-
for (int w = 0; w < width; w++)
572+
for (int h = height - 1; h >= 0; h--)
570573
{
571-
for (int h = 0; h < height; h++)
574+
for (int w = 0; w < width; w++)
572575
{
573-
Color32 currentPixel = cc[h * width + w];
576+
Color32 currentPixel = cc[(height - h - 1) * width + w];
574577
if (!blackAndWhite)
575578
{
576579
// For Color32, the r, g and b values are between
577580
// 0 and 255.
578-
result[b, textures[b].height - h - 1, w, 0] =
579-
currentPixel.r / 255.0f;
580-
result[b, textures[b].height - h - 1, w, 1] =
581-
currentPixel.g / 255.0f;
582-
result[b, textures[b].height - h - 1, w, 2] =
583-
currentPixel.b / 255.0f;
581+
resultTemp[b * hwp + h * wp + w * pixels] = currentPixel.r / 255.0f;
582+
resultTemp[b * hwp + h * wp + w * pixels + 1] = currentPixel.g / 255.0f;
583+
resultTemp[b * hwp + h * wp + w * pixels + 2] = currentPixel.b / 255.0f;
584584
}
585585
else
586586
{
587-
result[b, textures[b].height - h - 1, w, 0] =
587+
resultTemp[b * hwp + h * wp + w * pixels] =
588588
(currentPixel.r + currentPixel.g + currentPixel.b)
589589
/ 3;
590590
}
591591
}
592592
}
593593
}
594594

595+
System.Buffer.BlockCopy(resultTemp, 0, result, 0, batchSize * hwp * sizeof(float));
595596
return result;
596597
}
597598

0 commit comments

Comments
 (0)