rbgirshick / yacs

YACS -- Yet Another Configuration System
Apache License 2.0
1.28k stars 90 forks source link

Can we convert CfgNode to yaml file or anyother format? #31

Open dhaivat1729 opened 5 years ago

dhaivat1729 commented 5 years ago

I have a config node which has certain mode config nodes, is there anyway I can get a yaml file out of it? CfgNode({'USE_CUDA': True, 'NO_GPU': False, 'RANDOMIZATION': CfgNode({'SEED': 5}), 'BACKBONE': CfgNode({'MODEL_NAME': 'resnet50', 'RESNET_STOP_LAYER': 4}), 'INPUT': CfgNode({'MEAN': (0.485, 0.456, 0.406), 'STD': (0.229, 0.224, 0.225)}), 'DTYPE': CfgNode({'FLOAT': 'torch.cuda.FloatTensor', 'LONG': 'torch.cuda.LongTensor'}), 'ANCHORS': CfgNode({'ASPECT_RATIOS': (1, 1.5, 2), 'ANCHOR_SCALES': (192, 128, 256), 'N_ANCHORS_PER_LOCATION': 9}), 'RPN': CfgNode({'OUT_CHANNELS': 512, 'LAYER_CHANNELS': (512, 256, 128), 'N_ANCHORS_PER_LOCATION': 9, 'SOFTPLUS_BETA': 1, 'SOFTPLUS_THRESH': 20, 'CONV_MEAN': 0, 'CONV_VAR': 0.01, 'BIAS': 0, 'UNCERTAIN_MEAN': 0, 'UNCERTAIN_VAR': 0.01, 'UNCERTAIN_BIAS': 1.0, 'ACTIVATION_ALPHA': 1}), 'TRAIN': CfgNode({'OPTIM': 'adam', 'LR': 1e-05, 'MOMENTUM': 0.09, 'EPOCHS': 40, 'MILESTONES': (10, 18, 25), 'DSET_SHUFFLE': True, 'BATCH_SIZE': 1, 'FREEZE_BACKBONE': False, 'LR_DECAY': 0.1, 'LR_DECAY_EPOCHS': 50, 'SAVE_MODEL_EPOCHS': 5, 'TRAIN_TYPE': 'probabilistic', 'DATASET_DIVIDE': 0.9, 'NUSCENES_IMAGE_RESIZE_FACTOR': 1.5, 'CLASS_LOSS_SCALE': 5.0, 'FAKE_BATCHSIZE': 27})})

xl-sr commented 4 years ago

This works for me (where cfg is a CfgNode):

from contextlib import redirect_stdout

with open('test.yml', 'w') as f:
    with redirect_stdout(f): print(cfg.dump())
dhaivat1729 commented 4 years ago

@xl-sr it doesn't work for arrays actually. A: ["a", "b", "c"] isn't loaded the same way when we do what you did. Know any way to fix it?

InnovArul commented 4 years ago

@dhaivat666 Can you give a short reproducible snippet? To me, It is able to load the array when using @xl-sr's suggestion.

Robotatron commented 2 years ago

Using cfg.dump() worked. Trained a net with Detectron2, now if I do cfg.dump() I get:

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Input In [32], in <cell line: 1>()
----> 1 type(cfg.dump())

File /opt/conda/envs/icevision/lib/python3.9/site-packages/detectron2/config/config.py:93, in CfgNode.dump(self, *args, **kwargs)
     88 """
     89 Returns:
     90     str: a yaml string representation of the config
     91 """
     92 # to make it show up in docs
---> 93 return super().dump(*args, **kwargs)

File /opt/conda/envs/icevision/lib/python3.9/site-packages/yacs/config.py:206, in CfgNode.dump(self, **kwargs)
    203             cfg_dict[k] = convert_to_dict(v, key_list + [k])
    204         return cfg_dict
--> 206 self_as_dict = convert_to_dict(self, [])
    207 return yaml.safe_dump(self_as_dict, **kwargs)

File /opt/conda/envs/icevision/lib/python3.9/site-packages/yacs/config.py:203, in CfgNode.dump.<locals>.convert_to_dict(cfg_node, key_list)
    201 cfg_dict = dict(cfg_node)
    202 for k, v in cfg_dict.items():
--> 203     cfg_dict[k] = convert_to_dict(v, key_list + [k])
    204 return cfg_dict

File /opt/conda/envs/icevision/lib/python3.9/site-packages/yacs/config.py:193, in CfgNode.dump.<locals>.convert_to_dict(cfg_node, key_list)
    191 def convert_to_dict(cfg_node, key_list):
    192     if not isinstance(cfg_node, CfgNode):
--> 193         _assert_with_logging(
    194             _valid_type(cfg_node),
    195             "Key {} with value {} is not a valid type; valid types: {}".format(
    196                 ".".join(key_list), type(cfg_node), _VALID_TYPES
    197             ),
    198         )
    199         return cfg_node
    200     else:

File /opt/conda/envs/icevision/lib/python3.9/site-packages/yacs/config.py:545, in _assert_with_logging(cond, msg)
    543 if not cond:
    544     logger.debug(msg)
--> 545 assert cond, msg

AssertionError: Key __builtins__ with value <class 'dict'> is not a valid type; valid types: {<class 'float'>, <class 'list'>, <class 'NoneType'>, <class 'int'>, <class 'str'>, <class 'bool'>, <class 'tuple'>}