我试图在Java (JSE而不是Android)中运行Tensorflow来进行图像识别(分类)。
它适用于Inceptionv3模型,也适用于经过Inceptionv3再培训的模型。
但是对于MobileNet模型,它不起作用(例如遵循这个文章)。
该代码工作正常,但给出了错误的结果(错误的分类标签)。使用来自Java的MobileNet需要哪些代码/设置?
适用于Inceptionv3的代码是
try (Tensor image = Tensor.create(imageBytes)) {
float[] labelProbabilities = executeInceptionGraph(graphDef, image);
int bestLabelIdx = maxIndex(labelProbabilities);
result.setText("");
result.setText(String.format(
"BEST MATCH: %s (%.2f%% likely)",
labels.get(bestLabelIdx), labelProbabilities[bestLabelIdx] * 100f));
System.out.println(
String.format(
"BEST MATCH: %s (%.2f%% likely)",
labels.get(bestLabelIdx), labelProbabilities[bestLabelIdx] * 100f));
}这是Inceptionv3模型的工作,而不是MobileNet提供的错误,“期望args0是浮动的,但是提供了字符串”。
对于MobileNet,我们尝试了代码,
try (Graph g = new Graph()) {
GraphBuilder b = new GraphBuilder(g);
// Some constants specific to the pre-trained model at:
// https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip
//
// - The model was trained with images scaled to 224x224 pixels.
// - The colors, represented as R, G, B in 1-byte each were converted to
// float using (value - Mean)/Scale.
final int H = 224;
final int W = 224;
final float mean = 128f;
final float scale = 1f;
// Since the graph is being constructed once per execution here, we can use a constant for the
// input image. If the graph were to be re-used for multiple input images, a placeholder would
// have been more appropriate.
final Output<String> input = b.constant("input", imageBytes);
final Output<Float> output = b.div(
b.sub(
b.resizeBilinear(
b.expandDims(
b.cast(b.decodeJpeg(input, 3), Float.class),
b.constant("make_batch", 0)),
b.constant("size", new int[] {H, W})),
b.constant("mean", mean)),
b.constant("scale", scale));
try (Session s = new Session(g)) {
return s.runner().fetch(output.op().name()).run().get(0).expect(Float.class);
}
}这样做是可行的,但给出了错误的标签。
发布于 2019-02-25 20:59:25
不要在java中使用:)
我也遇到了同样的问题,尝试更改标度值,之后我从java和python获得了相同的标签。
// - The model was trained with images scaled to 224x224 pixels.
// - The colors, represented as R, G, B in 1-byte each were converted to
// float using (value - Mean)/Scale.
final int H = 224;
final int W = 224;
final float mean = 117f;
final float scale = 255f;https://datascience.stackexchange.com/questions/27858
复制相似问题