Skip to content
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

Why we have to rescale by 1. / 255 #1

Closed
anhlt opened this issue Oct 12, 2016 · 5 comments
Closed

Why we have to rescale by 1. / 255 #1

anhlt opened this issue Oct 12, 2016 · 5 comments

Comments

@anhlt
Copy link

anhlt commented Oct 12, 2016

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.

@Arsey
Copy link
Owner

Arsey commented 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 - Lasagne/Recipes#20

@anhlt
Copy link
Author

anhlt commented Oct 13, 2016

@Arsey thank for your answer. Interestingly , I found some preprocess_input implement by @karpathy on his neural-talk repository.

def preprocess_image(img):
    '''
    Preprocess an input image before processing by the caffe module.


    Preprocessing include:
    -----------------------
    1- Converting image to single precision data type
    2- Resizing the input image to cropped_dimensions used in extract_features() matlab script
    3- Reorder color Channel, RGB->BGR
    4- Convert color scale from 0-1 to 0-255 range (actually because image type is a float the 
        actual range could be negative or >255 during the cubic spline interpolation for image resize.
    5- Subtract the VGG dataset mean.
    6- Reorder the image to standard caffe input dimension order ( 3xHxW) 
    '''
    img      = img.astype(np.float32)
    img      = imresize(img,224,224) #cropping the image
    img      = img[:,:,[2,1,0]] #RGB-BGR
    img      = img*255

    mean = np.array([103.939, 116.779, 123.68]) #mean of the vgg 

    for i in range(0,3):
        img[:,:,i] = img[:,:,i] - mean[i] #subtracting the mean
    img = np.transpose(img, [2,0,1])
return img #HxWx3

As you can see, He wrote that

4- Convert color scale from 0-1 to 0-255 range (actually because image type is a float the 
    actual range could be negative or >255 during the cubic spline interpolation for image resize.

I don't think, high value of input can effect learning curve. But I will try both of them and compare the results.

@anhlt
Copy link
Author

anhlt commented Oct 15, 2016

@Arsey How accurary you get on validation set? I just got only about 80% .
Happy to have any comment on my repository.
https://github.com/anhlt/keras-102-flower-dataset/

@Arsey
Copy link
Owner

Arsey commented Oct 15, 2016

@anhlt I have about 81% accuracy after 250th epoch on fine tuning step. I see that you also have GTX 1070, just like me))). BTW which OS do you use?

@Arsey Arsey closed this as completed Oct 25, 2016
@Suraj023
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants