【问题标题】:Exception when trying to use tensorflow classify android example on a model trained from scratch尝试在从头开始训练的模型上使用 tensorflow 分类 android 示例时出现异常
【发布时间】:2017-07-31 12:59:04
【问题描述】:

我从头开始使用slim 训练了一个inception_v3 模型,创建了冻结模型,将其添加到 tensorflow android 示例中,但出现以下异常:


E/TensorFlowInferenceInterface: Failed to run TensorFlow inference with inputs:[input], outputs:[InceptionV3/Predictions/Reshape_1]
E/AndroidRuntime: FATAL EXCEPTION: inference
    Process: org.tensorflow.demo, PID: 14648
    java.lang.IllegalArgumentException: Tried to explicitly squeeze dimension 1 but dimension was not 1: 0
    [[Node: InceptionV3/Logits/SpatialSqueeze = Squeeze[T=DT_FLOAT, squeeze_dims=[1, 2], _device="/job:localhost/replica:0/task:0/cpu:0"](InceptionV3/Logits/Conv2d_1c_1x1/BiasAdd)]]
    at org.tensorflow.Session.run(Native Method)
    at org.tensorflow.Session.access$100(Session.java:48)
    at org.tensorflow.Session$Runner.runHelper(Session.java:295)
    at org.tensorflow.Session$Runner.runAndFetchMetadata(Session.java:257)
    at org.tensorflow.contrib.android.TensorFlowInferenceInterface.run(TensorFlowInferenceInterface.java:134)
    at org.tensorflow.demo.TensorFlowImageClassifier.recognizeImage(TensorFlowImageClassifier.java:150)
    at org.tensorflow.demo.ClassifierActivity$2.run(ClassifierActivity.java:228)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.os.HandlerThread.run(HandlerThread.java:61)

编辑:顺便说一下,我运行了 evaluation 和 bazel-bin/tensorflow/examples/label_image/label_image,并且都在 macOS 上处理了训练模型。

【问题讨论】:

    标签: android tensorflow


    【解决方案1】:

    问题是INPUT_SIZE。因为inception_v3上的默认图片大小是299x299:

      The default image size used to train this network is 299x299.
    

    但android示例中的输入大小是224:

      private static final int INPUT_SIZE = 224;
    

    将其更改为 299 解决了问题。

    【讨论】:

    • 是否有任何其他解决方案,或者任何解释为什么不同的大小会产生特定的错误?因为即使我通过 299x299 图像,我在使用预构建的 Inceptionv3 模型(没有重新训练)时也会遇到同样的错误。
    • @IBBoard 您应该使用训练时使用的尺寸,因此应该使用其他图像尺寸进行训练。
    • 谢谢 - 结果确实解决了它。显然,我使用的调用 TensorFlow 的模块正在做自己的缩放,所以我的 299x299 图像不会停留在 299x299。不幸的是,a) 没有说明这一点,b) 没有说明参数应该是什么。可访问的文档很少。我现在成功地通过分类器传递了东西,但它只关注一个类。不过,这是一个单独的问题。
    • 我正在从移动图库中获取图像,然后使用 InputSize 从 224 到 1024 调用 Classifier 的识别图像方法,并且在所有这些值中它都会抛出 ArrayIndexOutOfBound 异常。所以解决这个异常,我将 InputSize 传递为 1600 或更多,然后我得到异常为“java.lang.IllegalArgumentException:试图显式压缩维度 1,但维度不是 1:22 [[节点:MobilenetV1/Logits/SpatialSqueeze = Squeeze[T=DT_FLOAT, squeeze_dims=[1, 2], _device="/job:localhost/replica:0/task:0/cpu:0"](MobilenetV1/Logits/Conv2d_1c_1x1/BiasAdd)]]" 任何想法这个错误?
    • @sumeet 如前所述,我认为INPUT_SIZE 是由经过训练的模型固定和设置的,并且在使用分类器时必须使用相同的大小。
    猜你喜欢
    • 2017-08-11
    • 2022-06-22
    • 1970-01-01
    • 2022-01-26
    • 2017-09-08
    • 2018-04-10
    • 1970-01-01
    • 2018-07-13
    • 2020-10-05
    相关资源
    最近更新 更多