Brightness_range Keras : Data Augmentation with ImageDataGenerator

Brightness_range Keras featured image

Brightness_range Keras is an argument in ImageDataGenerator class of keras.preprocessing.image package. We can use it to adjust the brightness_range of any image for Data Augmentation. This article will explain to you the term Data Augmentation. In addition, We will also see how can we achieve Data Augmentation using brightness_range in Keras.

Data Augmentation with brightness_range –

Firstly, let’s understand the term Data Augmentation. Well! when you have less data for training or you want to add more variety of data in the dataset. You may generate more data by cropping, adding brightness, padding of existing data(Image). This technique is Data Argumentation in image processing.

As I have already mentioned that increasing and decreasing the brightness of the image. Also comes into the data Argumentation in Image processing.

Brightness_range TensorFlow Syntax-

 

Here is the syntax for the brightness_range argument in Tensorflow API. Basically, TensorFlow 2.0 is having a similar syntax to Keras under its package tensorflow.keras. 

from tensorflow.keras.preprocessing.image import ImageDataGenerator
imageDataGenerator_obj= ImageDataGenerator(brightness_range=(0.2, 0.8))

Here the range starts from zero which signifies no brightness of the image. Also, the upper range is 1 which signifies the maximum range of the brightness.

 

Brightness_range Keras Syntax –

Let’s see the implementation of brightness_range in core Keras API.

from keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(brightness_range=[0.2,1.0])

There is a big difference in the parameter of Tensorflow brightness_range with this API. In Keras, 1.0 is the neutral brightness. If you go down to 1 it will start darkening the image. And if you go above to 1 ( value) it will start brightening the image.

In the above syntax example, We have used the brightness_range=[0.2,1.0]. This will darken the image in this range.

 

Step by step Implementation of brightness_range Keras –

Let’s implement the data argumentation with it.

Step 1:

Import the relevant packages.

from numpy import expand_dims
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from io import BytesIO
from PIL import Image

 

Step 2:

Image loading and conversion into the array. I am working over google colab. Hence please change the code if you are doing it locally.

#specific to Google Colab only

from google.colab import files
uploaded = files.upload()

#Loading the image and converting into Byte
img_array= Image.open(BytesIO(uploaded["lamborghini_660_140220101539.jpg"]))

For instance, we have taken the sample image "lamborghini_660_140220101539.jpg", you may change at your convenience.

Step 3:

Data argumentation for the above image.
# dimesion adjustment
sam = expand_dims(img_array, 0)
# create image data augmentation generator
imageDataGenerator_obj = ImageDataGenerator(brightness_range=[0.3,0.9])

Step 4:

Now we will plot  the image.
iterator = imageDataGenerator_obj.flow(sam, batch_size=1)
for j in range(6):

	pyplot.subplot(330 + 1 + j)
	
	chunk = iterator.next()

	sub_img = chunk[0].astype('uint8')
	
	pyplot.imshow(sub_img)
	  

pyplot.show()

Full code with output-

Above all,  Here is the complete code from each step.

from numpy import expand_dims
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import ImageDataGenerator
from matplotlib import pyplot
from io import BytesIO
from PIL import Image
#Loading the image and coverting into Byte
img_array= Image.open(BytesIO(uploaded["lamborghini_660_140220101539.jpg"]))
# dimesion adjustment
sam = expand_dims(img_array, 0)
# create image data augmentation generator
imageDataGenerator_obj = ImageDataGenerator(brightness_range=[0.3,0.9])
# image ploting
iterator = imageDataGenerator_obj.flow(sam, batch_size=1)
for j in range(6):

	pyplot.subplot(330 + 1 + j)
	
	chunk = iterator.next()

	sub_img = chunk[0].astype('uint8')
	
	pyplot.imshow(sub_img)
	  

pyplot.show()

After that, Let’s see the output for the full code.

brightness_range
brightness_range

Above all, As you can see, We have generated the six different images from a single one. Just by changing the brightness. data argumentation also helps to stop overfitting the model.

Thanks 

Data Science Learner Team

Other Questions

These are the question asked on the Keras by the data science reader.

Q: I am getting No module named keras Import error. How to fix it?

This error comes where you have not install Keras module and importing it. It can be solved if you install it. To install you can use the pip command.

pip install Keras

There is a dedicated step-by-step fix to remove No module named keras error.

Q 2: How to install  TensorFlow in PyCharm

Kera requires TensorFlow to be installed in your system. So before implementing deep learning you have to install TensorFlow. You can install tensorflow using the pip command.

pip install --upgrade tensorflow

It will install TensorFlow on your system. There is a dedicated tutorial on how to install TensorFlow in pycharm. Please read it to know more.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner