question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Why we have to rescale by 1. / 255

See original GitHub issue
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
horizontal_flip=True)

I saw this process in Keras blog, and your implementation. But I don’t understand why?


def preprocess_input(x, dim_ordering='default'):
    if dim_ordering == 'default':
        dim_ordering = K.image_dim_ordering()
    assert dim_ordering in {'tf', 'th'}

    if dim_ordering == 'th':
        x[:, 0, :, :] -= 103.939
        x[:, 1, :, :] -= 116.779
        x[:, 2, :, :] -= 123.68
        # 'RGB'->'BGR'
        x = x[:, ::-1, :, :]
    else:
        x[:, :, :, 0] -= 103.939
        x[:, :, :, 1] -= 116.779
        x[:, :, :, 2] -= 123.68
        # 'RGB'->'BGR'
        x = x[:, :, :, ::-1]
return x

this is preprocess step on imagenet_util.py, It just minus the mean value of each dimension.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

38reactions
Arseycommented, Oct 12, 2016

Hi @anhlt! Rescale is a value by which we will multiply the data before any other processing. Our original images consist in RGB coefficients in the 0-255, but such values would be too high for our model to process (given a typical learning rate), so we target values between 0 and 1 instead by scaling with a 1/255. factor (the description taken from https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html)

Talking about preprocess_input function, I don’t know on which step it can/should be used. I’ve tried to train model with extracting those mean values and converting to BGR, but then model does not train at all and stack on a very low accuracy (near 0.0387 for each epoch). Then I’ve tried it on prediction step - results still bad. Also I’ve noticed that this function is not used inside of Keras.

About mean value here’s interesting info - https://github.com/Lasagne/Recipes/issues/20

2reactions
Suraj023commented, May 15, 2020

rescale is a value by which we will multiply the data before any other processing. Our original images consist in RGB coefficients in the 0-255, but such values would be too high for our models to process (given a typical learning rate), so we target values between 0 and 1 instead by scaling with a 1./255 factor.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras Image Preprocessing: scaling image pixels for training
Since 255 is the maximin pixel value. Rescale 1./255 is to transform every pixel value from range [0,255] -> [0,1]. And the benefits...
Read more >
do I use "rescale=1. / 255" or not? Building VGG-like CNN
Rescaling to 0-1 in general is always a good choice when training neural networks, so yes go for it. The reason behind this...
Read more >
Python Tip #8: Why should we Normalize image pixel values ...
As the pixel values range from 0 to 256, apart from 0 the range is 255. So dividing all the values by 255...
Read more >
Rescaling layer - Keras
Rescaling class · To rescale an input in the [0, 255] range to be in the [0, 1] range, you would pass scale=1./255...
Read more >
Neural Networks using Keras on Rescale
After the final dense layer is a softmax layer that squashes the output to a (0, 1) range that sums to 1. Training...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found