Examples/tensorflow/quantization/cle_bc.py
def perform_cle_bc(config: argparse.Namespace):
    """
    1. Instantiates Data Pipeline for evaluation
    2. Loads the pretrained resnet50 keras 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
        4.3. Exports CLE+BC applied model so it is ready to be run on-target

    :param config: This argparse.Namespace config expects following parameters:
                   tfrecord_dir: Path to a directory containing ImageNet TFRecords.
                                This folder should contain 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 and training
    data_pipeline = ImageNetDataPipeline(config)

    # 2. Loads the pretrained resnet50 keras model
    input_shape = (image_net_config.dataset['image_width'],
                   image_net_config.dataset['image_height'],
                   image_net_config.dataset['image_channels'])
    tf.keras.backend.clear_session()
    model = MobileNet(weights='imagenet', input_shape=input_shape)
    sess = tf.keras.backend.get_session()
    add_image_net_computational_nodes_in_graph(sess, model.output, image_net_config.dataset['images_classes'])

    # 3. Calculates Model accuracy

    # 3.1. Floating Point accuracy
    accuracy = data_pipeline.evaluate(sess)
    logger.info("Original Model Top-1 accuracy = %.2f", accuracy)

    # 3.2. Quant Simulator accuracy

    # It is recommended to fold Batch-norms before running on target.
    # But CLE needs original model/graph to operate upon, so making a
    # copy of model/graph here, before folding BN layers.
    BN_folded_sess = save_and_load_graph(sess=sess, meta_path=config.logdir)
    BN_folded_sess, _ = aimet_bnf.fold_all_batch_norms(BN_folded_sess, input_op_names=['input_1'],
                                                       output_op_names=[model.output.name.split(":")[0]])

    # Creating QuantSim model
    quant_sim = create_quant_sim_model(BN_folded_sess, start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, evaluator=data_pipeline.evaluate,
                                       logdir=config.logdir)
    # Calculating QuantSim model accuracy
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Original Model Top-1 accuracy on Quant Simulator = %.2f", accuracy)

    # 4. Applies AIMET CLE and BC
    logger.info("Starting Aimet Cross Layer Equalization and Bias Correction...")

    # 4.1. Applies AIMET CLE and calculates QuantSim accuracy
    cle_applied_sess = aimet_cross_layer_equalization(sess, start_op_names=['input_1'],
                                                      output_op_names=[model.output.name.split(":")[0]])

    # Creating QuantSim model
    quant_sim.session.close()
    quant_sim = create_quant_sim_model(cle_applied_sess, start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, evaluator=data_pipeline.evaluate,
                                       logdir=config.logdir)
    # Calculating QuantSim model accuracy
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("CLE applied Model Top-1 accuracy on Quant Simulator = %.2f", accuracy)

    # 4.2. Applies AIMET BC and calculates QuantSim accuracy

    # Here 'labels' has been added into start_op_names as the data_loader.data_set gives
    # a tuple of (images, labels) and aimet_bc.BiasCorrection.correct_bias API checks the
    # length of start_op_names against the length of data_set output. The 'labels' value
    # will be fed but not utilized though.
    cle_bc_applied_sess = aimet_bias_correction(cle_applied_sess, start_op_names=['input_1', 'labels'],
                                                output_op_names=[model.output.name.split(":")[0]],
                                                data_loader=data_pipeline.data_loader(),
                                                use_cuda=config.use_cuda)

    # Creating QuantSim model
    quant_sim.session.close()
    quant_sim = create_quant_sim_model(cle_bc_applied_sess, start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, evaluator=data_pipeline.evaluate,
                                       logdir=config.logdir)
    # Calculating QuantSim model accuracy
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("CLE+BC applied Model Top-1 accuracy on Quant Simulator = %.2f", accuracy)

    # 4.3 Exports CLE+BC applied model so it is ready to be run on-target
    logger.info("Saving Quantized model graph...")
    quant_sim.export(path=config.logdir, filename_prefix='quantized_model')
    logger.info("...Quantized model graph is saved!")

    logger.info("...Aimet Cross Layer Equalization and Bias Correction Done")

    1. Instantiates Data Pipeline for evaluation
    2. Loads the pretrained resnet50 keras 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
        4.3. Exports CLE+BC applied model so it is ready to be run on-target

    :param config: This argparse.Namespace config expects following parameters:
                   tfrecord_dir: Path to a directory containing ImageNet TFRecords.
                                This folder should contain 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 resnet50 keras model
3.
1 . Instantiates Data Pipeline for evaluation 2 . Loads the pretrained resnet50 keras model 3 .
def perform_cle_bc(config: argparse.Namespace):
    data_pipeline = ImageNetDataPipeline(config)
    input_shape = (image_net_config.dataset['image_width'],
                   image_net_config.dataset['image_height'],
                   image_net_config.dataset['image_channels'])
    tf.keras.backend.clear_session()
    model = MobileNet(weights='imagenet', input_shape=input_shape)
    sess = tf.keras.backend.get_session()
    add_image_net_computational_nodes_in_graph(sess, model.output, image_net_config.dataset['images_classes'])
    accuracy = data_pipeline.evaluate(sess)
    logger.info("Original Model Top-1 accuracy = %.2f", accuracy)
    BN_folded_sess = save_and_load_graph(sess=sess, meta_path=config.logdir)
    BN_folded_sess, _ = aimet_bnf.fold_all_batch_norms(BN_folded_sess, input_op_names=['input_1'],
                                                       output_op_names=[model.output.name.split(":")[0]])
    quant_sim = create_quant_sim_model(BN_folded_sess, start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, evaluator=data_pipeline.evaluate,
                                       logdir=config.logdir)
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Original Model Top-1 accuracy on Quant Simulator = %.2f", accuracy)
    logger.info("Starting Aimet Cross Layer Equalization and Bias Correction...")
    cle_applied_sess = aimet_cross_layer_equalization(sess, start_op_names=['input_1'],
                                                      output_op_names=[model.output.name.split(":")[0]])
    quant_sim.session.close()
    quant_sim = create_quant_sim_model(cle_applied_sess, start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, evaluator=data_pipeline.evaluate,
                                       logdir=config.logdir)
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("CLE applied Model Top-1 accuracy on Quant Simulator = %.2f", accuracy)
    cle_bc_applied_sess = aimet_bias_correction(cle_applied_sess, start_op_names=['input_1', 'labels'],
                                                output_op_names=[model.output.name.split(":")[0]],
                                                data_loader=data_pipeline.data_loader(),
                                                use_cuda=config.use_cuda)
    quant_sim.session.close()
    quant_sim = create_quant_sim_model(cle_bc_applied_sess, start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, evaluator=data_pipeline.evaluate,
                                       logdir=config.logdir)
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("CLE+BC applied Model Top-1 accuracy on Quant Simulator = %.2f", accuracy)
    logger.info("Saving Quantized model graph...")
    quant_sim.export(path=config.logdir, filename_prefix='quantized_model')
    logger.info("...Quantized model graph is saved!")
    logger.info("...Aimet Cross Layer Equalization and Bias Correction Done")
def perform_cle_bc ( config : argparse . Namespace ) : data_pipeline = ImageNetDataPipeline ( config ) input_shape = ( image_net_config . dataset [ 'image_width' ] , image_net_config . dataset [ 'image_height' ] , image_net_config . dataset [ 'image_channels' ] ) tf . keras . backend . clear_session ( ) model = MobileNet ( weights = 'imagenet' , input_shape = input_shape ) sess = tf . keras . backend . get_session ( ) add_image_net_computational_nodes_in_graph ( sess , model . output , image_net_config . dataset [ 'images_classes' ] ) accuracy = data_pipeline . evaluate ( sess ) logger . info ( "Original Model Top-1 accuracy = %.2f" , accuracy ) BN_folded_sess = save_and_load_graph ( sess = sess , meta_path = config . logdir ) BN_folded_sess , _ = aimet_bnf . fold_all_batch_norms ( BN_folded_sess , input_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] ) quant_sim = create_quant_sim_model ( BN_folded_sess , start_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] , use_cuda = config . use_cuda , evaluator = data_pipeline . evaluate , logdir = config . logdir ) accuracy = data_pipeline . evaluate ( quant_sim . session ) logger . info ( "Original Model Top-1 accuracy on Quant Simulator = %.2f" , accuracy ) logger . info ( "Starting Aimet Cross Layer Equalization and Bias Correction..." ) cle_applied_sess = aimet_cross_layer_equalization ( sess , start_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] ) quant_sim . session . close ( ) quant_sim = create_quant_sim_model ( cle_applied_sess , start_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] , use_cuda = config . use_cuda , evaluator = data_pipeline . evaluate , logdir = config . logdir ) accuracy = data_pipeline . evaluate ( quant_sim . session ) logger . info ( "CLE applied Model Top-1 accuracy on Quant Simulator = %.2f" , accuracy ) cle_bc_applied_sess = aimet_bias_correction ( cle_applied_sess , start_op_names = [ 'input_1' , 'labels' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] , data_loader = data_pipeline . data_loader ( ) , use_cuda = config . use_cuda ) quant_sim . session . close ( ) quant_sim = create_quant_sim_model ( cle_bc_applied_sess , start_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] , use_cuda = config . use_cuda , evaluator = data_pipeline . evaluate , logdir = config . logdir ) accuracy = data_pipeline . evaluate ( quant_sim . session ) logger . info ( "CLE+BC applied Model Top-1 accuracy on Quant Simulator = %.2f" , accuracy ) logger . info ( "Saving Quantized model graph..." ) quant_sim . export ( path = config . logdir , filename_prefix = 'quantized_model' ) logger . info ( "...Quantized model graph is saved!" ) logger . info ( "...Aimet Cross Layer Equalization and Bias Correction Done" )
1.
1 .
"""
    1. Instantiates Data Pipeline for evaluation
    2. Loads the pretrained resnet50 keras 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
        4.3. Exports CLE+BC applied model so it is ready to be run on-target

    :param config: This argparse.Namespace config expects following parameters:
                   tfrecord_dir: Path to a directory containing ImageNet TFRecords.
                                This folder should contain 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 and training # 2. Loads the pretrained resnet50 keras model # 3. Calculates Model accuracy # 3.1. Floating Point accuracy # 3.2. Quant Simulator accuracy # It is recommended to fold Batch-norms before running on target. # But CLE needs original model/graph to operate upon, so making a # copy of model/graph here, before folding BN layers. # Creating QuantSim model # Calculating QuantSim model accuracy # 4. Applies AIMET CLE and BC # 4.1. Applies AIMET CLE and calculates QuantSim accuracy # Creating QuantSim model # Calculating QuantSim model accuracy # 4.2. Applies AIMET BC and calculates QuantSim accuracy # Here 'labels' has been added into start_op_names as the data_loader.data_set gives # a tuple of (images, labels) and aimet_bc.BiasCorrection.correct_bias API checks the # length of start_op_names against the length of data_set output. The 'labels' value # will be fed but not utilized though. # Creating QuantSim model # Calculating QuantSim model accuracy # 4.3 Exports CLE+BC applied model so it is ready to be run on-target
config
argparse.Namespace
This argparse.Namespace config expects following parameters:
tfrecord_dir: Path to a directory containing ImageNet TFRecords.
This folder should contain 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 contain 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 .