-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Guides] Pre-processing of images for Xception model #20
Comments
Yes, that seems like an issue. We should fix it. |
I'll be working on this and hope to get back to you with a PR soon. |
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: 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) |
We need preprocessing to be part of the model, as a best practice. I will look into adding a new For the time being I would recommend using a |
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 /cc: @tanzhenyu
We can have a discussion regarding the same. Thanks. |
Let's move the discussion to the PR. |
As a part of the Transfer Learning tutorial on the website, Xception model have been used.
keras-io/guides/transfer_learning.py
Lines 426 to 432 in 2c5eb05
keras-io/guides/transfer_learning.py
Lines 530 to 532 in 2c5eb05
The preprocessing used to feed in data using the tf.data API as well as with the new Rescaling layer normalizes raw input pixels (0-255) into a range (0, 1). But, as per
keras_applications.xception.preprocess_input
,(https://github.com/tensorflow/tensorflow/blob/476ec938b253a9479de09aab88dceec6f0a304ed/tensorflow/python/keras/applications/xception.py#L318-L320) the corresponding preprocess_input uses
mode='tf'
(https://github.com/tensorflow/tensorflow/blob/476ec938b253a9479de09aab88dceec6f0a304ed/tensorflow/python/keras/applications/imagenet_utils.py#L181-L184) which normalizes input pixels in the range (-1, 1) instead of (0, 1).Sometimes calculating activations (for deep feature extraction step) from pre-trained weights are prone to these type of problems with different preprocessing input ranges. Is the example affected?
The text was updated successfully, but these errors were encountered: