Skip to content

Fix segmentation crashes, dead endpoint, and path/CORS hardening - #1

Open
Alvin-Nahabwe wants to merge 3 commits into
mainfrom
fix/dl-pipeline-critical
Open

Fix segmentation crashes, dead endpoint, and path/CORS hardening#1
Alvin-Nahabwe wants to merge 3 commits into
mainfrom
fix/dl-pipeline-critical

Conversation

@Alvin-Nahabwe

Copy link
Copy Markdown
Collaborator

Summary

Semantic segmentation was broken end-to-end — it could never have completed a training run or an inference call. This PR fixes that plus a set of correctness and hardening issues found in the same audit.

Critical (segmentation was 100% broken)

  • image_segmentation_train.py called Dataset.from_dict() but only imported DatasetDict → guaranteed NameError on the default is_presplit path. A second unbound-variable crash (all_mask_paths) hit the non-presplit path.
  • image_segmentation_inference.py used pred_seg in the overlay step but never computed it — the post-processing block was empty. The SMP branch also read model.config, which raw U-Net models don't have. Logits are now upsampled to the source resolution before argmax, and the palette is seeded so overlays are reproducible.
  • /inference/image-segmentation shelled out to semantic_segmentation_inference.py, which does not exist (the file is image_segmentation_inference.py).

Correctness

  • Segmentation augmentation called CoarseDropout(max_holes=…), a removed Albumentations API → crashed whenever augmentation was enabled. Aligned to the current API already used by the classification path.
  • Removed a duplicated --early_stopping_threshold argument and a duplicated, unreachable except block.
  • Removed /inference/asr: it shells out to a nonexistent asr_inference.py, and ASR has no training endpoint, registry entry, or script anywhere in the service.
  • Removed a duplicate import os.

Security / robustness

  • CORS allowed "*" together with allow_credentials=True, a combination browsers reject. Origins now come from ALLOWED_ORIGINS; credentials enable only for an explicit allow-list.
  • is_valid_checkpoint_path used startswith, so a sibling directory named model_outputs_evil passed the model_outputs containment check. Now uses os.path.commonpath.
  • The pid migration issued a raw-string SELECT, which raises ObjectNotExecutableError on SQLAlchemy 2.x — so the migration silently never ran. Now uses the inspector + text().
  • API_KEY now warns loudly when falling back to the development default instead of doing so silently.

Verification

python -m py_compile passes on every changed file; all edits reviewed as diffs.

Not verified: no GPU/torch environment was available, so no training or inference run was performed. Please smoke-test one run per task (train → poll → infer) before merging to a deployed environment.

Related

The R client in no-code-app has drifted from this API's contract (missing X-API-Key, data_zip vs data_file, weight_decay vs weight_decay_hf, plus a dead ASR task). A companion PR addresses that side.

🤖 Generated with Claude Code

Alvin-Nahabwe and others added 3 commits July 10, 2026 11:25
Semantic segmentation was broken end-to-end. Three independent defects:

- image_segmentation_train.py used Dataset.from_dict() without importing
  Dataset (NameError on the default is_presplit path), and referenced
  all_mask_paths before assignment on the non-presplit path.
- image_segmentation_inference.py used pred_seg in the overlay step but
  never computed it; the post-processing block was empty. The SMP branch
  also read model.config, which raw U-Net models do not have. Logits are
  now upsampled to the source resolution before argmax, and the palette
  is seeded so overlays are stable across runs.
- /inference/image-segmentation shelled out to semantic_segmentation_
  inference.py, which does not exist.

Also in this change:

- Segmentation augmentation called CoarseDropout with the removed
  max_holes/max_height API, crashing whenever augmentation was enabled.
  Aligned to the current API already used by the classification path.
- Removed a duplicated --early_stopping_threshold argument and a
  duplicated, unreachable except block.
- Removed /inference/asr: it shells out to a nonexistent asr_inference.py
  and ASR has no training endpoint, registry entry, or script.
- CORS allowed "*" together with credentials, which browsers reject.
  Origins are now set via ALLOWED_ORIGINS and credentials are only
  enabled for an explicit allow-list.
- is_valid_checkpoint_path used startswith, so a sibling directory named
  model_outputs_evil passed the model_outputs containment check.
- The pid migration issued a raw-string SELECT, which raises
  ObjectNotExecutableError on SQLAlchemy 2.x, so it silently never ran.
- API_KEY now warns loudly when falling back to the development default.

Verified with python -m py_compile on every changed file. Not exercised
against a GPU: no training or inference run was performed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Running the pipeline against a small synthetic dataset (the fixes in the
previous commit only got it to compile) surfaced six runtime defects that
static analysis could not, all triggered by current library versions
(transformers 5.x, albumentations 2.x, segmentation-models-pytorch 0.5).

Shared path (SegFormer and SMP):
- apply_transforms received masks as datasets' {path,bytes} dicts, not
  PIL images, and crashed on the first batch. Added mask_to_array(),
  which also reads palette ("P") masks as raw class ids rather than
  remapping them through .convert("L").
- Mean IoU silently computed 0: masks were flattened to 1D (mean_iou
  needs per-image 2D arrays) and SegFormer's quarter-resolution logits
  were never upsampled to the label size. Fixed both; this also restores
  early stopping and best-model selection, which key off eval_mean_iou.

SMP U-Net / U-Net++ path (previously crashed four different ways):
- The Trainer sets model.config.use_cache, but SMP exposes config as a
  read-only property. Shadow it (and load_state_dict) on a per-instance
  subclass carrying a PretrainedConfig; SMP's class is left untouched.
- SmpTrainer only overrode compute_loss, so evaluation called the model
  with pixel_values=/labels= kwargs it does not accept. Added a matching
  prediction_step.
- load_best_model_at_end calls load_state_dict(state_dict, strict) posi-
  tionally; SMP takes strict only as a keyword. Added a positional->kw
  adapter that preserves SMP's timm-encoder key remapping.
- The Trainer saved weights as model.safetensors with no config.json,
  but inference read pytorch_model.bin and needed config.json. Write the
  SMP config on save; load safetensors-or-bin on inference. Also guarded
  image_processor.save_pretrained (None for SMP).

Verified: both model families complete train -> evaluate -> save ->
inference on a synthetic fixture (CPU), producing real per-class IoU and
valid mask overlays. py_compile passes.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
object_detection_train.py passed tokenizer=image_processor to the HF
Trainer, but the deprecated `tokenizer` argument was removed in
transformers 5.x (renamed processing_class). DETR/YOLOS training crashed
with "Trainer.__init__() got an unexpected keyword argument 'tokenizer'".
The classification and segmentation scripts already use processing_class.

Verified: DETR completes train -> evaluate -> save -> inference on a
synthetic COCO fixture, producing real mAP metrics.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@Alvin-Nahabwe

Copy link
Copy Markdown
Collaborator Author

Follow-up commits pushed: f8b3fc2 (six runtime bugs found by running the pipeline on a synthetic dataset — mask decoding, mean-IoU computing 0, and four SMP U-Net crashes) and 185b09d (object detection tokenizerprocessing_class for transformers 5.x). All five model paths (classification, DETR, YOLO, SegFormer, U-Net) now complete train→evaluate→save→inference on CPU. Dependency pinning that makes this reproducible is in #2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant