Create layers | Computer Vision with Keras p.1

In this tutorial, we will see how to define a model and create layers with Keras. This tutorial will also be easy to follow but I recommend that you see the first one as well: Install Keras and OpenCV | Computer Vision with Keras

There are two ways to create a model with Keras: Functional API and Sequential model. Today we will not go into details but I will explain the basics of creating layers with the Functional API method.

in this article we will see:
1. Load the image with OpenCV
2. How to pass the image to the neural network
3. Create layers with Keras

1. Load the image with OpenCV

The first thing to do is upload the image. I used this image of the red panda.

image panda by Ivan Cujic (pexels.com)

To load the image we simply use an OpenCV code:

import cv2

img = cv2.imread("red_panda.jpg", cv2.IMREAD_GRAYSCALE)

cv2.imshow("red pand", img)
cv2.waitKey(0)

2. How to pass the image to the neural network

To create Create layers Keras, we need to create first the Input layer so we need to understand how to pass the information. Printing the image we see some arrays

input layer keras with image

Each array containing 3 values represents a pixel in BGR format. Each value has a range between 0 and 255 which represents the intensity of the color.

For example the array pixel [49 80 83] :
49 Blue
80 Green
83 Red

but in the case of object detection, we can use the image in Gray Scale format for it is not necessary to use colors. In this case, only 1 value will be enough for each pixel In fact, as you can see in the image below, the print of the array is lighter.

opencv gray scale format image

even the code to use for the conversion is very simple with OpenCV

img = cv2.imread("red_panda.jpg", cv2.IMREAD_GRAYSCALE)

To Create layers Keras, how many parameters must the Input Layer have?

The number of input parameters is defined by the shape of the image. In my case (1004, 1280).

here’s how to extract shape and pass values

...

img = cv2.imread("red_panda.jpg", cv2.IMREAD_GRAYSCALE)
height, width = img.shape
print("Img shape:", img.shape)


# Keras model structure
input_layer = keras.Input(shape=(height, width))
print("Input layer shape:", input_layer.shape)

...

If you have an image with a different size obviously the output will change, but in general, when you do this processing the image is resized, at the moment we do not go into the details of this topic.

img shape: (1004, 1280)
Input layer shape: (None, 1004, 1280)

3. Create layers with Keras

After processing the image and creating the first Input layer, we can also create other layers with Keras in the middle. There are several different layer types, in this example, we used Dense Layers.

First, we recall Keras and the type of layer required.

import keras
from keras.layers import Dense

To define the layer we have to keep in mind 2 things: the number of neurons and their connection. In this case, it is just an example so we enter the default number for the neurons and connect the layer to the previous level: input_layer.

# Keras model structure
input_layer = keras.Input(shape=(height, width))

Layer_1 = Dense(64)(input_layer)

Same thing for the others up to the output layer. In this case, we define the output of 2 values.

Layer_2 = Dense(32)(Layer_1)
output = Dense(2)(Layer_2)

In general, the output, as a result of the training, can be applied to the computer vision for the bounding box for object detection, classification, or segmentation.

Define model

Even if we are not completely finished we define the model that will have input_layer and output as parameters

# Define Model
model = keras.Model(inputs=input_layer, outputs=output)
model.summary()

To see if everything is correct we print the structure if we get a similar scheme everything has been set correctly

keras model output