每个推理的相同预测 [英] Same prediction for each inference

查看:30
本文介绍了每个推理的相同预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I saved a tensorflow model using tf.saved_model.builder.SavedModelBuilder. However, when I try to make predictions in java, in most of the time it returns the same results (for fc8 (alexnet) the layer before softmax) in some cases, it produces some real different results and it's most likely to be correct, so from that, I assume that the training is OK. Did anyone else experienced this? Does anyone have an idea what's wrong?

我的 Java 实现:

my Java implementation:

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);
        }
    }
}

推荐答案

我假设您的图中不存在随机操作,例如 dropout.(似乎是这样,因为你经常得到相同的结果).

I am assuming that there is no random operation left in your graph, such as dropout. (Seems to be the case, since you often get the same results).

唉,张量流中的一些操作似乎是不确定的,比如减少和卷积.我们必须接受这样一个事实,即 tensorflow 的网络是随机的野兽:它们的性能可以从统计上接近,但它们的输出是不确定的.

Alas, some operations in tensorflow seem to be non-deterministic, such as reductions and convolutions. We have to live with the fact that tensorflow's nets are stochastic beasts: their performance can be approached statistically but their outputs are non-deterministic.

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

这篇关于每个推理的相同预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆