Preprocessing images on caffe2 android app

Hello, I trained my CNN on PyTorch and have the three lines below as preprocessing: transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) . I am building an android app and I used the code here to build it https://github.com/cedrickchee/pytorch-android/tree/master/app/src. I am not sure about what this part of the cpp code means ? And how should I do the preprocessing that I need (center crop and normalize)? Thank you so much for help.

It looks like the code you’ve posted subtracts the mean in each channel, but does not divide by the standard deviation.
Also, the code is apparently dealing with unnormalized image values in [0, 255], which explains the mean values.
The complicated looking conversion is just the transformation of YUV to RGB format.

1 Like

I think the Resize and CenterCrop bits are not implemented in the Android version of TensorImageUtils and hence all the pain we are going through …

One addition here that does not solve the problem 100% as described, but it at least allows for one to train a model in python using a set of transformations and then run the model again on Android using a ‘similar’ set of transformation.

For Python I used:

transform = transforms.Compose([
transforms.Resize((224,224)),
# transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])

Note that I have commented out the CenterCrop that is not available on Pytorch for Android. Then on Android I use:
float norm_mean[] = new float[]{(float)0.485, (float)0.456, (float)0.406};
float norm_std[] = new float[]{(float)0.229, (float)0.224, (float)0.225};
Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap, 0,0, 224,224,
norm_mean, norm_std);

The transformations are not 100% the same because of the differences in implementation, but at least are similar enough to allow for alignment in model prediction…