Java 不兼容的类型错误

Java incompatible type error

以下代码是 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。我应该改变什么?

您提供的代码与您所说的复制的代码不符。源代码是:

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

删除一级数组...这解释了您看到的错误。

好的我想通了out.Replace以下

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

以下内容:

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

            result.copyTo(res);

            return res[0];

也许第一行代码适用于代码作者使用的版本,但我不能确定。第二组代码适用于 java 版本 7。