The main.py script serves as the CLI entrypoint for training and evaluating the ViTMatte model using Detectron2's LazyConfig system. It supports both full training runs and evaluation-only modes.
Usage Modes
- Training: By default, the script runs the training loop defined in
do_train. It instantiates the model, optimizer, and dataloaders from the provided configuration file and manages checkpoints, learning rate scheduling, and logging. - Evaluation Only: Use the
--eval-only flag to skip training and only run inference on the test dataset to evaluate model performance.
Configuration Requirements
The script expects a LazyConfig python file that defines the following structure:
model: An instantiable module.dataloader.train: An instantiable training dataloader.dataloader.test: An instantiable test dataloader.dataloader.evaluator: An instantiable evaluator for the test set.optimizer: An instantiable optimizer.lr_multiplier: An instantiable fvcore scheduler.train: A dictionary containing:output_dir (str): Directory for logs and checkpoints.init_checkpoint (str): Path to the initial checkpoint.amp.enabled (bool): Whether to use Automatic Mixed Precision.max_iter (int): Total number of training iterations.eval_period (int): Iteration interval for evaluation.log_period (int): Iteration interval for logging.device (str): Target device (e.g., 'cuda').checkpointer (dict): Configuration for the periodic checkpointer.ddp (dict): Configuration for Distributed Data Parallel.
CLI Arguments
The script uses default_argument_parser() from Detectron2, which typically includes:
--config-file: Path to the Python configuration file.--eval-only: Flag to run evaluation instead of training.--resume: Flag to resume training from the last checkpoint.--num-gpus: Number of GPUs to use.--opts: Arbitrary configuration overrides (e.g., train.max_iter=1000).
# Example: Train the model
python main.py --config-file configs/your_config.py
# Example: Evaluate a checkpoint
python main.py --config-file configs/your_config.py --eval-only
# Example: Resume training
python main.py --config-file configs/your_config.py --resume