繁体   English   中英

java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0, TensorFlow on Android

[英]java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0, TensorFlow on Android

我在 Keras 中创建了我的自定义模型来识别笑脸并将模型加载到 android 中,并遇到了 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at runtime 的这个问题,我的应用程序崩溃了。 我已经修改了https://github.com/MindorksOpenSource/AndroidTensorFlowMachineLearningExample 中的代码以适合我的模型。

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

操作系统平台和发行版:Windows 10

TensorFlow 安装自:anaconda

TensorFlow 版本:1.2.0

巴泽尔版本:不适用

CUDA/cuDNN 版本:不适用

GPU 型号和内存:不适用

重现的确切命令:不适用

源代码/日志

01-11 16:21:12.508 18038-18078/com.sridhar.deepak.objectdetection D/OpenGLRenderer:endAllStagingAnimators on 0xab6c06f0 (ListView) with handle 0xab7000d8

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

java.lang.IllegalArgumentException: 没有注册 OpKernel 来支持具有这些属性的 Op 'Switch'。 注册设备:[CPU],

注册内核:
设备='GPU'; [DT_STRING] 中的 T
设备='GPU'; [DT_BOOL] device='GPU' 中的 T; [DT_INT32] device='GPU' 中的 T; T in [DT_FLOAT] device='CPU'; T in [DT_FLOAT] device='CPU'; [DT_INT32] 中的 T

[[节点: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:关闭虚拟机 01-11 16:21:18.136 18038/deepsak.对象检测 E/AndroidRuntime:致命

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

主要活动

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";

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM