This continues of the Quickstart guide
This how-to guide assumes you’ve already completed the standard quickstart guide. We’ll edit the code created there to add parameters.
To simplify the comparison of jobs with different parameter settings, we will expose the parameters of the yolov8 train method, such as epochs
, as Valohai parameters.
Define parameters in valohai.yaml
First, inform Valohai that there are parameters in our step, assign default values to them, and update our command to include these parameters. Valohai will then pass arguments to your code like: python train.py --epochs=3 --verbose=True
.
Update the environment with the GPU machine you want to run the job on. You can see list of available GPU machines by typing vh environments --gpu
. Use the slug name in the YAML.
- 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
Parse parameters in Python
Now, within our script, we’ll use the standard argparse package to parse these parameters.
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 that contains a friendly name
# We can then point to this file with datum://latest-model
file_metadata = {
"valohai.alias": "latest-model"
}
# Attach the metadata to our file
with open("/valohai/outputs/best.onnx.metadata.json", "w") as outfile:
outfile.write(json.dumps(file_metadata))
Run from the command-line
Now you can execute your training script, and change the default parameter value straight from the command-line:
vh execution run yolo --adhoc --open-browser --epochs=1
On the details page, you’ll notice a new section for parameters, with “epochs” set to 1. When reviewing the logs, you’ll see that the yolov8 trainer is performing just one epoch.
When you navigate to the “Executions” tab, you’ll find a list of all executions, and the table now includes the parameters. This makes it easy to distinguish between different job configurations.