Commit ed0901f
authored
fix(api): make REST segmentation API robust for SAM3 (#535)
* fix(api): make REST segmentation API robust for SAM3
Validated the REST API (samgeo/api.py) end-to-end against real example
imagery and fixed several bugs found in the process:
- Model cache keyed only on (model_version, model_id), so an automatic
mask-generation instance was reused for a prompt-predict request and
crashed with "'SamGeo2' object has no attribute 'predictor'". The cache
key now includes the automatic flag; get_model returns the key so the
image-hash cache stays consistent.
- SAM3 mutates its encoded-image state during generate_*, so the image-hash
optimization (skipping set_image on repeat requests) made the 2nd+ SAM3
request fail with "expected scalar type BFloat16 but found Float".
_set_image_cached now always re-encodes for SAM3 (still caches SAM/SAM2).
- raster_to_vector crashed on an empty mask (no geometry column). It now
emits a valid empty FeatureCollection.
- /segment/automatic gained the empty-masks guard the other endpoints have
(404 instead of a 500 "No masks found").
- /segment/automatic now defaults to SAM3.
Verified on docs/examples/tree_image.tif: text/box/point/detections all
return GeoJSON across consecutive requests. Added regression tests.
* fix(api): include model-shaping kwargs in the model cache key
Address CodeRabbit review on #535: the cache key only considered
(model_version, model_id, automatic), so two requests differing only by
backend/confidence_threshold (SAM3) or points_per_side/pred_iou_thresh/
stability_score_thresh (SAM/SAM2) would reuse the first, differently
configured instance and silently run with the wrong settings.
The key now folds in all constructor kwargs via a small order-independent
_freeze_kwargs() helper. Added regression tests covering distinct configs
(SAM3 confidence_threshold, SAM2 points_per_side) and the freezing helper.1 parent cdaab8b commit ed0901f
4 files changed
Lines changed: 227 additions & 29 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
103 | 103 | | |
104 | 104 | | |
105 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
106 | 130 | | |
107 | 131 | | |
108 | 132 | | |
| |||
112 | 136 | | |
113 | 137 | | |
114 | 138 | | |
115 | | - | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
116 | 145 | | |
117 | 146 | | |
118 | 147 | | |
| |||
140 | 169 | | |
141 | 170 | | |
142 | 171 | | |
143 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
144 | 181 | | |
145 | 182 | | |
146 | 183 | | |
147 | | - | |
| 184 | + | |
| 185 | + | |
148 | 186 | | |
149 | 187 | | |
150 | 188 | | |
| |||
172 | 210 | | |
173 | 211 | | |
174 | 212 | | |
175 | | - | |
| 213 | + | |
| 214 | + | |
176 | 215 | | |
177 | 216 | | |
178 | 217 | | |
| |||
183 | 222 | | |
184 | 223 | | |
185 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
186 | 231 | | |
187 | 232 | | |
188 | | - | |
| 233 | + | |
189 | 234 | | |
190 | 235 | | |
191 | 236 | | |
192 | 237 | | |
193 | 238 | | |
194 | 239 | | |
195 | | - | |
| 240 | + | |
| 241 | + | |
196 | 242 | | |
197 | 243 | | |
198 | 244 | | |
| |||
545 | 591 | | |
546 | 592 | | |
547 | 593 | | |
548 | | - | |
| 594 | + | |
549 | 595 | | |
550 | 596 | | |
551 | 597 | | |
| |||
583 | 629 | | |
584 | 630 | | |
585 | 631 | | |
586 | | - | |
587 | | - | |
| 632 | + | |
588 | 633 | | |
589 | 634 | | |
590 | 635 | | |
591 | 636 | | |
592 | 637 | | |
593 | 638 | | |
594 | 639 | | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
595 | 646 | | |
596 | 647 | | |
597 | 648 | | |
| |||
600 | 651 | | |
601 | 652 | | |
602 | 653 | | |
603 | | - | |
| 654 | + | |
604 | 655 | | |
605 | 656 | | |
606 | 657 | | |
607 | | - | |
| 658 | + | |
608 | 659 | | |
609 | 660 | | |
610 | 661 | | |
| |||
707 | 758 | | |
708 | 759 | | |
709 | 760 | | |
710 | | - | |
711 | | - | |
712 | | - | |
713 | | - | |
714 | | - | |
| 761 | + | |
715 | 762 | | |
716 | 763 | | |
717 | 764 | | |
| |||
745 | 792 | | |
746 | 793 | | |
747 | 794 | | |
748 | | - | |
749 | | - | |
750 | | - | |
751 | | - | |
| 795 | + | |
| 796 | + | |
752 | 797 | | |
753 | 798 | | |
754 | 799 | | |
| |||
818 | 863 | | |
819 | 864 | | |
820 | 865 | | |
821 | | - | |
| 866 | + | |
822 | 867 | | |
823 | 868 | | |
824 | 869 | | |
825 | 870 | | |
826 | 871 | | |
827 | 872 | | |
828 | | - | |
829 | 873 | | |
830 | 874 | | |
831 | 875 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1444 | 1444 | | |
1445 | 1445 | | |
1446 | 1446 | | |
1447 | | - | |
| 1447 | + | |
| 1448 | + | |
| 1449 | + | |
| 1450 | + | |
| 1451 | + | |
| 1452 | + | |
| 1453 | + | |
| 1454 | + | |
1448 | 1455 | | |
1449 | 1456 | | |
1450 | 1457 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
110 | | - | |
111 | | - | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
112 | 131 | | |
113 | 132 | | |
114 | 133 | | |
115 | 134 | | |
116 | 135 | | |
117 | 136 | | |
118 | | - | |
| 137 | + | |
119 | 138 | | |
| 139 | + | |
120 | 140 | | |
121 | 141 | | |
122 | | - | |
123 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
124 | 145 | | |
125 | 146 | | |
126 | 147 | | |
| |||
149 | 170 | | |
150 | 171 | | |
151 | 172 | | |
152 | | - | |
| 173 | + | |
153 | 174 | | |
154 | 175 | | |
155 | 176 | | |
| |||
159 | 180 | | |
160 | 181 | | |
161 | 182 | | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
0 commit comments