Examples/torch/quantization/quantization_aware_training.py
def quantization_aware_training_example(config: argparse.Namespace):
    """
    1. Instantiates Data Pipeline for evaluation
    2. Loads the pretrained resnet18 Pytorch model
    3. Calculates Model accuracy
        3.1. Calculates floating point accuracy
        3.2. Calculates Quant Simulator accuracy
    4. Applies AIMET CLE and BC
        4.1. Applies AIMET CLE and calculates QuantSim accuracy
        4.2. Applies AIMET BC and calculates QuantSim accuracy


    :param config: This argparse.Namespace config expects following parameters:
                   tfrecord_dir: Path to a directory containing ImageNet TFRecords.
                                This folder should conatin files starting with:
                                'train*': for training records and 'validation*': for validation records
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
    """
    # 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 = model.eval()

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

    logger.info("Starting Model Quantization")

    # Quantize the model using AIMET QAT (quantization aware training) and calculate accuracy on Quant Simulator
    quantsim, accuracy = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate,
                                                     use_cuda=config.use_cuda,
                                                     logdir=config.logdir)

    logger.info("Quantized Model top-1 accuracy = %.2f", accuracy)

    # For good initialization apply, apply Post Training Quantization (PTQ) methods
    # such as Cross Layer Equalization (CLE) and Bias Correction (BC) (optional)
    data_loader = ImageNetDataLoader(is_training=False, images_dir=config.dataset_dir,
                                     image_size=image_net_config.dataset['image_size']).data_loader
    apply_cross_layer_equalization(model=model, input_shape=(1, 3, 224, 224))
    apply_bias_correction(model=model, data_loader=data_loader)

    quantsim, _ = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate, use_cuda=config.use_cuda,
                                              logdir=config.logdir)

    logger.info("Post Training Quantization (PTQ) Complete")

    # Finetune the quantized model
    logger.info("Starting Model Finetuning")
    data_pipeline.finetune(quantsim.model)

    # Calculate and log the accuracy of quantized-finetuned model
    accuracy = data_pipeline.evaluate(quantsim.model, use_cuda=config.use_cuda)
    logger.info("After Quantization Aware Training, top-1 accuracy = %.2f", accuracy)

    logger.info("Quantization Aware Training Complete")

    input_shape = (1, 3, 224, 224)
    dummy_input = torch.rand(input_shape)

    # Save the quantized model
    quantsim.export(path=config.logdir, filename_prefix='QAT_resnet', dummy_input=dummy_input.cpu())

    1. Instantiates Data Pipeline for evaluation
    2. Loads the pretrained resnet18 Pytorch model
    3. Calculates Model accuracy
        3.1. Calculates floating point accuracy
        3.2. Calculates Quant Simulator accuracy
    4. Applies AIMET CLE and BC
        4.1. Applies AIMET CLE and calculates QuantSim accuracy
        4.2. Applies AIMET BC and calculates QuantSim accuracy


    :param config: This argparse.Namespace config expects following parameters:
                   tfrecord_dir: Path to a directory containing ImageNet TFRecords.
                                This folder should conatin files starting with:
                                'train*': for training records and 'validation*': for validation records
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
    
1. Instantiates Data Pipeline for evaluation
2. Loads the pretrained resnet18 Pytorch model
3.
1 . Instantiates Data Pipeline for evaluation 2 . Loads the pretrained resnet18 Pytorch model 3 .
def quantization_aware_training_example(config: argparse.Namespace):
    data_pipeline = ImageNetDataPipeline(config)
    model = models.resnet18(pretrained=True)
    if config.use_cuda:
        model.to(torch.device('cuda'))
    model = model.eval()
    accuracy = data_pipeline.evaluate(model, use_cuda=config.use_cuda)
    logger.info("Original Model top-1 accuracy = %.2f", accuracy)
    logger.info("Starting Model Quantization")
    quantsim, accuracy = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate,
                                                     use_cuda=config.use_cuda,
                                                     logdir=config.logdir)
    logger.info("Quantized Model top-1 accuracy = %.2f", accuracy)
    data_loader = ImageNetDataLoader(is_training=False, images_dir=config.dataset_dir,
                                     image_size=image_net_config.dataset['image_size']).data_loader
    apply_cross_layer_equalization(model=model, input_shape=(1, 3, 224, 224))
    apply_bias_correction(model=model, data_loader=data_loader)
    quantsim, _ = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate, use_cuda=config.use_cuda,
                                              logdir=config.logdir)
    logger.info("Post Training Quantization (PTQ) Complete")
    logger.info("Starting Model Finetuning")
    data_pipeline.finetune(quantsim.model)
    accuracy = data_pipeline.evaluate(quantsim.model, use_cuda=config.use_cuda)
    logger.info("After Quantization Aware Training, top-1 accuracy = %.2f", accuracy)
    logger.info("Quantization Aware Training Complete")
    input_shape = (1, 3, 224, 224)
    dummy_input = torch.rand(input_shape)
    quantsim.export(path=config.logdir, filename_prefix='QAT_resnet', dummy_input=dummy_input.cpu())
def quantization_aware_training_example ( config : argparse . Namespace ) : data_pipeline = ImageNetDataPipeline ( config ) model = models . resnet18 ( pretrained = True ) if config . use_cuda : model . to ( torch . device ( 'cuda' ) ) model = model . eval ( ) accuracy = data_pipeline . evaluate ( model , use_cuda = config . use_cuda ) logger . info ( "Original Model top-1 accuracy = %.2f" , accuracy ) logger . info ( "Starting Model Quantization" ) quantsim , accuracy = calculate_quantsim_accuracy ( model = model , evaluator = data_pipeline . evaluate , use_cuda = config . use_cuda , logdir = config . logdir ) logger . info ( "Quantized Model top-1 accuracy = %.2f" , accuracy ) data_loader = ImageNetDataLoader ( is_training = False , images_dir = config . dataset_dir , image_size = image_net_config . dataset [ 'image_size' ] ) . data_loader apply_cross_layer_equalization ( model = model , input_shape = ( 1 , 3 , 224 , 224 ) ) apply_bias_correction ( model = model , data_loader = data_loader ) quantsim , _ = calculate_quantsim_accuracy ( model = model , evaluator = data_pipeline . evaluate , use_cuda = config . use_cuda , logdir = config . logdir ) logger . info ( "Post Training Quantization (PTQ) Complete" ) logger . info ( "Starting Model Finetuning" ) data_pipeline . finetune ( quantsim . model ) accuracy = data_pipeline . evaluate ( quantsim . model , use_cuda = config . use_cuda ) logger . info ( "After Quantization Aware Training, top-1 accuracy = %.2f" , accuracy ) logger . info ( "Quantization Aware Training Complete" ) input_shape = ( 1 , 3 , 224 , 224 ) dummy_input = torch . rand ( input_shape ) quantsim . export ( path = config . logdir , filename_prefix = 'QAT_resnet' , dummy_input = dummy_input . cpu ( ) )
1.
1 .
"""
    1. Instantiates Data Pipeline for evaluation
    2. Loads the pretrained resnet18 Pytorch model
    3. Calculates Model accuracy
        3.1. Calculates floating point accuracy
        3.2. Calculates Quant Simulator accuracy
    4. Applies AIMET CLE and BC
        4.1. Applies AIMET CLE and calculates QuantSim accuracy
        4.2. Applies AIMET BC and calculates QuantSim accuracy


    :param config: This argparse.Namespace config expects following parameters:
                   tfrecord_dir: Path to a directory containing ImageNet TFRecords.
                                This folder should conatin files starting with:
                                'train*': for training records and 'validation*': for validation records
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
    """ # Instantiate Data Pipeline for evaluation and training # Load the pretrained resnet18 model # Calculate FP32 accuracy # Quantize the model using AIMET QAT (quantization aware training) and calculate accuracy on Quant Simulator # For good initialization apply, apply Post Training Quantization (PTQ) methods # such as Cross Layer Equalization (CLE) and Bias Correction (BC) (optional) # Finetune the quantized model # Calculate and log the accuracy of quantized-finetuned model # Save the quantized model
config
argparse.Namespace
This argparse.Namespace config expects following parameters:
tfrecord_dir: Path to a directory containing ImageNet TFRecords.
This folder should conatin files starting with:
'train*': for training records and 'validation*': for validation records
use_cuda: A boolean var to indicate to run the test on GPU.
logdir: Path to a directory for logging.
This argparse . Namespace config expects following parameters : tfrecord_dir : Path to a directory containing ImageNet TFRecords . This folder should conatin files starting with : ' train * ' : for training records and ' validation * ' : for validation records use_cuda : A boolean var to indicate to run the test on GPU . logdir : Path to a directory for logging .