I've built a image classifier using Tensorflow which I am running on Android using the Android Tensorflow library. My issue is that when classifying an image on Android the predicted class is completely off. But when classifying the image using Python with the same model the predicted class is correct.
The below method is how I am converting my bitmap into an array of RGB pixel values.(which I've taken from sample-tensorflow-imageclassifier and here).
public static float[] getPixels(Bitmap bitmap) {
final int IMAGE_SIZE = 168;
int[] intValues = new int[IMAGE_SIZE * IMAGE_SIZE];
float[] floatValues = new float[IMAGE_SIZE * IMAGE_SIZE * 3];
if (bitmap.getWidth() != IMAGE_SIZE || bitmap.getHeight() != IMAGE_SIZE) {
// rescale the bitmap if needed
bitmap = ThumbnailUtils.extractThumbnail(bitmap, IMAGE_SIZE, IMAGE_SIZE);
}
bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
for (int i = 0; i < intValues.length; ++i) {
final int val = intValues[i];
// bitwise shifting - without our image is shaped [1, 168, 168, 1] but we need [1, 168, 168, 3]
floatValues[i * 3] = Color.red(val);
floatValues[i * 3 + 1] = Color.green(val);
floatValues[i * 3 + 2] = Color.blue(val);
}
return floatValues;
}
I have tried converting the float array of pixels received from getPixels(bitmap:Bitmap) back to a Bitmap and have noticed a difference in the colour, so I am guessing this is the issue? Is there a way to convert the pixels without losing colour information?
Attached is the original image and the image which I have converted back after applying the above function.
Image converted with above method
Any help will be greatly appreciated.