Overriding Config Parameters Using Commands

HAT supports modifying the config parameters from the command line by adding (key, value) after --opts. Where the key is the name of the parameter to be modified and the value is the value to be passed in, as in the config.py file:

Usage

python tools/train.py --stage float --config configs/classification/resnet18.py --opts "key1" "value1" "key2" "value2"

Usgae Example

For example, we have a configuration file config.py with the following content:

task_name = "resnet_cls"
num_classes=1000
devices = [0, 1, 2, 3]
data_shape = (3, 224, 224)
model = dict(
    backbone="resnet18",
    num_classes=num_classes,
)
Note

Samples in this document focus on the formats of different parameters when using --opts. In your projects, the parameters and field configurations may differ from the sample.

  • key supports multi-level parameter modification:

    python tools/train.py --stage float --config config.py --opts model.backbone "'resnet50'"

    The above command changes the value of the model.backbone field in config to "resnet50".

  • value can be number, str, list, tuple or dict.

    For example, the following command can change the value of the model.num_classes field in config to 10.

    python tools/train.py --stage float --config config.py --opts num_classes 10

    However, because of the parsing mechanism, you need to quote the value to be passed if its type is tuple or list, for example:

    python tools/train.py --stage float --config config.py --opts data_shape (3, 112, 112)  # Cause incorrect parsing
    python tools/train.py --stage float --config config.py --opts data_shape "(3, 112, 112)" # Correct format

    If the value type is str, you need to add extra quotes, for example:

    python tools/train.py --stage float --config config.py --opts model.backbone resnet18 # Cause incorrect parsing
    python tools/train.py --stage float --config config.py --opts model.backbone "resnet18" # Cause incorrect parsing
    python tools/train.py --stage float --config config.py --opts model.backbone "'resnet18'" # Correct format