ray.data.read_images(paths: Union[str, List[str]], *, filesystem: Optional[pyarrow.fs.FileSystem] = None, parallelism: int = - 1, meta_provider: Optional[ray.data.datasource.file_meta_provider.BaseFileMetadataProvider] = None, ray_remote_args: Dict[str, Any] = None, arrow_open_file_args: Optional[Dict[str, Any]] = None, partition_filter: Optional[ray.data.datasource.partitioning.PathPartitionFilter] = FileExtensionFilter(extensions=['.png', '.jpg', '.jpeg', '.tif', '.tiff', '.bmp', '.gif'], allow_if_no_extensions=False), partitioning: ray.data.datasource.partitioning.Partitioning = None, size: Optional[Tuple[int, int]] = None, mode: Optional[str] = None, include_paths: bool = False, ignore_missing_paths: bool = False, shuffle: Optional[Literal['files']] = None) ray.data.dataset.Dataset[source]#

Creates a Dataset from image files.


>>> import ray
>>> path = "s3://anonymous@ray-example-data/batoidea/JPEGImages/"
>>> ds = ray.data.read_images(path)
>>> ds.schema()
Column  Type
------  ----
image   numpy.ndarray(shape=(32, 32, 3), dtype=uint8)

If you need image file paths, set include_paths=True.

>>> ds = ray.data.read_images(path, include_paths=True)
>>> ds.schema()
Column  Type
------  ----
image   numpy.ndarray(shape=(32, 32, 3), dtype=uint8)
path    string
>>> ds.take(1)[0]["path"]

If your images are arranged like:



Then you can include the labels by specifying a Partitioning.

>>> import ray
>>> from ray.data.datasource.partitioning import Partitioning
>>> root = "s3://anonymous@ray-example-data/image-datasets/dir-partitioned"
>>> partitioning = Partitioning("dir", field_names=["class"], base_dir=root)
>>> ds = ray.data.read_images(root, size=(224, 224), partitioning=partitioning)
>>> ds.schema()
Column  Type
------  ----
image   numpy.ndarray(shape=(224, 224, 3), dtype=uint8)
class   string
  • paths – A single file or directory, or a list of file or directory paths. A list of paths can contain both files and directories.

  • filesystem – The pyarrow filesystem implementation to read from. These filesystems are specified in the pyarrow docs. Specify this parameter if you need to provide specific configurations to the filesystem. By default, the filesystem is automatically selected based on the scheme of the paths. For example, if the path begins with s3://, the S3FileSystem is used.

  • parallelism – The amount of parallelism to use for the dataset. Defaults to -1, which automatically determines the optimal parallelism for your configuration. You should not need to manually set this value in most cases. For details on how the parallelism is automatically determined and guidance on how to tune it, see Tuning read parallelism. Parallelism is upper bounded by the total number of records in all the CSV files.

  • meta_provider – A file metadata provider. Custom metadata providers may be able to resolve file metadata more quickly and/or accurately. In most cases, you do not need to set this. If None, this function uses a system-chosen implementation.

  • ray_remote_args – kwargs passed to remote() in the read tasks.

  • arrow_open_file_args – kwargs passed to pyarrow.fs.FileSystem.open_input_file. when opening input files to read.

  • partition_filter – A PathPartitionFilter. Use with a custom callback to read only selected partitions of a dataset. By default, this filters out any file paths whose file extension does not match *.png, *.jpg, *.jpeg, *.tiff, *.bmp, or *.gif.

  • partitioning – A Partitioning object that describes how paths are organized. Defaults to None.

  • size – The desired height and width of loaded images. If unspecified, images retain their original shape.

  • mode – A Pillow mode describing the desired type and depth of pixels. If unspecified, image modes are inferred by Pillow.

  • include_paths – If True, include the path to each image. File paths are stored in the 'path' column.

  • ignore_missing_paths – If True, ignores any file/directory paths in paths that are not found. Defaults to False.

  • shuffle – If setting to “files”, randomly shuffle input files order before read. Defaults to not shuffle with None.


A Dataset producing tensors that represent the images at the specified paths. For information on working with tensors, read the tensor data guide.


PublicAPI (beta): This API is in beta and may change before becoming stable.