2

So basically we are developing a script for android devices to normalise bitmap and our external team already developed image normalisation stuff in python.

We are using an android PyTorch library(version 1.10.0) to apply normalisation to bitmap image via tensor as shown in the below code

public static float[] TORCHVISION_NORM_MEAN_RGB = new float[] {0.485f, 0.456f, 0.406f};
  public static float[] TORCHVISION_NORM_STD_RGB = new float[] {0.229f, 0.224f, 0.225f};

Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bmp,
           TORCHVISION_NORM_MEAN_RGB, TORCHVISION_NORM_STD_RGB);

Here we are getting the same image as an original image from tensor array to bitmap. So we think that the normalisation of a tensor is not properly applied. Here we are looking for some bluish effect images which have been generated by python. For ex., we are expecting output like the below bluish image. bluish python image

But we are getting the same image after normalisation as the original one. for ex., this android image

and we are using the below code for converting tensor(inputTensor) to bitmap.

final float[] scoreInput = inputTensor.getDataAsFloatArray();
Bitmap outBitmap = TensorToBitmap.floatArrayToBitmap(scoreInput, 300, 300);

fun floatArrayToBitmap(floatArray: FloatArray, width: Int, height: Int): Bitmap {

            // Create empty bitmap in ARGB format
            val bmp: Bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            val pixels = IntArray(width * height * 4)

            // mapping smallest value to 0 and largest value to 255
            val maxValue = floatArray.maxOrNull() ?: 1.0f
            val minValue = floatArray.minOrNull() ?: -1.0f
            val delta = maxValue - minValue

            // Define if float min..max will be mapped to 0..255 or 255..0
            val conversion = { v: Float -> ((v - minValue) / delta * 255.0f).roundToInt() }

            // copy each value from float array to RGB channels
            for (i in 0 until width * height) {
                val r = conversion(floatArray[i])
                val g = conversion(floatArray[i + width * height])
                val b = conversion(floatArray[i + 2 * width * height])
                pixels[i] = rgb(r, g, b) // you might need to import for rgb()
            }
            bmp.setPixels(pixels, 0, width, 0, 0, width, height)

            return bmp
        }

Anyone can help to generate a bluish normalise image via android PyTorch?

0 Answers0