mosaicml / examples

Fast and flexible reference benchmarks
Apache License 2.0
424 stars 121 forks source link

How to add a custom key to config file? #450

Open pvti opened 3 months ago

pvti commented 3 months ago

Hi,

I'm currently working with the resnet50 training recipe. However, I'm aiming to adapt Mosaic to my custom MobileNetV2 model and need to incorporate a custom parameter into the model. I've made the following modifications:

In yamls/mobilenetv2.yaml:

# Model
model:
  name: mobilenetv2           # Name of the ResNet model to train either resnet{18, 34, 50, 101, 152}
  loss_name: binary_cross_entropy # Name of the loss function either 'cross_entropy' or 'binary_cross_entropy'
  num_classes: 1000        # Number of classes in the classification task
  compress_rate: [0.34]*8

And in model.py:

from mobilenetv2 import mobilenetv2

def build_composer_resnet(model_name: str = 'resnet50',
                          loss_name: str = 'cross_entropy',
                          num_classes: int = 1000,
                          compress_rate: list = [0.]*8):
    """Helper function to build a Composer ResNet model.

    Args:
        model_name (str, optional): Name of the ResNet model to use, either
            ['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152']. Default: ``'resnet50'``.
        loss_name (str, optional): Name of the loss function to use, either ['cross_entropy', 'binary_cross_entropy'].
            Default: ``'cross_entropy'``.
        num_classes (int, optional): Number of classes in the classification task. Default: ``1000``.
    """
    model = mobilenetv2(compress_rate=compress_rate, num_classes=num_classes)

However, upon running:

composer main.py yamls/mobilenetv2.yaml recipe_name=hot

I encountered the following error:

Building train dataloader
Built train dataloader

Building evaluation dataloader
Built evaluation dataloader

Building Composer model
Traceback (most recent call last):
  File "/home/van-tien.pham/momo/main.py", line 243, in <module>
    main(config)
  File "/home/van-tien.pham/momo/main.py", line 113, in main
    compress_rate=config.model.compress_rate,
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 355, in __getattr__
    self._format_and_raise(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/base.py", line 231, in _format_and_raise
    format_and_raise(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/_utils.py", line 899, in format_and_raise
    _raise(ex, cause)
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/_utils.py", line 797, in _raise
    raise ex.with_traceback(sys.exc_info()[2])  # set env var OC_CAUSE=1 for full trace
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 351, in __getattr__
    return self._get_impl(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 442, in _get_impl
    node = self._get_child(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/basecontainer.py", line 73, in _get_child
    child = self._get_node(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 480, in _get_node
    raise ConfigKeyError(f"Missing key {key!s}")
omegaconf.errors.ConfigAttributeError: Missing key compress_rate
    full_key: model.compress_rate
    object_type=dict
ERROR:composer.cli.launcher:Rank 1 crashed with exit code 1.
Waiting up to 30 seconds for all training processes to terminate. Press Ctrl-C to exit immediately.
Global rank 1 (PID 28118) exited with code 1
----------Begin global rank 1 STDOUT----------
Building train dataloader
Built train dataloader

Building evaluation dataloader
Built evaluation dataloader

Building Composer model

----------End global rank 1 STDOUT----------
----------Begin global rank 1 STDERR----------
Traceback (most recent call last):
  File "/home/van-tien.pham/momo/main.py", line 243, in <module>
    main(config)
  File "/home/van-tien.pham/momo/main.py", line 113, in main
    compress_rate=config.model.compress_rate,
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 355, in __getattr__
    self._format_and_raise(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/base.py", line 231, in _format_and_raise
    format_and_raise(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/_utils.py", line 899, in format_and_raise
    _raise(ex, cause)
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/_utils.py", line 797, in _raise
    raise ex.with_traceback(sys.exc_info()[2])  # set env var OC_CAUSE=1 for full trace
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 351, in __getattr__
    return self._get_impl(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 442, in _get_impl
    node = self._get_child(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/basecontainer.py", line 73, in _get_child
    child = self._get_node(
  File "/home/van-tien.pham/anaconda3/envs/mosaic/lib/python3.9/site-packages/omegaconf/dictconfig.py", line 480, in _get_node
    raise ConfigKeyError(f"Missing key {key!s}")
omegaconf.errors.ConfigAttributeError: Missing key compress_rate
    full_key: model.compress_rate
    object_type=dict

----------End global rank 1 STDERR----------
ERROR:composer.cli.launcher:Global rank 0 (PID 28117) exited with code -15

How can I resolve this error omegaconf.errors.ConfigAttributeError: Missing key compress_rate?

Thank you in advance for your help!