Java不兼容类型错误

毗湿奴

以下代码是Java程序的一部分,该Java程序使用Tensorflow库使用Inception v3模型进行预测。

private static float[] executeInceptionGraph(byte[] graphDef, Tensor image) {
    try (Graph g = new Graph()) {
        g.importGraphDef(graphDef);
        try (Session s = new Session(g);
                Tensor result = s.runner().feed("DecodeJpeg/contents", image).fetch("softmax").run().get(0)) {
            final long[] rshape = result.shape();
            if (result.numDimensions() != 2 || rshape[0] != 1)
            {
                throw new RuntimeException(
                        String.format(
                                "Expected model to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape %s",
                                Arrays.toString(rshape)));
            }
            int nlabels = (int) rshape[1];

            return result.copyTo(new float[1][nlabels])[0];
        }
    }
}

但是return语句显示错误:

incompatable types, required:float,found:Object.

我尝试将类型转换转换为float [],但是这给了我运行时错误 "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [[F cannot be cast to [F".

我从https://github.com/emara-geek/object-recognition-tensorflow下载了程序

我正在使用IntelliJ IDE。我应该改变什么?

毗湿奴

好吧,我想通了。替换以下内容

result.copyTo(new float[1][nlabels])[0];

到以下内容:

            float[][] res = new float[1][nlabels];

            result.copyTo(res);

            return res[0];

也许第一行代码可以与代码作者使用的版本一起使用,但是我不确定。第二组代码适用于Java vesion 7。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章