Code Monkey home page Code Monkey logo

Comments (6)

fchollet avatar fchollet commented on June 20, 2024 1

We need preprocessing to be part of the model, as a best practice.

I will look into adding a new offset argument to Rescaling to support this use case.

For the time being I would recommend using a Normalization layer (from layers.experimental.preprocessing). It can do both scaling and offsetting. Just set the weights correctly.

from keras-io.

fchollet avatar fchollet commented on June 20, 2024 1

Let's move the discussion to the PR.

from keras-io.

fchollet avatar fchollet commented on June 20, 2024

Yes, that seems like an issue. We should fix it.

from keras-io.

swghosh avatar swghosh commented on June 20, 2024

I'll be working on this and hope to get back to you with a PR soon.
Thanks for addressing the issue.

from keras-io.

swghosh avatar swghosh commented on June 20, 2024

It appears to me that the Rescaling layer used in the code can only support multiplicative op(s). Instead of using 1/255., I can use 1/127.5 but that will scale the data in range (0, 2) which is undesired.

In order to attain required normalisation range of (-1, 1), the inputs should be scaled as: x /= 127.5; x -= 1 or similar.

Should we replace the Rescaling layer with a new preprocess tf.function or use keras_applications.xception.preprocess_input that does exactly the same operation as above and pass it to ds.map?

@tf.function
def preprocess(x):
     x /= 127.5
     x -= 1
     return x

ds = ds.map(lambda x, y: (preprocess(x), y), AUTOTUNE)

or

preprocess = tf.keras.applications.xception.preprocess_input
ds = ds.map(lambda x, y: (preprocess(x), y), AUTOTUNE)

Although, it wouldn't be a good idea to let go away the Rescaling layer from a documentation example as it'd then indirectly discourage users from using the new Preprocessing Layers; one workaround would be to use the Rescaling layer combined with a Lambda layer to scale inputs appropriately.

Something like this:

# x has range (0, 255)
x = tf.keras.layers.experimental.preprocessing.Rescaling(1 / 127.5)(x) # x has range (0, 2)
x = tf.keras.layers.Lambda(lambda inp: inp - 1.)(x) # x has range (-1, 1)

from keras-io.

swghosh avatar swghosh commented on June 20, 2024

Thanks @fchollet.

I'll make the code changes to the script, use the Normalization layer and submit a PR as of now. Also, hope to get back to you soon regarding the offset argument for Rescaling as well.

/cc: @tanzhenyu

We need preprocessing to be part of the model, as a best practice.

I will look into adding a new offset argument to Rescaling to support this use case.

We can have a discussion regarding the same. Thanks.

from keras-io.

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.