quickstart-android icon indicating copy to clipboard operation
quickstart-android copied to clipboard

MLKit sample: classification live preview example not working (missing model source registration)

Open jinqian opened this issue 8 years ago • 11 comments

Environment

  • Android device: Samsung S8
  • Android OS version: Nougat (7.0)
  • Firebase/Play Services SDK version: 15.0.0

Problem

Cloud & local model source registrations are missing in the ML Kit example according to the documentation.

But the classification is not working even after fixing the registration issue.

Steps to reproduce

What I think was the prerequisite to test the classification sample:

  1. Create a Firebase Project
  2. Download google-services.json to the project
  3. Create a custom model with name mobilenet_v1 and upload the mobilenet_quant_v1_224.tflite

If we launch the project with only this, the live preview for classification is not working. To know why, just print the stacks of the exception caught in createCameraSource(String model) method inside LivePreviewActivity .

Observed Results:

No inference is executed, no result is displayed.

Expected Results:

Inference should be executed with the image feeded by camera source.

Possible Fix

To fix the registration issue is pretty simple (see code below), but I still can't make the classification of custom model work :(

private static final String CLOUD_MODEL_NAME = "mobilenet_v1";
private static final String LOCAL_MODEL_NAME = "mobilenet_quant_v1_224";
private static final String LOCAL_MODEL_PATH = "mobilenet_quant_v1_224.tflite";

CustomImageClassifier(Activity activity) throws FirebaseMLException {
        // register cloud model source
        FirebaseCloudModelSource cloudSource = new FirebaseCloudModelSource.Builder(CLOUD_MODEL_NAME)
            .enableModelUpdates(true)
            .build();
        FirebaseModelManager.getInstance().registerCloudModelSource(cloudSource);

        // register local model source
        FirebaseLocalModelSource localSource = new FirebaseLocalModelSource.Builder(LOCAL_MODEL_NAME)
            .setAssetFilePath(LOCAL_MODEL_PATH)
            .build();
        FirebaseModelManager.getInstance().registerLocalModelSource(localSource);
        FirebaseModelOptions modelOptions =
            new FirebaseModelOptions.Builder()
                .setCloudModelName(CLOUD_MODEL_NAME)
                .setLocalModelName(LOCAL_MODEL_NAME)
                .build();

        interpreter = FirebaseModelInterpreter.getInstance(modelOptions);
        labelList = loadLabelList(activity);
        Log.d(TAG, "Created a Custom Image Classifier.");
        int[] inputDims = {DIM_BATCH_SIZE, DIM_IMG_SIZE_X, DIM_IMG_SIZE_Y, DIM_PIXEL_SIZE};
        int[] outputDims = {1, labelList.size()};
        dataOptions =
            new FirebaseModelInputOutputOptions.Builder()
                .setInputFormat(0, FirebaseModelDataType.BYTE, inputDims)
                .setOutputFormat(0, FirebaseModelDataType.BYTE, outputDims)
                .build();
        Log.d(TAG, "Configured input & output data for the custom image classifier.");
    }

jinqian avatar May 22 '18 22:05 jinqian

@jinqian, when you said " To know why, just print the stacks of the exception caught in createCameraSource(String model) method inside LivePreviewActivity ." Would you mind sharing the exception call stack? Ideally, a full bug report (e.g. upload to Google Drive and share by link). Thanks!

isaisachen avatar May 22 '18 23:05 isaisachen

"Cloud & local model source registrations are missing in the ML Kit example according to the documentation."
@jinqian I do see the registration of model sources in mentioned. Or am I missing something?

psanketi avatar May 22 '18 23:05 psanketi

@isaisachen here is the stack:

05-23 14:41:24.638 21700-21700/com.google.firebase.samples.apps.mlkit E/LivePreviewActivity: can not create camera source: Classification
05-23 14:41:24.638 21700-21700/com.google.firebase.samples.apps.mlkit W/System.err: com.google.firebase.ml.common.FirebaseMLException: Local model source is not registered: mobilenet_quant_v1_224.tflite
        at com.google.firebase.ml.custom.FirebaseModelInterpreter.<init>(Unknown Source)
        at com.google.firebase.ml.custom.FirebaseModelInterpreter.zza(Unknown Source)
        at com.google.firebase.ml.custom.FirebaseModelInterpreter.getInstance(Unknown Source)
        at com.google.firebase.samples.apps.mlkit.custommodel.CustomImageClassifier.<init>(CustomImageClassifier.java:101)
        at com.google.firebase.samples.apps.mlkit.custommodel.CustomImageClassifierProcessor.<init>(CustomImageClassifierProcessor.java:37)
        at com.google.firebase.samples.apps.mlkit.LivePreviewActivity.createCameraSource(LivePreviewActivity.java:150)
        at com.google.firebase.samples.apps.mlkit.LivePreviewActivity.onItemSelected(LivePreviewActivity.java:114)
        at android.widget.AdapterView.fireOnSelected(AdapterView.java:1315)
        at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:1304)
        at android.widget.AdapterView.-wrap1(AdapterView.java)
05-23 14:41:24.639 21700-21700/com.google.firebase.samples.apps.mlkit W/System.err:     at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1271)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

jinqian avatar May 23 '18 12:05 jinqian

@psanketi I mean the model registration is missing in the CustomImageClassifier. What I understand from the documentation is that you need to register your cloud model, and local model (if you bundled the tflite file).

jinqian avatar May 23 '18 12:05 jinqian

@jinqian Thanks for reporting the Quick Start code issue. You are right, @gkaldev will fix it.

As for the exception, seems it's not aligning with the code you pasted.

There are two things:

  1. model name
  2. asset file name

(To clarify the usage a little bit: When you call "new FirebaseLocalModelSource.Builder(LOCAL_MODEL_NAME) ", it's model "name" and later setLocalModelName needs to use the same name. asset file name is only used in setAssetFilePath. )

The error says: 05-23 14:41:24.638 21700-21700/com.google.firebase.samples.apps.mlkit W/System.err: com.google.firebase.ml.common.FirebaseMLException: Local model source is not registered: mobilenet_quant_v1_224.tflite

The code you pasted looks correct to me. But the exception you pasted was thrown by below code and looks as if you were passing "mobilenet_quant_v1_224.tflite" in FirebaseModelOptions#setLocalModelName:

throw new FirebaseMLException( "Local model source is not registered: " + options.getLocalModelName(), FirebaseMLException.INVALID_ARGUMENT);

Could you make sure you tested with the code you pasted? Could you rename the LOCAL_MODEL_NAME and test again? Paste me the call stack if still not working. Thanks.

isaisachen avatar May 24 '18 00:05 isaisachen

Sorry about the misunderstanding, I didn't make myself clear enough: the code I pasted is a possible fix, not the one which produces exception.

The exception can be reproduced by just launching the example directly, in order to obtain the stack, add e.printStackTrace(); here: https://github.com/firebase/quickstart-android/blob/8baec79efbbabc7f229dedb52ddc788cab2517af/mlkit/app/src/main/java/com/google/firebase/samples/apps/mlkit/LivePreviewActivity.java#L172

jinqian avatar May 24 '18 07:05 jinqian

Ah, I see. Thanks for clarification @jinqian

But you mentioned "But the classification is not working even after fixing the registration issue." So it still doesn't work for you with your posted fix? If so, could you share the entire bug report w/ your model registration fix?

isaisachen avatar May 24 '18 16:05 isaisachen

Yes! I don't have any explicit error message or exception stack trace for that, what I can tell is images are fed to FirebaseModelInterpreter but no result seems to be produced no matter what object I point my camera to. I would like to know if it's because my configurations has a problem or something else underlying is wrong...

Thank you for your help! @isaisachen

jinqian avatar May 24 '18 16:05 jinqian

@jinqian While you don't see any other errors in your adb log, if you could share the bug report, I could possibly still spot other clues why it's not working for you.

But I took a quick look, another bug in the quick start is missing aaptOptions. @gkaldev Please also help fix this! Thank you!

// Do NOT compress tflite model files (need to call out to developers!)
aaptOptions {
    noCompress "tflite"
}

See also documentation here: https://firebase.google.com/docs/ml-kit/android/use-custom-models?authuser=0#make_models_available_locally

isaisachen avatar May 25 '18 18:05 isaisachen

@jinqian, commit da5e1c8 should make custom model classification work in the sample. Could you try that and let me know if it works for you? Then we can try to figure out if there is a separate issue causing your code to fail.

gkaldev avatar May 31 '18 19:05 gkaldev

Closing this issue due to inactivity. I believe the main issue has been resolved.

gkaldev avatar Aug 06 '18 16:08 gkaldev