Skip to content

Pipeline & config

plakakia.config

Configuration of the offline tiling pipeline.

TilingConfig dataclass

TilingConfig(
    input_dir_images: Path,
    input_dir_annotations: Path | None = None,
    output_dir_images: Path = Path("output/images"),
    output_dir_annotations: Path = Path(
        "output/annotations"
    ),
    input_format_annotations: InputFormat = "yolo",
    output_format_annotations: OutputFormat = "yolo",
    input_extension_images: str = "jpg",
    output_extension_images: str = "jpg",
    mask_extension: str = "png",
    hdf5_dataset: str | None = None,
    georeference: bool = False,
    bands: Sequence[int] | None = None,
    band_axis: int = -1,
    nodata: float | None = None,
    max_nodata_fraction: float = 1.0,
    axes: Sequence[int] = (0, 1),
    tile_size: int | Sequence[int] = 200,
    step_size: int | Sequence[int] = 50,
    cover_edges: bool = False,
    pad_image: bool = False,
    pad_size: int = 10,
    min_visibility: float = 1.0,
    annotation_mapping: Mapping[str, int] = dict(),
    save_empty_tiles: bool = False,
    clear_duplicates: bool = False,
    draw_boxes: bool = False,
    num_workers: int = 1,
)

Settings for :func:plakakia.pipeline.run.

Every field can be set from a YAML file with the same key, see :meth:from_yaml. Paths are resolved relative to the current working directory.

input_dir_images instance-attribute

input_dir_images: Path

Directory with the input images.

input_dir_annotations class-attribute instance-attribute

input_dir_annotations: Path | None = None

Directory with the input annotations (not needed for input_format_annotations='none').

output_dir_images class-attribute instance-attribute

output_dir_images: Path = Path('output/images')

Directory where image tiles are written.

output_dir_annotations class-attribute instance-attribute

output_dir_annotations: Path = Path('output/annotations')

Directory where annotation (or mask) tiles are written.

input_format_annotations class-attribute instance-attribute

input_format_annotations: InputFormat = 'yolo'

One of yolo, pascal_voc, coco, segmentation (masks) or none.

output_format_annotations class-attribute instance-attribute

output_format_annotations: OutputFormat = 'yolo'

One of yolo or pascal_voc. Ignored for segmentation and none.

input_extension_images class-attribute instance-attribute

input_extension_images: str = 'jpg'

Extension of the input images: jpg, png, tif, or npy / h5 for N-D arrays such as hyperspectral cubes.

output_extension_images class-attribute instance-attribute

output_extension_images: str = 'jpg'

Extension (and therefore encoding) of the image tiles. Use npy, h5 or tif for data OpenCV cannot encode (many bands, floats).

mask_extension class-attribute instance-attribute

mask_extension: str = 'png'

Extension of segmentation masks (png, npy, tif...). Mask tiles keep this format (JPEG masks are written as PNG).

hdf5_dataset class-attribute instance-attribute

hdf5_dataset: str | None = None

Name of the dataset to read from (and write to) HDF5 files. Optional when each input file holds a single dataset.

georeference class-attribute instance-attribute

georeference: bool = False

Read inputs as GeoTIFFs with rasterio (plakakia[geo]) and write every tile as a GeoTIFF with its own affine transform, CRS and nodata value, so tiles stay on the map. Rasterio arrays are (bands, height, width): use axes: [1, 2].

bands class-attribute instance-attribute

bands: Sequence[int] | None = None

Keep only these indices along band_axis before tiling, e.g. [3, 2, 1] to build an RGB composite from Sentinel-2 bands. None keeps every band.

band_axis class-attribute instance-attribute

band_axis: int = -1

Axis that bands selects from (0 for channels-first / GeoTIFF data).

nodata class-attribute instance-attribute

nodata: float | None = None

Pixel value meaning "no data" (nan allowed). A pixel is nodata when all of its bands equal it. Defaults to the GeoTIFF's own nodata value when georeference is set.

max_nodata_fraction class-attribute instance-attribute

max_nodata_fraction: float = 1.0

Skip tiles with a larger fraction of nodata pixels, e.g. 0.5 to drop tiles that are mostly outside a drone orthomosaic or satellite swath. 1.0 keeps every tile.

axes class-attribute instance-attribute

axes: Sequence[int] = (0, 1)

Array axes to tile. The first two are the (y, x) axes that boxes and masks refer to, e.g. [1, 2] for channels-first (B, H, W) cubes. Add more axes to tile them too, e.g. [0, 1, 2] to also split the bands of a (H, W, B) cube.

tile_size class-attribute instance-attribute

tile_size: int | Sequence[int] = 200

Tile size in pixels: one value for every tiled axis, or one per entry of axes.

step_size class-attribute instance-attribute

step_size: int | Sequence[int] = 50

Stride of the sliding window: one value, or one per entry of axes.

cover_edges class-attribute instance-attribute

cover_edges: bool = False

Add border-aligned tiles so the whole image is covered.

pad_image class-attribute instance-attribute

pad_image: bool = False

Pad every image with a constant border before tiling.

pad_size class-attribute instance-attribute

pad_size: int = 10

Border size in pixels when pad_image is set.

min_visibility class-attribute instance-attribute

min_visibility: float = 1.0

Minimum fraction of a box that must be inside a tile to be kept (1.0 = fully inside). Partially visible boxes are clipped to the tile.

annotation_mapping class-attribute instance-attribute

annotation_mapping: Mapping[str, int] = field(
    default_factory=dict
)

Class name to id mapping (needed for Pascal VOC inputs with text labels).

save_empty_tiles class-attribute instance-attribute

save_empty_tiles: bool = False

Also save tiles that contain no boxes (useful as background samples).

clear_duplicates class-attribute instance-attribute

clear_duplicates: bool = False

Drop tiles whose boxes are all already contained in a previously saved tile.

draw_boxes class-attribute instance-attribute

draw_boxes: bool = False

Draw the boxes on the saved tiles (for visual inspection only).

num_workers class-attribute instance-attribute

num_workers: int = 1

Number of worker processes; -1 uses all CPU cores.

tile_shape property

tile_shape: tuple[int, ...]

Tile size for each entry of :attr:axes.

step_shape property

step_shape: tuple[int, ...]

Step size for each entry of :attr:axes.

output_extension_annotations property

output_extension_annotations: str

File extension of the written annotation tiles.

label_names property

label_names: dict[int, str]

Inverse of :attr:annotation_mapping.

replace

replace(**changes: Any) -> TilingConfig

Return a copy with some fields replaced.

from_dict classmethod

from_dict(data: Mapping[str, Any]) -> TilingConfig

Build a config from a mapping, handling keys from older plakakia versions.

from_yaml classmethod

from_yaml(path: str | Path) -> TilingConfig

Load a config from a YAML file.

plakakia.pipeline

Offline tiling of whole datasets (images + annotations) to disk.

Sample dataclass

Sample(
    image_path: Path,
    annotation_path: Path | None = None,
    annotations: Annotations | None = None,
)

One input image and where its labels come from.

annotations class-attribute instance-attribute

annotations: Annotations | None = None

Pre-loaded annotations (used for COCO, where all labels live in one file).

SampleResult dataclass

SampleResult(
    image_path: Path,
    num_tiles: int = 0,
    num_boxes: int = 0,
    skipped: str | None = None,
)

Outcome of tiling one image.

discover_samples

discover_samples(config: TilingConfig) -> list[Sample]

List the images of a dataset and pair them with their annotations.

nodata_fraction

nodata_fraction(
    image: ndarray,
    nodata: float,
    coords: ndarray,
    spatial_axes: tuple[int, int] = (0, 1),
) -> np.ndarray

Fraction of nodata pixels in each tile, via a summed-area table (O(H*W + T)).

A pixel is nodata when every value along the non-spatial axes equals nodata (nan matches NaN).

Parameters:

Name Type Description Default
image ndarray

Array with spatial axes spatial_axes ((y, x)).

required
nodata float

The nodata value.

required
coords ndarray

(T, 4) tile coordinates (x1, y1, x2, y2).

required
spatial_axes tuple[int, int]

The (y, x) axes of image.

(0, 1)

Returns:

Type Description
ndarray

(T,) array of fractions in [0, 1].

process_sample

process_sample(
    sample: Sample, config: TilingConfig
) -> SampleResult

Tile one image or N-D array (and its labels or mask) and write the results to disk.

The first two entries of config.axes are the (y, x) axes that labels refer to; any further axes (e.g. spectral bands) are tiled as well, producing one output file per combination of spatial and extra window. Those files share the spatial tile's labels.

run

run(
    config: TilingConfig,
    samples: Iterable[Sample] | None = None,
    *,
    progress: bool = True,
) -> list[SampleResult]

Tile a whole dataset as described by config.

Parameters:

Name Type Description Default
config TilingConfig

Pipeline settings.

required
samples Iterable[Sample] | None

Samples to process; discovered from config when omitted.

None
progress bool

Show a progress bar.

True

Returns:

Name Type Description
One list[SampleResult]

class:SampleResult per input image, in input order.

plakakia.cli

Command line interface: plakakia --config config.yaml.

build_parser

build_parser() -> argparse.ArgumentParser

Create the argument parser.

main

main(argv: Sequence[str] | None = None) -> int

Run the CLI and return the process exit code.