Error occurred when initializing ObjectDetector: Mobile SSD models are expected to have exactly 4 outputs, found 3
Hi，@nerdyAlezF @srjoglekar246 @lintian06, could you please  take a look at this issue? Hi @libofei2004, your link isn't working ... is there any reason you are using an older version? Can you please try with nightly and let us know the results. Thanks. @pkgoogle Thank you, I have read issue#47595 and try to use the code from the link ,it can support me to customize inputs and outputs, the right link is  https://github.com/tensorflow/examples/blob/cbe96424b6b930fd74dbd4ef2b1e826a031a3554/lite/examples/object_detection/android/lib_interpreter/src/main/java/org/tensorflow/lite/examples/detection/tflite/TFLiteObjectDetectionAPIModel.java#L56
The lastest version I also tried, but met the same error.
https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android
I think my exported tflite model may be not suit for the tensorflow example. The model readme.md is as follows:
![image](https://github.com/tensorflow/tensorflow/assets/10001548/117370d9-bd29-4a51-8f91-ca81a7855100)
As mentioned by readme, the outputs num is indeed 3, but tensorflow example needs 4, how to solve it?@pkgoogle Hi @libofei2004, there are multiple things that need updating with this project so I will fix this, then we can address your issue. Thanks. Thank you @pkgoogle ,I will wait. Hi @libofei2004, I was able to compile a newer version of the project by updating the dependencies:

object_detection/android/build.gradle.kts:
```kts
dependencies {
        classpath 'com.android.tools.build:gradle:8.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'androidx.navigation:navigation-safe-args-gradle-plugin:2.5.0'
        classpath 'de.undercouch:gradle-download-task:4.1.2'


        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
```

additionally a namespace needs to be specified, in object_detection/android/app/build.gradle.kts:
```kts
...
android {
    ...
    namespace "org.tensorflow.lite.examples.objectdetection"
    ...
}
...
```

Before I commit this change, I will need to see if there are any other changes needed to support your workflow. It seems the issue is specific to your model ... can you share/upload your model or explain how I can recreate it?

Thanks. @pkgoogle , can you visit my model https://github.com/libofei2004/custom-vision-model, it includes tflite model, tflite float16 model, saved model and  tflite float16 with metadata. Hi @libofei2004, this example uses quantized models which is described in the README: https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android . There's assumptions built it which make it not work with non-quantized models... are any of your models quantized? if not, can you try with a quantized model? Hi @pkgoogle , I have run the example yet before,I can run it successfully by using the models mentioned in the README,but I can't run my model successfully.I also tied to quantize my model yet.  I think the problem is my model's  output number is not fit for the example . @libofei2004, can you please share the exact model you "can't run successfully"? I tried with model.tflite and when I run it effectively states the model isn't quantized, do you have a quantized model that you are attempting to run it on? @pkgoogle ,My models are all in  https://github.com/tensorflow/examples/tree/master/lite/examples/object_detection/android, they can run successfully in the https://github.com/Azure-Samples/cognitive-services-android-customvision-sample but not in tensorflow sample. I have tried to quantized the model by the script https://github.com/libofei2004/custom-vision-model/blob/main/quantization.py but failed,I don't exactly konw how to make a right quantization, maybe the model format exported from the custom vision platform is not standard. Hi @libofei2004, I reread your original description.. so the current problem is that your models have `kTfLiteFloat32` input tensors i.e. non-quantized, we can get around this with writing the metadata which is what you described here:

> I use metadata_writers py script to add metadata to tflite model and load model in the object_detection example

can you please elaborate on how you wrote the meta data with the script? i.e. did you import the tfllite-support library and ran a custom script or did you run a script we already provided? If so, what arguments did you pass? Thanks for your help. @pkgoogle ，My metadata script is https://github.com/libofei2004/custom-vision-model/blob/main/meta.py. Thank you. Hi @libofei2004, please review this [gist](https://colab.sandbox.google.com/gist/pkgoogle/f1fc8c1eed6bcfec49fd722c5e35112c/63115.ipynb). I uploaded all the models that you provided and an original mobilenetv1 model. As you can see your models have 3 outputs instead of the expected 4. This is prior to the metadata issue so the problem is in order to "drop" in this model you have to conform to the expected API: https://www.tensorflow.org/lite/api_docs/java/org/tensorflow/lite/task/vision/detector/ObjectDetector

Please review the API documentation. Of course you can use a custom ObjectDetector but you will have to code a bit to make it work with our example. I don't know the details of your model but it might be worth it to see if you can fit the outputs (by modifying the original model) to the expected API (perhaps if an output is unnecessary for your use case you can modify your model to output a dummy value).

As I currently see it, it is working as expected. Let me know if you have any additional questions.  @pkgoogle ,I can't visit the gist. I think either modify the tensorflow sample source code to fit the model or modify the model structure to fit the source code. Both are not easy. From the error message, it seems that the error lies in the JNI of the code, so changing the code might not be straightforward. My apologies, updated the wrong link, here's the right one (also edited on the original): [gist](https://colab.sandbox.google.com/gist/pkgoogle/f1fc8c1eed6bcfec49fd722c5e35112c/63115.ipynb). @libofei2004  Your understanding is correct.... the example code is designed to get you started, unfortunately we can not maintain example code for all potential use cases out there. Since your own model doesn't seem to fit well with the API, perhaps you can try the Interpreter API? which is more meant for a lower level of abstraction (and thus can be more customized to your use case):

https://www.tensorflow.org/lite/api_docs/java/org/tensorflow/lite/InterpreterApi @pkgoogle，thank you, but the  Interpreter API is hard to use, is there any examples with the API ? Hi @libofei2004, Does this help you? https://www.tensorflow.org/lite/inference_with_metadata/lite_support, the support library will give you higher level tools for input/output processing. @pkgoogle , with the error message: at org.tensorflow.lite.task.vision.detector.ObjectDetector.initJniWithByteBuffer(Native Method)..., I think the error  occurred at the low level （C++），Can I use Java API to modify the number of output parameters? Hi @libofei2004, you would have to modify the API which is more or less located here: https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/java/src/java/org/tensorflow/lite/task/vision/detector/ObjectDetector.java This would be non-trivial and unless you make a feature request in that repo that improves the optionality of the outputs for the API you would have to maintain that change itself. So... there probably is a way, if you dig down far enough. @pkgoogle ， I don't think I can get the sample running through modifying the ObjectDetector.java, the error must be in "modelBuffer"  and the real code parses it is in C++. Hi @libofei2004, apologies, I am less familiar with that repo overall, you are correct, I guess this was covered under "if you dig down far enough" but yeah I did a little more and it seems like here is probably where you would have to change: https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/java/src/native/task/vision/detector/object_detector_jni.cc#L115 This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63115">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/63115">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

No

### Source

source

### TensorFlow version

tf2.8

### Custom code

Yes

### OS platform and distribution

android (MIUI 13)

### Mobile device

MI 10 pro

### Python version

_No response_

### Bazel version

_No response_

### GCC/compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current behavior?

I used custom vision(https://www.customvision.ai/) to train my object detction model, and exported an tflite model , but my exported tensorflow tflite model cannot loaded rightly in tensorflow example（https://github.com/tensorflow/examples/tree/cbe96424b6b930fd74dbd4ef2b1e826a031a3554/lite/examples/object_detection）. It seems lack of metadata. If I use metadata_writers py script to add metadata to tflite model and load model in the object_detection example, an error like "Mobile SSD models are expected to have exactly 4 outputs, found 3" will occured.
I have read https://github.com/tensorflow/tensorflow/issues/47595, but still not find how to solve my problem, it seems customize inputs and outputs for [TFLiteObjectDetectionAPIModel](https://github.com/tensorflow/examples/blob/cbe96424b6b930fd74dbd4ef2b1e826a031a3554/lite/examples/object_detection/android/lib_interpreter/src/main/java/org/tensorflow/lite/examples/detection/tflite/TFLiteObjectDetectionAPIModel.java#L56) cannot solve the problem.

### Standalone code to reproduce the issue

```shell
I just run this code:
https://github.com/tensorflow/examples/tree/cbe96424b6b930fd74dbd4ef2b1e826a031a3554/lite/examples/object_detection
```


### Relevant log output

```shell
FATAL EXCEPTION: main
                                                                                                    Process: org.tensorflow.lite.examples.objectdetection, PID: 4946
                                                                                                    java.lang.IllegalStateException: Error getting native address of native library: task_vision_jni
                                                                                                    	at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromLibrary(TaskJniUtils.java:95)
                                                                                                    	at org.tensorflow.lite.task.vision.detector.ObjectDetector.createFromBufferAndOptions(ObjectDetector.java:219)
                                                                                                    	at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.<init>(TFLiteObjectDetectionAPIModel.java:87)
                                                                                                    	at org.tensorflow.lite.examples.detection.tflite.TFLiteObjectDetectionAPIModel.create(TFLiteObjectDetectionAPIModel.java:81)
                                                                                                    	at org.tensorflow.lite.examples.detection.DetectorActivity.onPreviewSizeChosen(DetectorActivity.java:103)
                                                                                                    	at org.tensorflow.lite.examples.detection.CameraActivity$7.onPreviewSizeChosen(CameraActivity.java:448)
                                                                                                    	at org.tensorflow.lite.examples.detection.CameraConnectionFragment.setUpCameraOutputs(CameraConnectionFragment.java:360)
                                                                                                    	at org.tensorflow.lite.examples.detection.CameraConnectionFragment.openCamera(CameraConnectionFragment.java:365)
                                                                                                    	at org.tensorflow.lite.examples.detection.CameraConnectionFragment.-$$Nest$mopenCamera(Unknown Source:0)
                                                                                                    	at org.tensorflow.lite.examples.detection.CameraConnectionFragment$3.onSurfaceTextureAvailable(CameraConnectionFragment.java:174)
                                                                                                    	at android.view.TextureView.getTextureLayer(TextureView.java:410)
                                                                                                    	at android.view.TextureView.draw(TextureView.java:353)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21885)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21876)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21876)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.draw(View.java:23021)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21885)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at androidx.coordinatorlayout.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1246)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.draw(View.java:23021)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21885)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21876)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21876)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21876)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21876)
                                                                                                    	at android.view.View.draw(View.java:22743)
                                                                                                    	at android.view.ViewGroup.drawChild(ViewGroup.java:4542)
                                                                                                    	at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4300)
                                                                                                    	at android.view.View.draw(View.java:23021)
                                                                                                    	at com.android.internal.policy.DecorView.draw(DecorView.java:891)
                                                                                                    	at android.view.View.updateDisplayListIfDirty(View.java:21885)
                                                                                                    	at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:534)
                                                                                                    	at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:542)
2024-03-04 23:22:36.073  4946-4946  AndroidRuntime          org...lite.examples.objectdetection  E  	at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:625)
                                                                                                    	at android.view.ViewRootImpl.draw(ViewRootImpl.java:4657)
                                                                                                    	at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:4375)
                                                                                                    	at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:3486)
                                                                                                    	at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2277)
                                                                                                    	at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:9037)
                                                                                                    	at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1142)
                                                                                                    	at android.view.Choreographer.doCallbacks(Choreographer.java:946)
                                                                                                    	at android.view.Choreographer.doFrame(Choreographer.java:875)
                                                                                                    	at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1127)
                                                                                                    	at android.os.Handler.handleCallback(Handler.java:938)
                                                                                                    	at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                    	at android.os.Looper.loopOnce(Looper.java:210)
                                                                                                    	at android.os.Looper.loop(Looper.java:299)
                                                                                                    	at android.app.ActivityThread.main(ActivityThread.java:8293)
                                                                                                    	at java.lang.reflect.Method.invoke(Native Method)
                                                                                                    	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:556)
                                                                                                    	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1045)
                                                                                                    Caused by: java.lang.IllegalArgumentException: Error occurred when initializing ObjectDetector: Mobile SSD models are expected to have exactly 4 outputs, found 3
                                                                                                    	at org.tensorflow.lite.task.vision.detector.ObjectDetector.initJniWithByteBuffer(Native Method)
                                                                                                    	at org.tensorflow.lite.task.vision.detector.ObjectDetector.access$100(ObjectDetector.java:88)
                                                                                                    	at org.tensorflow.lite.task.vision.detector.ObjectDetector$3.createHandle(ObjectDetector.java:223)
                                                                                                    	at org.tensorflow.lite.task.core.TaskJniUtils.createHandleFromLibrary(TaskJniUtils.java:91)
                                                                                                    	... 73 more
```
