Convolutional Neural Networks (CNNs) in R (original) (raw)

Last Updated : 11 Mar, 2026

Convolutional Neural Networks (CNNs) are deep learning models designed to analyze structured grid-like data such as images. They learn visual patterns directly from pixel values and identify features like edges, textures, shapes and objects. In R, CNN models can be built using libraries such as Keras and TensorFlow for tasks like image classification and object recognition.

working_of_cnn__

Convolutional Neural Networks

Key Components of Convolutional Neural Networks (CNN)

A Convolutional Neural Network (CNN) is composed of multiple layers, where each layer transforms the input data to extract increasingly complex features. These layers work together to learn patterns from images and perform tasks such as classification or object detection.

1. Input Layer

The input layer receives the raw image data and passes it to the network for further processing.

**Example: For an RGB image of size 32 × 32, the input volume becomes 32 × 32 × 3, where 3 represents the color channels (Red, Green, Blue).

2. Convolutional Layer

The convolutional layer extracts important visual patterns from the input using learnable filters.

**Example: If 12 filters are applied to a 32 × 32 × 3 image, the output feature map volume may become 32 × 32 × 12.

3. Activation Layer

The activation layer introduces non-linearity into the network, allowing it to learn complex patterns.

**Example: Applying ReLU to a 32 × 32 × 12 feature map keeps the output size the same while transforming the values.

4. Pooling Layer

The pooling layer reduces the spatial size of feature maps to make the model more efficient.

working_of_cnn

Max Pooling

**Example: Applying 2 × 2 Max Pooling with stride 2 on 32 × 32 × 12 reduces it to 16 × 16 × 12.

5. Flattening

Flattening converts the multi-dimensional feature maps into a one-dimensional vector.

**Example: Flattening a 16 × 16 × 12 feature map produces a vector of size 3072.

6. Fully Connected Layer

The fully connected layer combines extracted features to perform prediction.

**Example: The 3072-length vector from the flattening layer is connected to neurons that compute classification scores.

7. Dropout Layer

The dropout layer is used as a regularization technique to reduce overfitting during training.

**Example: A dropout rate of 0.5 randomly deactivates 50% of neurons during training to prevent the model from relying too heavily on specific neurons.

8. Output Layer

The output layer produces the final prediction of the CNN model.

**Example: For a 10-class classification problem, the Softmax function outputs 10 probability values, each representing the likelihood of a class.

How CNN Works

A Convolutional Neural Network processes an image through multiple layers to gradually extract features and make a final prediction. The process begins with the raw image as input and passes through convolution, activation, pooling and fully connected layers before producing the final output.

how_cnns_work

CNN working

**Step 1: The CNN receives the raw image as input in the form of a three-dimensional matrix representing width, height and color channels (RGB). This structure stores pixel intensity values while preserving the spatial arrangement of the image for feature learning.

**Step2: The convolution layer extracts important features from the input image using filters (kernels).

178

Convolution Operation

**Step 3: The convolution operation applies filters that move across the image using a defined stride to detect patterns. The output produced is called a feature map and multiple filters are used to capture different features such as edges, textures and shapes.

**Step4: After convolution, an activation function is applied to introduce non-linearity, allowing the model to learn complex patterns from the feature maps. The most commonly used activation function in CNNs is ReLU (Rectified Linear Unit).

**Step 5: Next, a pooling operation is applied the spatial dimensions of the feature maps while retaining important information.

**Step 6: In CNNs, convolution and pooling layers are repeated multiple times to learn deeper and more complex features. Early layers detect simple patterns like edges, while deeper layers capture textures, shapes and complex objects.

**Step 7: Next, flattening converts the multi-dimensional feature maps produced by convolution and pooling layers into a one-dimensional vector.

**Step 8: After flattening, the extracted features are passed to the fully connected layer, which performs high-level reasoning and produces the final prediction through the output layer.

Step By Step Implementation

Here we implement Convolutional Neural Network (CNN) in R using Keras and TensorFlow.

Step 1: Installing the required packages

Install and load the Keras library in R, which provides an interface for building deep learning models. The install_keras() function automatically installs the required TensorFlow backend and configures the Python environment needed to run neural networks.

R `

install.packages("keras") library(keras) install_keras()

library(tensorflow) tf$constant("Hello TensorFlow!")

`

**Output:

tf.Tensor(b'Hello TensorFlow!', shape=(), dtype=string)

Step 2: Loading and Preprocessing Datasets

Here we load the MNIST dataset and separate it into training and testing sets for building and evaluating the CNN model.

Load the keras library

library(keras)

Load the MNIST dataset

mnist <- dataset_mnist()

Split into training and testing datasets

x_train <- mnist$train$x y_train <- mnist$train$y x_test <- mnist$test$x y_test <- mnist$test$y

`

Step 3: Preprocessing the Images

In this step, the images are reshaped to include a single channel (28 × 28 × 1) and normalized to the range [0, 1] to improve CNN training efficiency.

R `

Reshape the images to (28, 28, 1) and normalize pixel values to the range [0, 1]

x_train <- array_reshape(x_train, c(nrow(x_train), 28, 28, 1)) x_test <- array_reshape(x_test, c(nrow(x_test), 28, 28, 1))

x_train <- x_train / 255 x_test <- x_test / 255

`

Step 4: One-Hot Encoding the Labels

Here the class labels are converted into one-hot encoded vectors so the CNN can perform multi-class classification.

one_hot_encode <- function(labels, num_classes) {

Create a matrix of zeros

encoded_labels <- matrix(0, nrow = length(labels), ncol = num_classes)

Set the appropriate index to 1 for each label

for (i in seq_along(labels)) { encoded_labels[i, labels[i] + 1] <- 1 }

return(encoded_labels) }

Apply the custom one-hot encoding function

y_train <- one_hot_encode(y_train, 10) y_test <- one_hot_encode(y_test, 10)

`

Step 5: Verifying Data Structure

We check the dimensions and structure of the preprocessed images and one-hot encoded labels to ensure they are ready for training the CNN.

R `

Check dimensions and type of data

str(x_train) str(y_train)

`

**Output:

num [1:60000, 1:28, 1:28, 1] 0 0 0 0 0 0 0 0 0 0 ...

num [1:60000, 1:10] 0 1 0 0 0 0 0 0 0 0 ...

Step 6: Building the CNN Model

Define the CNN architecture by stacking convolutional, pooling, flattening and fully connected layers to extract features and perform classification.

Initialize the model

model <- keras_model_sequential()

Add convolutional layers

model %>% layer_conv_2d(filters = 32, kernel_size = c(3, 3), activation = 'relu', input_shape = c(28, 28, 1)) %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>% layer_conv_2d(filters = 64, kernel_size = c(3, 3), activation = 'relu') %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>% layer_conv_2d(filters = 128, kernel_size = c(3, 3), activation = 'relu') %>% layer_max_pooling_2d(pool_size = c(2, 2)) %>%

Flatten the output from convolutional layers

layer_flatten() %>%

Add fully connected layers

layer_dense(units = 128, activation = 'relu') %>% layer_dropout(rate = 0.5) %>% layer_dense(units = 10, activation = 'softmax') # Output layer for 10 classes

`

Step 7: Compiling and Training the CNN

Here we compile the CNN with an optimizer, loss function and metrics, then train it on the dataset while monitoring validation performance.

model %>% compile( optimizer = optimizer_adam(), loss = 'categorical_crossentropy', metrics = c('accuracy') )

history <- model %>% fit( x_train, y_train, epochs = 10, batch_size = 64, validation_split = 0.2 )

`

Step 8: Evaluating the CNN Model

Trained CNN is evaluated on the test dataset to measure its final performance in terms of loss and accuracy on unseen data.

R `

score <- model %>% evaluate(x_test, y_test) print(score)

Print evaluation results

cat('Test loss:', score$loss, '\n') cat('Test accuracy:', score$accuracy, '\n')

`

**Output:

loss accuracy

0.05012181 0.98710001

Test loss: 0.05012181

Test accuracy: 0.9871

Step 9: Visualizing Training History

Here we plot the training and validation accuracy and loss over epochs to monitor the model’s learning progress and detect overfitting or underfitting.

R `

Plot training & validation accuracy values

plot(history$metrics$accuracy, type = 'l', col = 'blue', ylim = c(0, 1), xlab = 'Epoch', ylab = 'Accuracy', main = 'Model Accuracy') lines(history$metrics$val_accuracy, type = 'l', col = 'red') legend("bottomright", legend = c("Training Accuracy", "Validation Accuracy"), col = c("blue", "red"), lty = 1)

Plot training & validation loss values

plot(history$metrics$loss, type = 'l', col = 'blue', ylim = c(0, max(history$metrics$loss, history$metrics$val_loss)), xlab = 'Epoch', ylab = 'Loss', main = 'Model Loss') lines(history$metrics$val_loss, type = 'l', col = 'red') legend("topright", legend = c("Training Loss", "Validation Loss"), col = c("blue", "red"), lty = 1)

`

**Output:

Download full code from here

Applications

Advantages

Limitations