deepcontrail-instance 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (22) hide show
  1. deepcontrail_instance-0.1.0/LICENSE +211 -0
  2. deepcontrail_instance-0.1.0/PKG-INFO +219 -0
  3. deepcontrail_instance-0.1.0/README.md +188 -0
  4. deepcontrail_instance-0.1.0/pyproject.toml +61 -0
  5. deepcontrail_instance-0.1.0/pyproject.toml.orig +53 -0
  6. deepcontrail_instance-0.1.0/src/deepcontrail/instance/__init__.py +49 -0
  7. deepcontrail_instance-0.1.0/src/deepcontrail/instance/_evaluation.py +392 -0
  8. deepcontrail_instance-0.1.0/src/deepcontrail/instance/_growing.py +236 -0
  9. deepcontrail_instance-0.1.0/src/deepcontrail/instance/_lines.py +158 -0
  10. deepcontrail_instance-0.1.0/src/deepcontrail/instance/_overlap.py +77 -0
  11. deepcontrail_instance-0.1.0/src/deepcontrail/instance/_shape.py +190 -0
  12. deepcontrail_instance-0.1.0/src/deepcontrail/instance/_tuning.py +97 -0
  13. deepcontrail_instance-0.1.0/src/deepcontrail/instance/bundle.py +116 -0
  14. deepcontrail_instance-0.1.0/src/deepcontrail/instance/config.py +237 -0
  15. deepcontrail_instance-0.1.0/src/deepcontrail/instance/io.py +139 -0
  16. deepcontrail_instance-0.1.0/src/deepcontrail/instance/labels.py +211 -0
  17. deepcontrail_instance-0.1.0/src/deepcontrail/instance/mannstein.py +114 -0
  18. deepcontrail_instance-0.1.0/src/deepcontrail/instance/metrics.py +232 -0
  19. deepcontrail_instance-0.1.0/src/deepcontrail/instance/pipeline.py +90 -0
  20. deepcontrail_instance-0.1.0/src/deepcontrail/instance/py.typed +0 -0
  21. deepcontrail_instance-0.1.0/src/deepcontrail/instance/refine.py +119 -0
  22. deepcontrail_instance-0.1.0/src/deepcontrail/instance/scoring.py +405 -0
@@ -0,0 +1,211 @@
1
+ Copyright (c) 2026 EUROCONTROL
2
+
3
+ # European Union Public Licence v. 1.2
4
+
5
+ EUPL © the European Union 2007, 2016
6
+
7
+ This European Union Public Licence (the *EUPL*) applies to the Work (as defined below) which is provided under the
8
+ terms of this Licence. Any use of the Work, other than as authorised under this Licence is prohibited (to the extent such
9
+ use is covered by a right of the copyright holder of the Work).
10
+ The Work is provided under the terms of this Licence when the Licensor (as defined below) has placed the following
11
+ notice immediately following the copyright notice for the Work:
12
+
13
+ *Licensed under the EUPL or has expressed by any other means his willingness to license under the EUPL.*
14
+
15
+ ## 1. Definitions
16
+
17
+ In this Licence, the following terms have the following meaning:
18
+
19
+ * *The Licence*:this Licence.
20
+ * *The Original Work*:the work or software distributed or communicated by the Licensor under this Licence, available
21
+ as Source Code and also as Executable Code as the case may be.
22
+ * *Derivative Works*:the works or software that could be created by the Licensee, based upon the Original Work or
23
+ modifications thereof. This Licence does not define the extent of modification or dependence on the Original Work
24
+ required in order to classify a work as a Derivative Work; this extent is determined by copyright law applicable in
25
+ the country mentioned in Article 15.
26
+ * *The Work*:the Original Work or its Derivative Works.
27
+ * *The Source Code*:the human-readable form of the Work which is the most convenient for people to study and
28
+ modify.
29
+ * *The Executable Code*:any code which has generally been compiled and which is meant to be interpreted by
30
+ a computer as a program.
31
+ * *The Licensor*:the natural or legal person that distributes or communicates the Work under the Licence.
32
+ * *Contributor(s)*:any natural or legal person who modifies the Work under the Licence, or otherwise contributes to
33
+ the creation of a Derivative Work.
34
+ * *The Licensee* or *You*:any natural or legal person who makes any usage of the Work under the terms of the
35
+ Licence.
36
+ * *Distribution* or *Communication*:any act of selling, giving, lending, renting, distributing, communicating,
37
+ transmitting, or otherwise making available, online or offline, copies of the Work or providing access to its essential
38
+ functionalities at the disposal of any other natural or legal person.
39
+
40
+ ## 2. Scope of the rights granted by the Licence
41
+
42
+ The Licensor hereby grants You a worldwide, royalty-free, non-exclusive, sublicensable licence to do the following, for
43
+ the duration of copyright vested in the Original Work:
44
+
45
+ * use the Work in any circumstance and for all usage,
46
+ * reproduce the Work,
47
+ * modify the Work, and make Derivative Works based upon the Work,
48
+ * communicate to the public, including the right to make available or display the Work or copies thereof to the public
49
+ and perform publicly, as the case may be, the Work,
50
+ * distribute the Work or copies thereof,
51
+ * lend and rent the Work or copies thereof,
52
+ * sublicense rights in the Work or copies thereof.
53
+ Those rights can be exercised on any media, supports and formats, whether now known or later invented, as far as the
54
+ applicable law permits so.
55
+ In the countries where moral rights apply, the Licensor waives his right to exercise his moral right to the extent allowed
56
+ by law in order to make effective the licence of the economic rights here above listed.
57
+ The Licensor grants to the Licensee royalty-free, non-exclusive usage rights to any patents held by the Licensor, to the
58
+ extent necessary to make use of the rights granted on the Work under this Licence.
59
+
60
+ ## 3. Communication of the Source Code
61
+
62
+ The Licensor may provide the Work either in its Source Code form, or as Executable Code. If the Work is provided as
63
+ Executable Code, the Licensor provides in addition a machine-readable copy of the Source Code of the Work along with
64
+ each copy of the Work that the Licensor distributes or indicates, in a notice following the copyright notice attached to
65
+ the Work, a repository where the Source Code is easily and freely accessible for as long as the Licensor continues to
66
+ distribute or communicate the Work.
67
+
68
+ ## 4. Limitations on copyright
69
+
70
+ Nothing in this Licence is intended to deprive the Licensee of the benefits from any exception or limitation to the
71
+ exclusive rights of the rights owners in the Work, of the exhaustion of those rights or of other applicable limitations
72
+ thereto.
73
+
74
+ ## 5. Obligations of the Licensee
75
+
76
+ The grant of the rights mentioned above is subject to some restrictions and obligations imposed on the Licensee. Those
77
+ obligations are the following:
78
+
79
+ **Attribution right**: The Licensee shall keep intact all copyright, patent or trademarks notices and all notices that refer to
80
+ the Licence and to the disclaimer of warranties. The Licensee must include a copy of such notices and a copy of the
81
+ Licence with every copy of the Work he/she distributes or communicates. The Licensee must cause any Derivative Work
82
+ to carry prominent notices stating that the Work has been modified and the date of modification.
83
+
84
+ **Copyleft clause**: If the Licensee distributes or communicates copies of the Original Works or Derivative Works, this
85
+ Distribution or Communication will be done under the terms of this Licence or of a later version of this Licence unless
86
+ the Original Work is expressly distributed only under this version of the Licence — for example by communicating
87
+ *EUPL v. 1.2 only*. The Licensee (becoming Licensor) cannot offer or impose any additional terms or conditions on the
88
+ Work or Derivative Work that alter or restrict the terms of the Licence.
89
+
90
+ **Compatibility clause**: If the Licensee Distributes or Communicates Derivative Works or copies thereof based upon both
91
+ the Work and another work licensed under a Compatible Licence, this Distribution or Communication can be done
92
+ under the terms of this Compatible Licence. For the sake of this clause, *Compatible Licence* refers to the licences listed
93
+ in the appendix attached to this Licence. Should the Licensee's obligations under the Compatible Licence conflict with
94
+ his/her obligations under this Licence, the obligations of the Compatible Licence shall prevail.
95
+
96
+ **Provision of Source Code**: When distributing or communicating copies of the Work, the Licensee will provide
97
+ a machine-readable copy of the Source Code or indicate a repository where this Source will be easily and freely available
98
+ for as long as the Licensee continues to distribute or communicate the Work.
99
+ Legal Protection: This Licence does not grant permission to use the trade names, trademarks, service marks, or names
100
+ of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and
101
+ reproducing the content of the copyright notice.
102
+
103
+ ## 6. Chain of Authorship
104
+
105
+ The original Licensor warrants that the copyright in the Original Work granted hereunder is owned by him/her or
106
+ licensed to him/her and that he/she has the power and authority to grant the Licence.
107
+ Each Contributor warrants that the copyright in the modifications he/she brings to the Work are owned by him/her or
108
+ licensed to him/her and that he/she has the power and authority to grant the Licence.
109
+ Each time You accept the Licence, the original Licensor and subsequent Contributors grant You a licence to their contributions
110
+ to the Work, under the terms of this Licence.
111
+
112
+ ## 7. Disclaimer of Warranty
113
+
114
+ The Work is a work in progress, which is continuously improved by numerous Contributors. It is not a finished work
115
+ and may therefore contain defects or *bugs* inherent to this type of development.
116
+ For the above reason, the Work is provided under the Licence on an *as is* basis and without warranties of any kind
117
+ concerning the Work, including without limitation merchantability, fitness for a particular purpose, absence of defects or
118
+ errors, accuracy, non-infringement of intellectual property rights other than copyright as stated in Article 6 of this
119
+ Licence.
120
+ This disclaimer of warranty is an essential part of the Licence and a condition for the grant of any rights to the Work.
121
+
122
+ ## 8. Disclaimer of Liability
123
+
124
+ Except in the cases of wilful misconduct or damages directly caused to natural persons, the Licensor will in no event be
125
+ liable for any direct or indirect, material or moral, damages of any kind, arising out of the Licence or of the use of the
126
+ Work, including without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, loss
127
+ of data or any commercial damage, even if the Licensor has been advised of the possibility of such damage. However,
128
+ the Licensor will be liable under statutory product liability laws as far such laws apply to the Work.
129
+
130
+ ## 9. Additional agreements
131
+
132
+ While distributing the Work, You may choose to conclude an additional agreement, defining obligations or services
133
+ consistent with this Licence. However, if accepting obligations, You may act only on your own behalf and on your sole
134
+ responsibility, not on behalf of the original Licensor or any other Contributor, and only if You agree to indemnify,
135
+ defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against such Contributor by
136
+ the fact You have accepted any warranty or additional liability.
137
+
138
+ ## 10. Acceptance of the Licence
139
+
140
+ The provisions of this Licence can be accepted by clicking on an icon *I agree* placed under the bottom of a window
141
+ displaying the text of this Licence or by affirming consent in any other similar way, in accordance with the rules of
142
+ applicable law. Clicking on that icon indicates your clear and irrevocable acceptance of this Licence and all of its terms
143
+ and conditions.
144
+ Similarly, you irrevocably accept this Licence and all of its terms and conditions by exercising any rights granted to You
145
+ by Article 2 of this Licence, such as the use of the Work, the creation by You of a Derivative Work or the Distribution
146
+ or Communication by You of the Work or copies thereof.
147
+
148
+ ## 11. Information to the public
149
+
150
+ In case of any Distribution or Communication of the Work by means of electronic communication by You (for example,
151
+ by offering to download the Work from a remote location) the distribution channel or media (for example, a website)
152
+ must at least provide to the public the information requested by the applicable law regarding the Licensor, the Licence
153
+ and the way it may be accessible, concluded, stored and reproduced by the Licensee.
154
+
155
+ ## 12. Termination of the Licence
156
+
157
+ The Licence and the rights granted hereunder will terminate automatically upon any breach by the Licensee of the terms
158
+ of the Licence.
159
+ Such a termination will not terminate the licences of any person who has received the Work from the Licensee under
160
+ the Licence, provided such persons remain in full compliance with the Licence.
161
+
162
+ ## 13. Miscellaneous
163
+
164
+ Without prejudice of Article 9 above, the Licence represents the complete agreement between the Parties as to the
165
+ Work.
166
+ If any provision of the Licence is invalid or unenforceable under applicable law, this will not affect the validity or
167
+ enforceability of the Licence as a whole. Such provision will be construed or reformed so as necessary to make it valid
168
+ and enforceable.
169
+ The European Commission may publish other linguistic versions or new versions of this Licence or updated versions of
170
+ the Appendix, so far this is required and reasonable, without reducing the scope of the rights granted by the Licence.
171
+ New versions of the Licence will be published with a unique version number.
172
+ All linguistic versions of this Licence, approved by the European Commission, have identical value. Parties can take
173
+ advantage of the linguistic version of their choice.
174
+
175
+ ## 14. Jurisdiction
176
+
177
+ Without prejudice to specific agreement between parties,
178
+
179
+ * any litigation resulting from the interpretation of this License, arising between the European Union institutions,
180
+ bodies, offices or agencies, as a Licensor, and any Licensee, will be subject to the jurisdiction of the Court of Justice
181
+ of the European Union, as laid down in article 272 of the Treaty on the Functioning of the European Union,
182
+ * any litigation arising between other parties and resulting from the interpretation of this License, will be subject to
183
+ the exclusive jurisdiction of the competent court where the Licensor resides or conducts its primary business.
184
+
185
+ ## 15. Applicable Law
186
+
187
+ Without prejudice to specific agreement between parties,
188
+
189
+ * this Licence shall be governed by the law of the European Union Member State where the Licensor has his seat,
190
+ resides or has his registered office,
191
+ * this licence shall be governed by Belgian law if the Licensor has no seat, residence or registered office inside
192
+ a European Union Member State.
193
+
194
+ ## Appendix
195
+
196
+ *Compatible* Licences according to Article 5 EUPL are:
197
+
198
+ * GNU General Public License (GPL) v. 2, v. 3
199
+ * GNU Affero General Public License (AGPL) v. 3
200
+ * Open Software License (OSL) v. 2.1, v. 3.0
201
+ * Eclipse Public License (EPL) v. 1.0
202
+ * CeCILL v. 2.0, v. 2.1
203
+ * Mozilla Public Licence (MPL) v. 2
204
+ * GNU Lesser General Public Licence (LGPL) v. 2.1, v. 3
205
+ * Creative Commons Attribution-ShareAlike v. 3.0 Unported (CC BY-SA 3.0) for works other than software
206
+ * European Union Public Licence (EUPL) v. 1.1, v. 1.2
207
+ * Québec Free and Open-Source Licence — Reciprocity (LiLiQ-R) or Strong Reciprocity (LiLiQ-R+).
208
+ The European Commission may update this Appendix to later versions of the above licences without producing
209
+ a new version of the EUPL, as long as they provide the rights granted in Article 2 of this Licence and protect the
210
+ covered Source Code from exclusive appropriation.
211
+ All other changes or additions to this Appendix require the production of a new EUPL version.
@@ -0,0 +1,219 @@
1
+ Metadata-Version: 2.4
2
+ Name: deepcontrail-instance
3
+ Version: 0.1.0
4
+ Summary: DeepContrail — instance segmentation of contrails: separate contrails in a segmentation map.
5
+ Keywords: contrails,instance-segmentation,remote-sensing,satellite,GOES
6
+ Author: EUROCONTROL
7
+ License-Expression: EUPL-1.2
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Intended Audience :: Science/Research
11
+ Classifier: Programming Language :: Python :: 3.11
12
+ Classifier: Programming Language :: Python :: 3.12
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Scientific/Engineering :: Image Processing
15
+ Requires-Dist: deepcontrail-core>=0.1,<0.2
16
+ Requires-Dist: numpy>=1.24
17
+ Requires-Dist: scipy>=1.10
18
+ Requires-Dist: pydantic>=2.0
19
+ Requires-Dist: pyyaml>=6.0
20
+ Requires-Dist: pycocotools>=2.0 ; extra == 'map'
21
+ Requires-Dist: scikit-learn>=1.3,<1.10 ; extra == 'scores'
22
+ Requires-Python: >=3.11
23
+ Project-URL: Homepage, https://github.com/eurocontrol-asu/deepcontrail
24
+ Project-URL: Documentation, https://github.com/eurocontrol-asu/deepcontrail/tree/main/packages/instance#readme
25
+ Project-URL: Repository, https://github.com/eurocontrol-asu/deepcontrail
26
+ Project-URL: Issues, https://github.com/eurocontrol-asu/deepcontrail/issues
27
+ Project-URL: Changelog, https://github.com/eurocontrol-asu/deepcontrail/blob/main/CHANGELOG.md
28
+ Provides-Extra: map
29
+ Provides-Extra: scores
30
+ Description-Content-Type: text/markdown
31
+
32
+ # deepcontrail-instance
33
+
34
+ Instance segmentation of contrails, part of
35
+ [DeepContrail](https://github.com/eurocontrol-asu/deepcontrail): it splits a contrail
36
+ segmentation map into individual contrails, one mask and one score each.
37
+
38
+ **Status: preview.** The API may still change in the next releases.
39
+
40
+ It needs only `deepcontrail-core`, numpy, scipy, pydantic and PyYAML (no torch) and never imports
41
+ `deepcontrail.segmentation`: any model's probability map works as input. Two optional extras:
42
+ `[map]` (pycocotools) to save detections as COCO results, and `[scores]` (scikit-learn) to train a
43
+ score model.
44
+
45
+ ```bash
46
+ pip install deepcontrail-instance # the base package
47
+ pip install "deepcontrail-instance[map,scores]" # with both extras
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ ```python
53
+ import numpy as np
54
+ from deepcontrail.instance import detect_instances_from_probabilities
55
+
56
+ probs = np.load("scene_probs.npy") # (H, W) probabilities in [0, 1]
57
+ masks, scores = detect_instances_from_probabilities(probs)
58
+ # masks: one boolean (H, W) mask per contrail; scores: confidence in [0, 1]
59
+ ```
60
+
61
+ The pipeline thresholds the map (at 0.465), splits the mask into straight contrails with a
62
+ Mannstein-style detection, grows each contrail by one pixel into neighbours with probability above
63
+ 0.371 (annotators draw contrails wider than the thresholded mask), and weights each score by the
64
+ contrail's mean probability. Its settings live in `InstancePipelineConfig`; the defaults equal
65
+ [`configs/instance_pipeline.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/instance_pipeline.yaml), and
66
+ `InstancePipelineConfig.consensus()` equals
67
+ [`configs/instance_pipeline_consensus.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/instance_pipeline_consensus.yaml), for when
68
+ only contrails agreed by most annotators matter. `detect_instances(binary_mask)` runs plain
69
+ Mannstein on any binary mask (settings: `MannsteinConfig`, whose defaults are the pipeline's;
70
+ [`configs/mannstein_annotator_masks.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/mannstein_annotator_masks.yaml) holds the
71
+ settings for annotators' own masks).
72
+
73
+ Mannstein's oriented line filter (`MannsteinConfig(line_filter=True)`) is off in the shipped
74
+ settings: on these masks it kept every pixel and took most of the run time.
75
+
76
+ **Input.** A `(H, W)` map of probabilities in [0, 1] (after the sigmoid, not logits), on the same
77
+ pixel grid as the masks it is compared with. NaN or infinite values are rejected. A binary mask
78
+ also works as input: probability weighting then changes nothing, but growth can still add mask
79
+ pixels that no contrail took. For a plain binary mask, `detect_instances(mask)` runs the
80
+ Mannstein-style step alone.
81
+
82
+ **Known limits.**
83
+ - All settings are in pixels, tuned on GOES-16 / OpenContrails scenes (about 2 km per pixel,
84
+ contrails 1–3 px wide). Other resolutions need re-tuning.
85
+ - The cut (0.465) and the growth (into p > 0.371) were tuned on the validation predictions of
86
+ DeepContrail's earlier reference model (UPerNet + CAFormer-B36, trained on soft labels). They
87
+ carry over to the current Unet++ model, which scores higher with them on every measure below.
88
+ For another kind of model, re-tune the pipeline on its predictions for scenes it never trained
89
+ on; tuning needs instance annotations.
90
+ - Two contrails crossing at a small angle come out as one: at 10° always, at 20° with the
91
+ consensus settings. Touching, nearly parallel contrails also merge.
92
+ - The COCO mAP keeps at most 100 detections per sample (a warning says when a sample has more).
93
+
94
+ ## With a pretrained DeepContrail model
95
+
96
+ A pretrained bundle of `deepcontrail-segmentation` may carry the instance step tuned for its
97
+ model: `instance_pipeline.yaml` and a `score_model.json` trained on that model's predictions.
98
+ `load_instance_step` reads them from the bundle's folder (the defaults when they are absent) and
99
+ refuses a score model made with the other test-time-augmentation setting:
100
+
101
+ ```python
102
+ from deepcontrail.instance import load_instance_step, to_coco_results
103
+ from deepcontrail.segmentation import load_pretrained, predict
104
+
105
+ model = load_pretrained("eurocontrol-asu/deepcontrail-segmentation")
106
+ step = load_instance_step(model.directory, tta=model.config.tta)
107
+ probabilities = predict(model, composite, tta=model.config.tta)
108
+ masks, scores = step(probabilities)
109
+ results = to_coco_results(masks, scores, image_id="scene_1") # JSON-ready, needs [map]
110
+ ```
111
+
112
+ A score model records what it was trained on (`ScoreModel.trained_on`: semantic model, TTA,
113
+ scene size, scikit-learn and package versions); the pipeline refuses it on scenes of another size,
114
+ since its features are in pixels. `save_instance_step` writes the two files into a bundle folder.
115
+
116
+ ## Annotations and results
117
+
118
+ The annotation tools need only `instance_masks`: each annotator's mask, the agreement map, the
119
+ consensus mask and the consensus contrails (drawn by more than half of the annotators) are derived
120
+ from it by `deepcontrail.instance.labels`, for any number of annotators, one included.
121
+
122
+ Instance annotations use the `instance_masks/<split>/<id>.npy` format of the
123
+ [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
124
+ drawn by annotator a, 0 = not, -1 = annotator a drew no contrail n; `A = 1` for one annotator),
125
+ read with `deepcontrail.core.readers.load_instance_masks`. `instance_masks_from_label_image` and
126
+ `instance_masks_from_annotators` build it from a label image or from per-annotator masks.
127
+
128
+ `to_coco_results(masks, scores, image_id)` (needs `[map]`) saves detections as COCO results with
129
+ RLE masks, so overlapping contrails stay separate:
130
+
131
+ ```python
132
+ import json
133
+ from deepcontrail.instance import to_coco_results
134
+
135
+ json.dump(to_coco_results(masks, scores, image_id="scene_1"), open("instances.json", "w"))
136
+ ```
137
+
138
+ ## Learned scores
139
+
140
+ A `ScoreModel` predicts, from 20 features of each detected contrail (size, shape, probabilities,
141
+ surroundings), how well it matches the annotated contrails, and that prediction replaces its
142
+ score; the masks do not change. The features depend on the semantic model, so a score model is
143
+ valid only for the semantic model and pipeline settings it was trained with, and refuses other
144
+ pipeline settings. Training (`ScoreModel.fit`, with `training_data`) needs `[scores]`; a saved
145
+ model is plain JSON and predicts with numpy only:
146
+
147
+ ```python
148
+ from deepcontrail.instance import (
149
+ InstancePipelineConfig,
150
+ ScoreModel,
151
+ detect_instances_from_probabilities,
152
+ )
153
+
154
+ config = InstancePipelineConfig() # the shipped defaults, = configs/instance_pipeline.yaml
155
+ model = ScoreModel.from_json("score_model.json") # from a bundle, or train_score_model.py
156
+ masks, scores = detect_instances_from_probabilities(probs, config, score_model=model)
157
+ ```
158
+
159
+ ## Evaluation
160
+
161
+ `deepcontrail.instance.metrics.evaluate_annotator_samples` (needs `[map]`) scores detections
162
+ against each annotator's contrails as a separate ground truth: one sample per (scene, annotator)
163
+ pair. The headline is the COCO segmentation mAP averaged over IoU 0.25–0.75, pooled over all
164
+ samples; precision, recall and F1 are at IoU 0.5. Samples where the annotator drew no contrail are
165
+ skipped by default; `include_empty=True` keeps them, so every detection on them counts as false,
166
+ which is closer to real use.
167
+
168
+ Results on the OpenContrails 2025 test split (1,879 scenes, 2,700 annotator samples with at least
169
+ one contrail), from the probabilities of DeepContrail's reference semantic model (Unet++ +
170
+ CAFormer-B36, D4 TTA); the learned scores were trained on its validation predictions. Every
171
+ setting was chosen on validation scenes; the test split was only scored. The consensus columns
172
+ score each scene once against its consensus contrails.
173
+
174
+ | Settings | mAP | mAP, with empty | Precision (with empty) | Recall | F1 (with empty) | Consensus mAP | Consensus mAP, all scenes |
175
+ |---|---|---|---|---|---|---|---|
176
+ | `configs/instance_pipeline.yaml` (defaults) | 0.273 | 0.268 | 0.532 (0.504) | 0.387 | 0.448 (0.438) | 0.424 | 0.417 |
177
+ | `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 |
178
+ | defaults + learned scores | 0.280 | 0.273 | 0.532 (0.504) | 0.387 | 0.448 (0.438) | 0.435 | 0.424 |
179
+ | consensus settings + learned scores | 0.258 | 0.253 | 0.510 (0.485) | 0.350 | 0.415 (0.407) | 0.459 | 0.450 |
180
+
181
+ On each annotator's own mask instead of a prediction, the instance step alone reaches mAP 0.921
182
+ (`configs/mannstein_annotator_masks.yaml`). Most errors therefore come from the semantic step and
183
+ from contrail shape: the AP is 0.50 at IoU 0.25 but 0.02 at IoU 0.75. Annotators also disagree
184
+ strongly on individual contrails: one annotator scored against the others reaches F1 ≈ 0.35 on
185
+ validation scenes.
186
+
187
+ ## Scripts (repository only)
188
+
189
+ Command-line tools in [`scripts/`](https://github.com/eurocontrol-asu/deepcontrail/tree/main/packages/instance/scripts), run from the repository root. They read only
190
+ `instance_masks/<split>/` from a dataset, plus one `<id>.npy` probability map per scene for a
191
+ semantic model's predictions.
192
+
193
+ - **`evaluate_instances.py`**: the evaluation above, on predictions (`--pred-dir`), on the
194
+ consensus mask (`--use-gt-semantic`) or on each annotator's mask (`--use-annotator-semantic`);
195
+ `--score-model`, `--include-empty`, `--consensus-gt`.
196
+ - **`train_score_model.py`**: trains a `ScoreModel` (needs `[scores]`).
197
+ - **`optimize_pipeline.py`** and **`optimize_mannstein.py`**: Optuna searches of the pipeline and
198
+ Mannstein settings (optuna is in the repository's `experiments` group); they never read the
199
+ test split.
200
+
201
+ Tuning for your own semantic model: predict the scenes it never trained on (for example its
202
+ validation split), search the pipeline settings on them (`optimize_pipeline.py`), train a score
203
+ model on the same predictions (`train_score_model.py`), then score the test split once
204
+ (`evaluate_instances.py`).
205
+
206
+ ```bash
207
+ uv run python packages/instance/scripts/train_score_model.py --root <dataset_root> \
208
+ --pred-dir <validation probability maps> \
209
+ --pipeline-config packages/instance/configs/instance_pipeline.yaml --output-dir <dir>
210
+ ```
211
+
212
+ ## References and acknowledgements
213
+
214
+ The line filter and the size, length and straightness tests follow Mannstein, Meyer & Wendling
215
+ (1999), "Operational detection of contrails from NOAA-AVHRR data", *International Journal of
216
+ Remote Sensing* 20(8), 1641–1660, [doi:10.1080/014311699212650](https://doi.org/10.1080/014311699212650).
217
+
218
+ The direction-following region growing and the related shape tests build on ideas from a
219
+ student project (PIE) at ISAE-SUPAERO, carried out with EUROCONTROL.
@@ -0,0 +1,188 @@
1
+ # deepcontrail-instance
2
+
3
+ Instance segmentation of contrails, part of
4
+ [DeepContrail](https://github.com/eurocontrol-asu/deepcontrail): it splits a contrail
5
+ segmentation map into individual contrails, one mask and one score each.
6
+
7
+ **Status: preview.** The API may still change in the next releases.
8
+
9
+ It needs only `deepcontrail-core`, numpy, scipy, pydantic and PyYAML (no torch) and never imports
10
+ `deepcontrail.segmentation`: any model's probability map works as input. Two optional extras:
11
+ `[map]` (pycocotools) to save detections as COCO results, and `[scores]` (scikit-learn) to train a
12
+ score model.
13
+
14
+ ```bash
15
+ pip install deepcontrail-instance # the base package
16
+ pip install "deepcontrail-instance[map,scores]" # with both extras
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```python
22
+ import numpy as np
23
+ from deepcontrail.instance import detect_instances_from_probabilities
24
+
25
+ probs = np.load("scene_probs.npy") # (H, W) probabilities in [0, 1]
26
+ masks, scores = detect_instances_from_probabilities(probs)
27
+ # masks: one boolean (H, W) mask per contrail; scores: confidence in [0, 1]
28
+ ```
29
+
30
+ The pipeline thresholds the map (at 0.465), splits the mask into straight contrails with a
31
+ Mannstein-style detection, grows each contrail by one pixel into neighbours with probability above
32
+ 0.371 (annotators draw contrails wider than the thresholded mask), and weights each score by the
33
+ contrail's mean probability. Its settings live in `InstancePipelineConfig`; the defaults equal
34
+ [`configs/instance_pipeline.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/instance_pipeline.yaml), and
35
+ `InstancePipelineConfig.consensus()` equals
36
+ [`configs/instance_pipeline_consensus.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/instance_pipeline_consensus.yaml), for when
37
+ only contrails agreed by most annotators matter. `detect_instances(binary_mask)` runs plain
38
+ Mannstein on any binary mask (settings: `MannsteinConfig`, whose defaults are the pipeline's;
39
+ [`configs/mannstein_annotator_masks.yaml`](https://github.com/eurocontrol-asu/deepcontrail/blob/main/packages/instance/configs/mannstein_annotator_masks.yaml) holds the
40
+ settings for annotators' own masks).
41
+
42
+ Mannstein's oriented line filter (`MannsteinConfig(line_filter=True)`) is off in the shipped
43
+ settings: on these masks it kept every pixel and took most of the run time.
44
+
45
+ **Input.** A `(H, W)` map of probabilities in [0, 1] (after the sigmoid, not logits), on the same
46
+ pixel grid as the masks it is compared with. NaN or infinite values are rejected. A binary mask
47
+ also works as input: probability weighting then changes nothing, but growth can still add mask
48
+ pixels that no contrail took. For a plain binary mask, `detect_instances(mask)` runs the
49
+ Mannstein-style step alone.
50
+
51
+ **Known limits.**
52
+ - All settings are in pixels, tuned on GOES-16 / OpenContrails scenes (about 2 km per pixel,
53
+ contrails 1–3 px wide). Other resolutions need re-tuning.
54
+ - The cut (0.465) and the growth (into p > 0.371) were tuned on the validation predictions of
55
+ DeepContrail's earlier reference model (UPerNet + CAFormer-B36, trained on soft labels). They
56
+ carry over to the current Unet++ model, which scores higher with them on every measure below.
57
+ For another kind of model, re-tune the pipeline on its predictions for scenes it never trained
58
+ on; tuning needs instance annotations.
59
+ - Two contrails crossing at a small angle come out as one: at 10° always, at 20° with the
60
+ consensus settings. Touching, nearly parallel contrails also merge.
61
+ - The COCO mAP keeps at most 100 detections per sample (a warning says when a sample has more).
62
+
63
+ ## With a pretrained DeepContrail model
64
+
65
+ A pretrained bundle of `deepcontrail-segmentation` may carry the instance step tuned for its
66
+ model: `instance_pipeline.yaml` and a `score_model.json` trained on that model's predictions.
67
+ `load_instance_step` reads them from the bundle's folder (the defaults when they are absent) and
68
+ refuses a score model made with the other test-time-augmentation setting:
69
+
70
+ ```python
71
+ from deepcontrail.instance import load_instance_step, to_coco_results
72
+ from deepcontrail.segmentation import load_pretrained, predict
73
+
74
+ model = load_pretrained("eurocontrol-asu/deepcontrail-segmentation")
75
+ step = load_instance_step(model.directory, tta=model.config.tta)
76
+ probabilities = predict(model, composite, tta=model.config.tta)
77
+ masks, scores = step(probabilities)
78
+ results = to_coco_results(masks, scores, image_id="scene_1") # JSON-ready, needs [map]
79
+ ```
80
+
81
+ A score model records what it was trained on (`ScoreModel.trained_on`: semantic model, TTA,
82
+ scene size, scikit-learn and package versions); the pipeline refuses it on scenes of another size,
83
+ since its features are in pixels. `save_instance_step` writes the two files into a bundle folder.
84
+
85
+ ## Annotations and results
86
+
87
+ The annotation tools need only `instance_masks`: each annotator's mask, the agreement map, the
88
+ consensus mask and the consensus contrails (drawn by more than half of the annotators) are derived
89
+ from it by `deepcontrail.instance.labels`, for any number of annotators, one included.
90
+
91
+ Instance annotations use the `instance_masks/<split>/<id>.npy` format of the
92
+ [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
93
+ drawn by annotator a, 0 = not, -1 = annotator a drew no contrail n; `A = 1` for one annotator),
94
+ read with `deepcontrail.core.readers.load_instance_masks`. `instance_masks_from_label_image` and
95
+ `instance_masks_from_annotators` build it from a label image or from per-annotator masks.
96
+
97
+ `to_coco_results(masks, scores, image_id)` (needs `[map]`) saves detections as COCO results with
98
+ RLE masks, so overlapping contrails stay separate:
99
+
100
+ ```python
101
+ import json
102
+ from deepcontrail.instance import to_coco_results
103
+
104
+ json.dump(to_coco_results(masks, scores, image_id="scene_1"), open("instances.json", "w"))
105
+ ```
106
+
107
+ ## Learned scores
108
+
109
+ A `ScoreModel` predicts, from 20 features of each detected contrail (size, shape, probabilities,
110
+ surroundings), how well it matches the annotated contrails, and that prediction replaces its
111
+ score; the masks do not change. The features depend on the semantic model, so a score model is
112
+ valid only for the semantic model and pipeline settings it was trained with, and refuses other
113
+ pipeline settings. Training (`ScoreModel.fit`, with `training_data`) needs `[scores]`; a saved
114
+ model is plain JSON and predicts with numpy only:
115
+
116
+ ```python
117
+ from deepcontrail.instance import (
118
+ InstancePipelineConfig,
119
+ ScoreModel,
120
+ detect_instances_from_probabilities,
121
+ )
122
+
123
+ config = InstancePipelineConfig() # the shipped defaults, = configs/instance_pipeline.yaml
124
+ model = ScoreModel.from_json("score_model.json") # from a bundle, or train_score_model.py
125
+ masks, scores = detect_instances_from_probabilities(probs, config, score_model=model)
126
+ ```
127
+
128
+ ## Evaluation
129
+
130
+ `deepcontrail.instance.metrics.evaluate_annotator_samples` (needs `[map]`) scores detections
131
+ against each annotator's contrails as a separate ground truth: one sample per (scene, annotator)
132
+ pair. The headline is the COCO segmentation mAP averaged over IoU 0.25–0.75, pooled over all
133
+ samples; precision, recall and F1 are at IoU 0.5. Samples where the annotator drew no contrail are
134
+ skipped by default; `include_empty=True` keeps them, so every detection on them counts as false,
135
+ which is closer to real use.
136
+
137
+ Results on the OpenContrails 2025 test split (1,879 scenes, 2,700 annotator samples with at least
138
+ one contrail), from the probabilities of DeepContrail's reference semantic model (Unet++ +
139
+ CAFormer-B36, D4 TTA); the learned scores were trained on its validation predictions. Every
140
+ setting was chosen on validation scenes; the test split was only scored. The consensus columns
141
+ score each scene once against its consensus contrails.
142
+
143
+ | Settings | mAP | mAP, with empty | Precision (with empty) | Recall | F1 (with empty) | Consensus mAP | Consensus mAP, all scenes |
144
+ |---|---|---|---|---|---|---|---|
145
+ | `configs/instance_pipeline.yaml` (defaults) | 0.273 | 0.268 | 0.532 (0.504) | 0.387 | 0.448 (0.438) | 0.424 | 0.417 |
146
+ | `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 |
147
+ | defaults + learned scores | 0.280 | 0.273 | 0.532 (0.504) | 0.387 | 0.448 (0.438) | 0.435 | 0.424 |
148
+ | consensus settings + learned scores | 0.258 | 0.253 | 0.510 (0.485) | 0.350 | 0.415 (0.407) | 0.459 | 0.450 |
149
+
150
+ On each annotator's own mask instead of a prediction, the instance step alone reaches mAP 0.921
151
+ (`configs/mannstein_annotator_masks.yaml`). Most errors therefore come from the semantic step and
152
+ from contrail shape: the AP is 0.50 at IoU 0.25 but 0.02 at IoU 0.75. Annotators also disagree
153
+ strongly on individual contrails: one annotator scored against the others reaches F1 ≈ 0.35 on
154
+ validation scenes.
155
+
156
+ ## Scripts (repository only)
157
+
158
+ Command-line tools in [`scripts/`](https://github.com/eurocontrol-asu/deepcontrail/tree/main/packages/instance/scripts), run from the repository root. They read only
159
+ `instance_masks/<split>/` from a dataset, plus one `<id>.npy` probability map per scene for a
160
+ semantic model's predictions.
161
+
162
+ - **`evaluate_instances.py`**: the evaluation above, on predictions (`--pred-dir`), on the
163
+ consensus mask (`--use-gt-semantic`) or on each annotator's mask (`--use-annotator-semantic`);
164
+ `--score-model`, `--include-empty`, `--consensus-gt`.
165
+ - **`train_score_model.py`**: trains a `ScoreModel` (needs `[scores]`).
166
+ - **`optimize_pipeline.py`** and **`optimize_mannstein.py`**: Optuna searches of the pipeline and
167
+ Mannstein settings (optuna is in the repository's `experiments` group); they never read the
168
+ test split.
169
+
170
+ Tuning for your own semantic model: predict the scenes it never trained on (for example its
171
+ validation split), search the pipeline settings on them (`optimize_pipeline.py`), train a score
172
+ model on the same predictions (`train_score_model.py`), then score the test split once
173
+ (`evaluate_instances.py`).
174
+
175
+ ```bash
176
+ uv run python packages/instance/scripts/train_score_model.py --root <dataset_root> \
177
+ --pred-dir <validation probability maps> \
178
+ --pipeline-config packages/instance/configs/instance_pipeline.yaml --output-dir <dir>
179
+ ```
180
+
181
+ ## References and acknowledgements
182
+
183
+ The line filter and the size, length and straightness tests follow Mannstein, Meyer & Wendling
184
+ (1999), "Operational detection of contrails from NOAA-AVHRR data", *International Journal of
185
+ Remote Sensing* 20(8), 1641–1660, [doi:10.1080/014311699212650](https://doi.org/10.1080/014311699212650).
186
+
187
+ The direction-following region growing and the related shape tests build on ideas from a
188
+ student project (PIE) at ISAE-SUPAERO, carried out with EUROCONTROL.