API - Vision

Vision Transforms list

ToTensor([data_format])

Convert a PIL Image or numpy.ndarray to tensor.

Compose(transforms)

Composes several transforms together.

Crop(top, left, height, width)

Crops an image to a specified bounding box.

CentralCrop([size, central_fraction])

Crops the given image at the center.If the size is given, image will be cropped as size.

RandomCrop(size[, padding, pad_if_needed, …])

Crop the given image at a random location.

Pad(padding[, padding_value, mode])

Pad the given image on all sides with the given “pad” value.

PadToBoundingbox(top, left, height, width[, …])

Pad image with the specified height and width to target size.

Resize(size[, interpolation])

Resize the input image to the given size.

RandomResizedCrop(size[, scale, ratio, …])

Crop the given image to random size and aspect ratio.

RgbToGray([num_output_channels])

Converts a image from RGB to grayscale.

HsvToRgb

Converts a image from HSV to RGB.

RgbToHsv

Converts a image from RGB to HSV.

AdjustBrightness([brightness_factor])

Adjust brightness of the image.

AdjustContrast([contrast_factor])

Adjust contrast of the image.

AdjustHue([hue_factor])

Adjust hue of the image.

AdjustSaturation([saturation_factor])

Adjust saturation of the image.

RandomBrightness([brightness_factor])

Random adjust brightness of the image.

RandomContrast([contrast_factor])

Random adjust contrast of the image.

RandomHue([hue_factor])

Random adjust hue of the image.

RandomSaturation([saturation_factor])

Random adjust saturation of the image.

ColorJitter([brightness, contrast, …])

Randomly change the brightness, contrast, saturation and hue of an image.

FlipHorizontal

Flip an image horizontally.

FlipVertical

Flip an image vertically.

RandomFlipHorizontal([prob])

Horizontally flip the given image randomly with a given probability.

RandomFlipVertical([prob])

Vertically flip the given image randomly with a given probability.

Rotation([angle, interpolation, expand, …])

Rotate the image by angle.

RandomRotation(degrees[, interpolation, …])

Rotate the image by random angle.

RandomShift(shift[, interpolation, fill])

Shift the image by random translations.

RandomShear(shear[, interpolation, fill])

Shear the image by random angle.

RandomZoom(zoom[, interpolation, fill])

Zoom the image by random scale.

RandomAffine(degrees[, shift, zoom, shear, …])

Random affine transformation of the image keeping center invariant.

Transpose(order)

Transpose image(s) by swapping dimension.

HWC2CHW

Transpose a image shape (H, W, C) to shape (C, H, W).

CHW2HWC

Transpose a image shape (C, H, W) to shape (H, W, C).

Normalize(mean, std[, data_format])

Normalize a tensor image with mean and standard deviation.

StandardizePerImage

For each 3-D image x in image, computes (x - mean) / adjusted_stddev, where mean is the average of all values in x.

Vision IO list

load_image(path)

Load an image

save_image(image, file_name, path)

Save an image

load_images(path[, n_threads])

Load images from file

save_images(images, file_names, path)

Save images

Vision Transforms

ToTensor

class tensorlayerx.vision.transforms.ToTensor(data_format='HWC')[source]

Convert a PIL Image or numpy.ndarray to tensor.

Parameters

data_format (str) – Data format of output tensor, should be ‘HWC’ or ‘CHW’. Default: ‘HWC’.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.ToTensor(data_format='HWC')
>>> image = transform(image)
>>> print(image)

Compose

class tensorlayerx.vision.transforms.Compose(transforms)[source]

Composes several transforms together.

Parameters

transforms (list of 'transform' objects) – list of transforms to compose.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.Compose([tlx.vision.transforms.ToTensor(data_format='HWC'),tlx.vision.transforms.CentralCrop(size = 100)])
>>> image = transform(image)
>>> print(image)
>>> image shape : (100, 100, 3)

Crop

class tensorlayerx.vision.transforms.Crop(top, left, height, width)[source]

Crops an image to a specified bounding box.

Parameters
  • top (int) – Vertical coordinate of the top-left corner of the bounding box in image.

  • left (int) – Horizontal coordinate of the top-left corner of the bounding box in image.

  • height (int) – Height of the bounding box.

  • width (int) – Width of the bounding box.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.Crop(top=10, left=10, height=100, width=100)
>>> image = transform(image)
>>> print(image)
>>> image shape : (100, 100, 3)

CentralCrop

class tensorlayerx.vision.transforms.CentralCrop(size=None, central_fraction=None)[source]

Crops the given image at the center.If the size is given, image will be cropped as size. If the central_fraction is given, image will cropped as (H * central_fraction, W * fraction). Size has a higher priority.

Parameters
  • size (int or sequence of int) –

    • The output size of the cropped image.

    • If size is an integer, a square crop of size (size, size) is returned.

    • If size is a sequence of length 2, it should be (height, width).

  • central_fraction (float) – float (0, 1], fraction of size to crop

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.CentralCrop(size = (50, 50))
>>> image = transform(image)
>>> print(image)
>>> image shape : (50, 50, 3)
>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.CentralCrop(central_fraction=0.5)
>>> image = transform(image)
>>> print(image)
>>> image shape : (112, 112, 3)

RandomCrop

class tensorlayerx.vision.transforms.RandomCrop(size, padding=None, pad_if_needed=False, fill=0, padding_mode='constant')[source]

Crop the given image at a random location.

Parameters
  • size (int or sequence) –

    • Desired output size of the crop.

    • If size is an int instead of sequence like (h, w), a square crop (size, size) is made.

    • If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

  • padding (int or sequence, optional) –

    • Optional padding on each border of the image.

    • If a single int is provided this is used to pad all borders.

    • If sequence of length 2 is provided this is the padding on left/right and top/bottom respectively.

    • If a sequence of length 4 is provided, it is used to pad left, top, right, bottom borders respectively.

    • Default: 0.

  • pad_if_needed (boolean) – It will pad the image if smaller than the desired size to avoid raising an exception. Since cropping is done after padding, the padding seems to be done at a random offset.

  • fill (number or sequence) – Pixel fill value for constant fill. Default is 0. If a tuple of length 3, it is used to fill R, G, B channels respectively.

  • padding_mode (str) – Type of padding. Default is “constant”.”constant”, “reflect”, “symmetric” are supported.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomCrop(size=50, padding=10, pad_if_needed=False, fill=0, padding_mode='constant')
>>> image = transform(image)
>>> print(image)
>>> image shape : (70,70,3)

Pad

class tensorlayerx.vision.transforms.Pad(padding, padding_value=0, mode='constant')[source]

Pad the given image on all sides with the given “pad” value.

Parameters
  • padding (int or sequenece) –

    • Padding on each border.

    • If a single int is provided, this is used to pad all borders.

    • If sequence of length 2 is provided, this is the padding on left/right and top/bottom respectively.

    • If a sequence of length 4 is provided, this is the padding for the left, top, right and bottom borders respectively.

  • padding_value (number or sequenece) – Pixel fill value for constant fill. Default is 0. If a tuple or list of length 3, it is used to fill R, G, B channels respectively. tuple and list only is supported for PIL Image. This value is only used when the mode is constant.

  • mode (str) – Type of padding. Default is “constant”.”constant”, “reflect”, “symmetric” , “edge” are supported.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.Pad(padding=10, padding_value=0, mode='constant')
>>> image = transform(image)
>>> print(image)
>>> image shape : (244, 244, 3)

PadToBoundingbox

class tensorlayerx.vision.transforms.PadToBoundingbox(top, left, height, width, padding_value=0)[source]

Pad image with the specified height and width to target size.

Parameters
  • top (int) – Number of rows to add on top.

  • left (int) – Number of columns to add on the left.

  • height (int) – Height of output image.

  • width (int) – Width of output image.

  • padding_value (int or sequence) – value to pad.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand( 224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.PadToBoundingbox(top=10, left=10, height=300, width=300, padding_value=0)
>>> image = transform(image)
>>> print(image)
>>> image shape : (300, 300, 3)

Resize

class tensorlayerx.vision.transforms.Resize(size, interpolation='bilinear')[source]

Resize the input image to the given size.

Parameters
  • size (int or sequenece) –

    • Desired output size.

    • If size is a sequence like (h, w), output size will be matched to this.

    • If size is an int, smaller edge of the image will be matched to this number.

    • i.e, if height > width, then image will be rescaled to (size * height / width, size).

  • interpolation (str) – Interpolation method. Default: ‘bilinear’. ‘nearest’, ‘bilinear’, ‘bicubic’, ‘area’ and ‘lanczos’ are supported.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.Resize(size = (100,100), interpolation='bilinear')
>>> image = transform(image)
>>> print(image)
>>> image shape : (100, 100, 3)

RandomResizedCrop

class tensorlayerx.vision.transforms.RandomResizedCrop(size, scale=(0.08, 1.0), ratio=(0.75, 1.3333333333333333), interpolation='bilinear')[source]

Crop the given image to random size and aspect ratio.

Parameters
  • size (int or sequence) –

    • Desired output size of the crop.

    • If size is an int instead of sequence like (h, w), a square crop (size, size) is made.

    • If provided a sequence of length 1, it will be interpreted as (size[0], size[0]).

  • scale (tuple of float) – scale range of the cropped image before resizing, relatively to the origin image.

  • ratio (tuple of float) – aspect ratio range of the cropped image before resizing.

  • interpolation (str) – Type of interpolation. Default is “bilinear”.”nearest”,”bilinear” and “bicubic” are supported.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomResizedCrop(size = (100, 100), scale = (0.08, 1.0), ratio = (3./4.,4./3.), interpolation = 'bilinear')
>>> image = transform(image)
>>> print(image)
>>> image shape : (100,100,3)

RgbToGray

class tensorlayerx.vision.transforms.RgbToGray(num_output_channels=1)[source]

Converts a image from RGB to grayscale.

Parameters

num_output_channels (int) – (1 or 3) number of channels desired for output image. Default is 1.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RgbToGray(num_output_channels=1)
>>> image = transform(image)
>>> print(image)
>>> image shape : (224, 224, 1)

HsvToRgb

class tensorlayerx.vision.transforms.HsvToRgb[source]

Converts a image from HSV to RGB.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.HsvToRgb()
>>> image = transform(image)
>>> print(image)
>>> image shape : (224, 224, 3)

RgbToHsv

class tensorlayerx.vision.transforms.RgbToHsv[source]

Converts a image from RGB to HSV.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RgbToHsv()
>>> image = transform(image)
>>> print(image)
>>> image shape : (224, 224, 3)

AdjustBrightness

class tensorlayerx.vision.transforms.AdjustBrightness(brightness_factor=1)[source]

Adjust brightness of the image.

Parameters

brightness_factor (float) – How much to adjust the brightness. Can be any non negative number. 1 gives the original image. Default is 1.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.AdjustBrightness(brightness_factor=1)
>>> image = transform(image)
>>> print(image)

AdjustContrast

class tensorlayerx.vision.transforms.AdjustContrast(contrast_factor=1)[source]

Adjust contrast of the image.

Parameters

contrast_factor (float) – How much to adjust the contrast. Can be any non negative number. 1 gives the original image. Default is 1.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.AdjustContrast(contrast_factor=1)
>>> image = transform(image)
>>> print(image)

AdjustHue

class tensorlayerx.vision.transforms.AdjustHue(hue_factor=0)[source]

Adjust hue of the image.

Parameters

hue_factor (float) – How much to shift the hue channel. Should be in [-0.5, 0.5]. 0.5 and -0.5 give complete reversal of hue channel in HSV space in positive and negative direction respectively. 0 means no shift. Therefore, both -0.5 and 0.5 will give an image with complementary colors while 0 gives the original image. Default is 0.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.AdjustHue(hue_factor=0)
>>> image = transform(image)
>>> print(image)

AdjustSaturation

class tensorlayerx.vision.transforms.AdjustSaturation(saturation_factor=1)[source]

Adjust saturation of the image.

Parameters

saturation_factor (float) – How much to adjust the saturation. Can be any non negative number. 1 gives the original image. Default is 1.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.AdjustSaturation(saturation_factor=1)
>>> image = transform(image)
>>> print(image)

RandomBrightness

class tensorlayerx.vision.transforms.RandomBrightness(brightness_factor=(1, 1))[source]

Random adjust brightness of the image.

Parameters

brightness_factor (float or sequence) –

  • Brightness adjustment factor (default=(1, 1)).

  • If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness_factor), 1+brightness_factor].

  • If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomBrightness(brightness_factor=(0.5, 2))
>>> image = transform(image)
>>> print(image)

RandomContrast

class tensorlayerx.vision.transforms.RandomContrast(contrast_factor=(1, 1))[source]

Random adjust contrast of the image.

Parameters

contrast_factor (float or sequence) –

  • Contrast adjustment factor (default=(1, 1)).

  • If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast_factor), 1+contrast_factor].

  • If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomContrast(contrast_factor=(0.5, 2))
>>> image = transform(image)
>>> print(image)

RandomHue

class tensorlayerx.vision.transforms.RandomHue(hue_factor=(0, 0))[source]

Random adjust hue of the image.

Parameters

hue_factor (float or sequence) –

  • Hue adjustment factor (default=(0, 0)).

  • If it is a float, the factor is uniformly chosen from the range [-hue_factor, hue_factor].

  • If it is a sequence, it should be [min, max] for the range.Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomHue(hue_factor=(-0.5, 0.5))
>>> image = transform(image)
>>> print(image)

RandomSaturation

class tensorlayerx.vision.transforms.RandomSaturation(saturation_factor=(1, 1))[source]

Random adjust saturation of the image.

Parameters

saturation_factor (float or sequence) –

  • Saturation adjustment factor (default=(1, 1)).

  • If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation_factor), 1+saturation_factor].

  • If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomSaturation(saturation_factor=(0.5, 2))
>>> image = transform(image)
>>> print(image)

ColorJitter

class tensorlayerx.vision.transforms.ColorJitter(brightness=0, contrast=0, saturation=0, hue=0)[source]

Randomly change the brightness, contrast, saturation and hue of an image.

Parameters
  • brightness (float or sequence) –

    • Brightness adjustment factor (default=(1, 1)).

    • If it is a float, the factor is uniformly chosen from the range [max(0, 1-brightness_factor), 1+brightness_factor].

    • If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.

  • contrast (float or sequence) –

    • Contrast adjustment factor (default=(1, 1)).

    • If it is a float, the factor is uniformly chosen from the range [max(0, 1-contrast_factor), 1+contrast_factor].

    • If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.

  • saturation (float or sequence) –

    • Saturation adjustment factor (default=(1, 1)).

    • If it is a float, the factor is uniformly chosen from the range [max(0, 1-saturation_factor), 1+saturation_factor].

    • If it is a sequence, it should be [min, max] for the range.Should be non negative numbers.

  • hue (float or sequence) –

    • Hue adjustment factor (default=(0, 0)).

    • If it is a float, the factor is uniformly chosen from the range [-hue_factor, hue_factor].

    • If it is a sequence, it should be [min, max] for the range.Should have 0<= hue <= 0.5 or -0.5 <= min <= max <= 0.5.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.ColorJitter(brightness=(1,5), contrast=(1,5), saturation=(1,5), hue=(-0.2,0.2))
>>> image = transform(image)
>>> print(image)

FlipHorizontal

class tensorlayerx.vision.transforms.FlipHorizontal[source]

Flip an image horizontally.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.FlipHorizontal()
>>> image = transform(image)
>>> print(image)

FlipVertical

class tensorlayerx.vision.transforms.FlipVertical[source]

Flip an image vertically.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.FlipVertical()
>>> image = transform(image)
>>> print(image)

RandomFlipHorizontal

class tensorlayerx.vision.transforms.RandomFlipHorizontal(prob=0.5)[source]

Horizontally flip the given image randomly with a given probability.

Parameters

prob (float) – probability of the image being flipped. Default value is 0.5

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomFlipHorizontal(prob = 0.5)
>>> image = transform(image)
>>> print(image)

RandomFlipVertical

class tensorlayerx.vision.transforms.RandomFlipVertical(prob=0.5)[source]

Vertically flip the given image randomly with a given probability.

Parameters

prob (float) – probability of the image being flipped. Default value is 0.5

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomFlipVertical(prob = 0.5)
>>> image = transform(image)
>>> print(image)

Rotation

class tensorlayerx.vision.transforms.Rotation(angle=0, interpolation='bilinear', expand=False, center=None, fill=0)[source]

Rotate the image by angle.

Parameters
  • degrees (number) – degrees to rotate.

  • interpolation (str) – Interpolation method. Default is ‘bilinear’. ‘nearest’,’bilinear’ are supported.

  • expand (boolean) –

    • If true, expands the output to make it large enough to hold the entire rotated image.

    • If false or omitted, make the output image the same size as the input image.

    • Note that the expand flag assumes rotation around the center and no translation.

  • center (sequence or None) – Optional center of rotation, (x, y). Origin is the upper left corner. Default is the center of the image.

  • fill (number or sequence) – Pixel fill value for the area outside the rotated image. Default is 0.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.Rotation(angle = 0, interpolation = 'bilinear', expand = False, center = None, fill = 0)
>>> image = transform(image)
>>> print(image)

RandomRotation

class tensorlayerx.vision.transforms.RandomRotation(degrees, interpolation='bilinear', expand=False, center=None, fill=0)[source]

Rotate the image by random angle.

Parameters
  • degrees (number or sequnence) –

    • Range of degrees to select from.

    • If degrees is a number, the range of degrees will be (-degrees, +degrees).

    • If degrees is a sequence, the range of degrees will (degrees[0], degrees[1]).

  • interpolation (str) – Interpolation method. Default is ‘bilinear’. ‘nearest’,’bilinear’ are supported.

  • expand (boolean) –

    • If true, expands the output to make it large enough to hold the entire rotated image.

    • If false or omitted, make the output image the same size as the input image.

    • Note that the expand flag assumes rotation around the center and no translation.

  • center (sequence or None) – Optional center of rotation, (x, y). Origin is the upper left corner. Default is the center of the image.

  • fill (number or sequence) – Pixel fill value for the area outside the rotated image. Default is 0.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomRotation(degrees=30, interpolation='bilinear', expand=False, center=None, fill=0)
>>> image = transform(image)
>>> print(image)

RandomShift

class tensorlayerx.vision.transforms.RandomShift(shift, interpolation='bilinear', fill=0)[source]

Shift the image by random translations.

Parameters
  • shift (list or tuple) – Maximum absolute fraction for horizontal and vertical translations. shift=(a, b), then horizontal shift is randomly sampled in the range -img_width * a < dx < img_width * a. vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b.

  • interpolation (str) – Interpolation method. Default is ‘bilinear’. ‘nearest’,’bilinear’ are supported.

  • fill (number or sequence) – Pixel fill value for the area outside the sheared image. Default is 0.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomShift(shift=(0.2, 0.2), interpolation='bilinear', fill=0)
>>> image = transform(image)
>>> print(image)

RandomShear

class tensorlayerx.vision.transforms.RandomShear(shear, interpolation='bilinear', fill=0)[source]

Shear the image by random angle.

Parameters
  • shear (number or sequnence) –

    • Range of degrees to select from.

    • If shear is a number, a shear parallel to the x axis in the range (-shear, +shear) will be applied.

    • If shear is a sequence of 2 values a shear parallel to the x axis in the range (shear[0], shear[1]) will be applied.

    • If shear is a sequence of 4 values, a x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied.

  • interpolation (str) – Interpolation method. Default is ‘bilinear’.’nearest’,’bilinear’ are supported.

  • fill (number or sequence) – Pixel fill value for the area outside the sheared image. Default is 0.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomShear(shear=30, interpolation='bilinear', fill=0)
>>> image = transform(image)
>>> print(image)

RandomZoom

class tensorlayerx.vision.transforms.RandomZoom(zoom, interpolation='bilinear', fill=0)[source]

Zoom the image by random scale.

Parameters
  • zoom (list or tuple) – Scaling factor interval, e.g (a, b), then scale is randomly sampled from the range a <= scale <= b.

  • interpolation (str) – Interpolation method. Default is ‘bilinear’. ‘nearest’,’bilinear’ are supported.

  • fill (number or sequence) – Pixel fill value for the area outside the sheared image. Default is 0.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomZoom(zoom=(0.2, 0.5), interpolation='bilinear', fill=0)
>>> image = transform(image)
>>> print(image)

RandomAffine

class tensorlayerx.vision.transforms.RandomAffine(degrees, shift=None, zoom=None, shear=None, interpolation='bilinear', fill=0)[source]

Random affine transformation of the image keeping center invariant.

Parameters
  • degrees (number or sequnence) –

    • Range of degrees to select from.

    • If degrees is a number, the range of degrees will be (-degrees, +degrees).

    • If degrees is a sequence, the range of degrees will (degrees[0], degrees[1]).

    • Set to 0 to deactivate rotations.

  • shift (sequence or None) –

    • Maximum absolute fraction for horizontal and vertical translations.

    • shift=(a, b), then horizontal shift is randomly sampled in the range -img_width * a < dx < img_width * a.

    • vertical shift is randomly sampled in the range -img_height * b < dy < img_height * b.

    • Will not shift by default.

  • shear (number or sequnence or None) –

    • Range of degrees to select from.

    • If degrees is a number, a shear parallel to the x axis in the range (-shear, +shear) will be applied.

    • If shear is a sequence of 2 values a shear parallel to the x axis in the range (shear[0], shear[1]) will be applied.

    • If shear is a sequence of 4 values, a x-axis shear in (shear[0], shear[1]) and y-axis shear in (shear[2], shear[3]) will be applied.

    • Will not apply shear by default.

  • zoom (sequence or None) – Scaling factor interval, e.g (a, b), then scale is randomly sampled from the range a <= scale <= b. Will not zoom by default.

  • interpolation (str) – Interpolation method. Default is ‘bilinear’. ‘nearest’,’bilinear’ are supported.

  • fill (number or sequence) – Pixel fill value for the area outside the sheared image. Default is 0.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.RandomAffine(degrees=30, shift=(0.2,0.2), zoom=(0.2, 0.5), shear=30, interpolation='bilinear', fill=0)
>>> image = transform(image)
>>> print(image)

Transpose

class tensorlayerx.vision.transforms.Transpose(order)[source]

Transpose image(s) by swapping dimension.

Parameters

order (sequenece of int) – Desired output dimension order.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.Transpose(order=(2, 0 ,1))
>>> image = transform(image)
>>> print(image)
>>> image shape : (3, 224, 224)

HWC2CHW

class tensorlayerx.vision.transforms.HWC2CHW[source]

Transpose a image shape (H, W, C) to shape (C, H, W).

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.HWC2CHW()
>>> image = transform(image)
>>> print(image)
>>> image shape : (3, 224, 224)

CHW2HWC

class tensorlayerx.vision.transforms.CHW2HWC[source]

Transpose a image shape (C, H, W) to shape (H, W, C).

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand(3, 224, 224) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.CHW2HWC()
>>> image = transform(image)
>>> print(image)
>>> image shape : (224, 224, 3)

Normalize

class tensorlayerx.vision.transforms.Normalize(mean, std, data_format='HWC')[source]

Normalize a tensor image with mean and standard deviation.

Parameters
  • mean (number or sequence) – If mean is a number, mean will be applied for all channels. Sequence of means for each channel.

  • std (number or sequnece) – If std is a number, std will be applied for all channels.Sequence of standard deviations for each channel.

  • data_format (str) – Data format of input image, should be ‘HWC’ or ‘CHW’. Default: ‘HWC’.

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand( 224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.Normalize(mean = (155.0, 155.0, 155.0), std = (75.0, 75.0, 75.0),data_format='HWC')
>>> image = transform(image)
>>> print(image)

StandardizePerImage

class tensorlayerx.vision.transforms.StandardizePerImage[source]

For each 3-D image x in image, computes (x - mean) / adjusted_stddev, where mean is the average of all values in x. adjusted_stddev = max(stddev, 1.0/sqrt(N)) is capped away from 0 to protect against division by 0 when handling uniform images. N is the number of elements in x. stddev is the standard deviation of all values in x

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> image = (np.random.rand( 224, 224, 3) * 255.).astype(np.uint8)
>>> transform = tlx.vision.transforms.StandardizePerImage()
>>> image = transform(image)
>>> print(image)

Vision IO

load_image

class tensorlayerx.vision.load_image[source]

Load an image

Parameters
  • path (str) – path of the image.

  • Returns (numpy.ndarray) –

  • ------- – a numpy RGB image

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> path = './data/1.png'
>>> image = tlx.vision.load_image(path)
>>> print(image)

save_image

class tensorlayerx.vision.save_image[source]

Save an image

Parameters
  • image (numpy.ndarray) – The image to save

  • file_name (str) – image name to save

  • path (str) – path to save image

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> load_path = './data/1.png'
>>> save_path = './test/'
>>> image = tlx.vision.load_image(path)
>>> tlx.vision.save_image(image, file_name='1.png',path=save_path)

load_images

class tensorlayerx.vision.load_images[source]

Load images from file

Parameters
  • path (str) – path of the images.

  • n_threads (int) – The number of threads to read image.

  • Returns (list) –

  • ------- – a list of numpy RGB images

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> load_path = './data/'
>>> image = tlx.vision.load_images(path)

save_image

class tensorlayerx.vision.save_images[source]

Save images

Parameters
  • images (list) – a list of numpy RGB images

  • file_names (list) – a list of image names to save

  • path (str) – path to save images

Examples

With TensorLayerX

>>> import tensorlayerx as tlx
>>> load_path = './data/'
>>> save_path = './test/'
>>> images = tlx.vision.load_images(path)
>>> name_list = user_define
>>> tlx.vision.save_images(images, file_names=name_list,path=save_path)