Common operations#
Both Buffer
and Stream
allow to apply transformations to
samples when they are accessed. These transformations share the same API which
is described below in terms of the Buffer
class (but Stream
is
identical). For the methods specific to Buffer
or Stream
see
the corresponding pages.
General sample operations#
|
Creates batches from |
|
Filter samples based on the shape of the array. |
|
Transform the samples to either only contain this |
|
Apply the python function |
|
Apply the python function |
|
Remove instances of a certain value from an array and shift the whole array to the left. |
|
Rename a sample key. |
Image operations#
|
Center crop the image at |
|
Reduce an RGB image to gray-scale with various weights for red, green and blue. |
|
Crop the image randomly such that the result is a portion of the original area and within the given aspect ratio range. |
|
Extract a random crop of the requested size. |
|
Horizontally flip the image |
|
Resize the image to the requested size. |
|
Resize the image such that its smallest side is |
|
Rotate an image around its center point. |
I/O operations#
|
Load an audio file. |
|
Load the contents of a file. |
|
Load an array from a .npy file. |
|
Load an image file. |
|
Load a video file. |
|
Read data from tarfiles. |
Padding operations#
|
Pad the array at |
|
Pad the end of an array such that its size is a multiple of |
|
Pad the end of an array such that its size is |
Shape operations#
|
Extracts the shape of an array in the sample. |
|
Split the first dimension in |
|
Squeeze singleton dimensions. |
Tokenization#
|
Tokenize the contents of the array at |
Conditional operations#
A common issue when writing pipelines is configuring them according to the command line or configuration arguments. This usually results in code with a lot of redirections that is hard to read and reason about what operations are actually applied to the data.
For this reason all of the above methods have a conditional variant defined as
follows *_if(cond: bool, *args, **kwargs)
. This allows writing pipelines
that read from top to bottom without having to resort to redirection statements
in python.
# Assuming we have a buffer with image files and labels in dset
dset = (
dset
.load_image("image_file", output_key="image")
.image_random_crop_if(enable_random_crop, "image", 256, 256)
.image_random_h_flip_if(flip_prob > 0, "image", flip_prob)
.key_transform_if(brightness_range > 0, "image",
lambda x: ((1 + brightness_range * np.random.rand(x.shape[:2])[..., None]) * x).astype(x.dtype))
)