Metadata-Version: 2.4
Name: deepcontrail-instance
Version: 0.1.0
Summary: DeepContrail — instance segmentation of contrails: separate contrails in a segmentation map.
Keywords: contrails,instance-segmentation,remote-sensing,satellite,GOES
Author: EUROCONTROL
License-Expression: EUPL-1.2
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Science/Research
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Image Processing
Requires-Dist: deepcontrail-core>=0.1,<0.2
Requires-Dist: numpy>=1.24
Requires-Dist: scipy>=1.10
Requires-Dist: pydantic>=2.0
Requires-Dist: pyyaml>=6.0
Requires-Dist: pycocotools>=2.0 ; extra == 'map'
Requires-Dist: scikit-learn>=1.3,<1.10 ; extra == 'scores'
Requires-Python: >=3.11
Project-URL: Homepage, https://github.com/eurocontrol-asu/deepcontrail
Project-URL: Documentation, https://github.com/eurocontrol-asu/deepcontrail/tree/main/packages/instance#readme
Project-URL: Repository, https://github.com/eurocontrol-asu/deepcontrail
Project-URL: Issues, https://github.com/eurocontrol-asu/deepcontrail/issues
Project-URL: Changelog, https://github.com/eurocontrol-asu/deepcontrail/blob/main/CHANGELOG.md
Provides-Extra: map
Provides-Extra: scores
Description-Content-Type: text/markdown

# deepcontrail-instance

Instance segmentation of contrails, part of
[DeepContrail](https://github.com/eurocontrol-asu/deepcontrail): it splits a contrail
segmentation map into individual contrails, one mask and one score each.

**Status: preview.** The API may still change in the next releases.

It needs only `deepcontrail-core`, numpy, scipy, pydantic and PyYAML (no torch) and never imports
`deepcontrail.segmentation`: any model's probability map works as input. Two optional extras:
`[map]` (pycocotools) to save detections as COCO results, and `[scores]` (scikit-learn) to train a
score model.

```bash
pip install deepcontrail-instance                # the base package
pip install "deepcontrail-instance[map,scores]"  # with both extras
```

## Usage

```python
import numpy as np
from deepcontrail.instance import detect_instances_from_probabilities

probs = np.load("scene_probs.npy")  # (H, W) probabilities in [0, 1]
masks, scores = detect_instances_from_probabilities(probs)
# masks: one boolean (H, W) mask per contrail; scores: confidence in [0, 1]
```

The pipeline thresholds the map (at 0.465), splits the mask into straight contrails with a
Mannstein-style detection, grows each contrail by one pixel into neighbours with probability above
0.371 (annotators draw contrails wider than the thresholded mask), and weights each score by the
contrail's mean probability. Its settings live in `InstancePipelineConfig`; the defaults equal
[`configs/instance_pipeline.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/instance_pipeline.yaml), and
`InstancePipelineConfig.consensus()` equals
[`configs/instance_pipeline_consensus.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/instance_pipeline_consensus.yaml), for when
only contrails agreed by most annotators matter. `detect_instances(binary_mask)` runs plain
Mannstein on any binary mask (settings: `MannsteinConfig`, whose defaults are the pipeline's;
[`configs/mannstein_annotator_masks.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/mannstein_annotator_masks.yaml) holds the
settings for annotators' own masks).

Mannstein's oriented line filter (`MannsteinConfig(line_filter=True)`) is off in the shipped
settings: on these masks it kept every pixel and took most of the run time.

**Input.** A `(H, W)` map of probabilities in [0, 1] (after the sigmoid, not logits), on the same
pixel grid as the masks it is compared with. NaN or infinite values are rejected. A binary mask
also works as input: probability weighting then changes nothing, but growth can still add mask
pixels that no contrail took. For a plain binary mask, `detect_instances(mask)` runs the
Mannstein-style step alone.

**Known limits.**
- All settings are in pixels, tuned on GOES-16 / OpenContrails scenes (about 2 km per pixel,
  contrails 1–3 px wide). Other resolutions need re-tuning.
- The cut (0.465) and the growth (into p > 0.371) were tuned on the validation predictions of
  DeepContrail's earlier reference model (UPerNet + CAFormer-B36, trained on soft labels). They
  carry over to the current Unet++ model, which scores higher with them on every measure below.
  For another kind of model, re-tune the pipeline on its predictions for scenes it never trained
  on; tuning needs instance annotations.
- Two contrails crossing at a small angle come out as one: at 10° always, at 20° with the
  consensus settings. Touching, nearly parallel contrails also merge.
- The COCO mAP keeps at most 100 detections per sample (a warning says when a sample has more).

## With a pretrained DeepContrail model

A pretrained bundle of `deepcontrail-segmentation` may carry the instance step tuned for its
model: `instance_pipeline.yaml` and a `score_model.json` trained on that model's predictions.
`load_instance_step` reads them from the bundle's folder (the defaults when they are absent) and
refuses a score model made with the other test-time-augmentation setting:

```python
from deepcontrail.instance import load_instance_step, to_coco_results
from deepcontrail.segmentation import load_pretrained, predict

model = load_pretrained("eurocontrol-asu/deepcontrail-segmentation")
step = load_instance_step(model.directory, tta=model.config.tta)
probabilities = predict(model, composite, tta=model.config.tta)
masks, scores = step(probabilities)
results = to_coco_results(masks, scores, image_id="scene_1")  # JSON-ready, needs [map]
```

A score model records what it was trained on (`ScoreModel.trained_on`: semantic model, TTA,
scene size, scikit-learn and package versions); the pipeline refuses it on scenes of another size,
since its features are in pixels. `save_instance_step` writes the two files into a bundle folder.

## Annotations and results

The annotation tools need only `instance_masks`: each annotator's mask, the agreement map, the
consensus mask and the consensus contrails (drawn by more than half of the annotators) are derived
from it by `deepcontrail.instance.labels`, for any number of annotators, one included.

Instance annotations use the `instance_masks/<split>/<id>.npy` format of the
[DeepContrail dataset format](https://github.com/eurocontrol-asu/deepcontrail/tree/main/packages/core): an `(N, H, W, A)` int8 array (1 = pixel of contrail n
drawn by annotator a, 0 = not, -1 = annotator a drew no contrail n; `A = 1` for one annotator),
read with `deepcontrail.core.readers.load_instance_masks`. `instance_masks_from_label_image` and
`instance_masks_from_annotators` build it from a label image or from per-annotator masks.

`to_coco_results(masks, scores, image_id)` (needs `[map]`) saves detections as COCO results with
RLE masks, so overlapping contrails stay separate:

```python
import json
from deepcontrail.instance import to_coco_results

json.dump(to_coco_results(masks, scores, image_id="scene_1"), open("instances.json", "w"))
```

## Learned scores

A `ScoreModel` predicts, from 20 features of each detected contrail (size, shape, probabilities,
surroundings), how well it matches the annotated contrails, and that prediction replaces its
score; the masks do not change. The features depend on the semantic model, so a score model is
valid only for the semantic model and pipeline settings it was trained with, and refuses other
pipeline settings. Training (`ScoreModel.fit`, with `training_data`) needs `[scores]`; a saved
model is plain JSON and predicts with numpy only:

```python
from deepcontrail.instance import (
    InstancePipelineConfig,
    ScoreModel,
    detect_instances_from_probabilities,
)

config = InstancePipelineConfig()  # the shipped defaults, = configs/instance_pipeline.yaml
model = ScoreModel.from_json("score_model.json")  # from a bundle, or train_score_model.py
masks, scores = detect_instances_from_probabilities(probs, config, score_model=model)
```

## Evaluation

`deepcontrail.instance.metrics.evaluate_annotator_samples` (needs `[map]`) scores detections
against each annotator's contrails as a separate ground truth: one sample per (scene, annotator)
pair. The headline is the COCO segmentation mAP averaged over IoU 0.25–0.75, pooled over all
samples; precision, recall and F1 are at IoU 0.5. Samples where the annotator drew no contrail are
skipped by default; `include_empty=True` keeps them, so every detection on them counts as false,
which is closer to real use.

Results on the OpenContrails 2025 test split (1,879 scenes, 2,700 annotator samples with at least
one contrail), from the probabilities of DeepContrail's reference semantic model (Unet++ +
CAFormer-B36, D4 TTA); the learned scores were trained on its validation predictions. Every
setting was chosen on validation scenes; the test split was only scored. The consensus columns
score each scene once against its consensus contrails.

| Settings | mAP | mAP, with empty | Precision (with empty) | Recall | F1 (with empty) | Consensus mAP | Consensus mAP, all scenes |
|---|---|---|---|---|---|---|---|
| `configs/instance_pipeline.yaml` (defaults) | 0.273 | 0.268 | 0.532 (0.504) | 0.387 | 0.448 (0.438) | 0.424 | 0.417 |
| `configs/instance_pipeline_consensus.yaml` (no growth) | 0.252 | 0.248 | 0.510 (0.485) | 0.350 | 0.415 (0.407) | 0.446 | 0.439 |
| defaults + learned scores | 0.280 | 0.273 | 0.532 (0.504) | 0.387 | 0.448 (0.438) | 0.435 | 0.424 |
| consensus settings + learned scores | 0.258 | 0.253 | 0.510 (0.485) | 0.350 | 0.415 (0.407) | 0.459 | 0.450 |

On each annotator's own mask instead of a prediction, the instance step alone reaches mAP 0.921
(`configs/mannstein_annotator_masks.yaml`). Most errors therefore come from the semantic step and
from contrail shape: the AP is 0.50 at IoU 0.25 but 0.02 at IoU 0.75. Annotators also disagree
strongly on individual contrails: one annotator scored against the others reaches F1 ≈ 0.35 on
validation scenes.

## Scripts (repository only)

Command-line tools in [`scripts/`](https://github.com/eurocontrol-asu/deepcontrail/tree/main/packages/instance/scripts), run from the repository root. They read only
`instance_masks/<split>/` from a dataset, plus one `<id>.npy` probability map per scene for a
semantic model's predictions.

- **`evaluate_instances.py`**: the evaluation above, on predictions (`--pred-dir`), on the
  consensus mask (`--use-gt-semantic`) or on each annotator's mask (`--use-annotator-semantic`);
  `--score-model`, `--include-empty`, `--consensus-gt`.
- **`train_score_model.py`**: trains a `ScoreModel` (needs `[scores]`).
- **`optimize_pipeline.py`** and **`optimize_mannstein.py`**: Optuna searches of the pipeline and
  Mannstein settings (optuna is in the repository's `experiments` group); they never read the
  test split.

Tuning for your own semantic model: predict the scenes it never trained on (for example its
validation split), search the pipeline settings on them (`optimize_pipeline.py`), train a score
model on the same predictions (`train_score_model.py`), then score the test split once
(`evaluate_instances.py`).

```bash
uv run python packages/instance/scripts/train_score_model.py --root <dataset_root> \
    --pred-dir <validation probability maps> \
    --pipeline-config packages/instance/configs/instance_pipeline.yaml --output-dir <dir>
```

## References and acknowledgements

The line filter and the size, length and straightness tests follow Mannstein, Meyer & Wendling
(1999), "Operational detection of contrails from NOAA-AVHRR data", *International Journal of
Remote Sensing* 20(8), 1641–1660, [doi:10.1080/014311699212650](https://doi.org/10.1080/014311699212650).

The direction-following region growing and the related shape tests build on ideas from a
student project (PIE) at ISAE-SUPAERO, carried out with EUROCONTROL.
