surendramaran / YOLOv8-TfLite-Object-Detector

A sample android application of live object detection for any YOLOv8 detection model
https://www.surendramaran.com/
64 stars 22 forks source link

Cannot run with .TFlite with batch size > 1. #22

Open boyapdao opened 1 month ago

boyapdao commented 1 month ago

I have successfully run the source with the TFlite file with the following parameters, input Shape [1, 640, 640, 3] and output Shape [1, 10, 8400]. But after trying with another file with input Shape [4, 320, 320, 3], output Shape [4, 84, 2100] parameters it crashed. Following is the error in logcat:

FATAL EXCEPTION: pool-2-thread-1
Process: com.surendramaran.yolov8tflite, PID: 9281
java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (inputs_0) with 4915200 bytes from a Java Buffer with 1228800 bytes.
    at org.tensorflow.lite.TensorImpl.throwIfSrcShapeIsIncompatible(TensorImpl.java:426)
    at org.tensorflow.lite.TensorImpl.setTo(TensorImpl.java:150)
    at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:260)
    at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:101)
    at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:95)
    at org.tensorflow.lite.InterpreterImpl.run(InterpreterImpl.java:94)
    at org.tensorflow.lite.Interpreter.run(Interpreter.java:95)
    at com.surendramaran.yolov8tflite.Detector.detect(Detector.kt:135)
    at com.surendramaran.yolov8tflite.MainActivity.bindCameraUseCases$lambda$7(MainActivity.kt:130)
    at com.surendramaran.yolov8tflite.MainActivity.$r8$lambda$SwMDthyT4p6a561NxBZ96bJyetY(Unknown Source:0)
    at com.surendramaran.yolov8tflite.MainActivity$$ExternalSyntheticLambda6.analyze(D8$$SyntheticClass:0)
    at androidx.camera.core.ImageAnalysis.lambda$setAnalyzer$2(ImageAnalysis.java:527)
    at androidx.camera.core.ImageAnalysis$$ExternalSyntheticLambda2.analyze(D8$$SyntheticClass:0)
    at androidx.camera.core.ImageAnalysisAbstractAnalyzer.lambda$analyzeImage$0$androidx-camera-core-ImageAnalysisAbstractAnalyzer(ImageAnalysisAbstractAnalyzer.java:286)
    at androidx.camera.core.ImageAnalysisAbstractAnalyzer$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)

I understand that with the second file, the batch size has changed from 1 -> 4. How should I edit the code to be able to run with the second file? First file parameters: first file parameters

2nd file parameters: image

first file: 2806_int8.tflite.zip second file: yolov8n_float16_batch_1.tflite.zip label: labels.txt

surendramaran commented 1 month ago

But is it really yolov8?

On Mon, 5 Aug, 2024, 2:48 PM boyapdao, @.***> wrote:

I have successfully run the source with the TFlite file with the following parameters, input Shape [1, 640, 640, 3] and output Shape [1, 10, 8400]. But after trying with another file with input Shape [4, 320, 320, 3], output Shape [4, 84, 2100] parameters it crashed. Following is the error in logcat:

FATAL EXCEPTION: pool-2-thread-1 Process: com.surendramaran.yolov8tflite, PID: 9281 java.lang.IllegalArgumentException: Cannot copy to a TensorFlowLite tensor (inputs_0) with 4915200 bytes from a Java Buffer with 1228800 bytes. at org.tensorflow.lite.TensorImpl.throwIfSrcShapeIsIncompatible(TensorImpl.java:426) at org.tensorflow.lite.TensorImpl.setTo(TensorImpl.java:150) at org.tensorflow.lite.NativeInterpreterWrapper.run(NativeInterpreterWrapper.java:260) at org.tensorflow.lite.InterpreterImpl.runForMultipleInputsOutputs(InterpreterImpl.java:101) at org.tensorflow.lite.Interpreter.runForMultipleInputsOutputs(Interpreter.java:95) at org.tensorflow.lite.InterpreterImpl.run(InterpreterImpl.java:94) at org.tensorflow.lite.Interpreter.run(Interpreter.java:95) at com.surendramaran.yolov8tflite.Detector.detect(Detector.kt:135) at com.surendramaran.yolov8tflite.MainActivity.bindCameraUseCases$lambda$7(MainActivity.kt:130) at com.surendramaran.yolov8tflite.MainActivity.$r8$lambda$SwMDthyT4p6a561NxBZ96bJyetY(Unknown Source:0) at com.surendramaran.yolov8tflite.MainActivity$$ExternalSyntheticLambda6.analyze(D8$$SyntheticClass:0) at androidx.camera.core.ImageAnalysis.lambda$setAnalyzer$2(ImageAnalysis.java:527) at androidx.camera.core.ImageAnalysis$$ExternalSyntheticLambda2.analyze(D8$$SyntheticClass:0) at androidx.camera.core.ImageAnalysisAbstractAnalyzer.lambda$analyzeImage$0$androidx-camera-core-ImageAnalysisAbstractAnalyzer(ImageAnalysisAbstractAnalyzer.java:286) at androidx.camera.core.ImageAnalysisAbstractAnalyzer$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:923)

I understand that with the second file, the batch size has changed from 1 -> 4. How should I edit the code to be able to run with the second file? First file parameters: image.png (view on web) https://github.com/user-attachments/assets/4177359d-9678-40ab-8d73-c47d21dbea9a

2nd file parameters: image.png (view on web) https://github.com/user-attachments/assets/7053f2f0-47fc-4d56-addd-a3fec53473fa

— Reply to this email directly, view it on GitHub https://github.com/surendramaran/YOLOv8-TfLite-Object-Detector/issues/22, or unsubscribe https://github.com/notifications/unsubscribe-auth/APXHH4POTFBXK2AEOMHSLDDZP47N7AVCNFSM6AAAAABL74N7GOVHI2DSMVQWIX3LMV43ASLTON2WKOZSGQ2DQMBUGQ2DANI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

boyapdao commented 1 month ago

Yes, it's yolov8. first file: 2806_int8.tflite.zip second file: yolov8n_float16_batch_1.tflite.zip label: labels.txt

surendramaran commented 1 month ago

I haven't seen this type before, can you tell me how you got this model?

On Mon, 5 Aug 2024 at 15:33, boyapdao @.***> wrote:

Yes, it's yolov8. first file: 2806_int8.tflite.zip https://github.com/user-attachments/files/16494198/2806_int8.tflite.zip second file: yolov8n_float16_batch_1.tflite.zip https://github.com/user-attachments/files/16494206/yolov8n_float16_batch_1.tflite.zip

— Reply to this email directly, view it on GitHub https://github.com/surendramaran/YOLOv8-TfLite-Object-Detector/issues/22#issuecomment-2268688822, or unsubscribe https://github.com/notifications/unsubscribe-auth/APXHH4MV2P4QBP7E57AOIX3ZP5EY5AVCNFSM6AAAAABL74N7GOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDENRYGY4DQOBSGI . You are receiving this because you commented.Message ID: @.*** com>

boyapdao commented 1 month ago

Here is a model a team member sent me, he explained that changing the batch size from 1 -> 4 means it will analyze the object based on 4 images at a time instead of just 1. image. The goal is to test the time delay of the model with batch size 4 compared to batch size 1.