Flatten and Dense layers | Computer Vision with Keras p.6
We will see how to apply flatting operation and dense layer into a convolutional neural network with Keras
Flatten and Dense layers in a simple VGG16 architetture
To better understand the concept and purpose of using Flatten and Dense layers let’s see this simple architecture of the VGG16 model as an example.
There are several convolutional groups that end with a pooling layer. At the end of these elaborations, there is the Dense layer. These processes aim to have numerous image data at the input and reduce it to minimal information. If you want to know more about this aspect, I recommend that you see the research about convolutional neural network

In practice, we have to transform the image into very small numbers. For example, if we wanted to identify a dog we could say that the number close to zero identifies the dog and the number nearest to 1 identifies the cat.
How to Use Flatten and Dense layers
As we saw in the previous lesson Max pooling layer | Computer Vision with Keras p.5 we must always prepare the image and apply the Max pooling layer in addition to the Conv2D layer so for this part the code always remains the same.
img_size = 32 img = cv2.imread("dog.jpg") img = cv2.resize(img, (img_size, img_size)) # Model model = keras.Sequential() model.add(layers.Conv2D(input_shape=(img_size, img_size, 3), filters=64, kernel_size=(3, 3))) model.add(layers.MaxPooling2D(pool_size=(2, 2), strides=(1, 1)))
Trying to show the result of these elaborations we obtain an array with various numbers

all of this can be simplified with Flatten and Dense layers.
Apply Flatten layer
By applying the flatten operation we can have a simplification of the array. In fact, we will get an array that contains the values one after the other.
To better explain the concept, see the diagram below. On the left, you can see the array of an image that contains only the black and white color, on the right, the flatten operation is applied and the array contains the values one behind the other.

Its application is also very simple
model.add(layers.Flatten())
Apply Dense layers
The dense layer is perhaps the best-known part of the convolutional neural network and the image below represents this passage well. Their job is to process all the information and return only a few values to determine only if the object is present or not in the image.

In our example, we set units=10 in order to obtain 10 output values. The general purpose is to go from the image array to get very few values just to know if the image is present or not.
model.add(layers.Dense(units=10))
This is the output we get with our example.


Hi there, I’m the founder of Pysource.
I’m a Computer Vision Consultant, developer and Course instructor.
I help Companies and Freelancers to easily and efficiently build Computer Vision Software.

Learn to build Computer Vision Software easily and efficiently.
This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.
Sign UP for FREE