Skip to content

BatchVisualObservations() is not optimized. #927

@tcmxx

Description

@tcmxx

BatchVisualObservations() in CoreBrainInternal is very slow.

I think There are couple of reasons why:

  1. Based on profiling data, textures[b].height is very slow for some reason.
  2. pixel colors are not access in a continuous memory pattern.

The code I am using is like this, not sure if it is what you want but it is much faster

        this List<Texture2D> textures, bool blackAndWhite)
    {
        int batchSize = textures.Count;
        int width = textures[0].width;
        int height = textures[0].height;
        int pixels = 0;
        if (blackAndWhite)
            pixels = 1;
        else
            pixels = 3;
        float[,,,] result = new float[batchSize, height, width, pixels];
        float[] resultTemp = new float[batchSize * height * width * pixels];
        int hwp = height * width * pixels;
        int wp = width * pixels;
        for (int b = 0; b < batchSize; b++)
        {
            Color32[] cc = textures[b].GetPixels32();
            for (int h = height - 1; h >= 0; h--)
            {
                for (int w = 0; w < width; w++)
                {
                    Color32 currentPixel = cc[(height - h - 1) * width + w];
                    if (!blackAndWhite)
                    {
                        // For Color32, the r, g and b values are between
                        // 0 and 255.
                        /*result[b, height - h - 1, w, 0] =
                            currentPixel.r / 255.0f;
                        result[b, height - h - 1, w, 1] =
                            currentPixel.g / 255.0f;
                        result[b, height - h - 1, w, 2] =
                            currentPixel.b / 255.0f;*/

                        resultTemp[b * hwp + h * wp + w * pixels] = currentPixel.r / 255.0f;
                        resultTemp[b * hwp + h * wp + w * pixels + 1] = currentPixel.g / 255.0f;
                        resultTemp[b * hwp + h * wp + w * pixels + 2] = currentPixel.b / 255.0f;

                        /*result[b,h,w,0] = currentPixel.r / 255.0f;
                        result[b, h, w, 1] = currentPixel.g / 255.0f;
                        result[b, h, w, 2] = currentPixel.b / 255.0f;*/
                    }
                    else
                    {
                        /*result[b, height - h - 1, w, 0] =
                            (currentPixel.r + currentPixel.g + currentPixel.b)
                            / 3;*/
                         resultTemp[b * hwp + h * wp + w * pixels] =
                             (currentPixel.r + currentPixel.g + currentPixel.b)
                             / 3;
                        /*result[b, h, w, 0] =
                             (currentPixel.r + currentPixel.g + currentPixel.b)
                             / 3;*/
                    }
                }
            }
        }

        Buffer.BlockCopy(resultTemp, 0, result, 0, batchSize * height * width * pixels * sizeof(float));

        return result;
    }`

Metadata

Metadata

Assignees

Labels

discussionIssue contains general discussion.requestIssue contains a feature request.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions