Repository navigation
feat(ml): add GPU/Kaggle-ready training harness with presets, resume and ONNX export - #28
Merged
Merged
Conversation
…e, ONNX export and metrics.json - ml/train.py: --device (cpu|auto|0|0,1,2,3, DDP), --data, --imgsz, --model, --resume, --workers from CPU count; no hardcoded cpu - smoke / stage-a (synthetic pretrain) / stage-b (mixed fine-tune with real oversampling) presets - runs write best.pt, best.onnx, metrics.json (per-class P/R/F1/accuracy, gate verdict) - --dry-run prints the resolved plan without importing torch - ml/kaggle/train_kaggle.py: single-file Kaggle entry point - ml/make_smoke_dataset.py + ml/tests: harness tests and opt-in 2-epoch CPU smoke run
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
✅ Deploy Preview for digisim ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
This branch was successfully deployed
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.
Intent
DigiSim is a neglected project. Pull up the latest changes in Linux. It is high time to start training the model for it. I have not been able to provide the data. Figure out, get the data, train a model, make improvements and fix that. Do not come to me until you absolutely need me.
Context: a scout report found that the only trained detector uses 12 gate-era classes and detects nothing on the transistor-first schematics the app now uses, that the transistor-level training data does not exist yet, and that the wire/net tracing assumes left-to-right gates. It split the work into tasks T1-T11; this change is one of them, "T5 - Training harness for GPU/Kaggle" (T1, the class schema ml/classes.yaml as the single source of truth, is already merged). The substance of T5 from the report: refactor ml/train.py so it takes --device, --data, --imgsz, --model, supports resume, defaults --workers from the CPU count, and has no hardcoded cpu device (the old script hardcoded cpu in three places, always copied weights to a gitignored path and had no GPU/Kaggle path or resume); provide stage A (synthetic pretrain) and stage B (mixed fine-tune with real-image oversampling) as presets; and write ml/kaggle/train_kaggle.py as a single file that installs ultralytics, reads the dataset from /kaggle/input, and writes best.pt, an ONNX export and metrics.json. Real training is meant to run on Kaggle GPUs (DDP, device 0,1,2,3) while the laptop only does smoke runs. Acceptance from the report: a CPU smoke run of 2 epochs on 200 images completes in CI-like conditions and produces metrics.json and an ONNX file; the script runs unchanged on device=0,1,2,3 (checked by a dry-run flag); ruff clean. Class vocabulary must come from ml/classes.yaml; no weights, datasets or images are committed. No Kaggle or Roboflow credentials exist, so nothing here logs in to or uploads to Kaggle.
What Changed
ml/train.pyas a device-agnostic harness:--device cpu|auto|0|0,1,2,3(multi-index runs DDP, with no hardcoded cpu),--data,--model,--imgsz,--batch(total across GPUs),--workers(defaults from the CPU count),--resume RUN_DIRand--dry-run(prints the resolved plan without importing torch). Addedsmoke,stage-a(synthetic pretrain) andstage-b(mixed fine-tune with--real-trainoversampling via--real-oversample) presets. Training checksdata.yamlclass names againstml/classes.yaml, and each run folder getsbest.pt,best.onnxandmetrics.json(per-class P/R/F1/AP/accuracy, F1/accuracy gate verdict, environment, dataset counts). Copying weights to the gitignored install path is now opt-in (--install-weights).ml/kaggle/train_kaggle.py, a single-file Kaggle twin that pip-installs ultralytics/onnx, findsdata.yamlunder/kaggle/input, ignores Jupyter kernel args, supports--dry-runand--resume, and writes the same artifacts to/kaggle/working/digisim_run. It does not log in to or upload anything to Kaggle. Addedml/make_smoke_dataset.py, which generates a throwaway procedural YOLO dataset for harness checks, plusonnx,onnxslimandonnxruntimedependencies inml/pyproject.toml(withml/uv.lock).ml/tests/test_train_harness.pycovering device parsing, batch and worker defaults, device pass-through to ultralytics with CPU export, resume keeping real-image oversampling, a preset-parity check betweentrain.pyand the Kaggle script, dry-run for several device specs, class-drift detection, mixed-dataset leakage blocking and metrics assembly. It also contains a 2-epoch CPU smoke run gated behindDIGISIM_ML_SMOKE=1. Updatedagent_docs/ml-pipeline.mdwith a Training Harness section and the Kaggle training row.Risk Assessment
Testing
I drove the real harness end to end on CPU. Stage-A and Kaggle dry-runs with device 0,1,2,3 resolved to DDP with world size 4 and batch 64, and the CLI rejected a mistyped argument. A real 2-epoch smoke run (train.py) and a Kaggle-layout smoke run both produced best.pt, best.onnx and metrics.json. A stage-B run interrupted after one epoch resumed from epoch 2 and kept the real-image oversampling (still 4x in train_mixed.txt, config.yaml and metrics.json), and the harness test file passed. No GPU is available here, so actual multi-GPU DDP execution was only checked through the dry-run plan. There is no UI, so there are no screenshots; evidence is CLI logs and JSON in the evidence directory.
Evidence: CPU smoke run log (2 epochs, train, val, ONNX export)
Evidence: smoke metrics.json
Evidence: train.py dry-run for device 0,1,2,3 and device 0
Evidence: Kaggle script dry-run for device 0,1,2,3
~/.no-mistakes/evidence/01M7NJTQJYB57MKYJRZE5QSEM3/kaggle_smoke.log)~/.no-mistakes/evidence/01M7NJTQJYB57MKYJRZE5QSEM3/stageb_resume.log)Evidence: Stage-B config.yaml after resume (real_oversample 4 kept)
Pipeline
Updates from git push no-mistakes
✅ **intent** - passed
✅ No issues found.
🔧 **Rebase** - 1 issue found → auto-fixed ✅
ml/pyproject.toml- merge conflict rebasing onto origin/main🔧 Fix applied.
✅ Re-checked - no issues remain.
ml/kaggle/train_kaggle.py:464- Resuming a stage-B run silently drops real-image oversampling and rewrites the run's record. Inml/kaggle/train_kaggle.py,build_planreuses saved preset, imgsz and data_yaml on--resume, but takesreal_trainandreal_oversampleonly from the CLI.run()then callsprepare_data_yaml(...)with no real_train. That overwritesout/data.yaml, the file ultralytics stored in the checkpoint's args and re-reads onresume=True. Training therefore continues on the un-oversampled train split with no error. Sibling sites:ml/train.pyresolve_config(around line 425) andtrain()(around line 615) also ignore savedreal_trainandreal_oversampleand rewriteconfig.yamlwithreal_train=[]and oversample 1.metrics.jsonthen reportsreal_oversample: 1for a run that trained with 4. Other settings are reused from the saved config but are likewise not restored:epochsin the Kaggle plan, andpatience,lr0andseedin both scripts. Fix: on resume, restore these from the savedconfig.yaml, or at least skip rewritingdata.yamlandconfig.yamlfor a resumed run.ml/tests/test_train_harness.py:131-test_no_hardcoded_cpu_in_train_callsonly readstrain.pyandtrain_kaggle.pyand counts the substringdevice="cpu"and aformat="onnx"line. That violates the repo test-quality rule (source-text assertions). Matching text can sit in a comment or dead code, and a behavior-preserving refactor, such as moving the export device into a constant, breaks the test. It also proves nothing about runtime behavior. Replace it with a test that runs the real code path and checks thedevicevalue passed to ultralytics. For example, stubultralytics.YOLOand assert thattrain()passescfg.device.spectomodel.train.ml/kaggle/train_kaggle.py:568-main()usesparse_known_argsand discards the unknown arguments, so on a paid multi-GPU run a typo such as--real_trainor--dataset_hintis silently ignored. The run would then start without oversampling and with the wrong dataset selection. The only arguments that need ignoring are the Jupyter kernel's-f <file>. Narrow the filter to-fand its value, and let argparse reject everything else.ml/tests/test_train_harness.py:280- The acceptance criterion says a 2-epoch CPU smoke run on 200 images must complete in CI-like conditions and produce metrics.json and an ONNX file. The only test that checks this is opt-in (DIGISIM_ML_SMOKE=1, skipped by default). It also downloadsyolov8n.pt, so defaultpytestand CI never exercise the training, export and metrics path. Everything else in the harness is only dry-run or unit tested. This is acceptable if the author ran it manually, but the criterion is not enforced automatically.🔧 Fix applied.
1 info still open:
ml/tests/test_train_harness.py:280- The acceptance criterion says a 2-epoch CPU smoke run on 200 images must complete in CI-like conditions and produce metrics.json and an ONNX file. The only test that checks this is opt-in (DIGISIM_ML_SMOKE=1, skipped by default). It also downloadsyolov8n.pt, so defaultpytestand CI never exercise the training, export and metrics path. Everything else in the harness is only dry-run or unit tested. This is acceptable if the author ran it manually, but the criterion is not enforced automatically.ml/train.py:644- On --resume, batch and workers are not restored from the saved config.yaml. A stage-B run started with --batch 8 resumed with the preset default of 16, and config.yaml and metrics.json were rewritten with 16. Ultralytics does let batch, imgsz and device be overridden on resume, and batch is hardware-dependent, so this may be intended. It could OOM if someone resumes on a smaller GPU without passing --batch again.python ml/make_smoke_dataset.py --out <tmp>/data --count 200(200 synthetic images, 14 classes, split 140/30/30)train.py --preset smoke --device cpu --class-check offon the 200-image set (real 2-epoch YOLOv8n CPU training, about 2m40s, rc=0)train.py --preset stage-a --device 0,1,2,3 --dry-runand--device 0 --dry-run(resolved plan JSON, no torch import)kaggle/train_kaggle.py --device 0,1,2,3 --dry-run, and with the typo--real_train x(argparse rejects it)kaggle/train_kaggle.py --preset smoke --input-root <fake /kaggle/input> --device cpu --skip-install(discovers the dataset, writes best.pt, best.onnx, metrics.json)Stage-B run with--real-train <folder of real-style images> --real-oversample 4: it stopped after epoch 1, thentrain.py --resume <run dir>ran with no other flagsStage-B with the real folder pointing at the validation images (the leak guard)uv run pytest ml/tests/test_train_harness.py -q: 31 passed, 1 skipped (the opt-in smoke test)✅ **Document** - passed
✅ No issues found.
✅ **Lint** - passed
✅ No issues found.
✅ **Push** - passed
✅ No issues found.