首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从Java运行Tensorflow MobileNet

从Java运行Tensorflow MobileNet
EN

Data Science用户
提问于 2018-02-15 16:58:57
回答 1查看 1.1K关注 0票数 3

我试图在Java (JSE而不是Android)中运行Tensorflow来进行图像识别(分类)。

我正在使用来自这里这里的代码。

它适用于Inceptionv3模型,也适用于经过Inceptionv3再培训的模型。

但是对于MobileNet模型,它不起作用(例如遵循这个文章)。

该代码工作正常,但给出了错误的结果(错误的分类标签)。使用来自Java的MobileNet需要哪些代码/设置?

适用于Inceptionv3的代码是

代码语言:javascript
复制
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,我们尝试了代码,

代码语言:javascript
复制
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);
                    }
}

这样做是可行的,但给出了错误的标签。

EN

回答 1

Data Science用户

发布于 2019-02-25 20:59:25

不要在java中使用:)

我也遇到了同样的问题,尝试更改标度值,之后我从java和python获得了相同的标签。

代码语言:javascript
复制
        // - 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;
票数 0
EN
页面原文内容由Data Science提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://datascience.stackexchange.com/questions/27858

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档