'Android Studio tslite application wrong result problem
Hello everyone I am working about android application that classify marble types. I've learned a model with Resnet Model. After that I copied a code from github and changed tslite model with mine.I selected a marble photo from my test files and upload to the app. But every photo gives me same result. I think problem is on bytebuffer set part. How can I solve this problem? Thank you All!.
public void classifyImage(Bitmap image){
try {
Model model = Model.newInstance(getApplicationContext());
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 224, 224, 3}, DataType.FLOAT32);
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4 * imageSize * imageSize * 3);
byteBuffer.order(ByteOrder.nativeOrder());
int[] intValues = new int[imageSize * imageSize];
image.getPixels(intValues, 0, image.getWidth(), 0, 0, image.getWidth(), image.getHeight());
int pixel = 0;
//iterate over each pixel and extract R, G, and B values. Add those values individually to the byte buffer.
for(int i = 0; i < imageSize; i ++){
for(int j = 0; j < imageSize; j++){
int val = intValues[pixel++]; // RGB
byteBuffer.putFloat(((val >> 16) & 0xFF) * (1.f / 255));
byteBuffer.putFloat(((val >> 8) & 0xFF) * (1.f / 255));
byteBuffer.putFloat((val & 0xFF) * (1.f / 255));
}
}
inputFeature0.loadBuffer(byteBuffer);
// Runs model inference and gets result.
Model.Outputs outputs = model.process(inputFeature0);
TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
float[] confidences = outputFeature0.getFloatArray();
// find the index of the class with the biggest confidence.
int maxPos = 0;
float maxConfidence = 0;
for (int i = 0; i < confidences.length; i++) {
if (confidences[i] > maxConfidence) {
maxConfidence = confidences[i];
maxPos = i;
}
}
String[] classes = {"AfyonBeyaz","AfyonGrey", "Bejmermer", "Kaplanpostu", "Karacabeysiyah", "KristalEmprador"};
result.setText(classes[maxPos]);
// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
// TODO Handle the exception
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
