How to determine input shape in Keras TensorFlow
Hey coders, In this tutorial, we will learn how to determine the input shapes that the Keras and TensorFlow accepts as their input for training a neural network model. We will first learn how to find the shape and the color mode of the images that can be set as input to the Keras model.
Find the shape and color mode of the images
import tensorflow as tf import keras import cv2
The first step always is to import important libraries. We will be using the above libraries in our code to read the images and to determine the input shape for the Keras model.
# Set the image path img_path = '../Input_shape_keras/test_image.jpg' # Read the image image = cv2.imread(img_path)
First, save the path of the testing image in a variable and then read the image using OpenCV.
We can use the “.shape” function to find the shape of the image. It returns a tuple of integers that represent the shape and color mode of the image.
In the above output, the first two integer values represent the shape of the image. The third integer value represents the color mode/ channel of the image. If there are only two integers, then the data is of channel 1, i.e. gray format.
Color mode: It describes the color format of the images, i.e. GRAY, RGB, or RGBA. The color format is represented by channels 1, 3, or 4.
Note: Channel 1 – GRAY format
Channel 3 – RGB format
Channel 4 – RGBA format
image = cv2.resize(image,(32,32)) image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY) print(image.shape)
If required, we can perform a resizing operation. Here, the image is resized to 32×32 from 280×475.
Now that we understood about the image shapes and their color mode, let’s make a Keras model using a large dataset.
A Keras Example
In this example, we will use the cifar10 dataset. This dataset is a collection of 60,000 colored images of size 32×32 and channel 3(RGB). This dataset is used for object detection in machine learning. let’s create a Keras model that accepts (32,32,3) input shapes.
import tensorflow as tf import keras from keras.models import Sequential from keras.layers import Dense, Flatten, Conv2D, Dropout from keras.losses import sparse_categorical_crossentropy from keras.optimizers import Adam from keras.datasets import cifar10
First, import the required libraries & dataset for training our Keras model.
# Load the dataset data = cifar10.load_data() # Split the data into training and testing (input_train, target_train), (input_test, target_test) = data
Here, we are loading our cifar10 dataset. It will automatically download the dataset. After loading the dataset, we are splitting the data into two parts for training and testing the model.
As we can see, the data is split into two parts, from 60,000 images approximately 80% into training data(50,000) and 20% into testing data(10,000). The size of the images is 32×32 and the channel/color mode of the data is “RGB”.
# Creating the model model = Sequential()
Here, we are creating a Sequential model for training. A sequential model is used to create a layer by layer model. We can stack a new layer after each layer and the output of the previous layer is fed to the new layer.
# Adding the input layer to our model model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(32,32,3)))
Now, we are adding the first layer to our sequential model. This layer will be the input layer. Since we know that our data is of shape 32×32 and the channel is 3(RGB), we need to create the first layer such that it accepts the (32,32,3) input shape. Hence, we used the input_shape to make sure that this layer accepts the data.
Note: If the data is of shape 28×28 and the channel is 1(GRAY), i.e. (28,28,1). Then, the input layer will be
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1)))
# Adding the hidden layers and the output layer to our model model.add(Conv2D(64, kernel_size=(3, 3), activation='relu')) model.add(Conv2D(128, kernel_size=(3, 3), activation='relu')) model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.2)) model.add(Dense(10, activation='softmax'))
Then, we are adding some more layers to the model. The “Dense” layer is a deeply connected neural network layer. The “Dropout” layer is used to prevent the model from overfitting. The first layer is known as the input layer, the middle layers are called hidden layers, and the last layer is the output layer.
The above line will summarize our model and print the layers we created along with their outputs.
Now, we are configuring the model for training.
history = model.fit(input_train, target_train, batch_size=50, epochs=25, verbose=1, validation_split=0.2)
By using the “model.fit” and the training data, we are training our model. The training time of the model depends upon the number of layers used. If more number of layers, the model will take time for training.
testing_model = model.evaluate(input_test,target_test,verbose=2)
After training the model successfully, we are using the testing dataset to test the accuracy of the model.
# Changing the accuracy into a percentage testing_acc = testing_model*100 # Printing the accuracy Print('Test Accuracy - ', testing_acc,'%')
testing_accuracy - 51.71999931335449 %
We changed the accuracy into the percentage format and printed it.
In this tutorial, we learned to determine the input shapes in Keras with a working example. To use the dataset in our model, we need to set the input shape in the first layer of our Keras model using the parameter “input_shape” so that it matches the shape of the dataset.
I hope that this tutorial helped you in understanding the Keras input shapes efficiently. Thank you.