Forums - Fill FloatTensor took a much long time.

2 posts / 0 new
Last post
Fill FloatTensor took a much long time.
32437506
Join Date: 5 Dec 17
Posts: 6
Posted: Thu, 2017-12-14 22:41

When I put date into a f tensor ,I found it took so much long time ?Why? it spent so much time?  Here is my code.My tensor shape is 395*395*3, it took about 300+ms to fill tensor with data. but the excute process spent only 100+ms.

private void writeRgbBitmapAsFloat(Bitmap image, FloatBuffer meanImage, FloatTensor tensor) {

    final int[] pixels = new int[image.getWidth() * image.getHeight()];
    image.getPixels(pixels, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());


    Log.i("-----","prepare data------1 - 0: " + (System.currentTimeMillis() - mTimeCounter));
    mTimeCounter = System.currentTimeMillis();

    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            final int rgb = pixels[y * image.getWidth() + x];
            float b = ((rgb)       & 0xFF) - meanImage.get();
            float g = ((rgb >>  8) & 0xFF) - meanImage.get();
            float r = ((rgb >> 16) & 0xFF) - meanImage.get();
            float[] pixelFloats = {b, g, r};
            tensor.write(pixelFloats, 0, pixelFloats.length, y, x);
        }
    }

    Log.i("-----","prepare data------1-1: " + (System.currentTimeMillis() - mTimeCounter));
    mTimeCounter = System.currentTimeMillis();
}
  • Up0
  • Down0
simonr_rob
Join Date: 9 Aug 12
Posts: 7
Posted: Thu, 2017-12-21 03:01

Try:

 

float [] floatValues = new float[image.getWidth() * image.getHeight() * 3];

for (int i = 0; i < pixels.length; ++i) {
    floatValues[i * 3 + 0] = ((pixels[i] >> 16) & 0xFF) - meanImage.get();
    floatValues[i * 3 + 1] = ((pixels[i] >> 8) & 0xFF) - meanImage.get();
    floatValues[i * 3 + 2] = (pixels[i] & 0xFF)  - meanImage.get();
}

tensor.write(floatValues, 0, floatValues.length);

To see if it makes a difference.

 

Cheers,

Simon

  • Up0
  • Down0
or Register

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.