This continues the Quickstart guide
This how-to guide assumes you have already completed the standard quickstart guide. We will now modify the code created previously to add parameters.
To simplify comparing jobs with different parameter settings, we will expose certain parameters of the yolov8 training method (for example, epochs
) as Valohai parameters.
Define parameters in valohai.yaml
First, let Valohai know that our step includes parameters by defining them with default values, and then update our command to incorporate these parameters. Valohai will pass arguments to your code like: python train.py --epochs=3 --verbose=True
.
Next, update the environment configuration with the GPU machine you want to use. You can view a list of available GPU machines by running vh environments --gpu
. After that, include the machine’s slug name in the YAML file.
- 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 will use Python’s built-in argparse
library 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 includes a friendly name
# We can then reference 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
You can now run your training script and adjust the default parameter values directly from the command line:
vh execution run yolo --adhoc --open-browser --epochs=1
On the details page, you will find a new section for parameters, with epochs
set to 1. Checking the logs, you will see that the yolov8 trainer runs only one epoch.
When you navigate to the “Executions” tab, you will see a list of all executions and a table that includes the parameters. This makes it easier to distinguish between different job configurations.