liveBook · Manning

Convolutional neural network

As explained in Deep Learning with R, Third Edition

A convolutional neural network is a neural network that applies convolution-based operations to learn spatial or locally structured features, especially from grid-like data such as images.

A convolutional neural network (convnet or CNN) learns to recognize images from local patterns. Rather than connecting every pixel to a layer that learns patterns across the whole image, convolution layers look at small windows. A pattern learned in one location can then be recognized elsewhere. section 8.1.1

Classify handwritten digits

For 28 × 28 grayscale MNIST images, start with an input shape of (28, 28, 1). Stack Conv2D and MaxPooling2D layers, pool the final feature maps, and use a 10-unit softmax layer to predict the digit. Reshape images to include their channel and divide pixel values by 255 before training. This small model reaches 99.31% test accuracy in the reported run, compared with 97.8% for an earlier densely connected model. section 8.1

Listing 8.1 Instantiating a small convnet
library(keras3)

inputs <- keras_input(shape = c(28, 28, 1))
outputs <- inputs |>
  layer_conv_2d(filters = 64, kernel_size = 3, activation = "relu") |>
  layer_max_pooling_2d(pool_size = 2) |>
  layer_conv_2d(filters = 128, kernel_size = 3, activation = "relu") |>
  layer_max_pooling_2d(pool_size = 2) |>
  layer_conv_2d(filters = 256, kernel_size = 3, activation = "relu") |>
  layer_global_average_pooling_2d() |>
  layer_dense(10, activation = "softmax")

model <- keras_model(inputs = inputs, outputs = outputs)

The R/Keras model definition shows the full path from image input through alternating convolution and pooling layers to a softmax classifier. section 8.1

Each convolution applies the same learned transformation to patches of an input feature map. A feature map has height, width, and channels; its output channels indicate learned patterns rather than image colors. Early layers can detect edges and textures, while later layers combine simpler features into more abstract ones. As a typical convnet gets deeper, its feature maps become smaller across the image and deeper across channels. section 8.1.1 section 8.2.3

For a harder dogs-versus-cats task, a larger convnet starts with 180 × 180 color images and adds more convolution-and-pooling stages. It rescales pixels, then ends with one sigmoid output for binary classification. With only 2,000 training images, training from scratch establishes a useful baseline, but overfitting becomes the main obstacle. Data augmentation helps; feature extraction and fine-tuning of a pretrained convnet offer further options for small datasets. section 8.2 section 8.2.3

Listing 8.7 Instantiating a small convnet for dogs vs. cats classification
inputs <- keras_input(shape = c(180, 180, 3))  #1
outputs <- inputs |>
  layer_rescaling(1 / 255) |>  #2
  layer_conv_2d(filters = 32, kernel_size = 3, activation = "relu") |>
  layer_max_pooling_2d(pool_size = 2) |>
  layer_conv_2d(filters = 64, kernel_size = 3, activation = "relu") |>
  layer_max_pooling_2d(pool_size = 2) |>
  layer_conv_2d(filters = 128, kernel_size = 3, activation = "relu") |>
  layer_max_pooling_2d(pool_size = 2) |>
  layer_conv_2d(filters = 256, kernel_size = 3, activation = "relu") |>
  layer_max_pooling_2d(pool_size = 2) |>
  layer_conv_2d(filters = 512, kernel_size = 3, activation = "relu") |>
  layer_global_average_pooling_2d() |>  #3
  layer_dense(1, activation = "sigmoid")

model <- keras_model(inputs, outputs)

The larger R/Keras model makes the changes for color images and binary classification concrete. section 8.2.3

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through model construction, training and evaluation, then explains local convolutions and feature maps without a mathematical derivation.
Examples
Builds a MNIST digit classifier and a larger dogs-versus-cats classifier with R/Keras model listings.
What it assumes
Assumes some familiarity with Dense layers and the Functional API while introducing convolution through a working model.
What you can do after
Build it from scratch with R/Keras for image classification and evaluate when small-data overfitting calls for other strategies.
How it connects
Connects convolution to translation invariance, spatial hierarchies, pooling, data augmentation, feature extraction and fine-tuning.

Also explained in

Compare how 18 books explain convolutional neural network →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.