Offline tiling (CLI)¶
The plakakia command tiles a whole dataset, images plus labels or masks, and writes a
new dataset of tiles to disk, using all your CPU cores. Use it to prepare training data
once, before training.
In three steps¶
- Organise your data as one folder of images and one folder of labels, with matching
file names (
big.jpgandbig.txt). See input layout. - Describe the job in a
config.yamlfile. Start from the example below and change the paths. -
Run it:
plakakia --config config.yaml # or: python -m plakakia --config ... plakakia --config config.yaml -j 8 -q # 8 workers, no progress bar
data/ output/
├── images/ ├── images/
│ ├── field_01.jpg │ ├── tile_field_01_0_0_640_640.jpg
│ └── field_02.jpg ──► │ ├── tile_field_01_480_0_1120_640.jpg
└── labels/ │ └── ...
├── field_01.txt └── annotations/
└── field_02.txt ├── tile_field_01_0_0_640_640.txt
└── ...
Check the result
Set draw_boxes: true on a small subset first: the boxes are drawn on the saved
tiles, so you can see at a glance whether the labels landed in the right place.
make_tiles is kept as an alias for backwards compatibility.
Example config¶
# Example configuration for `plakakia --config examples/config.yaml`.
# Every key is optional except `input_dir_images` (and `input_dir_annotations`
# unless `input_format_annotations: none`). See the docs for the full reference.
# ----------------------------------------------------------------- inputs
input_dir_images: data/images
input_dir_annotations: data/labels
input_extension_images: jpg
input_format_annotations: yolo # yolo | pascal_voc | coco | segmentation | none
# ----------------------------------------------------------------- outputs
output_dir_images: output/images
output_dir_annotations: output/annotations
output_extension_images: jpg
output_format_annotations: yolo # yolo | pascal_voc
# ----------------------------------------------------------------- tiling
tile_size: 640 # side of the square tiles in pixels
step_size: 480 # sliding-window stride; < tile_size means overlapping tiles
cover_edges: true # add border-aligned tiles so no pixels are dropped
pad_image: false
pad_size: 10
# ----------------------------------------------------------------- labels
min_visibility: 1.0 # 1.0 = keep only boxes fully inside a tile
save_empty_tiles: false # keep tiles without boxes (background samples)
clear_duplicates: false # drop tiles whose boxes all appear in an earlier tile
annotation_mapping: # class name -> id (needed for Pascal VOC text labels)
person: 0
car: 1
# ----------------------------------------------------------------- misc
draw_boxes: false # draw boxes on saved tiles, for inspection only
num_workers: -1 # -1 = all CPU cores
Reference¶
All keys map one-to-one onto TilingConfig.
| Key | Default | Description |
|---|---|---|
input_dir_images |
required | Directory with the input images. |
input_dir_annotations |
required* | Directory with labels (*not needed for none). |
input_format_annotations |
yolo |
yolo, pascal_voc, coco, segmentation or none. |
input_extension_images |
jpg |
Extension of the input images. |
output_dir_images |
output/images |
Where image tiles are written. |
output_dir_annotations |
output/annotations |
Where label/mask tiles are written. |
output_format_annotations |
yolo |
yolo or pascal_voc. |
output_extension_images |
jpg |
Encoding of the image tiles. |
axes |
[0, 1] |
Axes to tile; the first two are (y, x). See hyperspectral data. |
tile_size |
200 |
Tile size in pixels: one value, or one per entry of axes. |
step_size |
50 |
Sliding-window stride: one value, or one per entry of axes. |
cover_edges |
false |
Add border-aligned tiles so no pixels are dropped. |
pad_image / pad_size |
false / 10 |
Pad images (and shift boxes) before tiling. |
min_visibility |
1.0 |
Minimum visible fraction of a box to keep it in a tile. |
save_empty_tiles |
false |
Also save tiles without boxes. |
clear_duplicates |
false |
Skip tiles whose boxes all appear in an earlier tile. |
annotation_mapping |
{} |
Class name to id mapping (Pascal VOC text labels). |
draw_boxes |
false |
Draw boxes on the saved tiles, for inspection. |
num_workers |
1 |
Worker processes; -1 uses all cores. |
hdf5_dataset |
null |
Dataset to read from HDF5 inputs. |
mask_extension |
png |
Format of segmentation masks (png, npy, tif...). |
georeference |
false |
Read and write GeoTIFFs with their georeferencing (rasterio). |
bands / band_axis |
null / -1 |
Select or reorder bands before tiling. |
nodata |
null |
Nodata value (defaults to the GeoTIFF's). |
max_nodata_fraction |
1.0 |
Skip tiles with more nodata than this. |
Input layout¶
| Format | Expected files |
|---|---|
yolo |
labels/<image stem>.txt |
pascal_voc |
labels/<image stem>.xml |
coco |
exactly one *.json in input_dir_annotations |
segmentation |
masks/<image stem>.<mask_extension> (same spatial size as the image) |
none |
no labels, every tile is saved |
Images without a label file are treated as having no objects.
Outputs¶
Each tile is named after its source image and its coordinates in the (padded) image:
output/images/tile_<stem>_<x1>_<y1>_<x2>_<y2>.jpg
output/annotations/tile_<stem>_<x1>_<y1>_<x2>_<y2>.txt # or .xml
output/annotations/mask_<stem>_<x1>_<y1>_<x2>_<y2>.png # segmentation
Masks keep their format, except JPEG masks, which are written as PNG because lossy
formats would corrupt class ids. Image tiles of N-D data tiled along extra axes get an
_ax<axis>-<start>-<stop> suffix per extra axis.