How to specify Firebase Model Inputs for a TensorFlow Lite model to make predictions for item recommendations in Android

Viewed 115

I am trying to make use of my model in Android by using Firebase Machine Learning Kit.

I tried to specify different inputs but it did not work. I need to find a way of how to make predictions in Android using a TensorFlow model fetched form the Firebase.

At the moment I can only input one value in Android inputs. How to specify 2 inputs in Android so that one input is for user id and another one is for movie id?

private void setupModel() {
    FirebaseCustomRemoteModel remoteModel = new FirebaseCustomRemoteModel.Builder("Recommender-Model").build();
    FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder()
            .requireWifi()
            .build();
    FirebaseModelManager.getInstance().download(remoteModel, conditions)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getApplicationContext(), "Download failure!", Toast.LENGTH_SHORT).show();
                    }
                }
            });

    FirebaseModelInputOutputOptions inputOutputOptions = null;
    try {
        inputOutputOptions = new FirebaseModelInputOutputOptions.Builder()
                .setInputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 1})
                .setOutputFormat(0, FirebaseModelDataType.FLOAT32, new int[]{1, 1})
                .build();
    } catch (FirebaseMLException e) {
        e.printStackTrace();
    }

    float[][] input = new float[1][1];
    input[0][0] = 1f;

    FirebaseModelInputs inputs = null;
    try {
        inputs = new FirebaseModelInputs.Builder()
                .add(input)
                .build();
    } catch (FirebaseMLException e) {
        e.printStackTrace();
    }


    FirebaseModelInterpreterOptions interpreterOptions =
            new FirebaseModelInterpreterOptions.Builder(remoteModel).build();

    try {
        FirebaseModelInterpreter.getInstance(interpreterOptions).run(inputs, inputOutputOptions)
                .addOnSuccessListener(
                        new OnSuccessListener<FirebaseModelOutputs>() {
                            @Override
                            public void onSuccess(FirebaseModelOutputs result) {
                                float[][] predictedRating = result.getOutput(0);
                                Toast.makeText(getApplicationContext(), "Result Fetched", Toast.LENGTH_SHORT).show();
                            }
                        })
                .addOnFailureListener(
                        new OnFailureListener() {
                            @Override
                            public void onFailure(@NonNull Exception e) {
                                Toast.makeText(getApplicationContext(), "Failure", Toast.LENGTH_SHORT).show();
                            }
                        });
    } catch (FirebaseMLException e) {
        e.printStackTrace();
    }
}

Predict function in TensorFlow looks like this:

model = Model(inputs = [u, m], outputs = x)
model.predict([test_user, test_movie], batch_size = 500)
1 Answers

You should be able to use:

    FirebaseModelInputs inputs = new FirebaseModelInputs.Builder()
        .add(u)  // add() as many input arrays as your model requires
        .add(m)
        .build();
Related