我正在尝试对我使用“Finetuning AlexNet with TensorFlow”训练的模型执行预测https://kratzert.github.io/2017/02/24/finetuning-alexnet-with-tensorflow.html
我使用tf.saved_model.builder.SavedModelBuilder
Python保存了模型,并使用SavedModelBundle.load
. 代码的主要部分是:
SavedModelBundle smb = SavedModelBundle.load(path, "serve");
Session s = smb.session();
byte[] imageBytes = readAllBytesOrExit(Paths.get(path));
Tensor image = constructAndExecuteGraphToNormalizeImage(imageBytes);
Tensor result = s.runner().feed("input_tensor", image).fetch("fc8/fc8").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];
float [] a = result.copyTo(new float[1][nlabels])[0];`
我收到此异常:
线程“main”中的异常 java.lang.IllegalArgumentException:您必须使用 dtype float [[Node: Placeholder_1 = Placeholder_output_shapes=[[]], dtype=DT_FLOAT, shape=[], _device= 为占位符张量“Placeholder_1”提供一个值"/job:localhost/replica:0/task:0/cpu:0"]]
我看到上面的代码对某些人有效,我无法弄清楚这里缺少什么。请注意,网络熟悉节点“input_tensor”和“fc8/fc8”,因为它没有说它不知道它们。
从错误消息来看,您正在使用的模型似乎期望被提供另一个值(图中的节点名称是Placeholder_1
并且预期类型是浮点标量张量)。
您似乎已经自定义了模型(而不是按照您逐字链接的文章进行操作)。也就是说,文章显示了多个需要输入的占位符,一个用于图像,另一个用于控制丢失。文章中定义为:
keep_prob = tf.placeholder(tf.float32)
而这个占位符的值需要补足。如果您正在进行推理,那么您希望设置keep_prob
为 1.0。就像是:
Tensor keep_prob = Tensor.create(1.0f);
Tensor result = s.runner()
.feed("input_tensor", image)
.feed("Placeholder_1", keep_prob)
.fetch("fc8/fc8")
.run()
.get(0);
希望有帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句