[英]Java incompatible type error
The following code is a part of a java program for making predictions with inception v3 model using Tensorflow library. 以下代码是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];
}
}
}
The return statement however shows the error: 但是return语句显示错误:
incompatable types, required:float,found:Object.
I tried type casting to float[] but that gave me a run time error "Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [[F cannot be cast to [F".
我尝试将类型强制转换为float [],但是这给了我一个运行时错误
"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [[F cannot be cast to [F".
强制转换"Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [[F cannot be cast to [F".
I downloaded the program from https://github.com/emara-geek/object-recognition-tensorflow 我从https://github.com/emara-geek/object-recognition-tensorflow下载了程序
Im using IntelliJ IDE. 我正在使用IntelliJ IDE。 What should I change?
我应该改变什么?
The code that you've provided does not match the code that you said you copied. 您提供的代码与您复制的代码不匹配。 The source code is:
源代码是:
return result.copyTo(new float[1][nlabels])[0];
which removes one level of the arrays... which explains the errors you are seeing. 这将删除一级数组...解释了您所看到的错误。
Okay I figured it out.Replace the following 好吧,我想通了。替换以下内容
result.copyTo(new float[1][nlabels])[0];
to the following: 到以下内容:
float[][] res = new float[1][nlabels];
result.copyTo(res);
return res[0];
Perhaps the first line of code works with the version being used by the code author but I cant be certain. 也许第一行代码可以与代码作者使用的版本一起使用,但是我不确定。 The second set of code worked on java vesion 7.
第二组代码适用于Java vesion 7。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.