【问题标题】:Same prediction for each inference每个推理的相同预测
【发布时间】:2017-07-06 23:36:00
【问题描述】:

我使用tf.saved_model.builder.SavedModelBuilder 保存了一个张量流模型。 但是,当我尝试在 java 中进行预测时,在大多数情况下,在某些情况下,它会返回相同的结果(对于 fc8 (alexnet),softmax 之前的层),它会产生一些真正不同的结果,并且它最有可能是正确的,因此,我认为培训是可以的。 有没有其他人经历过这个?有谁知道出了什么问题?

我的 Java 实现:

Tensor image = constructAndExecuteGraphToNormalizeImage(imageBytes);    
Tensor result = s.runner().feed("input_tensor", image).feed("Placeholder_1",t).fetch("fc8/fc8").run().get(0);

private static Tensor constructAndExecuteGraphToNormalizeImage(byte[] imageBytes) {
    try (Graph g = new Graph()) {
        TF.GraphBuilder b = new TF.GraphBuilder(g);
        // Some constants specific to the pre-trained model at:
        // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
        //
        // - The model was trained with images scaled to 224x224 pixels.
        // - The colors, represented as R, G, B in 1-byte each were converted to
        //   float using (value - Mean)/Scale.
        final int H = 227;
        final int W = 227;
        final float mean = 117f;
        final float scale = 1f;

        // Since the graph is being constructed once per execution here, we can use a constant for the
        // input image. If the graph were to be re-used for multiple input images, a placeholder would
        // have been more appropriate.
        final Output input = b.constant("input", imageBytes);
        final Output output =
                b.div(
                        b.sub(
                                b.resizeBilinear(
                                        b.expandDims(
                                                b.cast(b.decodeJpeg(input, 3), DataType.FLOAT),
                                                b.constant("make_batch", 0)),
                                        b.constant("size", new int[] {H, W})),
                                b.constant("mean", mean)),
                        b.constant("scale", scale));
        try (Session s = new Session(g)) {
            return s.runner().fetch(output.op().name()).run().get(0);
        }
    }
}

【问题讨论】:

    标签: java tensorflow


    【解决方案1】:

    我假设您的图表中没有任何随机操作,例如 dropout。 (似乎是这样,因为您经常得到相同的结果)。

    唉,some operations in tensorflow seem to be non-deterministic,比如归约和卷积。我们必须接受 tensorflow 的网络是随机野兽的事实:它们的性能可以通过统计方法接近,但它们的输出是不确定的。

    我相信 Theano 等其他一些框架在提出确定性操作方面比 tensorflow 走得更远。

    【讨论】:

      猜你喜欢
      • 2022-01-19
      • 2017-06-29
      • 2018-09-28
      • 2020-06-11
      • 2018-09-02
      • 1970-01-01
      • 2017-12-03
      • 2016-04-01
      • 1970-01-01
      相关资源
      最近更新 更多