> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.ovalbee.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.ovalbee.com/_mcp/server.

# Train Kfold YOLO

> Train one YOLO detector per fold on the other folds of a dataset, with images that are copies of each other kept in one fold, and save what each detector predicts on the images it never saw. Kfold label check reads those held-out predictions.

Runs on: gpu  Dockerfile

> **Note**
>
> First run can take longer while the worker pulls the configured Docker image.

## Overview

Splits the labelled images of a dataset into K folds and trains K YOLO detectors. Each detector trains on K-1 folds with the original labels and then predicts the fold it did not see, so every image gets predictions from a detector that never trained on it. These held-out ("out of fold") predictions are the input of [Kfold label check](/node-library/nodes/models-clip-kfold-label-check), which compares them with the labels to find suspicious labels and to measure how hard the dataset is to learn.

Images that are copies of one another are kept in the same fold, so no detector is tested on a picture it has seen in another form. Without that, the held-out predictions of a dataset with copies look better than they are.

The trained models are not kept: nothing is registered as a model and the checkpoints are deleted when a fold finishes. The report is updated after every fold, and its `REPORT_ID` output is set when the first fold starts. If a fold fails, the node fails; the folds that finished stay in the report. There is no resume.

```mermaid
flowchart LR
  n1["Dataset"] --> n3["Train Kfold YOLO"] --> n4["Kfold label check"]
  n2["Find copies"] --> n3
  n5["Similar pairs"] --> n3
```

## Prerequisites

| Requirement                   | What you need                                                                                                                                                                                                                                                                                                                           |
| ----------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **GPU**                       | One CUDA GPU; 8 GB is enough. YOLO26 small at image size 1024 and the automatic batch of 4 peaks near 3 GB, medium near 6 GB. On an out-of-memory error the batch is halved, down to 2, and the fold starts again; prediction falls back to the CPU when even one image does not fit.                                                   |
| **Labels**                    | A detection dataset whose images carry boxes. Labels of any shape are trained as boxes; when two boxes of one class overlap at IoU above 0.9, as when an object is labelled both as a polygon and as a rectangle, only one is trained. Images of a split with no labels at all are left out of the folds.                               |
| **Model**                     | A YOLO detection model in the workspace to start every fold from, such as YOLO26 large copied in from the catalog in the picker. Its weights are loaded the way [Train YOLO](/node-library/nodes/models-yolo-train-yolo) loads them, so the safe-weights setting applies: with it on, a pickled `.pt` is refused until it is converted. |
| **Copies reports** (optional) | Reports of Find copies and Similar pairs.                                                                                                                                                                                                                                                                                               |

## How it works

Loads the weights of **Model** and exports the dataset in YOLO format with its original labels, keeps one box of every object labelled twice (same class, IoU above 0.9), and reads the class list from the export. The class order of the export is the class order of `oof.parquet`.

Reads the copy groups of the connected reports: pixel copies (`copy_groups.parquet`, group id), file-name groups of Find copies (name group) and near copies of Similar pairs (`duplicate_groups.parquet`).

Merges the groups, then assigns whole groups to folds, heaviest first, to the fold with the least load among the images of the same split and the same value of the first tag that has 2 to 50 values. If the largest merged group holds more than a 2K-th of the images, the near-copy edges and then the file-name edges are dropped; pixel copies are never dropped. Above **Largest number of images**, whole groups are sampled, stratified by split, and the other images get no held-out predictions.

For each fold, trains a copy of the model (its network rebuilt for the dataset's classes) on the images of the other folds for a fixed number of epochs, with validation off and the last weights kept, so a held-out image never influences the model that predicts it. Then predicts the held-out images at score 0.01 and above, at most 300 boxes per image, and adds them to `oof.parquet`.

Uploads the report after every fold and once a minute while a fold trains.

## Inputs

**`Dataset`** `dataset` — required

Dataset to train on, with its original labels. Every labelled image is held out by exactly one model. The split of an image is read from its split tag and only used to keep the folds balanced. Key: `DATASET`.

---

**`Copies reports`** `report`

Reports of Find copies and Similar pairs. Images they put in one group are kept in the same fold, so a model is never tested on a copy of an image it trained on. Leave empty to group by nothing, which makes the held-out predictions look better than they are when the dataset has copies. Values come from `DATASET`. Key: `COPY_REPORT_IDS`.

---

**`Enabled`** `boolean` — required, default: true

Turn off to skip training. The node then completes with a report saying it was off. Key: `ENABLED`.

---

**`Model`** `model` — required

Detection model every fold starts from, trained in this workspace or installed from the zoo. YOLO26 small at image size 1024 finds the dataset's problems about as well as medium or large and fits an 8 GB GPU with room to spare. Each fold trains a copy of it on the other folds, no version is registered. Key: `MODEL`.

---

**`Folds`** `integer` — required, default: 4

Number of folds. Each model trains on the other K-1 folds, so a larger K means a model closer to one trained on everything, and more training time. Minimum: `3`. Maximum: `10`. Step: `1`. Key: `K`.

---

**`Epochs`** `integer` — required, default: 20

Training epochs of every fold. The last weights are used, with no validation, so a held-out image never influences the model that predicts it. Minimum: `1`. Maximum: `300`. Step: `1`. Key: `EPOCHS`.

---

**`Largest number of images`** `integer` — required, default: 5000

Above this many labelled images, whole groups of images are sampled (stratified by split) and the rest get no held-out predictions. The report says how many were left out. Minimum: `20`. Maximum: `100000`. Step: `100`. Key: `MAX_IMAGES`.

---

**`Image size`** `integer` — required, default: 1024

Training and prediction image size in pixels. Minimum: `32`. Maximum: `2048`. Step: `32`. Key: `IMAGE_SIZE`.

---

**`Batch size`** `string` — required, default: auto

`auto` is 4 for YOLO26 small, 2 for YOLO26 medium and large and 4 for any other model, sized for an 8 GB GPU at image size 1024, on a CUDA out-of-memory error the batch is halved, down to 2, and the fold restarts. Or a whole number. Key: `BATCH`.

---

## Outputs

**`Report`** `report`

Held-out predictions (oof.parquet), the fold of every image (folds.parquet), kfold_meta.json, per-fold progress, and findings.json. Updated after every fold while the node runs. Shown as an artifact. Key: `REPORT_ID`.

---

**`Folds trained`** `string`

How many folds finished, for example "4 folds". Shown on the node as a badge. Key: `FOLDS_TRAINED`.

---

## Report

The report holds `index.md` (the settings, a table with the state, timing and GPU peak of every fold, and how images were grouped), and the files Kfold label check reads:

* `oof.parquet`: one row per held-out box. Columns `asset_id`, `fold`, `cls` (position in the class list), `x1`, `y1`, `x2`, `y2` (pixels of the original image) and `score`. Grows by one fold at a time.
* `folds.parquet`: `asset_id, split, fold, group` for every image in the folds.
* `kfold_meta.json`: `k`, `classes` (names in the order of `cls`), the model, epochs, image size and batch used, which grouping sources were used and what the guard dropped, how many images were left out, notes on the inputs (among them how many duplicate boxes were left out of training), the timing and GPU peak of each fold, and the Ultralytics version.
* `fold_progress.json`: the state of each fold while the node runs.
* `findings.json`: one observation, `kfold.trained`, with the number of images that have held-out predictions and the number of labelled images.

The report asset carries the tag `kind: kfold_oof_v1`.

## JSON config

Machine-readable node interface for automation and advanced usage.

#### Show JSON

```json
{
  "name": "Train Kfold YOLO",
  "description": "Train one YOLO detector per fold on the other folds of a dataset, with images that are copies of each other kept in one fold, and save what each detector predicts on the images it never saw. Kfold label check reads those held-out predictions.",
  "category": "Train",
  "namespace": "ovalbee",
  "templateKey": "models/yolo/train_kfold_yolo",
  "version": "v1",
  "inputs": [
    {
      "key": "DATASET",
      "label": "Dataset",
      "type": "dataset",
      "description": "Dataset to train on, with its original labels. Every labelled image is held out by exactly one model. The split of an image is read from its split tag and only used to keep the folds balanced.",
      "required": true,
      "default": null,
      "visibleWhen": null,
      "options": {
        "creatable": false
      }
    },
    {
      "key": "COPY_REPORT_IDS",
      "label": "Copies reports",
      "type": "report",
      "kind": "dataset_intelligence_findings_v1",
      "description": "Reports of Find copies and Similar pairs. Images they put in one group are kept in the same fold, so a model is never tested on a copy of an image it trained on. Leave empty to group by nothing, which makes the held-out predictions look better than they are when the dataset has copies.",
      "required": false,
      "default": null,
      "visibleWhen": null,
      "options": {
        "minItems": 0,
        "ref": "DATASET"
      }
    },
    {
      "key": "ENABLED",
      "label": "Enabled",
      "type": "boolean",
      "description": "Turn off to skip training. The node then completes with a report saying it was off.",
      "required": true,
      "default": true,
      "visibleWhen": null
    },
    {
      "key": "MODEL",
      "label": "Model",
      "type": "model",
      "description": "Detection model every fold starts from, trained in this workspace or installed from the zoo. YOLO26 small at image size 1024 finds the dataset's problems about as well as medium or large and fits an 8 GB GPU with room to spare. Each fold trains a copy of it on the other folds; no version is registered.\n",
      "required": true,
      "default": null,
      "visibleWhen": null,
      "options": {
        "scope": "my",
        "framework": "yolo"
      }
    },
    {
      "key": "K",
      "label": "Folds",
      "type": "number",
      "description": "Number of folds. Each model trains on the other K-1 folds, so a larger K means a model closer to one trained on everything, and more training time.",
      "required": true,
      "default": 4,
      "visibleWhen": null,
      "options": {
        "type": "integer",
        "min": 3,
        "max": 10,
        "step": 1,
        "slider": false
      }
    },
    {
      "key": "EPOCHS",
      "label": "Epochs",
      "type": "number",
      "description": "Training epochs of every fold. The last weights are used, with no validation, so a held-out image never influences the model that predicts it.",
      "required": true,
      "default": 20,
      "visibleWhen": null,
      "options": {
        "type": "integer",
        "min": 1,
        "max": 300,
        "step": 1,
        "slider": false
      }
    },
    {
      "key": "MAX_IMAGES",
      "label": "Largest number of images",
      "type": "number",
      "description": "Above this many labelled images, whole groups of images are sampled (stratified by split) and the rest get no held-out predictions. The report says how many were left out.",
      "required": true,
      "default": 5000,
      "visibleWhen": null,
      "options": {
        "type": "integer",
        "min": 20,
        "max": 100000,
        "step": 100,
        "slider": false
      }
    },
    {
      "key": "IMAGE_SIZE",
      "label": "Image size",
      "type": "number",
      "description": "Training and prediction image size in pixels.",
      "required": true,
      "default": 1024,
      "visibleWhen": null,
      "options": {
        "type": "integer",
        "min": 32,
        "max": 2048,
        "step": 32,
        "slider": false
      }
    },
    {
      "key": "BATCH",
      "label": "Batch size",
      "type": "string",
      "description": "`auto` is 4 for YOLO26 small, 2 for YOLO26 medium and large and 4 for any other model, sized for an 8 GB GPU at image size 1024; on a CUDA out-of-memory error the batch is halved, down to 2, and the fold restarts. Or a whole number.",
      "required": true,
      "default": "auto",
      "visibleWhen": null
    }
  ],
  "outputs": [
    {
      "key": "REPORT_ID",
      "label": "Report",
      "type": "report",
      "kind": [
        "dataset_intelligence_findings_v1",
        "kfold_oof_v1"
      ],
      "description": "Held-out predictions (oof.parquet), the fold of every image (folds.parquet), kfold_meta.json, per-fold progress, and findings.json. Updated after every fold while the node runs.",
      "artifact": true,
      "badge": false,
      "badgeOpens": null,
      "hidden": false
    },
    {
      "key": "FOLDS_TRAINED",
      "label": "Folds trained",
      "type": "string",
      "description": "How many folds finished, for example \"4 folds\".",
      "artifact": false,
      "badge": true,
      "badgeOpens": null,
      "hidden": false
    }
  ],
  "runtime": {
    "type": "docker",
    "devices": [
      "gpu"
    ],
    "dockerImage": "ovalbee/node-yolo:0.0.41"
  },
  "automation": {
    "isolated": true,
    "requires_configuration": true,
    "manual_run": true,
    "discard_history": "on_success"
  },
  "widgets": {
    "secondaryWidget": {
      "id": "report-preview",
      "settings": {
        "reportAsset": {
          "type": "variable",
          "value": "self.outputs.REPORT_ID"
        },
        "name": {
          "type": "input",
          "value": "Open report"
        }
      }
    }
  }
}
```