Using Parameters

Configure dynamic parameters for ML experiments. Learn to define parameters in valohai.yaml and parse them in Python for flexible model training.

💡 About this tutorial: We use YOLOv8 as a practical example to demonstrate Valohai's features. You don't need computer vision knowledge—the patterns you learn here apply to any ML framework. This tutorial focuses on defining parameters and configuration values while ensuring proper versioning and tracking of your ML workflows.

Define parameters in valohai.yaml

Tell Valohai which arguments your training script accepts. Parameters become command-line arguments automatically, no boilerplate needed.

- step:
    name: yolo
    image: docker.io/ultralytics/ultralytics:8.0.180-python
    command: python train.py {parameters}
    environment: aws-eu-west-1-p3-2xlarge
    parameters:
        - name: epochs
          type: integer
          default: 3
        - name: verbose
          type: flag
          default: False
          pass-true-as: --verbose=True
          pass-false-as: --verbose=False

What's happening here:

  • {parameters} placeholder → Valohai injects --epochs=3 --verbose=True

  • environment → GPU instance type (find available options: vh environments --gpu)

  • Flag parameters need explicit pass-true/false values for boolean handling

Parse parameters in Python

Use Python's standard argparse to receive these values. No special Valohai SDK required.

import shutil
from ultralytics import YOLO
import argparse
import json

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--epochs', type=int, default=3)
    parser.add_argument('--verbose', type=bool, default=False)
    return parser.parse_args()

args = parse_args()

# Load a model
model = YOLO("yolov8n.pt")  # load a pretrained model (recommended for training)

# Use the model
model.train(data="coco128.yaml", epochs=args.epochs, verbose=args.verbose)  # train the model
path = model.export(format="onnx")  # export the model to ONNX format

# Copy the exported model to the Valohai outputs directory
shutil.copy(path, '/valohai/outputs/')

# Define a JSON dictionary containing a friendly name
# You can then reference this file with datum://latest-model
file_metadata = {
    "valohai.alias": "latest-model"
}

# Attach the metadata to the file
with open("/valohai/outputs/best.onnx.metadata.json", "w") as f:
    f.write(file_metadata)

Run with custom parameters

Override defaults directly from the CLI:

vh execution run yolo --adhoc --open-browser --epochs=1

Your execution details page now shows:

  • Parameters section: Displays epochs: 1 and other values

  • Logs: Confirms YOLOv8 ran exactly 1 epoch

Compare experiments at a glance

Navigate to the Executions tab to see all your runs in a table with parameter columns. No more guessing which job used which configuration.


Last updated

Was this helpful?