java.lang.IndexOutOfBoundsException:索引0无效,大小为0,Android上的TensorFlow

时间:2018-02-22 03:22:08

标签: java android tensorflow

我在keras中创建了自定义模型以识别快乐的面孔并将模型加载到android中并遇到了这个问题 java.lang.IndexOutOfBoundsException:索引0无效,运行时大小为0,我的应用程序崩溃了。我从这里修改了代码https://github.com/MindorksOpenSource/AndroidTensorFlowMachineLearningExample 适合我的模特。

是否存在protobuf文件创建的问题?我测试了我的模型,它在python中运行良好。以下是日志文件和源代码。请帮助解决这个问题!谢谢!

OS平台和分发:Windows 10

TensorFlow安装于:anaconda

TensorFlow版本:1.2.0

Bazel版本:N / A

CUDA / cuDNN版本:N / A

GPU型号和内存:N / A

重现的确切命令:N / A

源代码/日志

  

01-11 16:21:12.508 18038-18078 / com.sridhar.deepak.objectdetection   D / OpenGLRenderer:0xab6c06f0(ListView)上的endAllStagingAnimators   处理0xab7000d8

     

01-11 16:21:18.135   18038-18038 / com.sridhar.deepak.objectdetection   E / TensorFlowInferenceInterface:无法运行TensorFlow会话:

     

java.lang.IllegalArgumentException:没有注册OpKernel   支持Op' Switch'与这些attrs。已注册的设备:[CPU],

     

注册内核:
  设备=' GPU&#39 ;; T在[DT_STRING]中   设备=' GPU&#39 ;; [DT_BOOL]中的T                                                                                                       设备=' GPU&#39 ;; [DT_INT32]中的T                                                                                                       设备=' GPU&#39 ;; [DT_FLOAT]中的T                                                                                                       设备=' CPU&#39 ;; [DT_FLOAT]中的T                                                                                                       设备=' CPU&#39 ;; T [DT_INT32]

     

[[Node:bn0 / cond / Switch = Switch [T = DT_BOOL](bn0 / keras_learning_phase,   bn0 / keras_learning_phase)]] 01-11 16:21:18.135   18038-18038 / com.sridhar.deepak.objectdetection D / AndroidRuntime:   关闭VM 01-11 16:21:18.136   18038-18038 / com.sridhar.deepak.objectdetection E / AndroidRuntime:FATAL

     

例外:主要                                                                                       处理:com.sridhar.deepak.objectdetection,PID:18038                                                                                       java.lang.IndexOutOfBoundsException:索引0无效,大小为0                                                                                           在   java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)                                                                                           at java.util.ArrayList.get(ArrayList.java:308)                                                                                           在   org.tensorflow.contrib.android.TensorFlowInferenceInterface.getTensor(TensorFlowInferenceInterface.java:473)                                                                                           在   org.tensorflow.contrib.android.TensorFlowInferenceInterface.readNodeIntoFloatBuffer(TensorFlowInferenceInterface.java:320)                                                                                           在   org.tensorflow.contrib.android.TensorFlowInferenceInterface.readNodeFloat(TensorFlowInferenceInterface.java:275)                                                                                           在   com.sridhar.deepak.objectdetection.TensorFlowImageClassifier.recognizeImage(TensorFlowImageClassifier.java:161)                                                                                           在   com.sridhar.deepak.objectdetection.HappyFaceDetector $ 2.onPictureTaken(HappyFaceDetector.java:82)                                                                                           在   com.flurgle.camerakit.CameraView $ CameraListenerMiddleWare.onPictureTaken(CameraView.java:296)                                                                                           在com.flurgle.camerakit.Camera1 $ 2.onPictureTaken(Camera1.java:185)                                                                                           在   android.hardware.Camera $ EventHandler.handleMessage(Camera.java:1118)                                                                                           在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                           在android.os.Looper.loop(Looper.java:154)                                                                                           在android.app.ActivityThread.main(ActivityThread.java:5527)                                                                                           at java.lang.reflect.Method.invoke(Native Method)                                                                                           在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:739)                                                                                           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)

     

01-11   16:21:18.136 18038-18038 / com.sridhar.deepak.objectdetection   E / MQSEventManagerDelegate:无法获取MQSService。 01-11   16:21:19.470 18038-18038 / com.sridhar.deepak.objectdetection I / Process:   发送信号。 PID:18038 SIG:9

TensorFlowImageClassifier文件

@Override
public List<Recognition> recognizeImage(final Bitmap bitmap,int s) {
    // Log this method so that it can be analyzed with systrace.
    Trace.beginSection("recognizeImage");

    Trace.beginSection("preprocessBitmap");
    // Preprocess the image data from 0-255 int to normalized float based
    // on the provided parameters.
    if (s==1) {
        bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        for (int i = 0; i < intValues.length; ++i) {
            final int val = intValues[i];
            floatValues[i * 3 + 0] = (((val >> 16) & 0xFF) - imageMean) / imageStd;
            floatValues[i * 3 + 1] = (((val >> 8) & 0xFF) - imageMean) / imageStd;
            floatValues[i * 3 + 2] = ((val & 0xFF) - imageMean) / imageStd;
        }
    }else {
        bitmap.getPixels(intValues, 0, bitmap.getWidth(), 0, 0, bitmap.getWidth(), bitmap.getHeight());
        for (int i = 0; i < intValues.length; ++i) {
            final int val = intValues[i];
            floatValues[i * 3 + 0] = (((val >> 16) & 0xFF))/imageStd;
            floatValues[i * 3 + 1] = (((val >> 8) & 0xFF))/imageStd;
            floatValues[i * 3 + 2] = ((val & 0xFF))/imageStd;
            floatValues[i * 3 + 0] = floatValues[i * 3 + 0] - 1;
            floatValues[i * 3 + 1] = floatValues[i * 3 + 1] - 1;
            floatValues[i * 3 + 2] = floatValues[i * 3 + 2] - 1;

        }
    }
    Trace.endSection();

    // Copy the input data into TensorFlow.
    Trace.beginSection("fillNodeFloat");
    inferenceInterface.fillNodeFloat(
            inputName, new int[]{1, inputSize, inputSize, 3}, floatValues);
    Trace.endSection();

    // Run the inference call.
    Trace.beginSection("runInference");
    inferenceInterface.runInference(outputNames);
    Trace.endSection();

    // Copy the output Tensor back into the output array.
    Trace.beginSection("readNodeFloat");
    inferenceInterface.readNodeFloat(outputName, outputs);
    Trace.endSection();

MainActivity

private static final int INPUT_SIZE = 64;
private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128;
private static final String INPUT_NAME = "input_1";
private static final String OUTPUT_NAME = "fc/Sigmoid";

private static final String MODEL_FILE = "file:///android_asset/happy_model.pb";
private static final String LABEL_FILE =
        "file:///android_asset/face_label.txt";

1 个答案:

答案 0 :(得分:0)

我更新了TensorFlowImageClassifier类,类似于tensorflow演示应用程序。这个问题现在已经解决了。