java.lang.IllegalArgumentException: Cannot find an axis to label. A valid axis to label should have size larger than 1 in TensorFlow lite model

Viewed 188

I'm building an image classifier android app to classify cat and dog images by using my own Tensor Flow model built using Python.I generated the meta data for my model and added to android studio by using ML Model plugin which is under beta.I automatically generates code for the given model with meta-data so that it is easy to integrate with Android app. I used Sigmoid function at outputlayer of the file and by label file has

enter image description here Here is my model meta data

enter image description here

This is the sample code enter image description here

Here is my code it picks image from gallery and convert to Bitmap and send it to model.But when i call the predict function by passing bitmap image the app crashes with this error.How to solve this error and why does it occur.

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/35/ORIGINAL/NONE/image/jpeg/2016858078 flg=0x1 clip={text/uri-list {...}} }} to activity {com.enigmabits.myapplication/com.enigmabits.myapplication.MainActivity}: java.lang.IllegalArgumentException: Cannot find an axis to label. A valid axis to label should have size larger than 1.
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5015)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:5056)
        at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.IllegalArgumentException: Cannot find an axis to label. A valid axis to label should have size larger than 1.
        at org.tensorflow.lite.support.label.TensorLabel.getFirstAxisWithSizeGreaterThanOne(TensorLabel.java:214)
        at org.tensorflow.lite.support.label.TensorLabel.<init>(TensorLabel.java:105)
        at com.enigmabits.myapplication.ml.DogvsCat$Outputs.getProbabilityAsCategoryList(DogvsCat.java:100)
        at com.enigmabits.myapplication.MainActivity.predictImage(MainActivity.java:91)
        at com.enigmabits.myapplication.MainActivity.onActivityResult(MainActivity.java:73)
        at android.app.Activity.dispatchActivityResult(Activity.java:8310)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:5008)  

MainActivity.java

package com.enigmabits.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.Image;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.enigmabits.myapplication.ml.DogvsCat;

import org.tensorflow.lite.support.image.TensorImage;
import org.tensorflow.lite.support.label.Category;

import java.io.IOException;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   Button pickbutton;
   Button predictbutton;
   ImageView img;

    private static final int PICK_IMAGE = 100;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pickbutton = findViewById(R.id.pickbtn);
        predictbutton = findViewById(R.id.predictbtn);
        img = findViewById(R.id.imageView);

        pickbutton.setOnClickListener(this);
        predictbutton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId())
        {
            case R.id.predictbtn:
            {
                break;
            }

            case R.id.pickbtn:
            { openGallery();
                break;
            }
        }

    }

    private void openGallery() {
        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            //img.setImageURI(data.getData());
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),data.getData());
                img.setImageBitmap(bitmap);
                predictImage(bitmap);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    void predictImage(Bitmap b)
    {
        try {
            DogvsCat model = DogvsCat.newInstance(this);

            // Creates inputs for reference.
            TensorImage image = TensorImage.fromBitmap(b);

            // Runs model inference and gets result.
            DogvsCat.Outputs outputs = model.process(image);
            List<Category> probability = outputs.getProbabilityAsCategoryList();
             predictbutton
            // Releases model resources if no longer used.
            model.close();
        } catch (IOException e) {
            Log.d("error",e.getLocalizedMessage());
        }
    }
}

I get error when this line is excecuted

   List<Category> probability = outputs.getProbabilityAsCategoryList();
0 Answers
Related