java label api_使用python API进行的培训作为Java API中LabelImage模块的输入?
我有java tensorflow API的問(wèn)題.我使用python tensorflow API運(yùn)行訓(xùn)練,生成文件output_graph.pb和output_labels.txt.現(xiàn)在出于某種原因,我想使用這些文件作為java tensorflow API中LabelImage模塊的輸入.我認(rèn)為一切都會(huì)正常工作,因?yàn)樵撃K只需要一個(gè).pb和一個(gè).txt.不過(guò),當(dāng)我運(yùn)行模塊時(shí),我收到此錯(cuò)誤:
2017-04-26 10:12:56.711402: W tensorflow/core/framework/op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().
Exception in thread "main" java.lang.IllegalArgumentException: No Operation named [input] in the Graph
at org.tensorflow.Session$Runner.operationByName(Session.java:343)
at org.tensorflow.Session$Runner.feed(Session.java:137)
at org.tensorflow.Session$Runner.feed(Session.java:126)
at it.zero11.LabelImage.executeInceptionGraph(LabelImage.java:115)
at it.zero11.LabelImage.main(LabelImage.java:68)
如果你幫助我找到問(wèn)題所在,我將非常感激.此外,我想問(wèn)你是否有辦法從java tensorflow API運(yùn)行培訓(xùn),因?yàn)檫@會(huì)使事情變得更容易.
更確切地說(shuō):
output_graph_def = graph_util.convert_variables_to_constants(
sess, graph.as_graph_def(), [FLAGS.final_tensor_name])
with gfile.FastGFile(FLAGS.output_graph, 'wb') as f:
f.write(output_graph_def.SerializeToString())
with gfile.FastGFile(FLAGS.output_labels, 'w') as f:
f.write('\n'.join(image_lists.keys()) + '\n')
解決方法:
LabelImage.java中默認(rèn)使用的模型與正在重新訓(xùn)練的模型不同,因此輸入和輸出節(jié)點(diǎn)的名稱不對(duì)齊.請(qǐng)注意,TensorFlow模型是圖形,feed()和fetch()的參數(shù)是圖形中節(jié)點(diǎn)的名稱.因此,您需要知道適合您的模型的名稱.
看一下retrain.py,它似乎有一個(gè)節(jié)點(diǎn),它將JPEG文件的原始內(nèi)容作為輸入(節(jié)點(diǎn)DecodeJpeg/contents),并在節(jié)點(diǎn)final_result中生成標(biāo)簽集.
如果是這種情況,那么你會(huì)在Java中執(zhí)行類似下面的操作(并且您不需要構(gòu)造圖形來(lái)對(duì)圖像進(jìn)行標(biāo)準(zhǔn)化,因?yàn)檫@似乎是重新訓(xùn)練模型的一部分,因此將LabelImage.java:64替換為某些內(nèi)容喜歡:
try (Tensor image = Tensor.create(imageBytes);
Graph g = new Graph()) {
g.importGraphDef(graphDef);
try (Session s = new Session(g);
// Note the change to the name of the node and the fact
// that it is being provided the raw imageBytes as input
Tensor result = s.runner().feed("DecodeJpeg/contents", image).fetch("final_result").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[] probabilities = result.copyTo(new float[1][nlabels])[0];
// At this point nlabels = number of classes in your retrained model
DoSomethingWith(probabilities);
}
}
希望有所幫助.
標(biāo)簽:image-recognition,java,python,tensorflow
來(lái)源: https://codeday.me/bug/20191007/1864494.html
總結(jié)
以上是生活随笔為你收集整理的java label api_使用python API进行的培训作为Java API中LabelImage模块的输入?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java静态方法声明_方法本地类中的Ja
- 下一篇: python join函数用法-Pyth