Deploy The YOLOv5 Model With Tensorflow Lite

Viewed 78

I want to make an application that guesses the name of the selected food image.I used yolov5. Output is correct on test images in colab. I'm testing with the same image. https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb I created the yolov5s-fp16.lite file from here.

https://www.youtube.com/watch?v=tySgZ1rEbW4 I followed this video for it to work on android platform.

my tf lite file on android

try {
    Yolov5sFp16 model = Yolov5sFp16.newInstance(context);

    // Creates inputs for reference.
    TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, 640, 640, 3}, DataType.FLOAT32);
    inputFeature0.loadBuffer(byteBuffer);

    // Runs model inference and gets result.
    Yolov5sFp16.Outputs outputs = model.process(inputFeature0);
    TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();

    // Releases model resources if no longer used.
    model.close();
} catch (IOException e) {
    // TODO Handle the exception
}

predictBtn

predictBtn.setOnClickListener(new View.OnClickListener() {
             @Override
            public void onClick(View view){

                try {
                    Yolov5sFp16 model = Yolov5sFp16.newInstance(MainActivity.this);

                    // Creates inputs for reference.
                    TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{4, 640, 640, 3}, DataType.FLOAT32);
                    Log.d("shape", inputFeature0.toString());

                    bitmap = Bitmap.createScaledBitmap(bitmap, 640 ,640,true );

                     inputFeature0.loadBuffer(TensorImage.fromBitmap(bitmap).getBuffer());

                    // Runs model inference and gets result.
                    Yolov5sFp16.Outputs outputs = model.process(inputFeature0);

                    TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();


                    result.setText(labels[getMax(outputFeature0.getFloatArray())]+" ");

                     // Releases model resources if no longer used.
                    model.close();
                } catch (IOException e) {
                    // TODO Handle the exception
                }




            }
        });
    }

ERROR

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myapplicationdeploy, PID: 6744
    java.lang.IllegalArgumentException: The size of byte buffer and the shape do not match.
        at org.tensorflow.lite.support.common.SupportPreconditions.checkArgument(SupportPreconditions.java:104)
        at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:309)
        at org.tensorflow.lite.support.tensorbuffer.TensorBuffer.loadBuffer(TensorBuffer.java:328)
        at com.example.myapplicationdeploy.MainActivity$3.onClick(MainActivity.java:108)
        at android.view.View.performClick(View.java:6597)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1194)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

When I debug, it gives an error in the buffer part. What should I do ?What should the dimensions be? I would love any ideas. THANKS EVERYONE.:))))

0 Answers
Related