Document label mapping (marinedata v1.0.0)
Browse files
README.md
CHANGED
|
@@ -122,31 +122,149 @@ Labels are in English. Where a source and its labels differ in vocabulary, the n
|
|
| 122 |
|
| 123 |
## Quick start
|
| 124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
```python
|
| 126 |
from datasets import load_dataset
|
| 127 |
|
| 128 |
-
|
| 129 |
-
ds = load_dataset("reefsupport/marine-data", "coral-masks")
|
| 130 |
-
row = ds["train"][0]
|
| 131 |
-
row["image"], row["mask"] # PIL images
|
| 132 |
-
row["attribution"], row["licence"] # keep these when you redistribute
|
| 133 |
-
|
| 134 |
-
# Large configurations: stream instead of downloading everything.
|
| 135 |
-
stream = load_dataset("reefsupport/marine-data", "fish-boxes", split="train", streaming=True)
|
| 136 |
row = next(iter(stream))
|
| 137 |
-
row["
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
```
|
| 139 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
Decode a mask and look up class names (semantic masks are `uint8` index PNGs, `255` = ignore):
|
| 141 |
|
| 142 |
```python
|
| 143 |
import numpy as np
|
|
|
|
| 144 |
|
|
|
|
|
|
|
| 145 |
mask = np.array(row["mask"]) # (H, W) class ids
|
| 146 |
names = {c["id"]: c["label_native"] for c in row["class_map"]} # per-image id -> the source's own label
|
| 147 |
```
|
| 148 |
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
## Dataset structure
|
| 152 |
|
|
|
|
| 122 |
|
| 123 |
## Quick start
|
| 124 |
|
| 125 |
+
Load a configuration with the `datasets` library, in one of three ways. Every row carries its `licence` and `attribution`; keep them when you redistribute.
|
| 126 |
+
|
| 127 |
+
Quick look in a notebook: stream the split and read the first row.
|
| 128 |
+
|
| 129 |
```python
|
| 130 |
from datasets import load_dataset
|
| 131 |
|
| 132 |
+
stream = load_dataset("reefsupport/marine-data", "coral-masks", split="train", streaming=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
row = next(iter(stream))
|
| 134 |
+
print(row["licence"], row["attribution"])
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
Scripts and training: download the config once, cached afterwards (all splits are fetched; the full `coral-masks` config is about 12 GB).
|
| 138 |
+
|
| 139 |
+
```python
|
| 140 |
+
ds = load_dataset("reefsupport/marine-data", "coral-masks", split="train")
|
| 141 |
```
|
| 142 |
|
| 143 |
+
One source only: download just that source's shards (file names are `<split>-<source>-<n>-of-<total>.parquet`).
|
| 144 |
+
|
| 145 |
+
```python
|
| 146 |
+
ds = load_dataset(
|
| 147 |
+
"reefsupport/marine-data",
|
| 148 |
+
"coral-masks",
|
| 149 |
+
split="train",
|
| 150 |
+
data_files={"train": "data/coral-masks/train-reef-support-seaview-labels-*.parquet"},
|
| 151 |
+
)
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
In a plain Python script, stopping a stream after a few rows can keep the process from exiting (an upstream issue in
|
| 155 |
+
pyarrow's dataset scanner). Download the config or a single source instead.
|
| 156 |
+
|
| 157 |
Decode a mask and look up class names (semantic masks are `uint8` index PNGs, `255` = ignore):
|
| 158 |
|
| 159 |
```python
|
| 160 |
import numpy as np
|
| 161 |
+
from datasets import load_dataset
|
| 162 |
|
| 163 |
+
stream = load_dataset("reefsupport/marine-data", "coral-masks", split="train", streaming=True)
|
| 164 |
+
row = next(iter(stream))
|
| 165 |
mask = np.array(row["mask"]) # (H, W) class ids
|
| 166 |
names = {c["id"]: c["label_native"] for c in row["class_map"]} # per-image id -> the source's own label
|
| 167 |
```
|
| 168 |
|
| 169 |
+
Boxes in `fish-boxes` are normalised to 0-1:
|
| 170 |
+
|
| 171 |
+
```python
|
| 172 |
+
stream = load_dataset("reefsupport/marine-data", "fish-boxes", split="train", streaming=True)
|
| 173 |
+
row = next(iter(stream))
|
| 174 |
+
print(row["boxes"][0]) # {"x_min": .., "y_min": .., "x_max": .., "y_max": .., "label": "fish"}
|
| 175 |
+
```
|
| 176 |
+
|
| 177 |
+
Select one source inside a configuration: `ds.filter(lambda r: r["source"] == "coralscapes")`.
|
| 178 |
+
|
| 179 |
+
## Label mapping
|
| 180 |
+
|
| 181 |
+
The masks keep the native ids of each source, and those ids differ between sources, so `marinedata.labels` maps every source onto one of three shared schemes with fixed class ids. 255 is the ignore value in every scheme and, for partially annotated sources, it means "not annotated" rather than background, so mask the loss to `labels.supervised_classes(source, scheme)` for those sources.
|
| 182 |
+
|
| 183 |
+
The `marinedata` package is not published on PyPI. Install the tagged release; the `hf` extra adds `datasets` and Pillow:
|
| 184 |
+
|
| 185 |
+
```bash
|
| 186 |
+
pip install "marinedata[hf] @ git+https://github.com/reefsupport/marine-data@v1.0.0"
|
| 187 |
+
```
|
| 188 |
+
|
| 189 |
+
Stream a row and remap it with `remap_row`, which adds `label`, an HxW `uint8` array:
|
| 190 |
+
|
| 191 |
+
```python
|
| 192 |
+
from datasets import load_dataset
|
| 193 |
+
from marinedata import labels
|
| 194 |
+
|
| 195 |
+
stream = load_dataset("reefsupport/marine-data", "coral-masks", split="train", streaming=True)
|
| 196 |
+
row = labels.remap_row(next(iter(stream)), "benthic-coarse") # adds `label`, an HxW uint8 array
|
| 197 |
+
names = labels.class_names("benthic-coarse")
|
| 198 |
+
present = sorted({int(v) for v in row["label"].ravel()} - {labels.IGNORE_INDEX})
|
| 199 |
+
print(row["source"], [names[i] for i in present])
|
| 200 |
+
```
|
| 201 |
+
|
| 202 |
+
In a script, download the config or one source as described under [Quick start](#quick-start) and pass its rows to `remap_row` the same way:
|
| 203 |
+
|
| 204 |
+
```python
|
| 205 |
+
ds = load_dataset("reefsupport/marine-data", "scene-masks", split="train")
|
| 206 |
+
row = labels.remap_row(ds[0], "scene")
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
```python
|
| 210 |
+
ds = load_dataset(
|
| 211 |
+
"reefsupport/marine-data",
|
| 212 |
+
"coral-masks",
|
| 213 |
+
split="train",
|
| 214 |
+
data_files={"train": "data/coral-masks/train-reef-support-seaview-labels-*.parquet"},
|
| 215 |
+
)
|
| 216 |
+
row = labels.remap_row(ds[0], "benthic-coarse")
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
The other configurations use the `scene` scheme. `scene-masks` rows work with `remap_row` as above:
|
| 220 |
+
|
| 221 |
+
```python
|
| 222 |
+
stream = load_dataset("reefsupport/marine-data", "scene-masks", split="train", streaming=True)
|
| 223 |
+
row = labels.remap_row(next(iter(stream)), "scene")
|
| 224 |
+
names = labels.class_names("scene")
|
| 225 |
+
present = sorted({int(v) for v in row["label"].ravel()} - {labels.IGNORE_INDEX})
|
| 226 |
+
print(row["source"], [names[i] for i in present])
|
| 227 |
+
```
|
| 228 |
+
|
| 229 |
+
`instance-masks` rows carry a list of `instances` instead of one semantic mask, so map each instance label with `labels.map_label` (it returns `None` for a label the scheme does not cover):
|
| 230 |
+
|
| 231 |
+
```python
|
| 232 |
+
stream = load_dataset("reefsupport/marine-data", "instance-masks", split="train", streaming=True)
|
| 233 |
+
row = next(iter(stream))
|
| 234 |
+
ids = [labels.map_label(row["source"], inst["label_native"], "scene") for inst in row["instances"]]
|
| 235 |
+
print(row["source"], ids)
|
| 236 |
+
```
|
| 237 |
+
|
| 238 |
+
`fish-boxes` rows have `boxes` and name their source in `source_id`:
|
| 239 |
+
|
| 240 |
+
```python
|
| 241 |
+
stream = load_dataset("reefsupport/marine-data", "fish-boxes", split="train", streaming=True)
|
| 242 |
+
row = next(iter(stream))
|
| 243 |
+
ids = [labels.map_label(row["source_id"], box["label"], "scene") for box in row["boxes"]]
|
| 244 |
+
print(row["source_id"], ids)
|
| 245 |
+
```
|
| 246 |
+
|
| 247 |
+
Ids are the position in the class list, and 255 (ignore) applies to every scheme.
|
| 248 |
+
|
| 249 |
+
| Scheme | Classes (id, name, meaning) | Configurations |
|
| 250 |
+
|---|---|---|
|
| 251 |
+
| `benthic-coarse` | 0 `HC` hard coral; 1 `MIL` fire coral; 2 `SC` soft coral; 3 `ALGAE` algae; 4 `ABIOTIC` abiotic; 5 `OTHER_FAUNA` other fauna | `coral-masks` |
|
| 252 |
+
| `coral-binary` | 0 `NOT_CORAL` every other mapped label; 1 `CORAL` hard, soft and fire coral | `coral-masks` |
|
| 253 |
+
| `scene` | 0 `BW` background waterbody; 1 `HD` human divers; 2 `PF` plants and sea-grass; 3 `WR` wrecks and ruins; 4 `RO` robots and instruments; 5 `RI` reefs and invertebrates; 6 `FV` fish and vertebrates; 7 `SR` sea-floor and rocks | `scene-masks`, `instance-masks`, `fish-boxes` |
|
| 254 |
+
|
| 255 |
+
**Label policy.** The schemes are set up for training on several sources at once. Dead coral is ignored by default: in `benthic-coarse` and `coral-binary`, a label whose condition is dead goes to 255 (the five dead coral classes of Coralscapes, and `dead clam`), because Coralscapes separates alive, dead and bleached coral while the Reef Support and Seaview masks record no condition. Bleached coral is alive and stays `HC` / `CORAL`. The Coralscapes class `unknown hard substrate` mixes rock with octocorals and is ignored in both schemes. A few Coralscapes labels the registry leaves open are mapped: `sponge` and `anemone` to `OTHER_FAUNA`, `algae covered substrate` to `ALGAE`. To opt out of the dead-coral default, pass `exclude_conditions=()`; `("bleached",)` ignores bleached coral but keeps dead coral:
|
| 256 |
+
|
| 257 |
+
```python
|
| 258 |
+
table = labels.lut("coralscapes", "benthic-coarse", exclude_conditions=()) # dead coral is HC again
|
| 259 |
+
```
|
| 260 |
+
|
| 261 |
+
Partially annotated sources label a subset of the classes. For `reef-support-benthic-own` and `reef-support-seaview-labels` (`HC` and `SC`, or `CORAL` in `coral-binary`) everything else in the image, including real coral of other kinds, is 255. In `coral-binary` they supply positives and no `NOT_CORAL` examples. The instance and box sources (`uiis`, `uiis10k`, `usis10k`, `roboflow-aquarium`) are partial too: pixels outside the annotated objects are unannotated, not background. `labels.is_dense(source)` returns the flag.
|
| 262 |
+
|
| 263 |
+
**Seaview masks include whole-frame hard coral.** About 8 percent of the Seaview rows label the whole frame as hard coral.
|
| 264 |
+
These frames are close-ups of dense coral, so their masks carry little boundary information. Weight or sample the
|
| 265 |
+
source accordingly when you mix it with sources that outline individual colonies.
|
| 266 |
+
|
| 267 |
+
The class lists, the mapping of every native label and the full label policy are in [`docs/LABELS.md`](https://github.com/reefsupport/marine-data/blob/v1.0.0/docs/LABELS.md).
|
| 268 |
|
| 269 |
## Dataset structure
|
| 270 |
|