Metadata-Version: 2.5
Name: repzo-yolo-hub
Version: 0.2.0
Summary: Train Ultralytics YOLO models for Repzo object-detection model versions: dataset, live metrics, weights and plots in one call.
Author: Repzo
License-Expression: LicenseRef-Proprietary
License-File: LICENSE
Keywords: object-detection,repzo,training,ultralytics,yolo
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Requires-Python: >=3.9
Requires-Dist: pyyaml>=5.3
Requires-Dist: requests>=2.28
Requires-Dist: ultralytics<9,>=8.4.0
Provides-Extra: test
Requires-Dist: pytest>=7; extra == 'test'
Description-Content-Type: text/markdown

# repzo-yolo-hub

Train Ultralytics YOLO models for Repzo object-detection model versions. One call does the whole job:

1. downloads the version's dataset zip and base weights,
2. trains with the settings configured on the version in Repzo (`epochs`, `imgsz`, `batch`, `optimizer`, `lr0`),
3. streams every epoch's metrics to Repzo while it trains,
4. sends the per-class precision, recall and mAP of the trained model,
5. uploads `best.pt` and `last.pt` (which marks the version **trained**), then the run's plots, `results.csv` and `args.yaml`.

It replaces the Ultralytics HUB client flow. Ultralytics HUB shut down on 2026-07-31, and ultralytics 8.4.115 removed the HUB client.

## Install

```bash
pip install -U repzo-yolo-hub
```

This also installs `ultralytics` (and PyTorch) when they are missing. Ultralytics 8.4.0 or newer is required; 8.4.0, 8.4.114 and 8.4.183 are tested. A model trained on a newer release still loads in 8.4.114, the version Repzo's inference runs.

## Train a model version

Copy the model version id from the version page in Repzo, and use an API key from **Settings > API keys**.

```python
from repzo_yolo_hub import train

results = train("6ad727555d667e3f6518abd6", api_key="YOUR_API_KEY")
print(results.save_dir)  # runs/detect/6ad727555d667e3f6518abd6
```

From a terminal:

```bash
repzo-yolo-hub train 6ad727555d667e3f6518abd6 --api-key YOUR_API_KEY
```

Any Ultralytics train argument overrides the version's settings, which helps when the GPU is smaller than planned:

```python
train("6ad727555d667e3f6518abd6", api_key="YOUR_API_KEY", batch=8, device=0)
```

```bash
repzo-yolo-hub train 6ad727555d667e3f6518abd6 batch=8 device=0
```

### Settings

| Argument | Environment variable | Default |
| --- | --- | --- |
| `api_key` / `--api-key` | `REPZO_API_KEY` | none, required |
| `base_url` / `--base-url` | `REPZO_BASE_URL` | `https://sv.api.repzo.me` (staging: `https://staging.sv.api.repzo.me`) |

`login("YOUR_API_KEY")` checks the key once and remembers it for the rest of the Python session.

### What it checks first

- **Wrong key or base URL**: stops before downloading anything.
- **Version of another company, or a wrong id**: stops with "not found".
- **Version already trained**: stops. `force=True` (`--force`) trains it again from its initial weights, and the new upload replaces the trained weights.
- **Dataset still being built**: stops and asks you to wait until Repzo has built the version's dataset.

## If something goes wrong

**The notebook disconnected during training.** Run the same call with `resume=True` (`--resume`). It picks up `runs/detect/<id>/weights/last.pt`.

**Training finished but the upload failed.** The run is still on disk. Send it without training again:

```python
from repzo_yolo_hub import upload_run

upload_run("6ad727555d667e3f6518abd6", results.save_dir, api_key="YOUR_API_KEY")
```

```bash
repzo-yolo-hub upload 6ad727555d667e3f6518abd6 runs/detect/6ad727555d667e3f6518abd6
```

`upload_run` also works for a run you trained some other way: it sends every epoch from `results.csv`, the weights, and the plots. Per-class results are sent only for runs trained with `train()`, which saves them as `per_class_metrics.json` in the run folder.

**Check what Repzo has for a version:**

```bash
repzo-yolo-hub info 6ad727555d667e3f6518abd6
```

## Coming from the Ultralytics HUB snippet

| Before | Now |
| --- | --- |
| `pip install ultralytics==8.4.114` | `pip install -U repzo-yolo-hub` |
| `ULTRALYTICS_HUB_*` environment variables, `hub.login(...)`, `YOLO(".../ul-hub/v1/models/<id>")`, `model.train()` | `train("<id>", api_key=...)` |
| Manual script that uploads the plots after training | Done by `train()` |
| Version-configured `batch` / `optimizer` / `lr0` ignored | Applied |
| An epoch with `NaN` losses broke every later metrics upload ("Bad Request") | `NaN`/`inf` are sent as `null` |
| A checkpoint upload every 15 min marked the version trained mid-run | Only the final `best.pt` does |

`train()` also accepts the old model URL (`https://sv.api.repzo.me/ul-hub/v1/models/<id>`) in place of the id.

## Per-class results

When training ends, `train()` reads the final validation of `best.pt` and sends one row per class to Repzo (stored on the version as `metrics_per_label`):

```python
results.per_class
# [{'class_index': 0, 'name': 'Pepsi: 1L', 'images': 6, 'instances': 11,
#   'precision': 0.91, 'recall': 0.82, 'mAP50': 0.88, 'mAP50_95': 0.61}, ...]
```

`images` and `instances` count the validation images and boxes of the class. A class with no validation boxes has no precision, recall or mAP: add validation images for it.

## Good to know

- Datasets are cached per version under the Ultralytics datasets folder (`<datasets_dir>/repzo/<id>`). `refresh_dataset=True` downloads again.
- Metrics upload in a background thread and never slow training down. If some fail, they are retried, and the whole run is re-synced from `results.csv` at the end.
- Multi-GPU (DDP) runs upload their metrics at the end instead of live: Ultralytics starts DDP workers in a separate process that does not run these callbacks.
- On ultralytics 8.4.114 and older, the dead Ultralytics HUB callbacks are switched off for the run. With a key saved by an old `hub.login()`, plain ultralytics now crashes before the first epoch; through this package it trains normally.

## Endpoints used

All under `<base_url>/ul-hub/v1`, authenticated with the `api-key` header:

| Call | Purpose |
| --- | --- |
| `POST /auth` | check the key |
| `GET /models/{id}` | dataset URL, base weights, train settings, status |
| `POST /models/{id}` | per-epoch metrics: `{"metrics": {"<epoch>": "<json>"}, "type": "metrics"}`; per-class results: `{"per_class": [...], "type": "per_class"}` |
| `POST /models/{id}/upload` | multipart weights (`best.pt`, `last.pt`) and run outputs (`results.png`, `results.csv`, `confusion_matrix.png`, `confusion_matrix_normalized.png`, `F1_curve.png`, `P_curve.png`, `PR_curve.png`, `R_curve.png`, `labels.jpg`, `val_batch0_labels.jpg`, `val_batch0_pred.jpg`, `args.yaml`) |

## Development

```bash
uv venv && uv pip install -e ".[test]"
pytest
```

Release: bump `src/repzo_yolo_hub/_version.py` and `CHANGELOG.md`, then `uv build && uv publish`.
