Running Tensorflow MobileNet from Java2019 Community Moderator ElectionTesting 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 rowIssue 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

How to report a triplet of septets in NMR tabulation?

How can I fix this gap between bookcases I made?

DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

The use of multiple foreign keys on same column in SQL Server

Is Social Media Science Fiction?

How do you conduct xenoanthropology after first contact?

Pronouncing Dictionary.com's W.O.D "vade mecum" in English

TGV timetables / schedules?

Infinite past with a beginning?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Motorized valve interfering with button?

Banach space and Hilbert space topology

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

If Manufacturer spice model and Datasheet give different values which should I use?

Set-theoretical foundations of Mathematics with only bounded quantifiers

What are these boxed doors outside store fronts in New York?

What defenses are there against being summoned by the Gate spell?

Prevent a directory in /tmp from being deleted

Why are only specific transaction types accepted into the mempool?

How to make payment on the internet without leaving a money trail?

Why is the design of haulage companies so “special”?

What is the command to reset a PC without deleting any files

How long does it take to type this?



Running Tensorflow MobileNet from Java



2019 Community Moderator ElectionTesting 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 rowIssue 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,52551330




1,52551330










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









$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









    $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









      $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









        $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












        share|improve this answer



        share|improve this answer










        answered Feb 25 at 20:59









        Eduardo SantAna da SilvaEduardo SantAna da Silva

        1




        1



























            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

            Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

            Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

            Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?