liveBook · Manning

Conv2DTranspose

As explained in Deep Learning with R, Third Edition

Conv2DTranspose is a Keras layer that applies a transposed two-dimensional convolution to increase the spatial resolution of feature representations.

Conv2DTranspose is a Keras layer that learns to upsample feature maps, restoring spatial resolution after convolutional layers compress an image. section 11.3.2

Expanding a segmentation feature map

A feature map of shape (25, 25, 256) can pass through corresponding Conv2DTranspose layers before a final Conv2D layer produces a (200, 200, 3) pixel-class output. With strides = 2 and padding = "same", a (50, 50, 128) map can return to (100, 100, 64). section 11.3.2

A segmentation model typically uses Conv2DTranspose layers to:

  • Expand a compact feature map back toward the input image dimensions. section 11.3.2
  • Follow a stack of strided Conv2D layers with corresponding upsampling layers. section 11.5
  • Support per-pixel predictions by feeding the expanded representation to a final Conv2D layer. section 11.3.2
get_model <- function(img_size, num_classes) {

  conv <- function(..., padding = "same", activation = "relu")  #1
    layer_conv_2d(..., padding = padding, activation = activation)

  conv_transpose <- function(..., padding = "same", activation = "relu") 
    layer_conv_2d_transpose(..., padding = padding, activation = activation)

  input <- keras_input(shape = c(img_size, 3))
  output <- input |>
    layer_rescaling(scale = 1/255) |>  #2
    conv(64, 3, strides = 2) |>
    conv(64, 3) |>
    conv(128, 3, strides = 2) |>
    conv(128, 3) |>
    conv(256, 3, strides = 2) |>
    conv(256, 3) |>
    conv_transpose(256, 3) |>
    conv_transpose(256, 3, strides = 2) |>
    conv_transpose(128, 3) |>
    conv_transpose(128, 3, strides = 2) |>
    conv_transpose(64, 3) |>
    conv_transpose(64, 3, strides = 2) |>
    conv(num_classes, 3, activation = "softmax")  #3

  keras_model(input, output)
}

model <- get_model(img_size = img_size, num_classes = 3)
model

Model: "functional"

┌─────────────────────────────────┬────────────────────────┬───────────────┐

│ Layer (type)                    │ Output Shape           │       Param # │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ input_layer (InputLayer)        │ (None, 200, 200, 3)    │             0 │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ rescaling (Rescaling)           │ (None, 200, 200, 3)    │             0 │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d (Conv2D)                 │ (None, 100, 100, 64)   │         1,792 │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_1 (Conv2D)               │ (None, 100, 100, 64)   │        36,928 │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_2 (Conv2D)               │ (None, 50, 50, 128)    │        73,856 │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_3 (Conv2D)               │ (None, 50, 50, 128)    │       147,584 │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_4 (Conv2D)               │ (None, 25, 25, 256)    │       295,168 │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_5 (Conv2D)               │ (None, 25, 25, 256)    │       590,080 │

└─────────────────────────────────┴────────────────────────┴───────────────┘

┌─────────────────────────────────┬────────────────────────┬───────────────┐

│ conv2d_transpose                │ (None, 25, 25, 256)    │       590,080 │

│ (Conv2DTranspose)               │                        │               │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_transpose_1              │ (None, 50, 50, 256)    │       590,080 │

│ (Conv2DTranspose)               │                        │               │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_transpose_2              │ (None, 50, 50, 128)    │       295,040 │

│ (Conv2DTranspose)               │                        │               │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_transpose_3              │ (None, 100, 100, 128)  │       147,584 │

│ (Conv2DTranspose)               │                        │               │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_transpose_4              │ (None, 100, 100, 64)   │        73,792 │

│ (Conv2DTranspose)               │                        │               │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_transpose_5              │ (None, 200, 200, 64)   │        36,928 │

│ (Conv2DTranspose)               │                        │               │

├─────────────────────────────────┼────────────────────────┼───────────────┤

│ conv2d_6 (Conv2D)               │ (None, 200, 200, 3)    │         1,731 │

└─────────────────────────────────┴────────────────────────┴───────────────┘

 Total params: 2,880,643 (10.99 MB)

 Trainable params: 2,880,643 (10.99 MB)

 Non-trainable params: 0 (0.00 B)

Runnable R/Keras model code shows Conv2DTranspose layers alternating ordinary and stride-2 upsampling stages before a softmax segmentation output. section 11.3.2

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains learned upsampling through output-shape examples and a complete layer sequence, without mathematical derivation.
Examples
Uses a R/Keras segmentation model that expands (25, 25, 256) features into (200, 200, 3) pixel-class predictions.
What you can do after
Build a segmentation model in R/Keras by pairing strided Conv2D compression with Conv2DTranspose expansion.
How it connects
Connects Conv2DTranspose with Conv2D, strided convolution, feature maps, softmax, and image segmentation.

Also explained in

Compare how 2 books explain Conv2DTranspose →

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.