Examples/tensorflow/quantization/qat.py
def perform_qat(config: argparse.Namespace):
    """
    1. Instantiates Data Pipeline for evaluation and training
    2. Loads the pretrained resnet50 keras model
    3. Calculates floating point accuracy
    4. Quantization Sim Model
        4.1. Creates Quantization Sim model using AIMET QuantizationSimModel
        4.2. Calculates and logs the accuracy of quantizer sim model
    5. Quantization Aware Training
        5.1. Trains the quantization aware model
        5.2. Calculates and logs the accuracy of quantization Aware trained model
        5.3. Exports quantization aware 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
                   parity_config_file: An optional parity config file, used in Quantizer
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for training.
                   learning_rate: A float type learning rate for model training
                   decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
                                epochs in training.
    """

    # 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'])
    update_ops_name = [op.name for op in model.updates]

    # 3. Calculates floating point accuracy
    accuracy = data_pipeline.evaluate(sess)
    logger.info("Original Model Top-1 accuracy = %.2f", accuracy)

    # 4. Quantization Sim Model
    logger.info("Starting Model QuantSim...")

    # 4.1. Creates Quantization Sim model using AIMET QuantizationSimModel

    # It is recommended to fold Batch-norms before making model runnable 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]])

    quant_sim = create_quant_sim_model(sess=BN_folded_sess,
                                       start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, parity_config_file=config.parity_config_file,
                                       evaluator=data_pipeline.evaluate)

    # 4.2. Calculates and logs the accuracy of quantizer sim model
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Model Top-1 Accuracy on Quant Simulator = %.2f", accuracy)

    logger.info("...Model QuantSim Done")

    # 5. Quantization Aware Training
    logger.info("Starting Model QAT...")

    # 5.1. Trains the quantization aware model
    data_pipeline.train(quant_sim.session, update_ops_name=update_ops_name)

    # 5.2. Calculates and logs the accuracy of quantization aware trained model
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Quantization aware trained model Top-1 Accuracy on Quant Simulator = %.2f", accuracy)

    # 5.3. Exports quantization aware 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("...Model QAT Done")

    1. Instantiates Data Pipeline for evaluation and training
    2. Loads the pretrained resnet50 keras model
    3. Calculates floating point accuracy
    4. Quantization Sim Model
        4.1. Creates Quantization Sim model using AIMET QuantizationSimModel
        4.2. Calculates and logs the accuracy of quantizer sim model
    5. Quantization Aware Training
        5.1. Trains the quantization aware model
        5.2. Calculates and logs the accuracy of quantization Aware trained model
        5.3. Exports quantization aware 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
                   parity_config_file: An optional parity config file, used in Quantizer
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for training.
                   learning_rate: A float type learning rate for model training
                   decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
                                epochs in training.
    
1. Instantiates Data Pipeline for evaluation and training
2. Loads the pretrained resnet50 keras model
3. Calculates floating point accuracy
4.
1 . Instantiates Data Pipeline for evaluation and training 2 . Loads the pretrained resnet50 keras model 3 . Calculates floating point accuracy 4 .
def perform_qat(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'])
    update_ops_name = [op.name for op in model.updates]
    accuracy = data_pipeline.evaluate(sess)
    logger.info("Original Model Top-1 accuracy = %.2f", accuracy)
    logger.info("Starting Model QuantSim...")
    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(sess=BN_folded_sess,
                                       start_op_names=['input_1'],
                                       output_op_names=[model.output.name.split(":")[0]],
                                       use_cuda=config.use_cuda, parity_config_file=config.parity_config_file,
                                       evaluator=data_pipeline.evaluate)
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Model Top-1 Accuracy on Quant Simulator = %.2f", accuracy)
    logger.info("...Model QuantSim Done")
    logger.info("Starting Model QAT...")
    data_pipeline.train(quant_sim.session, update_ops_name=update_ops_name)
    accuracy = data_pipeline.evaluate(quant_sim.session)
    logger.info("Quantization aware trained 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("...Model QAT Done")
def perform_qat ( 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' ] ) update_ops_name = [ op . name for op in model . updates ] accuracy = data_pipeline . evaluate ( sess ) logger . info ( "Original Model Top-1 accuracy = %.2f" , accuracy ) logger . info ( "Starting Model QuantSim..." ) 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 ( sess = BN_folded_sess , start_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] , use_cuda = config . use_cuda , parity_config_file = config . parity_config_file , evaluator = data_pipeline . evaluate ) accuracy = data_pipeline . evaluate ( quant_sim . session ) logger . info ( "Model Top-1 Accuracy on Quant Simulator = %.2f" , accuracy ) logger . info ( "...Model QuantSim Done" ) logger . info ( "Starting Model QAT..." ) data_pipeline . train ( quant_sim . session , update_ops_name = update_ops_name ) accuracy = data_pipeline . evaluate ( quant_sim . session ) logger . info ( "Quantization aware trained 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 ( "...Model QAT Done" )
1.
1 .
"""
    1. Instantiates Data Pipeline for evaluation and training
    2. Loads the pretrained resnet50 keras model
    3. Calculates floating point accuracy
    4. Quantization Sim Model
        4.1. Creates Quantization Sim model using AIMET QuantizationSimModel
        4.2. Calculates and logs the accuracy of quantizer sim model
    5. Quantization Aware Training
        5.1. Trains the quantization aware model
        5.2. Calculates and logs the accuracy of quantization Aware trained model
        5.3. Exports quantization aware 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
                   parity_config_file: An optional parity config file, used in Quantizer
                   use_cuda: A boolean var to indicate to run the test on GPU.
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for training.
                   learning_rate: A float type learning rate for model training
                   decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
                                epochs in training.
    """ # 1. Instantiates Data Pipeline for evaluation and training # 2. Loads the pretrained resnet50 keras model # 3. Calculates floating point accuracy # 4. Quantization Sim Model # 4.1. Creates Quantization Sim model using AIMET QuantizationSimModel # It is recommended to fold Batch-norms before making model runnable on target # 4.2. Calculates and logs the accuracy of quantizer sim model # 5. Quantization Aware Training # 5.1. Trains the quantization aware model # 5.2. Calculates and logs the accuracy of quantization aware trained model # 5.3. Exports quantization aware 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
parity_config_file: An optional parity config file, used in Quantizer
use_cuda: A boolean var to indicate to run the test on GPU.
logdir: Path to a directory for logging.
epochs: Number of epochs (type int) for training.
learning_rate: A float type learning rate for model training
decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
epochs in training.
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 parity_config_file : An optional parity config file used in Quantizer use_cuda : A boolean var to indicate to run the test on GPU . logdir : Path to a directory for logging . epochs : Number of epochs ( type int ) for training . learning_rate : A float type learning rate for model training decay_steps : A number used to adjust ( decay ) the learning rate after every decay_steps epochs in training .