Flower Recognition Using Convolutional Neural Network
Last Updated : 23 Jul, 2025
Convolutional Neural Network (CNN) are a type of deep learning model specifically designed for processing structured grid data such as images. In this article we will build a CNN model to classify different types of flowers from a dataset containing images of various flowers like roses, daisies, dandelions, sunflowers and tulips. This project demonstrates how CNNs can be applied to solve a supervised image classification problem.
1. Importing modules
For this project we will be using:
Pandas β This library helps to load the data frame in a 2D array format.
Numpy β Numpy arrays are very fast and can perform large computations.
Matplotlibβ This library is used to draw visualizations.
OpenCV β This library focuses on image processing and handling.
Tensorflow β It has a range of functions to achieve complex functionalities with single lines of code.
keras: A deep learning library for building and training neural networks offering high-level APIs for ease of use.
zipfile: A module for reading and writing ZIP files, useful for handling compressed datasets.
os: Provides a way to interact with the operating system, such as managing file paths and directories.
2. Importing Dataset and Preprocessing
You can download flowers dataset containing images of various flowers from kaggle. After downloading the dataset the images need to be resized to fit the input size of the model i.e 224x224 pixels in this case.
base_dir: The directory where the unzipped contents will be stored.
os.makedirs(base_dir, exist_ok=True): Creates the flowers directory if it doesn't already exist.
(exist_ok=True): If the directory exists it will not raise an error
zip_ref.extractall(base_dir): Extracts all the files from the ZIP file and places them in the flowers directory.
img_size: Defines the target size for resizing the images.
batch: The batch size or the number of images processed in one go during training.
3. Image Data Generator
Next we will use an ImageDataGenerator to apply data augmentation and split the dataset into training and validation sets:
ImageDataGenerator: is used to preprocess the images and apply real-time data augmentation to the training set.
rescale: Rescales pixel values to the range [0, 1].
shear_range, zoom_range, horizontal_flip: Apply random transformations to the images to increase variety in the dataset and help the model generalize better.
validation_split: Used to separate a portion of the data for validation.
flow_from_directory: reads the images from the directory and prepares batches of images for training and validation.
Output :
Found 3454 images belonging to 1 classes. Found 863 images belonging to 1 classes.
4. Model Development
We will now define our CNN model architecture using the Keras as it contains all the functionalities that one may need to define the architecture of a Convolutional Neural Network and train it on the data.
Conv2D: Convolutional layers are used to detect features in the image. Each layer helps the model learn more complex features as the depth increases.
MaxPooling2D: Reduces the spatial dimensions of the feature maps.
Flatten: Converts the 2D feature maps into a 1D vector to be input into the fully connected layers.
Dense: Fully connected layers that help the model make predictions.
Activation('relu'): Rectified Linear Unit (ReLU) activation function introduces non-linearity.
softmax: The final activation function used in multi-class classification problems to output probabilities for each class.
model.summary(): Prints a detailed summary of the model architecture, including the number of layers, their types, output shapes and the total number of trainable parameters.
This code visualizes the CNN model's structure providing a clear and detailed view of its architecture including layer types, output shapes and activations.
keras.utils.plot_model: This function generates a graphical representation of the model architecture.
show_shapes=True: Displays the shape of the output at each layer helping to understand how the data dimensions change as it passes through the network.
show_dtype=True: Shows the data type of the tensors at each layer providing additional insight into the modelβs structure.
show_layer_activations=True: Displays the activation function used in each layer making it easier to understand how each layer processes the input data.
It starts with multiple Conv2D layers with ReLU activation followed by MaxPooling2D layers that progressively reduce the spatial dimensions of the input image. After several convolutional and pooling layers the feature maps are flattened into a 1D vector using the Flatten layer. The model then passes through Dense layers with the final layer using softmax activation to classify the input into one of five categories of flowers. Each layer's input and output shapes are displayed helping to understand the flow of data and transformations at each step.
We can see that our model is working fine and making accurate prediction. With this simple yet effective CNN model we have successfully built a flower recognition system that can classify images into five different flower types. By using convolutional layers and data augmentation weβve created a model that can generalize well to new images. You can experiment with other hyperparameters, network architectures and datasets to further improve the model's performance.