Preprocessing images on caffe2 android app

wlou: <p>Hello, I trained my CNN on PyTorch and have the three lines below as preprocessing:         transforms.Resize(256),<br>
transforms.CenterCrop(224),<br>
transforms.ToTensor(),<br>
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 <a href="https://github.com/cedrickchee/pytorch-android/tree/master/app/src" rel="noopener nofollow ugc">https://github.com/cedrickchee/pytorch-android/tree/master/app/src</a>. 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. <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/3/3ae98ea1d7bd948448041aaad195a7b8622d6da9.png" data-download-href="https://discuss.pytorch.org/uploads/default/3ae98ea1d7bd948448041aaad195a7b8622d6da9" title="android"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/3/3ae98ea1d7bd948448041aaad195a7b8622d6da9_2_690x359.png" alt="android" data-base62-sha1="8pa5p991NI8D7bgDj7eD2Vy1QVj" width="690" height="359" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/3/3ae98ea1d7bd948448041aaad195a7b8622d6da9_2_690x359.png, https://discuss.pytorch.org/uploads/default/optimized/2X/3/3ae98ea1d7bd948448041aaad195a7b8622d6da9_2_1035x538.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/3/3ae98ea1d7bd948448041aaad195a7b8622d6da9.png 2x" data-dominant-color="FBFBFC"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">android</span><span class="informations">1245×649 45.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

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

tnoulas: <p>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 …</p>

tnoulas: <p>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.</p>
<p>For Python I used:</p>
<p>transform =  transforms.Compose([<br>
transforms.Resize((224,224)),<br>
# transforms.CenterCrop(224),<br>
transforms.ToTensor(),<br>
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])<br>
])</p>
<p>Note that I have commented out the CenterCrop that is not available on Pytorch for Android. Then on Android I use:<br>
float norm_mean[] = new float[]{(float)0.485, (float)0.456, (float)0.406};<br>
float norm_std[] = new float[]{(float)0.229, (float)0.224, (float)0.225};<br>
Tensor inputTensor = TensorImageUtils.bitmapToFloat32Tensor(bitmap, 0,0, 224,224,<br>
norm_mean, norm_std);</p>
<p>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…</p>