Examples/tensorflow/quantization/ada_round.py
def perform_adaround(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 Adaround and calculates QuantSim accuracy
        4.1. Applies AIMET Adaround
        4.2. Calculates Adaround applied model Quant Simulator accuracy
        4.3. Exports Adaround 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 = ResNet50(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.
    BN_folded_sess, _ = aimet_bnf.fold_all_batch_norms(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 Adaround and calculates QuantSim accuracy
    logger.info("Starting Aimet Adaround...")

    # 4.1. Applies AIMET Adaround
    adaround_applied_BN_folded_sess = aimet_adaround(BN_folded_sess, start_op_names=['input_1'],
                                                     output_op_names=[model.output.name.split(":")[0]],
                                                     data_set=data_pipeline.data_loader().dataset,
                                                     working_dir=config.logdir,
                                                     encoding_file_prefix='adaround_model')

    # 4.2. Calculates Adaround applied model Quant Simulator accuracy
    # Creating QuantSim model
    quant_sim.session.close()
    quant_sim = create_quant_sim_model(adaround_applied_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,
                                       encoding_filename=os.path.join(config.logdir, 'adaround_model.encodings'))
    # Calculating QuantSim model accuracy
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Adaround applied Model Top-1 accuracy on Quant Simulator = %.2f", accuracy)

    # 4.3 Exports Adaround 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 Adaround 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 Adaround and calculates QuantSim accuracy
        4.1. Applies AIMET Adaround
        4.2. Calculates Adaround applied model Quant Simulator accuracy
        4.3. Exports Adaround 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_adaround(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 = ResNet50(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, _ = aimet_bnf.fold_all_batch_norms(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 Adaround...")
    adaround_applied_BN_folded_sess = aimet_adaround(BN_folded_sess, start_op_names=['input_1'],
                                                     output_op_names=[model.output.name.split(":")[0]],
                                                     data_set=data_pipeline.data_loader().dataset,
                                                     working_dir=config.logdir,
                                                     encoding_file_prefix='adaround_model')
    quant_sim.session.close()
    quant_sim = create_quant_sim_model(adaround_applied_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,
                                       encoding_filename=os.path.join(config.logdir, 'adaround_model.encodings'))
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Adaround 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 Adaround Done")
def perform_adaround ( 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 = ResNet50 ( 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 , _ = aimet_bnf . fold_all_batch_norms ( 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 Adaround..." ) adaround_applied_BN_folded_sess = aimet_adaround ( BN_folded_sess , start_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] , data_set = data_pipeline . data_loader ( ) . dataset , working_dir = config . logdir , encoding_file_prefix = 'adaround_model' ) quant_sim . session . close ( ) quant_sim = create_quant_sim_model ( adaround_applied_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 , encoding_filename = os . path . join ( config . logdir , 'adaround_model.encodings' ) ) accuracy = data_pipeline . evaluate ( quant_sim . session ) logger . info ( "Adaround 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 Adaround 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 Adaround and calculates QuantSim accuracy
        4.1. Applies AIMET Adaround
        4.2. Calculates Adaround applied model Quant Simulator accuracy
        4.3. Exports Adaround 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. # Creating QuantSim model # Calculating QuantSim model accuracy # 4. Applies AIMET Adaround and calculates QuantSim accuracy # 4.1. Applies AIMET Adaround # 4.2. Calculates Adaround applied model Quant Simulator accuracy # Creating QuantSim model # Calculating QuantSim model accuracy # 4.3 Exports Adaround 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 .