asus4 / tf-lite-unity-sample

TensorFlow Lite Samples on Unity
834 stars 249 forks source link

Running Android GPUDelegate in BG thread fails on API Level 31 #263

Closed SusheelNath closed 10 months ago

SusheelNath commented 1 year ago

Environment:

Describe the bug

To Reproduce Steps to reproduce the behavior:

  1. Clone tf-lite-unity-sample master
  2. Download Unity 2021.3.13f1
  3. Upgrade project and set RunBackground On.
  4. Set API Level 31 for Android
  5. Deploy .apk and test on Android mobile phone

Expected behavior TensorFlowLite BlazePose should work, with RunBackground toggle on, implying smoother experience.

Screenshots (https://user-images.githubusercontent.com/33517815/203538438-22984ef1-a0f9-4987-a3c2-56a8c93a9a1a.jpeg)

asus4 commented 1 year ago

Hi @SusheelNath, could you look at #269? Changing the API level from 31 to 30 might fix this issue.

asus4 commented 1 year ago

I noticed the new app should be API level 31 or higher to submit an app to the Google Play Store.

Adding the following code to the AndroidManifect.xml might fix the issue

<uses-native-library android:name="libOpenCL.so" android:required="false" />
<uses-native-library android:name="libOpenCL-pixel.so" android:required="false" />

References: https://stackoverflow.com/questions/71485371/android-12api-level-31-tflitegpudelegate-invoke-gpudelegate-must-run-on-the https://developer.android.com/about/versions/12/behavior-changes-12#uses-native-library

SusheelNath commented 1 year ago

Hi Koki,

Thank you for your response on this issue. I will try this out and keep you posted.

Warm regards, Susheel

On Mon, Jan 9, 2023 at 10:55 AM Koki Ibukuro @.***> wrote:

I noticed the new app should be API level 31 or higher https://developer.android.com/google/play/requirements/target-sdk to submit an app to the Google Play Store.

Adding the following code to the AndroidManifect.xml might fix the issue

References:

https://stackoverflow.com/questions/71485371/android-12api-level-31-tflitegpudelegate-invoke-gpudelegate-must-run-on-the

https://developer.android.com/about/versions/12/behavior-changes-12#uses-native-library

— Reply to this email directly, view it on GitHub https://github.com/asus4/tf-lite-unity-sample/issues/263#issuecomment-1375428775, or unsubscribe https://github.com/notifications/unsubscribe-auth/AH7XB5ZTFF6KO7JWHFCGW3DWRPVCHANCNFSM6AAAAAASI32QFU . You are receiving this because you were mentioned.Message ID: @.***>

SusheelNath commented 1 year ago

Hi Koki,

We tried with adding the lines to the AndroidManifest.xml file. The problem still persists, RunBackground = On with API Level 31 - > Not working.

With context as above, this is for Android and with RunBackground = Off with API Level 31 -> it works. On iOS, everything works as intended.

karahanoguzhan commented 1 year ago

Hi @SusheelNath, did you solve the issue?

SusheelNath commented 1 year ago

Hi @karahanoguzhan I wasn't able to unfortunately.

@asus4 Is there any other pathway that I can try out to solving this?

asus4 commented 1 year ago

@SusheelNath Sorry for the wait, I'll look into it when I find the time.

asus4 commented 1 year ago

Also, I would appreciate any detailed adb log including the Android model name, version etc to determine the issue.

asus4 commented 1 year ago

Hi @SusheelNath @karahanoguzhan, I looked into this today.

On my Androids - tested with Google Pixel 6 and Sony Xperia XZ2, adding the <uses-native-library> tags in AndroidManifest.xml seems to fix the issues.

Steps:

In Unity 2021 LTS, the legacy way of creating a file at Plugins/Android/AndroidManifest.xml seems outdated. Here are detailed steps to build it:

  1. In the Build Settings window, enable the "Export Project" option and export an Android project instead of building an apk directly from Unity
  2. Open unityLibrary/manifests/AndroidManifest.xml and add the following lines:
    <uses-native-library android:name="libOpenCL.so" android:required="false" />
    <uses-native-library android:name="libOpenCL-car.so" android:required="false" />
    <uses-native-library android:name="libOpenCL-pixel.so" android:required="false" />

    androidmanifest

  3. Maybe you need to upgrade the Gradle version to the latest 7.2.1 as I got an error " tag is unknown."
  4. Remove the line 'android.enableR8' from the file Gradle Scripts/gradle.properties. android-enableR8

Then, I succeeded in running BlazePose with the background option.


I suppose the reason for this error is the fallback system in the Android GpuDelegate. The default priority of the GpuDelegate is OpenCL, then fallback to OpenGLES if OpenCL is not supported on your device. And as of Android API level 31, you have to add the uses-native-library tag to access the OpenCL library. So the GpuDelegate fallbacks to the OpenGLES, and it throws the error as the OpenGLES requires to run in the same thread.

Could you help us if the above steps also fix the issues on your devices? If it solves the error, we can probably make these changes automatically by implementing IPostGenerateGradleAndroidProject.

SusheelNath commented 1 year ago

Hi @asus4 @karahanoguzhan,

The above fix works. Tried it out on Samsung Galaxy S10FE & Samsung Galaxy S21. Thank you for your efforts in this.

If you could kindly reflect this fix on your repository as well as your UPM packages? It would be a great help not only for our side but also to any other developer who comes across this issue.

Appreciate it and once again, thank you Koki.

Warm regards, Susheel

SusheelNath commented 1 year ago

Hi @asus4, I have given the performance a try with the fix.

Issue: For API Level 31 and even lesser, the RunBackgroundOn performs worse than RunBackgroundOff.

Findings: This is the same findings with your base repository as well.

Background: Earlier in your repository (potentially before the Tensorflow package upgrades?), it used to be the other way around, where RunBackgroundOn used to perform much better than RunBackgroundOff.

Questions: 1) Is there any changes to be done to your code base for this? 2) Was this intended? 3) Is there any steps that can be done from our side to make the performance better? (with any, RunBackgroundOn/Off)

asus4 commented 1 year ago

@SusheelNath Is it only happening on Android? If so, it might be related to #243.

There are no clues right now. If we could determine from which commit this slowdown issue happens, we would have a better indication of the cause.

Anyway, can you open a new issue for the performance issue? It looks like a different issue from the API Level.

SusheelNath commented 1 year ago

@asus4 Hi Koki, I will do so. I will create a new thread to mark the performance issue.

I will let you close this issue yourself so that you can keep track of the fix to be implemented in your repository and the packages.

Appreciate your help on this.

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

asus4 commented 1 year ago

I have found a simpler way worth testing it.

https://lightship.dev/docs/ardk/ardk_fundamentals/building_android.html

stale[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Im-Zapp commented 11 months ago

Hi @asus4 , i was wondering if there was a simpler "within unity" solution available for it as I'm facing the same issue after setting target api to 33 as required by Google Play guidelines.

Issue: Works fine in editor but fails to detect body landmarks on Android

asus4 commented 11 months ago

Right, would be nice if we could automate the above modification on the Android project in the post-process build.

Im-Zapp commented 11 months ago

That's great, eagerly waiting for it :)

asus4 commented 10 months ago

I simplified that process at the PR #316. You only need to do is adding the following tags in your custom AndroidManifest.xml 🎉

<uses-native-library android:name="libOpenCL.so" android:required="false"/>
<uses-native-library android:name="libOpenCL-car.so" android:required="false"/>
<uses-native-library android:name="libOpenCL-pixel.so" android:required="false"/>
Im-Zapp commented 9 months ago

Hi @asus4 Was able to get it to work. Thanks a lot :)