Git Product home page Git Product logo

Comments (4)

galeone avatar galeone commented on August 24, 2024 1

I'm glad I helped 👍

On a side note, I usually recommend also exporting the pre-processing steps inside the SavedModel.

This forces you to define everything using pure TensorFlow functions (e.g. tf.image.resize and not pillow stuff) that's the only way to have the very same behavior in Python and in other languages. For example, the pillow resize produces a completely different representation (the pixel values!) of the OpenCV resize, and both produce a different value from the tf.image.resize method. Portability is possible only by writing pure TensorFlow programs - otherwise, you end up in a rabbit hole I don't recommend (at work we analyzed thing thing and wrote an article about it https://blog.zuru.tech/blog/the-dangers-behind-image-resizing)

from tfgo.

galeone avatar galeone commented on August 24, 2024

Hi @alimoeeny

The image.Read* functions automatically do these things:

  • Read the file
  • Decode using the correct codec (jpeg,png,gif, ...). Create a H,W,D tensor
  • Add the batch dimension. Create a 1, H, W, D tensor.
  • Convert the image from dtype uint8 to dtype float32
  • Scales the image in the [0,1] range.

Are you sure your python code is doing the same preprocessing the Read function is doing?

Moreover I guess the line batch := tg.Batchify(root, []tf.Output{img.Value()}) can be removed since img should already be a tensor with a batch dimension of 1.

For answering your questions:

  1. I suggest first trying with Python code. Once you have your SavedModel, try to import it with tf.saved_model.load, and feed the model in the same way you'd do it in Go. You can specify with tf.device a context manager for the execution of the loaded model, and make it work on your CPU, to simulate what will happen by default in Go. I'd just print out the numbers (e.g the predictions or whatever the output is).
    I do the same thing in Go and verify that I can get the same numbers.
  2. "serve" is the default tag contained in an exported SavedModel. You can export the saved model with saved_model_cli (via cli) to analyze its content. A saved model can have multiple tags - by default, there's a single tag "serve" that contains your model. This tag can be used for versioning the model or for having multipel version of the same model inside the SavedModel (very rare scenario). You can find more info on the doc

from tfgo.

alimoeeny avatar alimoeeny commented on August 24, 2024

Thank you very much @galeone for you VERY FAST response.
I think you identified the problem.
In python I don't "Scale the image in the [0,1] range" before passing it to the model. (if I am not mistaken the model I am working with has a preprocessing stage that normalizes the image and scales it and all.

  • I modified the python code to normalize the image to [0,1] and got an answer close to what I get on the go side.
  • I modified the tfgo code to scale to [0,255]range but the answer was NOT close to the python's original output.

I think I am going to train a simple model with scaled inputs and see if I can get the same answer on both python and tfgo side.

from tfgo.

alimoeeny avatar alimoeeny commented on August 24, 2024

Thank you very much, I am going to close this issue then :)

from tfgo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.