1

I am working on an application for image classification based on this android-studio project example: https://github.com/tensorflow/examples/blob/master/lite/examples/image_classification/android/EXPLORE_THE_CODE.md

The issue is that the CNN model that I am using is trained on image pixel range between 0 and 255 and the image classification in the link example is done on tensor image format (pixels between 0 and 1) so the classification will be wrong.

I need to multiply the Tensor by 255 but I am not figuring out how it can be done here :

/** Loads input image, and applys preprocessing. */
private TensorImage loadImage(final Bitmap bitmap, int sensorOrientation) {
  // Loads bitmap into a TensorImage.
  image.load(bitmap);

  // Creates processor for the TensorImage.
  int cropSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
  int numRoration = sensorOrientation / 90;
  ImageProcessor imageProcessor =
      new ImageProcessor.Builder()
          .add(new ResizeWithCropOrPadOp(cropSize, cropSize))
          .add(new ResizeOp(imageSizeX, imageSizeY, ResizeMethod.BILINEAR))
          .add(new Rot90Op(numRoration))
          .add(getPreprocessNormalizeOp())
          .build();
  return imageProcessor.process(inputImageBuffer);
}

Can someone help me please?

Thank you in advance for your help!!

andreadaou
  • 43
  • 6
  • 1
    try by removing this line `.add(getPreprocessNormalizeOp()` as it normalizes your input image. – imtiaz ul Hassan Sep 23 '21 at 11:04
  • This normalization is mean and standard deviation normalization ((image-mean)/std) so removing it will not let me go back to 0-255 :( – andreadaou Sep 23 '21 at 11:10
  • 1
    The original pixels are in the range (0,255). Normally different methods are used to preprocess input images. In your case `((image-mean)/std)` has been used. This converts your pixels from -1 to +1. Min-max scaling divides every pixel with 255 converting them to 0-1 range. But both aren't used at one time :) – imtiaz ul Hassan Sep 23 '21 at 11:47
  • But passing from bitmap to tensor here `TensorImage.inputImageBuffer.load(bitmap);` will rearrange the dimensions of the image from [HxWxC] to [CxHxW] and pass pixels from [0-255] to [0-1] no? As I know using `.ToTensor()` transformation in PyTorch for example will do this. So for example in python I multiply the obtained image by 255 to go back to [0-255] and the classification will then work perfectly. – andreadaou Sep 23 '21 at 11:56
  • I think I understand now your answer. I also read [link](https://stackoverflow.com/questions/57963341/why-does-the-tensorflow-lite-example-use-image-mean-and-image-std-when-adding-pi). In fact it is not working when removing the `.add(getPreprocessNormalizeOp()` although the range must be now from 0 to 255 as you mentioned but I don't know why. Thank you for your help!!!! – andreadaou Sep 23 '21 at 12:50

0 Answers0