新建一个experiment.yaml的文件
# my_project/experiment.yaml
SYSTEM:
NUM_GPUS: 2
TRAIN:
SCALES: (1, 2)
然后新建一个python文件
from config import get_cfg_defaults
# local variable usage pattern, or:
# from config import cfg # global singleton usage pattern
if __name__ == "__main__":
cfg = get_cfg_defaults()
cfg.merge_from_file("experiment.yaml")
print("GPUS:"+str(cfg.SYSTEM.NUM_GPUS))
cfg.freeze()
print(cfg)
结果为
GPUS:2
SYSTEM:
NUM_GPUS: 2
NUM_WORKERS: 4
TRAIN:
HYPERPARAMETER_1: 0.1
SCALES: (1, 2)
其中get_cfg_defaults,相当于构建了一个容器,可以将子一级变成父一级的变量(也就是变成父类中的一个变量),只需要使用 . 这个符号就能访问该变量。
新建一个hrnet.yaml文件、
CUDNN:
BENCHMARK: true
DETERMINISTIC: false
ENABLED: true
GPUS: (0,1,2,3)
OUTPUT_DIR: 'output'
LOG_DIR: 'log'
WORKERS: 4
PRINT_FREQ: 100
DATASET:
DATASET: cityscapes
ROOT: 'data/'
TEST_SET: 'list/cityscapes/val.lst'
TRAIN_SET: 'list/cityscapes/train.lst'
NUM_CLASSES: 19
MODEL:
NAME: seg_hrnet
PRETRAINED: 'pretrained_models/hrnetv2_w48_imagenet_pretrained.pth'
EXTRA:
FINAL_CONV_KERNEL: 1
STAGE2:
NUM_MODULES: 1
NUM_BRANCHES: 2
BLOCK: BASIC
NUM_BLOCKS:
- 4
- 4
NUM_CHANNELS:
- 48
- 96
FUSE_METHOD: SUM
STAGE3:
NUM_MODULES: 4
NUM_BRANCHES: 3
BLOCK: BASIC
NUM_BLOCKS:
- 4
- 4
- 4
NUM_CHANNELS:
- 48
- 96
- 192
FUSE_METHOD: SUM
STAGE4:
NUM_MODULES: 3
NUM_BRANCHES: 4
BLOCK: BASIC
NUM_BLOCKS:
- 4
- 4
- 4
- 4
NUM_CHANNELS:
- 48
- 96
- 192
- 384
FUSE_METHOD: SUM
LOSS:
USE_OHEM: false
OHEMTHRES: 0.9
OHEMKEEP: 131072
TRAIN:
IMAGE_SIZE:
- 1024
- 512
BASE_SIZE: 2048
BATCH_SIZE_PER_GPU: 3
SHUFFLE: true
BEGIN_EPOCH: 0
END_EPOCH: 484
RESUME: true
OPTIMIZER: sgd
LR: 0.01
WD: 0.0005
MOMENTUM: 0.9
NESTEROV: false
FLIP: true
MULTI_SCALE: true
DOWNSAMPLERATE: 1
IGNORE_LABEL: 255
SCALE_FACTOR: 16
TEST:
IMAGE_SIZE:
- 2048
- 1024
BASE_SIZE: 2048
BATCH_SIZE_PER_GPU: 4
FLIP_TEST: false
MULTI_SCALE: false
然后新建一个python文件,
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from yacs.config import CfgNode as CN
_C = CN()
_C.OUTPUT_DIR = ''
_C.LOG_DIR = ''
_C.GPUS = (0,)
_C.WORKERS = 4
_C.PRINT_FREQ = 20
_C.AUTO_RESUME = False
_C.PIN_MEMORY = True
_C.RANK = 0
# Cudnn related params
_C.CUDNN = CN()
_C.CUDNN.BENCHMARK = True
_C.CUDNN.DETERMINISTIC = False
_C.CUDNN.ENABLED = True
# common params for NETWORK
_C.MODEL = CN()
_C.MODEL.NAME = 'seg_hrnet'
_C.MODEL.PRETRAINED = ''
_C.MODEL.EXTRA = CN(new_allowed=True)
_C.LOSS = CN()
_C.LOSS.USE_OHEM = False
_C.LOSS.OHEMTHRES = 0.9
_C.LOSS.OHEMKEEP = 100000
_C.LOSS.CLASS_BALANCE = False
# DATASET related params
_C.DATASET = CN()
_C.DATASET.ROOT = ''
_C.DATASET.DATASET = 'cityscapes'
_C.DATASET.NUM_CLASSES = 19
_C.DATASET.TRAIN_SET = 'list/cityscapes/train.lst'
_C.DATASET.EXTRA_TRAIN_SET = ''
_C.DATASET.TEST_SET = 'list/cityscapes/val.lst'
# training
_C.TRAIN = CN()
_C.TRAIN.IMAGE_SIZE = [1024, 512] # width * height
_C.TRAIN.BASE_SIZE = 2048
_C.TRAIN.DOWNSAMPLERATE = 1
_C.TRAIN.FLIP = True
_C.TRAIN.MULTI_SCALE = True
_C.TRAIN.SCALE_FACTOR = 16
_C.TRAIN.LR_FACTOR = 0.1
_C.TRAIN.LR_STEP = [90, 110]
_C.TRAIN.LR = 0.01
_C.TRAIN.EXTRA_LR = 0.001
_C.TRAIN.OPTIMIZER = 'sgd'
_C.TRAIN.MOMENTUM = 0.9
_C.TRAIN.WD = 0.0001
_C.TRAIN.NESTEROV = False
_C.TRAIN.IGNORE_LABEL = -1
_C.TRAIN.BEGIN_EPOCH = 0
_C.TRAIN.END_EPOCH = 484
_C.TRAIN.EXTRA_EPOCH = 0
_C.TRAIN.RESUME = False
_C.TRAIN.BATCH_SIZE_PER_GPU = 32
_C.TRAIN.SHUFFLE = True
# only using some training samples
_C.TRAIN.NUM_SAMPLES = 0
# testing
_C.TEST = CN()
_C.TEST.IMAGE_SIZE = [2048, 1024] # width * height
_C.TEST.BASE_SIZE = 2048
_C.TEST.BATCH_SIZE_PER_GPU = 32
# only testing some samples
_C.TEST.NUM_SAMPLES = 0
_C.TEST.MODEL_FILE = ''
_C.TEST.FLIP_TEST = False
_C.TEST.MULTI_SCALE = False
_C.TEST.SCALE_LIST = [1]
# debug
_C.DEBUG = CN()
_C.DEBUG.DEBUG = False
_C.DEBUG.SAVE_BATCH_IMAGES_GT = False
_C.DEBUG.SAVE_BATCH_IMAGES_PRED = False
_C.DEBUG.SAVE_HEATMAPS_GT = False
_C.DEBUG.SAVE_HEATMAPS_PRED = False
if __name__ == '__main__':
cfg = _C
cfg.defrost()
cfg.merge_from_file("hrnet.yaml")
print(cfg.TEST.IMAGE_SIZE)
cfg.freeze()
print(cfg)
结果为:
[2048, 1024]
AUTO_RESUME: False
CUDNN:
BENCHMARK: True
DETERMINISTIC: False
ENABLED: True
DATASET:
DATASET: cityscapes
EXTRA_TRAIN_SET:
NUM_CLASSES: 19
ROOT: data/
TEST_SET: list/cityscapes/val.lst
TRAIN_SET: list/cityscapes/train.lst
DEBUG:
DEBUG: False
SAVE_BATCH_IMAGES_GT: False
SAVE_BATCH_IMAGES_PRED: False
SAVE_HEATMAPS_GT: False
SAVE_HEATMAPS_PRED: False
GPUS: (0, 1, 2, 3)
LOG_DIR: log
LOSS:
CLASS_BALANCE: False
OHEMKEEP: 131072
OHEMTHRES: 0.9
USE_OHEM: False
MODEL:
EXTRA:
FINAL_CONV_KERNEL: 1
STAGE2:
BLOCK: BASIC
FUSE_METHOD: SUM
NUM_BLOCKS: [4, 4]
NUM_BRANCHES: 2
NUM_CHANNELS: [48, 96]
NUM_MODULES: 1
STAGE3:
BLOCK: BASIC
FUSE_METHOD: SUM
NUM_BLOCKS: [4, 4, 4]
NUM_BRANCHES: 3
NUM_CHANNELS: [48, 96, 192]
NUM_MODULES: 4
STAGE4:
BLOCK: BASIC
FUSE_METHOD: SUM
NUM_BLOCKS: [4, 4, 4, 4]
NUM_BRANCHES: 4
NUM_CHANNELS: [48, 96, 192, 384]
NUM_MODULES: 3
NAME: seg_hrnet
PRETRAINED: pretrained_models/hrnetv2_w48_imagenet_pretrained.pth
OUTPUT_DIR: output
PIN_MEMORY: True
PRINT_FREQ: 100
RANK: 0
TEST:
BASE_SIZE: 2048
BATCH_SIZE_PER_GPU: 4
FLIP_TEST: False
IMAGE_SIZE: [2048, 1024]
MODEL_FILE:
MULTI_SCALE: False
NUM_SAMPLES: 0
SCALE_LIST: [1]
TRAIN:
BASE_SIZE: 2048
BATCH_SIZE_PER_GPU: 3
BEGIN_EPOCH: 0
DOWNSAMPLERATE: 1
END_EPOCH: 484
EXTRA_EPOCH: 0
EXTRA_LR: 0.001
FLIP: True
IGNORE_LABEL: 255
IMAGE_SIZE: [1024, 512]
LR: 0.01
LR_FACTOR: 0.1
LR_STEP: [90, 110]
MOMENTUM: 0.9
MULTI_SCALE: True
NESTEROV: False
NUM_SAMPLES: 0
OPTIMIZER: sgd
RESUME: True
SCALE_FACTOR: 16
SHUFFLE: True
WD: 0.0005
WORKERS: 4
这里要注意一个重要的点:在_C中申明过后的变量才能被赋值和修改,如果_C中没有某个变量,而yaml中有,那就会报错,因为该变量未能事先申明。
但从_C.MODEL.EXTRA = CN(new_allowed=True)这句代码可以看出,我们申明了父类的变量,而且允许子类新建,那么yaml中如下部分可以被申明。
MODEL:
EXTRA:
FINAL_CONV_KERNEL: 1
STAGE2:
BLOCK: BASIC
FUSE_METHOD: SUM
NUM_BLOCKS: [4, 4]
NUM_BRANCHES: 2
NUM_CHANNELS: [48, 96]
NUM_MODULES: 1
STAGE3:
BLOCK: BASIC
FUSE_METHOD: SUM
NUM_BLOCKS: [4, 4, 4]
NUM_BRANCHES: 3
NUM_CHANNELS: [48, 96, 192]
NUM_MODULES: 4
STAGE4:
BLOCK: BASIC
FUSE_METHOD: SUM
NUM_BLOCKS: [4, 4, 4, 4]
NUM_BRANCHES: 4
NUM_CHANNELS: [48, 96, 192, 384]
NUM_MODULES: 3