CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-mmengine

Engine of OpenMMLab projects for training deep learning models based on PyTorch with large-scale training frameworks, configuration management, and monitoring capabilities

Pending
Overview
Eval results
Files

configuration.mddocs/

Configuration Management

Advanced configuration system supporting Python-style and plain-text configuration files with inheritance, variable interpolation, and runtime modification capabilities. The system enables hierarchical configuration management essential for complex training pipelines.

Capabilities

Config Class

Main configuration class for loading and managing configuration files with support for inheritance, variable interpolation, and runtime modification.

class Config:
    def __init__(self, cfg_dict: dict = None, cfg_text: str = None, filename: str = None):
        """
        Initialize a Config object.
        
        Parameters:
        - cfg_dict: Dictionary containing configuration data
        - cfg_text: String containing configuration text  
        - filename: Path to configuration file
        """

    @staticmethod
    def fromfile(filename: str, use_predefined_variables: bool = True, import_custom_modules: bool = True) -> 'Config':
        """
        Load configuration from file.
        
        Parameters:
        - filename: Path to configuration file (.py or .json)
        - use_predefined_variables: Whether to use predefined variables
        - import_custom_modules: Whether to import custom modules
        
        Returns:
        Config object loaded from file
        """

    def merge_from_dict(self, options: dict, allow_list_keys: bool = True):
        """
        Merge configuration from dictionary.
        
        Parameters:
        - options: Dictionary of options to merge
        - allow_list_keys: Whether to allow list-type keys
        """

    def dump(self, file: str = None) -> str:
        """
        Dump configuration to string or file.
        
        Parameters:
        - file: Optional file path to save configuration
        
        Returns:
        Configuration as string
        """

    def pretty_text(self) -> str:
        """
        Get pretty formatted configuration text.
        
        Returns:
        Formatted configuration string
        """

    @property
    def filename(self) -> str:
        """Get the filename of the configuration."""

    @property 
    def text(self) -> str:
        """Get the text content of the configuration."""

ConfigDict Class

Dictionary-like configuration object with attribute access support, enabling convenient access to configuration values using dot notation.

class ConfigDict(dict):
    def __init__(self, *args, **kwargs):
        """Initialize ConfigDict with dictionary functionality plus attribute access."""

    def __getattr__(self, name: str):
        """
        Get configuration value as attribute.
        
        Parameters:
        - name: Attribute name
        
        Returns:
        Configuration value
        """

    def __setattr__(self, name: str, value):
        """
        Set configuration value as attribute.
        
        Parameters:
        - name: Attribute name
        - value: Value to set
        """

    def __delattr__(self, name: str):
        """
        Delete configuration attribute.
        
        Parameters:
        - name: Attribute name to delete
        """

    def to_dict(self) -> dict:
        """
        Convert ConfigDict to regular dictionary.
        
        Returns:
        Regular dictionary representation
        """

Argument Parsing Integration

Support for integrating configuration with command-line argument parsing through argparse actions.

class DictAction:
    def __init__(self, option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None):
        """
        Argparse action for handling dictionary arguments.
        
        Parameters:
        - option_strings: Option strings for the argument
        - dest: Destination attribute name
        - nargs: Number of arguments
        - const: Constant value
        - default: Default value
        - type: Argument type
        - choices: Valid choices
        - required: Whether argument is required
        - help: Help text
        - metavar: Metavar for help
        """

    def __call__(self, parser, namespace, values, option_string=None):
        """
        Process the argument values.
        
        Parameters:
        - parser: ArgumentParser instance
        - namespace: Namespace object
        - values: Argument values
        - option_string: Option string used
        """

Configuration Utilities

Utility functions for reading and processing configuration files with support for base configurations and inheritance.

def read_base(filename: str, scope: str = None) -> dict:
    """
    Read base configuration file.
    
    Parameters:
    - filename: Path to base configuration file
    - scope: Scope for configuration variables
    
    Returns:
    Dictionary containing configuration data
    """

Usage Examples

Basic Configuration Loading

from mmengine import Config

# Load from Python configuration file
cfg = Config.fromfile('config.py')

# Access configuration values
print(cfg.model.type)
print(cfg.dataset.batch_size)

# Load from JSON file
cfg = Config.fromfile('config.json')

Configuration Inheritance

# config.py with base configuration
_base_ = './base_config.py'

# Override specific values
model = dict(
    type='ResNet',
    depth=50,
    num_classes=1000
)

# Load configuration with inheritance
cfg = Config.fromfile('config.py')

Runtime Configuration Modification

from mmengine import Config

cfg = Config.fromfile('config.py')

# Merge from dictionary
cfg.merge_from_dict({
    'optimizer.lr': 0.001,
    'dataset.batch_size': 32
})

# Using ConfigDict for attribute access
cfg_dict = cfg.get('model', ConfigDict())
cfg_dict.num_classes = 10

Command Line Integration

import argparse
from mmengine import Config, DictAction

parser = argparse.ArgumentParser()
parser.add_argument('config', help='config file')
parser.add_argument('--cfg-options', action=DictAction, help='override config options')

args = parser.parse_args()
cfg = Config.fromfile(args.config)

if args.cfg_options:
    cfg.merge_from_dict(args.cfg_options)

Install with Tessl CLI

npx tessl i tessl/pypi-mmengine

docs

configuration.md

dataset.md

distributed.md

fileio.md

index.md

logging.md

models.md

optimization.md

registry.md

training.md

visualization.md

tile.json