Examples/torch/compression/channel_pruning.py
def channel_pruning_example(config: argparse.Namespace):
    """
    1. Instantiate Data Pipeline for evaluation and training
    2. Load the pretrained resnet18 model
    3. Calculate floating point accuracy
    4. Compression
        4.1. Compress the model using AIMET Channel Pruning
        4.2. Log the statistics
        4.3. Save the compressed model
        4.4. Calculate and log the accuracy of compressed model
    5. Finetuning
        5.1 Finetune the compressed model
        5.2 Calculate and log the accuracy of compressed-finetuned model

    :param config: This argparse.Namespace config expects following parameters:
                   dataset_dir: Path to a directory containing ImageNet dataset.
                                This folder should conatin at least 2 subfolders:
                                'train': for training dataset and 'val': for validation dataset.
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for finetuning.
                   learning_rate: A float type learning rate for model finetuning
                   learning_rate_schedule: A list of epoch indices for learning rate schedule used in finetuning. Check
                                           https://pytorch.org/docs/stable/_modules/torch/optim/lr_scheduler.html#MultiStepLR
                                           for more details.
    """

    # Instantiate Data Pipeline for evaluation and training
    data_pipeline = ImageNetDataPipeline(config)

    # Load the pretrained resnet18 model
    model = models.resnet18(pretrained=True)
    if config.use_cuda:
        model.to(torch.device('cuda'))
    model.eval()

    # Calculate floating point accuracy
    accuracy = data_pipeline.evaluate(model, use_cuda=config.use_cuda)
    logger.info("Original Model top-1 accuracy = %.2f", accuracy)

    logger.info("Starting Channel Pruning")

    # Compress the model using AIMET Channel Pruning
    # in auto mode, AIMET uses the Greedy Compression-Ratio Selection algorithm
    data_loader = ImageNetDataLoader(is_training=True, images_dir=_config.dataset_dir, image_size=224).data_loader
    compressed_model, stats = aimet_channel_pruning(model=model, evaluator=data_pipeline.evaluate,
                                                    data_loader=data_loader)

    logger.info(stats)
    with open(os.path.join(config.logdir, 'log.txt'), "w") as outfile:
        outfile.write("%s\n\n" % (stats))

    # Calculate and log the accuracy of compressed model
    accuracy = data_pipeline.evaluate(compressed_model, use_cuda=config.use_cuda)
    logger.info("After Channel Pruning, top-1 accuracy = %.2f", accuracy)

    logger.info("Model Channel Pruning Complete")

    # Finetune the compressed model
    logger.info("Starting Model Finetuning")
    data_pipeline.finetune(compressed_model)

    # Calculate and log the accuracy of compressed-finetuned model
    accuracy = data_pipeline.evaluate(compressed_model, use_cuda=config.use_cuda)
    logger.info("Finetuned Compressed Model top-1 accuracy = %.2f", accuracy)
    logger.info("Model Finetuning Complete")

    # Save the compressed model
    torch.save(compressed_model, os.path.join(config.logdir, 'compressed_model.pth'))

    1. Instantiate Data Pipeline for evaluation and training
    2. Load the pretrained resnet18 model
    3. Calculate floating point accuracy
    4. Compression
        4.1. Compress the model using AIMET Channel Pruning
        4.2. Log the statistics
        4.3. Save the compressed model
        4.4. Calculate and log the accuracy of compressed model
    5. Finetuning
        5.1 Finetune the compressed model
        5.2 Calculate and log the accuracy of compressed-finetuned model

    :param config: This argparse.Namespace config expects following parameters:
                   dataset_dir: Path to a directory containing ImageNet dataset.
                                This folder should conatin at least 2 subfolders:
                                'train': for training dataset and 'val': for validation dataset.
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for finetuning.
                   learning_rate: A float type learning rate for model finetuning
                   learning_rate_schedule: A list of epoch indices for learning rate schedule used in finetuning. Check
                                           https://pytorch.org/docs/stable/_modules/torch/optim/lr_scheduler.html#MultiStepLR
                                           for more details.
    
1. Instantiate Data Pipeline for evaluation and training
2. Load the pretrained resnet18 model
3. Calculate floating point accuracy
4.
1 . Instantiate Data Pipeline for evaluation and training 2 . Load the pretrained resnet18 model 3 . Calculate floating point accuracy 4 .
def channel_pruning_example(config: argparse.Namespace):
    data_pipeline = ImageNetDataPipeline(config)
    model = models.resnet18(pretrained=True)
    if config.use_cuda:
        model.to(torch.device('cuda'))
    model.eval()
    accuracy = data_pipeline.evaluate(model, use_cuda=config.use_cuda)
    logger.info("Original Model top-1 accuracy = %.2f", accuracy)
    logger.info("Starting Channel Pruning")
    data_loader = ImageNetDataLoader(is_training=True, images_dir=_config.dataset_dir, image_size=224).data_loader
    compressed_model, stats = aimet_channel_pruning(model=model, evaluator=data_pipeline.evaluate,
                                                    data_loader=data_loader)
    logger.info(stats)
    with open(os.path.join(config.logdir, 'log.txt'), "w") as outfile:
        outfile.write("%s\n\n" % (stats))
    accuracy = data_pipeline.evaluate(compressed_model, use_cuda=config.use_cuda)
    logger.info("After Channel Pruning, top-1 accuracy = %.2f", accuracy)
    logger.info("Model Channel Pruning Complete")
    logger.info("Starting Model Finetuning")
    data_pipeline.finetune(compressed_model)
    accuracy = data_pipeline.evaluate(compressed_model, use_cuda=config.use_cuda)
    logger.info("Finetuned Compressed Model top-1 accuracy = %.2f", accuracy)
    logger.info("Model Finetuning Complete")
    torch.save(compressed_model, os.path.join(config.logdir, 'compressed_model.pth'))
def channel_pruning_example ( config : argparse . Namespace ) : data_pipeline = ImageNetDataPipeline ( config ) model = models . resnet18 ( pretrained = True ) if config . use_cuda : model . to ( torch . device ( 'cuda' ) ) model . eval ( ) accuracy = data_pipeline . evaluate ( model , use_cuda = config . use_cuda ) logger . info ( "Original Model top-1 accuracy = %.2f" , accuracy ) logger . info ( "Starting Channel Pruning" ) data_loader = ImageNetDataLoader ( is_training = True , images_dir = _config . dataset_dir , image_size = 224 ) . data_loader compressed_model , stats = aimet_channel_pruning ( model = model , evaluator = data_pipeline . evaluate , data_loader = data_loader ) logger . info ( stats ) with open ( os . path . join ( config . logdir , 'log.txt' ) , "w" ) as outfile : outfile . write ( "%s\n\n" % ( stats ) ) accuracy = data_pipeline . evaluate ( compressed_model , use_cuda = config . use_cuda ) logger . info ( "After Channel Pruning, top-1 accuracy = %.2f" , accuracy ) logger . info ( "Model Channel Pruning Complete" ) logger . info ( "Starting Model Finetuning" ) data_pipeline . finetune ( compressed_model ) accuracy = data_pipeline . evaluate ( compressed_model , use_cuda = config . use_cuda ) logger . info ( "Finetuned Compressed Model top-1 accuracy = %.2f" , accuracy ) logger . info ( "Model Finetuning Complete" ) torch . save ( compressed_model , os . path . join ( config . logdir , 'compressed_model.pth' ) )
1.
1 .
"""
    1. Instantiate Data Pipeline for evaluation and training
    2. Load the pretrained resnet18 model
    3. Calculate floating point accuracy
    4. Compression
        4.1. Compress the model using AIMET Channel Pruning
        4.2. Log the statistics
        4.3. Save the compressed model
        4.4. Calculate and log the accuracy of compressed model
    5. Finetuning
        5.1 Finetune the compressed model
        5.2 Calculate and log the accuracy of compressed-finetuned model

    :param config: This argparse.Namespace config expects following parameters:
                   dataset_dir: Path to a directory containing ImageNet dataset.
                                This folder should conatin at least 2 subfolders:
                                'train': for training dataset and 'val': for validation dataset.
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for finetuning.
                   learning_rate: A float type learning rate for model finetuning
                   learning_rate_schedule: A list of epoch indices for learning rate schedule used in finetuning. Check
                                           https://pytorch.org/docs/stable/_modules/torch/optim/lr_scheduler.html#MultiStepLR
                                           for more details.
    """ # Instantiate Data Pipeline for evaluation and training # Load the pretrained resnet18 model # Calculate floating point accuracy # Compress the model using AIMET Channel Pruning # in auto mode, AIMET uses the Greedy Compression-Ratio Selection algorithm # Calculate and log the accuracy of compressed model # Finetune the compressed model # Calculate and log the accuracy of compressed-finetuned model # Save the compressed model
config
argparse.Namespace
This argparse.Namespace config expects following parameters:
dataset_dir: Path to a directory containing ImageNet dataset.
This folder should conatin at least 2 subfolders:
'train': for training dataset and 'val': for validation dataset.
use_cuda: A boolean var to indicate to run the test on GPU.
logdir: Path to a directory for logging.
epochs: Number of epochs (type int) for finetuning.
learning_rate: A float type learning rate for model finetuning
learning_rate_schedule: A list of epoch indices for learning rate schedule used in finetuning.
This argparse . Namespace config expects following parameters : dataset_dir : Path to a directory containing ImageNet dataset . This folder should conatin at least 2 subfolders : ' train ' : for training dataset and ' val ' : for validation dataset . use_cuda : A boolean var to indicate to run the test on GPU . logdir : Path to a directory for logging . epochs : Number of epochs ( type int ) for finetuning . learning_rate : A float type learning rate for model finetuning learning_rate_schedule : A list of epoch indices for learning rate schedule used in finetuning .