Fix segmentation crashes, dead endpoint, and path/CORS hardening - #1
Open
Alvin-Nahabwe wants to merge 3 commits into
Open
Fix segmentation crashes, dead endpoint, and path/CORS hardening#1Alvin-Nahabwe wants to merge 3 commits into
Alvin-Nahabwe wants to merge 3 commits into
Conversation
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>
Collaborator
Author
|
Follow-up commits pushed: |
This was referenced Jul 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.pycalledDataset.from_dict()but only importedDatasetDict→ guaranteedNameErroron the defaultis_presplitpath. A second unbound-variable crash (all_mask_paths) hit the non-presplit path.image_segmentation_inference.pyusedpred_segin the overlay step but never computed it — the post-processing block was empty. The SMP branch also readmodel.config, which raw U-Net models don't have. Logits are now upsampled to the source resolution beforeargmax, and the palette is seeded so overlays are reproducible./inference/image-segmentationshelled out tosemantic_segmentation_inference.py, which does not exist (the file isimage_segmentation_inference.py).Correctness
CoarseDropout(max_holes=…), a removed Albumentations API → crashed whenever augmentation was enabled. Aligned to the current API already used by the classification path.--early_stopping_thresholdargument and a duplicated, unreachableexceptblock./inference/asr: it shells out to a nonexistentasr_inference.py, and ASR has no training endpoint, registry entry, or script anywhere in the service.import os.Security / robustness
"*"together withallow_credentials=True, a combination browsers reject. Origins now come fromALLOWED_ORIGINS; credentials enable only for an explicit allow-list.is_valid_checkpoint_pathusedstartswith, so a sibling directory namedmodel_outputs_evilpassed themodel_outputscontainment check. Now usesos.path.commonpath.pidmigration issued a raw-stringSELECT, which raisesObjectNotExecutableErroron SQLAlchemy 2.x — so the migration silently never ran. Now uses the inspector +text().API_KEYnow warns loudly when falling back to the development default instead of doing so silently.Verification
python -m py_compilepasses 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-apphas drifted from this API's contract (missingX-API-Key,data_zipvsdata_file,weight_decayvsweight_decay_hf, plus a dead ASR task). A companion PR addresses that side.🤖 Generated with Claude Code