Examples/torch/quantization/cle_bc.py
def cle_bc_example(config: argparse.Namespace):
    """
    Example code that shows the following
    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)

    # Applying cross-layer equalization (CLE)
    # Note that this API will equalize the model in-place
    apply_cross_layer_equalization(model=model, input_shape=(1, 3, 224, 224))

    # Calculate quantized (INT8) accuracy after CLE
    accuracy = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate, use_cuda=config.use_cuda)
    logger.info("Quantized (INT8) Model Top-1 Accuracy After CLE = %.2f", accuracy)

    # Applying Bias Correction
    # Bias Correction needs representative data samples (a small subset of either the training or validation data)
    data_loader = ImageNetDataLoader(is_training=False, images_dir=_config.dataset_dir,
                                     image_size=image_net_config.dataset['image_size']).data_loader
    # Note that this API will bias-correct the model in-place
    apply_bias_correction(model=model, data_loader=data_loader)

    # Calculating accuracy on Quant Simulator
    accuracy = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate, use_cuda=config.use_cuda)
    logger.info("Quantized (INT8) Model Top-1 Accuracy After Bias Correction = %.2f", accuracy)

    # Save the quantized model
    torch.save(model, "resnet_model_cle_bc.pt")

    logger.info("Cross Layer Equalization (CLE) and Bias Correction (BC) complete")

    Example code that shows the following
    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.
    
Example code that shows the following
1. Instantiates Data Pipeline for evaluation
2. Loads the pretrained resnet18 Pytorch model
3.
Example code that shows the following 1 . Instantiates Data Pipeline for evaluation 2 . Loads the pretrained resnet18 Pytorch model 3 .
def cle_bc_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)
    apply_cross_layer_equalization(model=model, input_shape=(1, 3, 224, 224))
    accuracy = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate, use_cuda=config.use_cuda)
    logger.info("Quantized (INT8) Model Top-1 Accuracy After CLE = %.2f", accuracy)
    data_loader = ImageNetDataLoader(is_training=False, images_dir=_config.dataset_dir,
                                     image_size=image_net_config.dataset['image_size']).data_loader
    apply_bias_correction(model=model, data_loader=data_loader)
    accuracy = calculate_quantsim_accuracy(model=model, evaluator=data_pipeline.evaluate, use_cuda=config.use_cuda)
    logger.info("Quantized (INT8) Model Top-1 Accuracy After Bias Correction = %.2f", accuracy)
    torch.save(model, "resnet_model_cle_bc.pt")
    logger.info("Cross Layer Equalization (CLE) and Bias Correction (BC) complete")
def cle_bc_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 ) apply_cross_layer_equalization ( model = model , input_shape = ( 1 , 3 , 224 , 224 ) ) accuracy = calculate_quantsim_accuracy ( model = model , evaluator = data_pipeline . evaluate , use_cuda = config . use_cuda ) logger . info ( "Quantized (INT8) Model Top-1 Accuracy After CLE = %.2f" , accuracy ) data_loader = ImageNetDataLoader ( is_training = False , images_dir = _config . dataset_dir , image_size = image_net_config . dataset [ 'image_size' ] ) . data_loader apply_bias_correction ( model = model , data_loader = data_loader ) accuracy = calculate_quantsim_accuracy ( model = model , evaluator = data_pipeline . evaluate , use_cuda = config . use_cuda ) logger . info ( "Quantized (INT8) Model Top-1 Accuracy After Bias Correction = %.2f" , accuracy ) torch . save ( model , "resnet_model_cle_bc.pt" ) logger . info ( "Cross Layer Equalization (CLE) and Bias Correction (BC) complete" )
Example code that shows the following
1.
Example code that shows the following 1 .
"""
    Example code that shows the following
    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 # Applying cross-layer equalization (CLE) # Note that this API will equalize the model in-place # Calculate quantized (INT8) accuracy after CLE # Applying Bias Correction # Bias Correction needs representative data samples (a small subset of either the training or validation data) # Note that this API will bias-correct the model in-place # Calculating accuracy on Quant Simulator # 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 .