as_images_tensor: Convert list of image arrays to a tensor

View source: R/deepCNN.r

as_images_tensorR Documentation

Convert list of image arrays to a tensor

Description

Convert list of image arrays to a tensor

Usage

as_images_tensor(imagelist, height, width, depth = NULL, channels = 3L)

Arguments

imagelist

A list of images returned by as_images_array().

height

The height of an image, equal to the number of rows.

width

The width of an image, equal to the number of columns.

depth

The depth of an 3D image. The default value NULL indicates 2D images.

channels

The number of channels of an image. A color channel is a primary color (like red, green and blue), equal to a color valence (denotes how light effects the color sensation of an eye or in common of the brain). Primary colors can be mixed to produce any color. A channel equal 1 indicates a grayscale image, 3 a color image.

Details

The supported types of images are 2D and 3D images. The resulting tensor has the corresponding shapes:

  • 2D image: samples (number of images), height, width and channels.

  • 3D image: samples (number of images), height, width, depth and channels.

Value

A tensor of corresponding shape depending on the type of images (2D or 3D images).

See Also

Other Convolutional Neural Network (CNN): alexnet(), as_CNN_image_X(), as_CNN_image_Y(), as_CNN_temp_X(), as_CNN_temp_Y(), as_images_array(), images_load(), images_resize(), inception_resnet_v2(), inception_v3(), lenet5(), mobilenet(), mobilenet_v2(), mobilenet_v3(), nasnet(), resnet, unet(), vgg, xception(), zfnet()

Examples

  # Get image file names
  base_dir <- "c:/users/.../images" # any folder where image files are stored
  filelist <- list.files(path = base_dir, pattern = "\\.jpg$", full.names = T) # JPEG images
  # Image dimensions (2D images)
  height   <- 200L
  width    <- 200L
  channels <- 3L

  # with keras (no functions are specified)
  CNN_X <- images_load(filelist, h = height, w = width, ch = channels) %>%
    images_resize() %>%
    as_images_array() %>%
    as_images_tensor(height = height, width = width, channels = channels)

  # with magick
  magick_resize <- function(img, height, width) {
    magick::image_scale(img, magick::geometry_size_pixels(width = width, height = height, preserve_aspect = FALSE))
  }

  magick_array <- function(img, channels) {
    as.integer(magick::image_data(img, channels))
  }

  CNN_X <- images_load(filelist, FUN = magick::image_read) %>%
    images_resize(FUN = magick_resize, h = height, w = width) %>%
    as_images_array(FUN = magick_array, ch = "rgb") %>%
    as_images_tensor(height = height, width = width, channels = channels)

stschn/deepANN documentation built on June 25, 2024, 7:27 a.m.