Examples/torch/compression/spatial_svd.py
def spatial_svd_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 Spatial SVD
        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)

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

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

    # Compress the model using AIMET Weight SVD
    logger.info("Starting Spatial SVD")
    compressed_model, stats = aimet_spatial_svd(model=model, evaluator=data_pipeline.evaluate)

    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("Compressed Model Top-1 accuracy = %.2f", accuracy)

    logger.info("Spatial SVD 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 Spatial SVD
        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 spatial_svd_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 Spatial SVD")
    compressed_model, stats = aimet_spatial_svd(model=model, evaluator=data_pipeline.evaluate)
    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("Compressed Model Top-1 accuracy = %.2f", accuracy)
    logger.info("Spatial SVD 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 spatial_svd_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 Spatial SVD" ) compressed_model , stats = aimet_spatial_svd ( model = model , evaluator = data_pipeline . evaluate ) 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 ( "Compressed Model Top-1 accuracy = %.2f" , accuracy ) logger . info ( "Spatial SVD 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 Spatial SVD
        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 # Loads the pretrained resnet18 model # Calculates floating point accuracy # Compress the model using AIMET Weight SVD # 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 .