为什么我收到错误:初始化Tensorflow?

时间:2017-02-22 08:50:07

标签: android tensorflow

我试图通过本教程构建.apk文件 - https://www.oreilly.com/learning/tensorflow-on-android

我想使用自己的retrained_graph.pb

我更改ClassifierActivity.java(在教程中为TensorFlowImageListener.java,但我没有找到此文件)。 我换了:

private static final int INPUT_SIZE = 299;
private static final int IMAGE_MEAN = 128;
private static final float IMAGE_STD = 128;
private static final String INPUT_NAME = "Mul:0";
private static final String OUTPUT_NAME = "final_result:0";

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

但是当我在设备上运行apk时,它会与Error initializing TensorFlow崩溃。

D/AndroidRuntime( 6524): Shutting down VM
E/AndroidRuntime( 6524): FATAL EXCEPTION: main
E/AndroidRuntime( 6524): Process: org.tensorflow.demo, PID: 6524
E/AndroidRuntime( 6524): java.lang.RuntimeException: Error initializing TensorFlow!
E/AndroidRuntime( 6524):    at org.tensorflow.demo.ClassifierActivity.onPreviewSizeChosen(ClassifierActivity.java:130)
E/AndroidRuntime( 6524):    at org.tensorflow.demo.CameraActivity$1.onPreviewSizeChosen(CameraActivity.java:158)
E/AndroidRuntime( 6524):    at org.tensorflow.demo.CameraConnectionFragment.setUpCameraOutputs(CameraConnectionFragment.java:394)
E/AndroidRuntime( 6524):    at org.tensorflow.demo.CameraConnectionFragment.openCamera(CameraConnectionFragment.java:411)
E/AndroidRuntime( 6524):    at org.tensorflow.demo.CameraConnectionFragment.access$000(CameraConnectionFragment.java:63)
E/AndroidRuntime( 6524):    at org.tensorflow.demo.CameraConnectionFragment$1.onSurfaceTextureAvailable(CameraConnectionFragment.java:94)
E/AndroidRuntime( 6524):    at android.view.TextureView.getHardwareLayer(TextureView.java:370)
E/AndroidRuntime( 6524):    at android.view.View.updateDisplayListIfDirty(View.java:15443)
E/AndroidRuntime( 6524):    at android.view.View.getDisplayList(View.java:15488)
E/AndroidRuntime( 6524):    at android.view.View.draw(View.java:16258)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
E/AndroidRuntime( 6524):    at android.view.View.updateDisplayListIfDirty(View.java:15461)
E/AndroidRuntime( 6524):    at android.view.View.getDisplayList(View.java:15488)
E/AndroidRuntime( 6524):    at android.view.View.draw(View.java:16258)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
E/AndroidRuntime( 6524):    at android.view.View.draw(View.java:16539)
E/AndroidRuntime( 6524):    at android.widget.FrameLayout.draw(FrameLayout.java:598)
E/AndroidRuntime( 6524):    at android.view.View.updateDisplayListIfDirty(View.java:15466)
E/AndroidRuntime( 6524):    at android.view.View.getDisplayList(View.java:15488)
E/AndroidRuntime( 6524):    at android.view.View.draw(View.java:16258)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
E/AndroidRuntime( 6524):    at android.view.View.updateDisplayListIfDirty(View.java:15461)
E/AndroidRuntime( 6524):    at android.view.View.getDisplayList(View.java:15488)
E/AndroidRuntime( 6524):    at android.view.View.draw(View.java:16258)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
E/AndroidRuntime( 6524):    at android.view.View.updateDisplayListIfDirty(View.java:15461)
E/AndroidRuntime( 6524):    at android.view.View.getDisplayList(View.java:15488)
E/AndroidRuntime( 6524):    at android.view.View.draw(View.java:16258)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.drawChild(ViewGroup.java:3713)
E/AndroidRuntime( 6524):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3506)
E/AndroidRuntime( 6524):    at android.view.View.draw(View.java:16539)
E/AndroidRuntime( 6524):    at android.widget.FrameLayout.draw(FrameLayout.java:598)
E/AndroidRuntime( 6524):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:3111)
E/AndroidRuntime( 6524):    at android.view.View.updateDisplayListIfDirty(View.java:15466)
E/AndroidRuntime( 6524):    at android.view.View.getDisplayList(View.java:15488)
E/AndroidRuntime( 6524):    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:309)
E/AndroidRuntime( 6524):    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:315)
E/AndroidRuntime( 6524):    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:354)
E/AndroidRuntime( 6524):    at android.view.ViewRootImpl.draw(ViewRootImpl.java:2956)
E/AndroidRuntime( 6524):    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2753)
E/AndroidRuntime( 6524):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2339)
E/AndroidRuntime( 6524):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1314)
E/AndroidRuntime( 6524):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7057)
E/AndroidRuntime( 6524):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:829)
E/AndroidRuntime( 6524):    at android.view.Choreographer.doCallbacks(Choreographer.java:606)
E/AndroidRuntime( 6524):    at android.view.Choreographer.doFrame(Choreographer.java:576)
E/AndroidRuntime( 6524):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:815)
E/AndroidRuntime( 6524):    at android.os.Handler.handleCallback(Handler.java:739)
E/AndroidRuntime( 6524):    at android.os.Handler.dispatchMessage(Handler.java:95)
E/AndroidRuntime( 6524):    at android.os.Looper.loop(Looper.java:145)
E/AndroidRuntime( 6524):    at android.app.ActivityThread.main(ActivityThread.java:6917)
E/AndroidRuntime( 6524):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 6524):    at java.lang.reflect.Method.invoke(Method.java:372)
E/AndroidRuntime( 6524):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
E/AndroidRuntime( 6524):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
E/AndroidRuntime( 6524): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'org.tensorflow.Output org.tensorflow.Operation.output(int)' o
W/ActivityManager(  783):   Force finishing activity 1 org.tensorflow.demo/.ClassifierActivity
W/ResourcesManager( 6576): Asset path '/system/framework/sec_platform_library.jar' does not exist or contains no resources.
W/ResourcesManager( 6576): Asset path '/system/framework/smatlib.jar' does not exist or contains no resources.
W/ResourcesManager( 6576): Asset path '/system/framework/secvisualeffect.jar' does not exist or contains no resources.
W/ResourcesManager( 6576): Asset path '/system/framework/sechardware.jar' does not exist or contains no resources.
D/InputDispatcher(  783): Focused application set to: xxxx
D/InputDispatcher(  783): Focus left window: 6524
D/PointerIcon(  783): setMouseIconStyle1 pointerType: 1001 iconType:101 flag:0 pid:783 uid:1000
D/PointerIcon(  783): setMouseCustomIcon IconType is same.101

我发现此代码处理异常:

@Override
  public void onPreviewSizeChosen(final Size size, final int rotation) {
    final float textSizePx =
        TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE_DIP, getResources().getDisplayMetrics());
    borderedText = new BorderedText(textSizePx);
    borderedText.setTypeface(Typeface.MONOSPACE);

    try {
      classifier =
          TensorFlowImageClassifier.create(
              getAssets(),
              MODEL_FILE,
              LABEL_FILE,
              INPUT_SIZE,
              IMAGE_MEAN,
              IMAGE_STD,
              INPUT_NAME,
              OUTPUT_NAME);
    } catch (final Exception e) {
      throw new RuntimeException("Error initializing TensorFlow!", e);
    }

...

  }
tensorflow/tensorflow/examples/android/src/org/tensorflow/demo/ClassifierActivity.java

中的

我已经花了2天,伙计们请帮助我!错误的原因是什么?

1 个答案:

答案 0 :(得分:3)

我解决了这个问题,java代码中的注释没有更新,现在只需更改:

private static final String INPUT_NAME = "Mul:0";
private static final String OUTPUT_NAME = "final_result:0";

要:

private static final String INPUT_NAME = "Mul";
private static final String OUTPUT_NAME = "final_result";