Examples/torch/quantization/quantization_aware_training.py
def calculate_quantsim_accuracy(model: torch.nn.Module, evaluator: aimet_common.defs.EvalFunction,
                                use_cuda: bool = False, logdir: str = '') -> Tuple[torch.nn.Module, float]:
    """
    Calculates model accuracy on quantized simulator and returns quantized model with accuracy.

    :param model: the loaded model
    :param evaluator: the Eval function to use for evaluation
    :param iterations: No of batches to use in computing encodings.
                       Not used in image net dataset
    :param num_val_samples_per_class: No of samples to use from every class in
                                      computing encodings. Not used in pascal voc
                                      dataset
    :param use_cuda: the cuda device.
    :return: a tuple of quantsim and accuracy of model on this quantsim
    """

    input_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'],
                   image_net_config.dataset['image_height'],)
    if use_cuda:
        model.to(torch.device('cuda'))
        dummy_input = torch.rand(input_shape).cuda()
    else:
        dummy_input = torch.rand(input_shape)

    # Number of batches to use for computing encodings
    # Only 5 batches are used here to speed up the process, also the
    # number of images in these 5 batches should be sufficient for
    # compute encodings
    iterations = 5

    quantsim = QuantizationSimModel(model=model, quant_scheme='tf_enhanced',
                                    dummy_input=dummy_input, rounding_mode='nearest',
                                    default_output_bw=8, default_param_bw=8, in_place=False)

    quantsim.compute_encodings(forward_pass_callback=partial(evaluator, use_cuda=use_cuda),
                               forward_pass_callback_args=iterations)

    quantsim.export(path=logdir, filename_prefix='resnet_encodings', dummy_input=dummy_input.cpu())
    accuracy = evaluator(quantsim.model, use_cuda=use_cuda)

    return quantsim, accuracy

    Calculates model accuracy on quantized simulator and returns quantized model with accuracy.

    :param model: the loaded model
    :param evaluator: the Eval function to use for evaluation
    :param iterations: No of batches to use in computing encodings.
                       Not used in image net dataset
    :param num_val_samples_per_class: No of samples to use from every class in
                                      computing encodings. Not used in pascal voc
                                      dataset
    :param use_cuda: the cuda device.
    :return: a tuple of quantsim and accuracy of model on this quantsim
    
Calculates model accuracy on quantized simulator and returns quantized model with accuracy.
Calculates model accuracy on quantized simulator and returns quantized model with accuracy .
def calculate_quantsim_accuracy(model: torch.nn.Module, evaluator: aimet_common.defs.EvalFunction,
                                use_cuda: bool = False, logdir: str = '') -> Tuple[torch.nn.Module, float]:
    input_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'],
                   image_net_config.dataset['image_height'],)
    if use_cuda:
        model.to(torch.device('cuda'))
        dummy_input = torch.rand(input_shape).cuda()
    else:
        dummy_input = torch.rand(input_shape)
    iterations = 5
    quantsim = QuantizationSimModel(model=model, quant_scheme='tf_enhanced',
                                    dummy_input=dummy_input, rounding_mode='nearest',
                                    default_output_bw=8, default_param_bw=8, in_place=False)
    quantsim.compute_encodings(forward_pass_callback=partial(evaluator, use_cuda=use_cuda),
                               forward_pass_callback_args=iterations)
    quantsim.export(path=logdir, filename_prefix='resnet_encodings', dummy_input=dummy_input.cpu())
    accuracy = evaluator(quantsim.model, use_cuda=use_cuda)
    return quantsim, accuracy
def calculate_quantsim_accuracy ( model : torch . nn . Module , evaluator : aimet_common . defs . EvalFunction , use_cuda : bool = False , logdir : str = '' ) -> Tuple [ torch . nn . Module , float ] : input_shape = ( 1 , image_net_config . dataset [ 'image_channels' ] , image_net_config . dataset [ 'image_width' ] , image_net_config . dataset [ 'image_height' ] , ) if use_cuda : model . to ( torch . device ( 'cuda' ) ) dummy_input = torch . rand ( input_shape ) . cuda ( ) else : dummy_input = torch . rand ( input_shape ) iterations = 5 quantsim = QuantizationSimModel ( model = model , quant_scheme = 'tf_enhanced' , dummy_input = dummy_input , rounding_mode = 'nearest' , default_output_bw = 8 , default_param_bw = 8 , in_place = False ) quantsim . compute_encodings ( forward_pass_callback = partial ( evaluator , use_cuda = use_cuda ) , forward_pass_callback_args = iterations ) quantsim . export ( path = logdir , filename_prefix = 'resnet_encodings' , dummy_input = dummy_input . cpu ( ) ) accuracy = evaluator ( quantsim . model , use_cuda = use_cuda ) return quantsim , accuracy
Calculates model accuracy on quantized simulator and returns quantized model with accuracy.
Calculates model accuracy on quantized simulator and returns quantized model with accuracy .
"""
    Calculates model accuracy on quantized simulator and returns quantized model with accuracy.

    :param model: the loaded model
    :param evaluator: the Eval function to use for evaluation
    :param iterations: No of batches to use in computing encodings.
                       Not used in image net dataset
    :param num_val_samples_per_class: No of samples to use from every class in
                                      computing encodings. Not used in pascal voc
                                      dataset
    :param use_cuda: the cuda device.
    :return: a tuple of quantsim and accuracy of model on this quantsim
    """ # Number of batches to use for computing encodings # Only 5 batches are used here to speed up the process, also the # number of images in these 5 batches should be sufficient for # compute encodings
a tuple of quantsim and accuracy of model on this quantsim
a tuple of quantsim and accuracy of model on this quantsim
model
torch.nn.Module
the loaded model
the loaded model
evaluator
aimet_common.defs.EvalFunction
the Eval function to use for evaluation
the Eval function to use for evaluation
use_cuda
bool
the cuda device.
the cuda device .
logdir
str
iterations
No of batches to use in computing encodings.
Not used in image net dataset
No of batches to use in computing encodings . Not used in image net dataset
num_val_samples_per_class
No of samples to use from every class in
computing encodings. Not used in pascal voc
dataset
No of samples to use from every class in computing encodings . Not used in pascal voc dataset