【发布时间】:2018-08-01 19:31:55
【问题描述】:
我在 Keras 中创建了我的自定义模型以识别快乐的面孔并将模型加载到 android 中并遇到了 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at runtime and my app crashed 这个问题。我已经从这里修改了代码https://github.com/MindorksOpenSource/AndroidTensorFlowMachineLearningExample 适合我的模型。
是protobuf文件创建的问题吗?我已经测试了我的模型,它在 python 中运行良好。以下是日志文件和源代码。请帮忙解决这个问题!谢谢!
操作系统平台和分发:Windows 10
TensorFlow 安装自:anaconda
TensorFlow 版本:1.2.0
Bazel 版本:不适用
CUDA/cuDNN 版本:不适用
GPU 型号和内存:不适用
重现的确切命令: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'。注册设备:[CPU],
注册内核:
设备='GPU'; [DT_STRING]中的 T
设备='GPU'; [DT_BOOL] 中的 T 设备='GPU'; [DT_INT32] 中的 T 设备='GPU'; [DT_FLOAT] 中的 T 设备='CPU'; [DT_FLOAT] 中的 T 设备='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-18038/com.sridhar.deepak.objectdetection 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) 在 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) 在 java.lang.reflect.Method.invoke(本机方法) 在 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/进程: 发送信号。 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";
【问题讨论】:
标签: java android tensorflow