scico.flax.examples.data_preprocessing#

Image manipulation utils.

Functions

build_blur_kernel(kernel_size, blur_sigma[, ...])

Construct a blur kernel as specified.

build_image_dataset(imgs_train, imgs_test, ...)

Preprocess and assemble dataset for training.

flip(img)

Horizontal flip of an image or a batch of images.

get_bsds_data(path[, verbose])

Download BSDS500 data from the BSDB project.

images_read(path[, ext])

Read a collection of color images from a set of files.

preprocess_images(images, output_size[, ...])

Preprocess (scale, crop, etc.) set of images.

rotation90(img)

Rotate an image, or a batch of images, by 90 degrees.

Classes

CenterCrop(output_size)

Crop central part of an image to a specified size.

PaddedCircularConvolve(output_size, ...[, dtype])

Define padded convolutional operator.

PositionalCrop(output_size)

Crop an image from a given corner to a specified size.

RandomNoise(noise_level[, range_flag])

Add Gaussian noise to an image or a batch of images.

scico.flax.examples.data_preprocessing.rotation90(img)[source]#

Rotate an image, or a batch of images, by 90 degrees.

Rotate an image or a batch of images by 90 degrees counterclockwise. An image is an array with size H x W x C with H and W spatial dimensions and C number of channels. A batch of images is an array with size N x H x W x C with N number of images.

Parameters:

img (Array) – The array to be rotated.

Return type:

Array

Returns:

An image, or batch of images, rotated by 90 degrees counterclockwise.

scico.flax.examples.data_preprocessing.flip(img)[source]#

Horizontal flip of an image or a batch of images.

Horizontally flip an image or a batch of images. An image is an array with size H x W x C with H and W spatial dimensions and C number of channels. A batch of images is an array with size N x H x W x C with N number of images.

Parameters:

img (Array) – The array to be flipped.

Return type:

Array

Returns:

An image, or batch of images, flipped horizontally.

class scico.flax.examples.data_preprocessing.CenterCrop(output_size)[source]#

Bases: object

Crop central part of an image to a specified size.

Crop central part of an image. An image is an array with size H x W x C with H and W spatial dimensions and C number of channels.

Parameters:

output_size (Union[Tuple[int, ...], int]) – Desired output size. If int, square crop is made.

__call__(image)[source]#

Apply center crop.

Parameters:

image (Array) – The array to be cropped.

Return type:

Array

Returns:

The cropped image.

class scico.flax.examples.data_preprocessing.PositionalCrop(output_size)[source]#

Bases: object

Crop an image from a given corner to a specified size.

Crop an image from a given corner. An image is an array with size H x W x C with H and W spatial dimensions and C number of channels.

Parameters:

output_size (Union[Tuple[int, ...], int]) – Desired output size. If int, square crop is made.

__call__(image, top, left)[source]#

Apply positional crop.

Parameters:
  • image (Array) – The array to be cropped.

  • top (int) – Vertical top coordinate of corner to start cropping.

  • left (int) – Horizontal left coordinate of corner to start cropping.

Return type:

Array

Returns:

The cropped image.

class scico.flax.examples.data_preprocessing.RandomNoise(noise_level, range_flag=False)[source]#

Bases: object

Add Gaussian noise to an image or a batch of images.

Add Gaussian noise to an image or a batch of images. An image is an array with size H x W x C with H and W spatial dimensions and C number of channels. A batch of images is an array with size N x H x W x C with N number of images. The Gaussian noise is a Gaussian random variable with mean zero and given standard deviation. The standard deviation can be a fix value corresponding to the specified noise level or randomly selected on a range between 50% and 100% of the specified noise level.

Parameters:
  • noise_level (float) – Standard dev of the Gaussian noise.

  • range_flag (bool) – If True, the standard dev is randomly selected between 50% and 100% of noise_level set. Default: False.

__call__(image)[source]#

Add Gaussian noise.

Parameters:

image (Array) – The array to add noise to.

Return type:

Array

Returns:

The noisy image.

scico.flax.examples.data_preprocessing.preprocess_images(images, output_size, gray_flag=False, num_img=None, multi_flag=False, stride=None, dtype=<class 'numpy.float32'>)[source]#

Preprocess (scale, crop, etc.) set of images.

Preprocess set of images, converting to gray scale, or cropping or sampling multiple patches from each one, or selecting a subset of them, according to specified setup.

Parameters:
  • images (Array) – Array of color images.

  • output_size (Union[Tuple[int, ...], int]) – Desired output size. If int, square crop is made.

  • gray_flag (bool) – If True, converts to gray scale.

  • num_img (Optional[int]) – If specified, reads that number of images, if not reads all the images in path.

  • multi_flag (bool) – If True, samples multiple patches of specified size in each image.

  • stride (Union[Tuple[int, ...], int, None]) – Stride between patch origins (indexed from left-top corner). If int, the same stride is used in h and w.

  • dtype (Any) – dtype of array. Default: float32.

Return type:

Array

Returns:

Preprocessed array.

scico.flax.examples.data_preprocessing.build_image_dataset(imgs_train, imgs_test, config, transf=None)[source]#

Preprocess and assemble dataset for training.

Preprocess images according to the specified configuration and assemble a dataset into a structure that can be used for training machine learning models. Keep training and testing partitions. Each dictionary returned has images and labels, which are arrays of dimensions (N, H, W, C) with N: number of images; H, W: spatial dimensions and C: number of channels.

Parameters:
  • imgs_train – 4D array (NHWC) with images for training.

  • imgs_test – 4D array (NHWC) with images for testing.

  • config (ConfigImageSetDict) – Configuration of image data set to read.

  • transf (Optional[Callable]) – Operator for blurring or other non-trivial transformations. Default: None.

Return type:

Tuple[DataSetDict, ...]

Returns:

tuple

A tuple (train_ds, test_ds) containing:

  • train_ds : Dictionary of training data (includes images and labels).

  • test_ds : Dictionary of testing data (includes images and labels).

scico.flax.examples.data_preprocessing.images_read(path, ext='jpg')[source]#

Read a collection of color images from a set of files.

Read a collection of color images from a set of files in the specified directory. All files with extension ext (i.e. matching glob *.ext) in directory path are assumed to be image files and are read. Images may have different aspect ratios, therefore, they are transposed to keep the aspect ratio of the first image read.

Parameters:
  • path (str) – Path to directory containing the image files.

  • ext (str) – Filename extension.

Return type:

Array

Returns:

Collection of color images as a 4D array.

scico.flax.examples.data_preprocessing.get_bsds_data(path, verbose=False)[source]#

Download BSDS500 data from the BSDB project.

Download the BSDS500 dataset, a set of 500 color images of size 481x321 or 321x481, from the Berkeley Segmentation Dataset and Benchmark project.

The downloaded data is converted to .npz format for convenient access via numpy.load. The converted data is saved in a file bsds500.npz in the directory specified by path. Note that train and test folders are merged to get a set of 400 images for training while the val folder is reserved as a set of 100 images for testing. This is done in multiple works such as [58].

Parameters:
  • path (str) – Directory in which converted data is saved.

  • verbose (bool) – Flag indicating whether to print status messages.

scico.flax.examples.data_preprocessing.build_blur_kernel(kernel_size, blur_sigma, dtype=<class 'numpy.float32'>)[source]#

Construct a blur kernel as specified.

Parameters:
  • kernel_size (Tuple[int, ...]) – Size of the blur kernel.

  • blur_sigma (float) – Standard deviation of the blur kernel.

  • dtype (Any) – Output dtype. Default: float32.

class scico.flax.examples.data_preprocessing.PaddedCircularConvolve(output_size, channels, kernel_size, blur_sigma, dtype=<class 'numpy.float32'>)[source]#

Bases: LinearOperator

Define padded convolutional operator.

Inheritance diagram of PaddedCircularConvolve

The operator pads the signal with a reflection of the borders before convolving with the kernel provided at initialization. It crops the result of the convolution to maintain the same signal size.

Parameters:
  • output_size (Union[Tuple[int, ...], int]) – Size of the image to blur.

  • channels (int) – Number of channels in image to blur.

  • kernel_size (Union[Tuple[int, ...], int]) – Size of the blur kernel.

  • blur_sigma (float) – Standard deviation of the blur kernel.

  • dtype (Any) – Output dtype. Default: float32.