shubham0204 / FaceRecognition_With_FaceNet_Android

Face Recognition using the FaceNet model and MLKit on Android.
https://towardsdatascience.com/using-facenet-for-on-device-face-recognition-with-android-f84e36e19761
Apache License 2.0
252 stars 88 forks source link

Works incorrectly in samsung A21s #25

Open emreakcan opened 2 years ago

emreakcan commented 2 years ago

App working correctly on all devices I have, but somehow on Samsung A21s, it is producing incorrect results. It is nearly producing same output for all faces probability between .2 .3, for l2, cosine is also the same.

I thought maybe camera is the problem, so I embedded the pictures inside drawable and matched, but still it is producing nearly the same results for everything.

I noticed FloatArray returned from getCroppedFaceEmbedding producing different results for different phones.

My xiaomi mi9 works alright.

Do you have any ideas?

emreakcan commented 2 years ago

if I run the L2Norm with the FloatArrays that I get from the Xiaomi, Samsung can do the calculation correctly.

shubham0204 commented 2 years ago

This is because of different device configurations. Probably switching off the GpuDelegate and XNNPack might help. In the FaceNetModel.kt class, you'll see these lines,

init {
    // Initialize TFLiteInterpreter
    val interpreterOptions = Interpreter.Options().apply {
        // Add the GPU Delegate if supported.
        // See -> https://www.tensorflow.org/lite/performance/gpu#android
        if ( CompatibilityList().isDelegateSupportedOnThisDevice ) {
            addDelegate( GpuDelegate( CompatibilityList().bestOptionsForThisDevice ))
        }
        else {
            // Number of threads for computation
            setNumThreads( 4 )
        }
        setUseXNNPACK( true )
    }
    interpreter = Interpreter(FileUtil.loadMappedFile(context, model.assetsFilename ) , interpreterOptions )
    Logger.log("Using ${model.name} model.")
}

Replace these lines with,

init {
    // Initialize TFLiteInterpreter
    val interpreterOptions = Interpreter.Options().apply {    
         setNumThreads( 4 )
    }
    interpreter = Interpreter(FileUtil.loadMappedFile(context, model.assetsFilename ) , interpreterOptions )
    Logger.log("Using ${model.name} model.")
}
shubham0204 commented 2 years ago

@emreakcan Could you resolve the error by removing GpuDelegate?

DineshIT commented 2 years ago

HI @shubham0204,

I'm also facing the same issue with face recognition. Initially, it generated multiple results for the same face. But now after removing the GPUDelegate and XNNPack, for all the faces it is giving me the same result as unknown.

shubham0204 commented 2 years ago

@DineshIT can you send me some more details of the Samsung A21 device on which you're testing the app? I need these details specifically:

  1. Android OS Version
  2. GPU Renderer
  3. Supported ABIs
  4. CPU architecture

You can get these details by installing the Device Info app on the device.

DineshIT commented 2 years ago

Hi Shubham,

Please find the details below.

  1. Android OS Version - Android 11 - R [API Level 30]
  2. GPU Renderer - Mali-G52
  3. Supported ABIs - arm64-v8a, armeabi-v7a, armeabi
  4. CPU architecture - 8 * 2.00 Ghz

If you need any other details please let me know.

On Mon, Apr 25, 2022 at 7:52 PM Shubham Panchal @.***> wrote:

@DineshIT https://github.com/DineshIT can you send me some more details of the Samsung A21 device on which you're testing the app? I need these details specifically:

  1. Android OS Version
  2. GPU Renderer
  3. Supported ABIs
  4. CPU architecture

You can get these details by installing the Device Info https://play.google.com/store/apps/details?id=com.ytheekshana.deviceinfo app on the device.

— Reply to this email directly, view it on GitHub https://github.com/shubham0204/FaceRecognition_With_FaceNet_Android/issues/25#issuecomment-1108643191, or unsubscribe https://github.com/notifications/unsubscribe-auth/AC7WWBMXE6FUFWM34CDSPTDVG2TBVANCNFSM5JHXRYAQ . You are receiving this because you were mentioned.Message ID: <shubham0204/FaceRecognition_With_FaceNet_Android/issues/25/1108643191@ github.com>

-- -With great regards Dineshkumar

DineshIT commented 2 years ago

Hi @shubham0204

I have worked on this issue and fixed it by adding the properties in the interpreter class object.

Please check my commit in the repo and merge it to handle this issue.

Hi @emreakcan

You can make the mentioned changes in your FaceNetModel Class and verify whether this issue got fixed at your end. If you got success please share with us.

shubham0204 commented 2 years ago

@DineshIT Can you open a PR in this repo, so that I can review the changes?

DineshIT commented 2 years ago

PR has been created