smartrain 0.0.1__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 (115) hide show
  1. smartrain-0.0.1/PKG-INFO +150 -0
  2. smartrain-0.0.1/README.md +122 -0
  3. smartrain-0.0.1/pyproject.toml +55 -0
  4. smartrain-0.0.1/setup.cfg +4 -0
  5. smartrain-0.0.1/smartrain/__init__.py +3 -0
  6. smartrain-0.0.1/smartrain/__main__.py +4 -0
  7. smartrain-0.0.1/smartrain/clearml_upload.py +149 -0
  8. smartrain-0.0.1/smartrain/cli.py +1253 -0
  9. smartrain-0.0.1/smartrain/cli_argparse.py +11 -0
  10. smartrain-0.0.1/smartrain/cli_prompts.py +194 -0
  11. smartrain-0.0.1/smartrain/cli_replay.py +85 -0
  12. smartrain-0.0.1/smartrain/cvat11_converter.py +633 -0
  13. smartrain-0.0.1/smartrain/cvat_cli.py +165 -0
  14. smartrain-0.0.1/smartrain/data_yaml_normalize.py +229 -0
  15. smartrain-0.0.1/smartrain/dataset_access.py +98 -0
  16. smartrain-0.0.1/smartrain/dataset_augment.py +1374 -0
  17. smartrain-0.0.1/smartrain/dataset_balance.py +907 -0
  18. smartrain-0.0.1/smartrain/dataset_former.py +1330 -0
  19. smartrain-0.0.1/smartrain/dataset_hash.py +306 -0
  20. smartrain-0.0.1/smartrain/dataset_orient.py +895 -0
  21. smartrain-0.0.1/smartrain/dataset_passport.py +72 -0
  22. smartrain-0.0.1/smartrain/dataset_prune.py +386 -0
  23. smartrain-0.0.1/smartrain/dataset_report.py +1416 -0
  24. smartrain-0.0.1/smartrain/dataset_roi_yolo.py +999 -0
  25. smartrain-0.0.1/smartrain/dataset_stats.py +1287 -0
  26. smartrain-0.0.1/smartrain/datasets_json_former.py +1494 -0
  27. smartrain-0.0.1/smartrain/external_model_ref.py +44 -0
  28. smartrain-0.0.1/smartrain/external_providers/__init__.py +2 -0
  29. smartrain-0.0.1/smartrain/external_providers/adapters.py +287 -0
  30. smartrain-0.0.1/smartrain/external_providers/base.py +21 -0
  31. smartrain-0.0.1/smartrain/external_providers/installer.py +272 -0
  32. smartrain-0.0.1/smartrain/external_providers/launchers/__init__.py +2 -0
  33. smartrain-0.0.1/smartrain/external_providers/launchers/mfel_infer_launcher.py +105 -0
  34. smartrain-0.0.1/smartrain/external_providers/launchers/mfel_train_launcher.py +139 -0
  35. smartrain-0.0.1/smartrain/external_providers/launchers/mfel_val_launcher.py +129 -0
  36. smartrain-0.0.1/smartrain/external_providers/launchers/mp_infer_launcher.py +36 -0
  37. smartrain-0.0.1/smartrain/external_providers/launchers/mp_train_launcher.py +42 -0
  38. smartrain-0.0.1/smartrain/external_providers/probe.py +64 -0
  39. smartrain-0.0.1/smartrain/external_providers/registry.py +73 -0
  40. smartrain-0.0.1/smartrain/external_providers/runner.py +82 -0
  41. smartrain-0.0.1/smartrain/heatmap_cli.py +56 -0
  42. smartrain-0.0.1/smartrain/inference_cli.py +937 -0
  43. smartrain-0.0.1/smartrain/interactive_contract.py +18 -0
  44. smartrain-0.0.1/smartrain/label_pred_match.py +642 -0
  45. smartrain-0.0.1/smartrain/migrate_models_to_smartrain.py +191 -0
  46. smartrain-0.0.1/smartrain/model_convert_cli.py +491 -0
  47. smartrain-0.0.1/smartrain/model_release_cli.py +467 -0
  48. smartrain-0.0.1/smartrain/model_training_module.py +2210 -0
  49. smartrain-0.0.1/smartrain/path_portable.py +58 -0
  50. smartrain-0.0.1/smartrain/plot_creator.py +23 -0
  51. smartrain-0.0.1/smartrain/provider_global_index.py +150 -0
  52. smartrain-0.0.1/smartrain/provider_install_state.py +34 -0
  53. smartrain-0.0.1/smartrain/providers_cli.py +188 -0
  54. smartrain-0.0.1/smartrain/registry_cli.py +298 -0
  55. smartrain-0.0.1/smartrain/results_analyzer.py +1181 -0
  56. smartrain-0.0.1/smartrain/sahi_cli.py +91 -0
  57. smartrain-0.0.1/smartrain/train_backend_registry.py +98 -0
  58. smartrain-0.0.1/smartrain/train_model_catalog.py +105 -0
  59. smartrain-0.0.1/smartrain/train_model_resolver.py +53 -0
  60. smartrain-0.0.1/smartrain/train_profile.py +132 -0
  61. smartrain-0.0.1/smartrain/train_resume.py +240 -0
  62. smartrain-0.0.1/smartrain/training_queue.py +263 -0
  63. smartrain-0.0.1/smartrain/training_queue_cli.py +255 -0
  64. smartrain-0.0.1/smartrain/weighted_yolo_dataset.py +85 -0
  65. smartrain-0.0.1/smartrain/workspace_path_repair.py +229 -0
  66. smartrain-0.0.1/smartrain/workspace_paths.py +283 -0
  67. smartrain-0.0.1/smartrain/yolo_labels.py +150 -0
  68. smartrain-0.0.1/smartrain.egg-info/PKG-INFO +150 -0
  69. smartrain-0.0.1/smartrain.egg-info/SOURCES.txt +113 -0
  70. smartrain-0.0.1/smartrain.egg-info/dependency_links.txt +1 -0
  71. smartrain-0.0.1/smartrain.egg-info/entry_points.txt +2 -0
  72. smartrain-0.0.1/smartrain.egg-info/requires.txt +24 -0
  73. smartrain-0.0.1/smartrain.egg-info/top_level.txt +1 -0
  74. smartrain-0.0.1/tests/test_cli_prompts.py +47 -0
  75. smartrain-0.0.1/tests/test_cli_replay.py +103 -0
  76. smartrain-0.0.1/tests/test_cli_subprocess.py +417 -0
  77. smartrain-0.0.1/tests/test_cvat11_converter.py +82 -0
  78. smartrain-0.0.1/tests/test_cvat11_native_dataset_former.py +72 -0
  79. smartrain-0.0.1/tests/test_data_yaml_normalize.py +60 -0
  80. smartrain-0.0.1/tests/test_dataset_access_roi_hash.py +490 -0
  81. smartrain-0.0.1/tests/test_dataset_augment.py +170 -0
  82. smartrain-0.0.1/tests/test_dataset_augment_copy_paste.py +53 -0
  83. smartrain-0.0.1/tests/test_dataset_balance.py +332 -0
  84. smartrain-0.0.1/tests/test_dataset_former_partial.py +523 -0
  85. smartrain-0.0.1/tests/test_dataset_orient.py +149 -0
  86. smartrain-0.0.1/tests/test_dataset_prune.py +107 -0
  87. smartrain-0.0.1/tests/test_dataset_report.py +152 -0
  88. smartrain-0.0.1/tests/test_dataset_stats.py +278 -0
  89. smartrain-0.0.1/tests/test_dataset_stats_compare.py +130 -0
  90. smartrain-0.0.1/tests/test_datasets_list_support.py +111 -0
  91. smartrain-0.0.1/tests/test_datasets_scan_summary.py +211 -0
  92. smartrain-0.0.1/tests/test_external_model_ref.py +26 -0
  93. smartrain-0.0.1/tests/test_external_providers_adapters.py +105 -0
  94. smartrain-0.0.1/tests/test_external_providers_installer.py +52 -0
  95. smartrain-0.0.1/tests/test_external_providers_installer_runtime.py +114 -0
  96. smartrain-0.0.1/tests/test_external_providers_probe.py +39 -0
  97. smartrain-0.0.1/tests/test_external_providers_runner.py +119 -0
  98. smartrain-0.0.1/tests/test_fusion_split.py +23 -0
  99. smartrain-0.0.1/tests/test_imports.py +54 -0
  100. smartrain-0.0.1/tests/test_inference_cli.py +280 -0
  101. smartrain-0.0.1/tests/test_mfel_launchers.py +87 -0
  102. smartrain-0.0.1/tests/test_path_portable.py +111 -0
  103. smartrain-0.0.1/tests/test_provider_global_index.py +32 -0
  104. smartrain-0.0.1/tests/test_providers_doctor_runtime.py +41 -0
  105. smartrain-0.0.1/tests/test_providers_install_cli.py +24 -0
  106. smartrain-0.0.1/tests/test_providers_uninstall_cli.py +24 -0
  107. smartrain-0.0.1/tests/test_train_interactive.py +635 -0
  108. smartrain-0.0.1/tests/test_train_model_catalog.py +75 -0
  109. smartrain-0.0.1/tests/test_train_profile.py +128 -0
  110. smartrain-0.0.1/tests/test_train_resume.py +95 -0
  111. smartrain-0.0.1/tests/test_train_runtime_data_yaml.py +74 -0
  112. smartrain-0.0.1/tests/test_train_val_batch_defaults.py +114 -0
  113. smartrain-0.0.1/tests/test_training_metadata_provider.py +24 -0
  114. smartrain-0.0.1/tests/test_workspace.py +52 -0
  115. smartrain-0.0.1/tests/test_workspace_path_repair.py +68 -0
@@ -0,0 +1,150 @@
1
+ Metadata-Version: 2.4
2
+ Name: smartrain
3
+ Version: 0.0.1
4
+ Summary: YOLO datasets, training queue, runs analytics — workspace-first CLI
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
7
+ Requires-Dist: ultralytics>=8.0.0
8
+ Requires-Dist: pyyaml>=6.0
9
+ Requires-Dist: tqdm>=4.65.0
10
+ Requires-Dist: pandas>=2.0.0
11
+ Requires-Dist: matplotlib>=3.7.0
12
+ Requires-Dist: pillow>=10.0.0
13
+ Requires-Dist: numpy>=1.24.0
14
+ Requires-Dist: albumentations>=1.4.0
15
+ Requires-Dist: typer>=0.9.0
16
+ Requires-Dist: rich>=13.0.0
17
+ Requires-Dist: prompt_toolkit>=3.0.0
18
+ Requires-Dist: fpdf2>=2.7.0
19
+ Requires-Dist: odfpy>=1.4.0
20
+ Requires-Dist: pypandoc-binary>=1.14.0
21
+ Requires-Dist: weasyprint>=60.0
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=7.4.0; extra == "dev"
24
+ Provides-Extra: clearml
25
+ Requires-Dist: clearml>=1.14.0; extra == "clearml"
26
+ Provides-Extra: sahi
27
+ Requires-Dist: sahi>=0.11.0; extra == "sahi"
28
+
29
+ > Russian version: [docs/ru/README.md](docs/ru/README.md)
30
+
31
+ # Smart Train (`smartrain`)
32
+
33
+ A CLI toolkit for preparing YOLO datasets, training models, running queues, and analyzing runs.
34
+
35
+ ## Quick start
36
+
37
+ Requirements: Python `3.10+`.
38
+
39
+ ```bash
40
+ git clone <repo-url>
41
+ cd smart-train
42
+ pip install -e .
43
+ ```
44
+
45
+ Work from the project root (current directory):
46
+
47
+ ```bash
48
+ smartrain deploy
49
+ smartrain scan
50
+ smartrain fusion --dataset ds_a --dataset ds_b --classes "class_a,class_b"
51
+ smartrain train --data 2026-01-01_12-00-00-merged -y
52
+ ```
53
+
54
+ ## What's included
55
+
56
+ - Single entry point: `smartrain` (module `smartrain.cli`).
57
+ - Single-workspace model: `raw_data/`, `datasets/`, `runs/`, `analytics/`, `models/`, `inference/`, `tmp/`.
58
+ - Pipeline support: `scan -> fusion -> train -> analyze`.
59
+ - Additional tools: `queue`, `registry`, `report`, `model`, `normalize-data-yaml`, `migrate-models`, `clearml-upload`, `plot`, `cvat`, `sahi`, `heatmap`, `orient`.
60
+
61
+ ## How it works
62
+
63
+ `smartrain` uses a single workspace root and builds a process around file contracts:
64
+
65
+ - `scan` synchronizes sources and updates the dataset catalog;
66
+ - `fusion` generates the final dataset for training;
67
+ - `train` creates a run directory with metrics and metadata;
68
+ - `analyze` and `registry` work on artifacts in `runs/`.
69
+
70
+ ## Key commands
71
+
72
+ | Command | Purpose |
73
+ |---|---|
74
+ | `smartrain deploy` | Initialize the workspace structure |
75
+ | `smartrain scan` | Synchronize sources and update the dataset catalog |
76
+ | `smartrain fusion` | Build the final training dataset |
77
+ | `smartrain train` | Train and validate YOLO models |
78
+ | `smartrain inference` | Run inference on folder or dataset split and save JSON report |
79
+ | `smartrain queue` / `smartrain queue-run` | Manage and run the command queue |
80
+ | `smartrain analyze` | Summaries, run comparison, PR curves, and inference benchmarks |
81
+ | `smartrain registry` | Catalog run artifacts and promoted models |
82
+
83
+ ## Documentation
84
+
85
+ Current documentation is organized into sections in `docs/`:
86
+
87
+ - [Documentation navigation](docs/index.md)
88
+ - [Getting started and core workflows](docs/getting-started/quickstart.md)
89
+ - [CLI guide](docs/cli/overview.md)
90
+ - [API and format reference](docs/reference/api.md)
91
+ - [Architecture and diagrams](docs/development/architecture.md)
92
+
93
+ ## Testing
94
+
95
+ ```bash
96
+ pip install -e ".[dev]"
97
+ pytest
98
+ ```
99
+
100
+ ## Important details
101
+
102
+ - Interactive mode starts only when a command is launched with zero arguments (TTY required).
103
+ - Interactive dataset commands: `fusion`, `augment`, `balance`, `stats`, `roi`, `orient`, `inference`; plus `train`.
104
+ - Dataset cleanup command: `prune` (`prune empty` for empty pairs, `prune dedup` for duplicate images by content).
105
+ - If any arguments are provided but required ones are missing, commands return a clear "incomplete arguments" error instead of interactive prompts.
106
+ - Command help now includes practical `Examples` / `Quick examples` blocks for common workflows.
107
+ - `smartrain balance` presets:
108
+ - `--preset weights-safe` for conservative balancing
109
+ - `--preset rfs-aggressive` for stronger tail upsampling
110
+ - `--preset hybrid-default` as a general default
111
+ - `smartrain balance` eval splits: `--eval-coverage` is on by default (keeps `val`/`test` non-empty when possible and improves class coverage there); use `--no-eval-coverage` to disable. The interactive wizard asks for this option.
112
+ - For `hash --validate`: `0` for a match, `1` for a mismatch, `2` for an error.
113
+ - By default, the workspace queue uses `queue.txt` and `tmp/status.txt`.
114
+ - Dependency extras:
115
+ - `pip install -e ".[dev]"` for development and testing
116
+ - `pip install -e ".[clearml]"` for ClearML
117
+ - `pip install -e ".[sahi]"` for SAHI
118
+
119
+ ## Common workflows
120
+
121
+ Scanning with an explicit source list:
122
+
123
+ ```bash
124
+ smartrain scan --datasets-list /path/to/workspace/raw_data/datasets_list.txt
125
+ ```
126
+
127
+ Check dataset hash:
128
+
129
+ ```bash
130
+ smartrain hash --dataset my_dataset
131
+ smartrain hash /path/to/dataset --validate a1b2c3d4
132
+ ```
133
+
134
+ Starting a queue without opening a GUI terminal:
135
+
136
+ ```bash
137
+ smartrain queue run --no-gui
138
+ ```
139
+
140
+ Quick run overview:
141
+
142
+ ```bash
143
+ smartrain analyze scan
144
+ smartrain analyze export-table -o runs_summary.csv
145
+ ```
146
+
147
+ ## Developers
148
+
149
+ - [@palexab](https://github.com/palexab)
150
+ - [@greisersem](https://github.com/greisersem)
@@ -0,0 +1,122 @@
1
+ > Russian version: [docs/ru/README.md](docs/ru/README.md)
2
+
3
+ # Smart Train (`smartrain`)
4
+
5
+ A CLI toolkit for preparing YOLO datasets, training models, running queues, and analyzing runs.
6
+
7
+ ## Quick start
8
+
9
+ Requirements: Python `3.10+`.
10
+
11
+ ```bash
12
+ git clone <repo-url>
13
+ cd smart-train
14
+ pip install -e .
15
+ ```
16
+
17
+ Work from the project root (current directory):
18
+
19
+ ```bash
20
+ smartrain deploy
21
+ smartrain scan
22
+ smartrain fusion --dataset ds_a --dataset ds_b --classes "class_a,class_b"
23
+ smartrain train --data 2026-01-01_12-00-00-merged -y
24
+ ```
25
+
26
+ ## What's included
27
+
28
+ - Single entry point: `smartrain` (module `smartrain.cli`).
29
+ - Single-workspace model: `raw_data/`, `datasets/`, `runs/`, `analytics/`, `models/`, `inference/`, `tmp/`.
30
+ - Pipeline support: `scan -> fusion -> train -> analyze`.
31
+ - Additional tools: `queue`, `registry`, `report`, `model`, `normalize-data-yaml`, `migrate-models`, `clearml-upload`, `plot`, `cvat`, `sahi`, `heatmap`, `orient`.
32
+
33
+ ## How it works
34
+
35
+ `smartrain` uses a single workspace root and builds a process around file contracts:
36
+
37
+ - `scan` synchronizes sources and updates the dataset catalog;
38
+ - `fusion` generates the final dataset for training;
39
+ - `train` creates a run directory with metrics and metadata;
40
+ - `analyze` and `registry` work on artifacts in `runs/`.
41
+
42
+ ## Key commands
43
+
44
+ | Command | Purpose |
45
+ |---|---|
46
+ | `smartrain deploy` | Initialize the workspace structure |
47
+ | `smartrain scan` | Synchronize sources and update the dataset catalog |
48
+ | `smartrain fusion` | Build the final training dataset |
49
+ | `smartrain train` | Train and validate YOLO models |
50
+ | `smartrain inference` | Run inference on folder or dataset split and save JSON report |
51
+ | `smartrain queue` / `smartrain queue-run` | Manage and run the command queue |
52
+ | `smartrain analyze` | Summaries, run comparison, PR curves, and inference benchmarks |
53
+ | `smartrain registry` | Catalog run artifacts and promoted models |
54
+
55
+ ## Documentation
56
+
57
+ Current documentation is organized into sections in `docs/`:
58
+
59
+ - [Documentation navigation](docs/index.md)
60
+ - [Getting started and core workflows](docs/getting-started/quickstart.md)
61
+ - [CLI guide](docs/cli/overview.md)
62
+ - [API and format reference](docs/reference/api.md)
63
+ - [Architecture and diagrams](docs/development/architecture.md)
64
+
65
+ ## Testing
66
+
67
+ ```bash
68
+ pip install -e ".[dev]"
69
+ pytest
70
+ ```
71
+
72
+ ## Important details
73
+
74
+ - Interactive mode starts only when a command is launched with zero arguments (TTY required).
75
+ - Interactive dataset commands: `fusion`, `augment`, `balance`, `stats`, `roi`, `orient`, `inference`; plus `train`.
76
+ - Dataset cleanup command: `prune` (`prune empty` for empty pairs, `prune dedup` for duplicate images by content).
77
+ - If any arguments are provided but required ones are missing, commands return a clear "incomplete arguments" error instead of interactive prompts.
78
+ - Command help now includes practical `Examples` / `Quick examples` blocks for common workflows.
79
+ - `smartrain balance` presets:
80
+ - `--preset weights-safe` for conservative balancing
81
+ - `--preset rfs-aggressive` for stronger tail upsampling
82
+ - `--preset hybrid-default` as a general default
83
+ - `smartrain balance` eval splits: `--eval-coverage` is on by default (keeps `val`/`test` non-empty when possible and improves class coverage there); use `--no-eval-coverage` to disable. The interactive wizard asks for this option.
84
+ - For `hash --validate`: `0` for a match, `1` for a mismatch, `2` for an error.
85
+ - By default, the workspace queue uses `queue.txt` and `tmp/status.txt`.
86
+ - Dependency extras:
87
+ - `pip install -e ".[dev]"` for development and testing
88
+ - `pip install -e ".[clearml]"` for ClearML
89
+ - `pip install -e ".[sahi]"` for SAHI
90
+
91
+ ## Common workflows
92
+
93
+ Scanning with an explicit source list:
94
+
95
+ ```bash
96
+ smartrain scan --datasets-list /path/to/workspace/raw_data/datasets_list.txt
97
+ ```
98
+
99
+ Check dataset hash:
100
+
101
+ ```bash
102
+ smartrain hash --dataset my_dataset
103
+ smartrain hash /path/to/dataset --validate a1b2c3d4
104
+ ```
105
+
106
+ Starting a queue without opening a GUI terminal:
107
+
108
+ ```bash
109
+ smartrain queue run --no-gui
110
+ ```
111
+
112
+ Quick run overview:
113
+
114
+ ```bash
115
+ smartrain analyze scan
116
+ smartrain analyze export-table -o runs_summary.csv
117
+ ```
118
+
119
+ ## Developers
120
+
121
+ - [@palexab](https://github.com/palexab)
122
+ - [@greisersem](https://github.com/greisersem)
@@ -0,0 +1,55 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "smartrain"
7
+ version = "0.0.1"
8
+ description = "YOLO datasets, training queue, runs analytics — workspace-first CLI"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ dependencies = [
12
+ "ultralytics>=8.0.0",
13
+ "pyyaml>=6.0",
14
+ "tqdm>=4.65.0",
15
+ "pandas>=2.0.0",
16
+ "matplotlib>=3.7.0",
17
+ "pillow>=10.0.0",
18
+ "numpy>=1.24.0",
19
+ "albumentations>=1.4.0",
20
+ "typer>=0.9.0",
21
+ "rich>=13.0.0",
22
+ "prompt_toolkit>=3.0.0",
23
+ "fpdf2>=2.7.0",
24
+ "odfpy>=1.4.0",
25
+ "pypandoc-binary>=1.14.0",
26
+ "weasyprint>=60.0",
27
+ ]
28
+
29
+ [project.scripts]
30
+ smartrain = "smartrain.cli:main"
31
+
32
+ [tool.setuptools.packages.find]
33
+ where = ["."]
34
+ include = ["smartrain*"]
35
+
36
+ [project.optional-dependencies]
37
+ dev = [
38
+ "pytest>=7.4.0",
39
+ ]
40
+ clearml = [
41
+ "clearml>=1.14.0",
42
+ ]
43
+ sahi = [
44
+ "sahi>=0.11.0",
45
+ ]
46
+
47
+ [tool.pytest.ini_options]
48
+ testpaths = ["tests"]
49
+ pythonpath = ["."]
50
+ filterwarnings = [
51
+ "ignore:Error fetching version info:UserWarning:albumentations\\.check_version",
52
+ "ignore:ShiftScaleRotate is a special case of Affine transform\\. Please use Affine transform instead\\.:UserWarning:albumentations\\.core\\.validation",
53
+ "ignore:The distutils package is deprecated and slated for removal in Python 3\\.12\\.:DeprecationWarning:redis\\.connection",
54
+ "ignore::UserWarning:torch\\.autograd\\.graph",
55
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,3 @@
1
+ """Package to install: CLI `smartrain`, workspace from the current directory."""
2
+
3
+ __version__ = "0.1.0"
@@ -0,0 +1,4 @@
1
+ from smartrain.cli import main
2
+
3
+ if __name__ == "__main__":
4
+ main()
@@ -0,0 +1,149 @@
1
+ """
2
+ After the fact, loading the Ultralytics run catalog into ClearML.
3
+ """
4
+ from __future__ import annotations
5
+
6
+ import argparse
7
+ import os
8
+ from pathlib import Path
9
+
10
+ from smartrain.cli_argparse import CliArgumentParser
11
+
12
+
13
+ def build_clearml_upload_arg_parser() -> argparse.ArgumentParser:
14
+ p = CliArgumentParser(description="Loading run directory in ClearML")
15
+ p.add_argument(
16
+ "run_dir",
17
+ type=str,
18
+ help="Run directory (usually .../runs/<dataset>/<timestamp_...>/ or with a train subdirectory)",
19
+ )
20
+ p.add_argument(
21
+ "--project",
22
+ type=str,
23
+ default=None,
24
+ help="ClearML project name (aka CLEARML_PROJECT or smartrain)",
25
+ )
26
+ p.add_argument(
27
+ "--task-name",
28
+ type=str,
29
+ default=None,
30
+ help="Task name (default run directory name)",
31
+ )
32
+ p.add_argument(
33
+ "--train-subdir",
34
+ type=str,
35
+ default="train",
36
+ help="Subdirectory with args.yaml and weights (default train)",
37
+ )
38
+ p.add_argument(
39
+ "--no-images",
40
+ action="store_true",
41
+ help="Do not load images from the run tree",
42
+ )
43
+ return p
44
+
45
+
46
+ def _find_train_dir(run_dir: Path, train_subdir: str) -> Path:
47
+ run_dir = run_dir.resolve()
48
+ t = run_dir / train_subdir
49
+ if (t / "args.yaml").is_file() or (t / "weights" / "best.pt").is_file():
50
+ return t
51
+ if (run_dir / "args.yaml").is_file():
52
+ return run_dir
53
+ raise FileNotFoundError(
54
+ f"Args.yaml not found in {t} or {run_dir}. Please specify run root or --train-subdir."
55
+ )
56
+
57
+
58
+ def upload_run(
59
+ run_dir: str,
60
+ *,
61
+ project: str | None = None,
62
+ task_name: str | None = None,
63
+ train_subdir: str = "train",
64
+ upload_images: bool = True,
65
+ ) -> None:
66
+ try:
67
+ from clearml import Task
68
+ except ImportError as e:
69
+ raise ImportError(
70
+ "Install clearml: pip install 'smartrain[clearml]' or pip install clearml"
71
+ ) from e
72
+
73
+ import yaml
74
+ import pandas as pd
75
+ from PIL import Image
76
+
77
+ rd = Path(run_dir).expanduser().resolve()
78
+ if not rd.is_dir():
79
+ raise NotADirectoryError(run_dir)
80
+
81
+ train_d = _find_train_dir(rd, train_subdir)
82
+ proj = project or os.environ.get("CLEARML_PROJECT") or "smartrain"
83
+ tname = task_name or rd.name
84
+
85
+ task = Task.init(project_name=proj, task_name=tname, task_type=Task.TaskTypes.training)
86
+
87
+ args_path = train_d / "args.yaml"
88
+ if args_path.is_file():
89
+ with open(args_path, encoding="utf-8") as f:
90
+ hyperparameters = yaml.safe_load(f) or {}
91
+ if isinstance(hyperparameters, dict):
92
+ task.connect(hyperparameters)
93
+
94
+ results_csv = train_d / "results.csv"
95
+ if results_csv.is_file():
96
+ results_data = pd.read_csv(results_csv)
97
+ has_epoch = "epoch" in results_data.columns
98
+ for i, (_, row) in enumerate(results_data.iterrows()):
99
+ it = int(row["epoch"]) if has_epoch and pd.notna(row.get("epoch")) else i
100
+ for column in results_data.columns:
101
+ if column == "epoch":
102
+ continue
103
+ try:
104
+ val = float(row[column])
105
+ except (TypeError, ValueError):
106
+ continue
107
+ task.get_logger().report_scalar(
108
+ title=column,
109
+ series="training",
110
+ iteration=it,
111
+ value=val,
112
+ )
113
+
114
+ if upload_images:
115
+ exts = (".jpg", ".jpeg", ".png", ".webp")
116
+ for root, _, files in os.walk(rd):
117
+ for file in files:
118
+ if not file.lower().endswith(exts):
119
+ continue
120
+ file_path = Path(root) / file
121
+ try:
122
+ image = Image.open(file_path).convert("RGB")
123
+ except OSError:
124
+ continue
125
+ rel = str(file_path.relative_to(rd))
126
+ task.get_logger().report_image(rel, rel, iteration=0, image=image)
127
+
128
+ best_pt = train_d / "weights" / "best.pt"
129
+ if best_pt.is_file():
130
+ task.upload_artifact("best_model", artifact_object=str(best_pt))
131
+
132
+ task.close()
133
+ print(f"[OK] ClearML: project={proj!r}, task={tname!r}")
134
+
135
+
136
+ def main(argv: list[str] | None = None) -> None:
137
+ p = build_clearml_upload_arg_parser()
138
+ args = p.parse_args(argv)
139
+ upload_run(
140
+ args.run_dir,
141
+ project=args.project,
142
+ task_name=args.task_name,
143
+ train_subdir=args.train_subdir,
144
+ upload_images=not args.no_images,
145
+ )
146
+
147
+
148
+ if __name__ == "__main__":
149
+ main()