Running Tensorflow MobileNet from JavaTesting a tensorflow network: in_top_k() replacement for multilabel...

Translation needed for 130 years old church document

How to not let the Identify spell spoil everything?

What is a good reason for every spaceship to carry a weapon on board?

Stuck on a Geometry Puzzle

Can you determine if focus is sharp without diopter adjustment if your sight is imperfect?

What species should be used for storage of human minds?

Midterm in Mathematics Courses

How to politely refuse in-office gym instructor for steroids and protein

Is there a way to store 9th-level spells in a Glyph of Warding or similar method?

Book where a space ship journeys to the center of the galaxy to find all the stars had gone supernova

How is this property called for mod?

Eww, those bytes are gross

In harmony: key or the flow?

Single-row INSERT...SELECT much slower than separate SELECT

Crack the bank account's password!

Why did Luke use his left hand to shoot?

Categorical Unification of Jordan Holder Theorems

Why do all the books in Game of Thrones library have their covers facing the back of the shelf?

I have trouble understanding this fallacy: "If A, then B. Therefore if not-B, then not-A."

Article. The word "Respect"

"Starve to death" Vs. "Starve to the point of death"

Why is 'diphthong' pronounced the way it is?

Cat is tipping over bed-side lamps during the night

How much light is too much?



Running Tensorflow MobileNet from Java


Testing a tensorflow network: in_top_k() replacement for multilabel classificationTensorFlow doesn't learn when input=output (or probably I am missing something)Neural Network for Multiple Output RegressionFine-tuning a model from an existing checkpoint with TensorFlow-SlimTensorFlow: Regression using Deep Neural NetworkTensorflow predicting same value for every row(Java) Neural Network Performs Bad On The MNIST DatasetIssue with Custom object detection using tensorflow when Training on a single type of objectWhy normalize when all features are on the same scale?How to split a keras model into submodels after it's created













2












$begingroup$


I am trying to run Tensorflow for image recognition (classification) in Java (JSE not Android).



I am using the code from here, and here.



It works for Inceptionv3 models, and for models retrained from Inceptionv3.



But for MobileNet models it does not work, (such as following this article).



The code works, but gives the wrong results (wrong classify labels). What code/settings are required to use MobileNet from Java?



The code that works for Inceptionv3 is



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));
}


This works of Inceptionv3 models, but not MobileNet,
Gives the error, "Expects args[0] to be float but string is provided"



For MobileNet we tried the code,



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);
}
}


This works, but gives the wrong labels.










share|improve this question











$endgroup$












  • $begingroup$
    Have you found an answer to your question? Did you manage to get MobileNet to run without using TensorFlow lite?
    $endgroup$
    – Henry
    Aug 25 '18 at 10:49






  • 1




    $begingroup$
    No, we gave up running Tensorflow in Java, and switched to Python.
    $endgroup$
    – James
    Aug 26 '18 at 12:30










  • $begingroup$
    Thanks, I am currently also failing at setting up MobileNet. I might switch to Python, too.
    $endgroup$
    – Henry
    Aug 26 '18 at 12:52
















2












$begingroup$


I am trying to run Tensorflow for image recognition (classification) in Java (JSE not Android).



I am using the code from here, and here.



It works for Inceptionv3 models, and for models retrained from Inceptionv3.



But for MobileNet models it does not work, (such as following this article).



The code works, but gives the wrong results (wrong classify labels). What code/settings are required to use MobileNet from Java?



The code that works for Inceptionv3 is



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));
}


This works of Inceptionv3 models, but not MobileNet,
Gives the error, "Expects args[0] to be float but string is provided"



For MobileNet we tried the code,



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);
}
}


This works, but gives the wrong labels.










share|improve this question











$endgroup$












  • $begingroup$
    Have you found an answer to your question? Did you manage to get MobileNet to run without using TensorFlow lite?
    $endgroup$
    – Henry
    Aug 25 '18 at 10:49






  • 1




    $begingroup$
    No, we gave up running Tensorflow in Java, and switched to Python.
    $endgroup$
    – James
    Aug 26 '18 at 12:30










  • $begingroup$
    Thanks, I am currently also failing at setting up MobileNet. I might switch to Python, too.
    $endgroup$
    – Henry
    Aug 26 '18 at 12:52














2












2








2





$begingroup$


I am trying to run Tensorflow for image recognition (classification) in Java (JSE not Android).



I am using the code from here, and here.



It works for Inceptionv3 models, and for models retrained from Inceptionv3.



But for MobileNet models it does not work, (such as following this article).



The code works, but gives the wrong results (wrong classify labels). What code/settings are required to use MobileNet from Java?



The code that works for Inceptionv3 is



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));
}


This works of Inceptionv3 models, but not MobileNet,
Gives the error, "Expects args[0] to be float but string is provided"



For MobileNet we tried the code,



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);
}
}


This works, but gives the wrong labels.










share|improve this question











$endgroup$




I am trying to run Tensorflow for image recognition (classification) in Java (JSE not Android).



I am using the code from here, and here.



It works for Inceptionv3 models, and for models retrained from Inceptionv3.



But for MobileNet models it does not work, (such as following this article).



The code works, but gives the wrong results (wrong classify labels). What code/settings are required to use MobileNet from Java?



The code that works for Inceptionv3 is



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));
}


This works of Inceptionv3 models, but not MobileNet,
Gives the error, "Expects args[0] to be float but string is provided"



For MobileNet we tried the code,



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);
}
}


This works, but gives the wrong labels.







tensorflow java inception






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 15 '18 at 17:14









Stephen Rauch

1,51551129




1,51551129










asked Feb 15 '18 at 16:58









JamesJames

8615




8615












  • $begingroup$
    Have you found an answer to your question? Did you manage to get MobileNet to run without using TensorFlow lite?
    $endgroup$
    – Henry
    Aug 25 '18 at 10:49






  • 1




    $begingroup$
    No, we gave up running Tensorflow in Java, and switched to Python.
    $endgroup$
    – James
    Aug 26 '18 at 12:30










  • $begingroup$
    Thanks, I am currently also failing at setting up MobileNet. I might switch to Python, too.
    $endgroup$
    – Henry
    Aug 26 '18 at 12:52


















  • $begingroup$
    Have you found an answer to your question? Did you manage to get MobileNet to run without using TensorFlow lite?
    $endgroup$
    – Henry
    Aug 25 '18 at 10:49






  • 1




    $begingroup$
    No, we gave up running Tensorflow in Java, and switched to Python.
    $endgroup$
    – James
    Aug 26 '18 at 12:30










  • $begingroup$
    Thanks, I am currently also failing at setting up MobileNet. I might switch to Python, too.
    $endgroup$
    – Henry
    Aug 26 '18 at 12:52
















$begingroup$
Have you found an answer to your question? Did you manage to get MobileNet to run without using TensorFlow lite?
$endgroup$
– Henry
Aug 25 '18 at 10:49




$begingroup$
Have you found an answer to your question? Did you manage to get MobileNet to run without using TensorFlow lite?
$endgroup$
– Henry
Aug 25 '18 at 10:49




1




1




$begingroup$
No, we gave up running Tensorflow in Java, and switched to Python.
$endgroup$
– James
Aug 26 '18 at 12:30




$begingroup$
No, we gave up running Tensorflow in Java, and switched to Python.
$endgroup$
– James
Aug 26 '18 at 12:30












$begingroup$
Thanks, I am currently also failing at setting up MobileNet. I might switch to Python, too.
$endgroup$
– Henry
Aug 26 '18 at 12:52




$begingroup$
Thanks, I am currently also failing at setting up MobileNet. I might switch to Python, too.
$endgroup$
– Henry
Aug 26 '18 at 12:52










1 Answer
1






active

oldest

votes


















0












$begingroup$

Don't give to use in java :)



I had the same problem, try to change the scale value, I had the same labels from java and python after that.



        // - 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;





share|improve this answer








New contributor




Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






$endgroup$













    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "557"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f27858%2frunning-tensorflow-mobilenet-from-java%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0












    $begingroup$

    Don't give to use in java :)



    I had the same problem, try to change the scale value, I had the same labels from java and python after that.



            // - 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;





    share|improve this answer








    New contributor




    Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    $endgroup$


















      0












      $begingroup$

      Don't give to use in java :)



      I had the same problem, try to change the scale value, I had the same labels from java and python after that.



              // - 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;





      share|improve this answer








      New contributor




      Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$
















        0












        0








        0





        $begingroup$

        Don't give to use in java :)



        I had the same problem, try to change the scale value, I had the same labels from java and python after that.



                // - 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;





        share|improve this answer








        New contributor




        Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        $endgroup$



        Don't give to use in java :)



        I had the same problem, try to change the scale value, I had the same labels from java and python after that.



                // - 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;






        share|improve this answer








        New contributor




        Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 4 hours ago









        Eduardo SantAna da SilvaEduardo SantAna da Silva

        1




        1




        New contributor




        Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Eduardo SantAna da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Data Science Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            Use MathJax to format equations. MathJax reference.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f27858%2frunning-tensorflow-mobilenet-from-java%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

            How do i solve the “ No module named 'mlxtend' ” issue on Jupyter?

            Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...