Reputation: 883
I am using Keras Tensorflow ImageDataGenerator and usually it is used with rescaling factor 1./255 to rescale the initial values from 0 to 255 to 0 to 1 instead. However, I would like to rescale it to -1,1 range.
So instead of:
train_image_generator = ImageDataGenerator(
rescale=1./255,
)
I tried:
train_image_generator = ImageDataGenerator(
rescale=((1./127.5)-1)
)
Next this would be applied to a directory:
train_datagen = train_image_generator.flow_from_directory(
directory=training_dir,
target_size=(x, y),
shuffle=True,
batch_size=x,
class_mode='binary'
)
Checking some values could be done like this:
train_datagen[1]
But according to the documentation this is a factor we use to multiply the data by the value provided. So it can be only a factor which is used to multiply, so it makes no sense to substract 1 here, because the data is rescaled using the value -0.99215686275 and indeeed when I check the actual values I can see negative values like -130.xx. So this does not work. As I need it to be rescaled to -1,1 and not 0,1 since I want to use the pretrained MobileNet V2 later on, my question is, how can I do this?
I am not talking about a way of avoiding using ImageDataGenerator:
def format_example(image, label):
image = tf.cast(image, tf.float32)
image = (image/127.5) - 1
image = tf.image.resize(image, (IMG_SIZE, IMG_SIZE))
return image, label
and so on.
So I would like to use ImageDataGenerator. So I could rescale by 1.0/127.5, but still I need to substract 1. Is there a way to later substract 1 from the values in the train_datagen? Something like
train_datagen.actualvalues-1
(I know this is not working.)
Furthermore: I need a solution which works together with image augmentation, so usually I have:
train_image_generator = ImageDataGenerator(
horizontal_flip=True,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
fill_mode="nearest",
zoom_range=0.2,
rescale=((1./127.5)-1) #1./255,
)
Now, my only problem is that rescale=((1./127.5)-1)
doesn't work. How can I solve this, how can I rescale to [-1,1] instead of [0,1]?
Upvotes: 1
Views: 2484
Reputation: 311
Use preprocessing_function parameter.
def prep_fn(img):
img = img.astype(np.float32) / 255.0
img = (img - 0.5) * 2
return img
gen = ImageDataGenerator(
preprocessing_function=prep_fn
)
Upvotes: 10