Examples/tensorflow/compression/spatial_svd.py
def compress_and_finetune(config: argparse.Namespace):
    """
    1. Instantiates Data Pipeline for evaluation and training
    2. Loads the pretrained resnet50 model
    3. Calculates floating point accuracy
    4. Compression
        4.1. Compresses the model using AIMET Spatial SVD
        4.2. Logs the statistics
        4.3. Saves the compressed model
        4.4. Calculates and logs the accuracy of compressed model
    5. Finetuning
        5.1. Finetunes the compressed model
        5.2. Calculates and logs the accuracy of compressed-finetuned model

    :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
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for finetuning.
                   learning_rate: A float type learning rate for model finetuning
                   decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
                                epochs in finetuning.
    """

    # 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. Compression
    logger.info("Starting Model Compression...")

    # 4.1. Compresses the model using AIMET Spatial SVD
    compressed_sess, stats = aimet_spatial_svd(sess=sess,
                                               input_op_names=['input_1'],
                                               output_op_names=[model.output.name.split(":")[0]],
                                               evaluator=data_pipeline.evaluate, working_dir=config.logdir)

    # 4.2. Logs the statistics
    logger.info(stats)
    with open(os.path.join(config.logdir, 'log.txt'), "w") as outfile:
        outfile.write("%s\n\n" % stats)

    # 4.3. Saves the compressed model
    save_model_to_meta(compressed_sess, meta_path=os.path.join(config.logdir, 'compressed_model'))

    # 4.4. Calculates and logs the accuracy of compressed model
    accuracy = data_pipeline.evaluate(compressed_sess)
    logger.info("Compressed Model Top-1 accuracy = %.2f", accuracy)

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

    # 5. Finetuning
    logger.info("Starting Model Finetuning...")

    # 5.1. Finetunes the compressed model
    data_pipeline.finetune(compressed_sess, update_ops_name=update_ops_name)

    # 5.2. Calculates and logs the accuracy of compressed-finetuned model
    accuracy = data_pipeline.evaluate(compressed_sess)
    logger.info("Finetuned Compressed Model Top-1 accuracy = %.2f", accuracy)

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

    1. Instantiates Data Pipeline for evaluation and training
    2. Loads the pretrained resnet50 model
    3. Calculates floating point accuracy
    4. Compression
        4.1. Compresses the model using AIMET Spatial SVD
        4.2. Logs the statistics
        4.3. Saves the compressed model
        4.4. Calculates and logs the accuracy of compressed model
    5. Finetuning
        5.1. Finetunes the compressed model
        5.2. Calculates and logs the accuracy of compressed-finetuned model

    :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
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for finetuning.
                   learning_rate: A float type learning rate for model finetuning
                   decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
                                epochs in finetuning.
    
1. Instantiates Data Pipeline for evaluation and training
2. Loads the pretrained resnet50 model
3. Calculates floating point accuracy
4.
1 . Instantiates Data Pipeline for evaluation and training 2 . Loads the pretrained resnet50 model 3 . Calculates floating point accuracy 4 .
def compress_and_finetune(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 Compression...")
    compressed_sess, stats = aimet_spatial_svd(sess=sess,
                                               input_op_names=['input_1'],
                                               output_op_names=[model.output.name.split(":")[0]],
                                               evaluator=data_pipeline.evaluate, working_dir=config.logdir)
    logger.info(stats)
    with open(os.path.join(config.logdir, 'log.txt'), "w") as outfile:
        outfile.write("%s\n\n" % stats)
    save_model_to_meta(compressed_sess, meta_path=os.path.join(config.logdir, 'compressed_model'))
    accuracy = data_pipeline.evaluate(compressed_sess)
    logger.info("Compressed Model Top-1 accuracy = %.2f", accuracy)
    logger.info("...Model Compression Done")
    logger.info("Starting Model Finetuning...")
    data_pipeline.finetune(compressed_sess, update_ops_name=update_ops_name)
    accuracy = data_pipeline.evaluate(compressed_sess)
    logger.info("Finetuned Compressed Model Top-1 accuracy = %.2f", accuracy)
    logger.info("...Model Finetuning Done")
def compress_and_finetune ( 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 Compression..." ) compressed_sess , stats = aimet_spatial_svd ( sess = sess , input_op_names = [ 'input_1' ] , output_op_names = [ model . output . name . split ( ":" ) [ 0 ] ] , evaluator = data_pipeline . evaluate , working_dir = config . logdir ) logger . info ( stats ) with open ( os . path . join ( config . logdir , 'log.txt' ) , "w" ) as outfile : outfile . write ( "%s\n\n" % stats ) save_model_to_meta ( compressed_sess , meta_path = os . path . join ( config . logdir , 'compressed_model' ) ) accuracy = data_pipeline . evaluate ( compressed_sess ) logger . info ( "Compressed Model Top-1 accuracy = %.2f" , accuracy ) logger . info ( "...Model Compression Done" ) logger . info ( "Starting Model Finetuning..." ) data_pipeline . finetune ( compressed_sess , update_ops_name = update_ops_name ) accuracy = data_pipeline . evaluate ( compressed_sess ) logger . info ( "Finetuned Compressed Model Top-1 accuracy = %.2f" , accuracy ) logger . info ( "...Model Finetuning Done" )
1.
1 .
"""
    1. Instantiates Data Pipeline for evaluation and training
    2. Loads the pretrained resnet50 model
    3. Calculates floating point accuracy
    4. Compression
        4.1. Compresses the model using AIMET Spatial SVD
        4.2. Logs the statistics
        4.3. Saves the compressed model
        4.4. Calculates and logs the accuracy of compressed model
    5. Finetuning
        5.1. Finetunes the compressed model
        5.2. Calculates and logs the accuracy of compressed-finetuned model

    :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
                   logdir: Path to a directory for logging.
                   epochs: Number of epochs (type int) for finetuning.
                   learning_rate: A float type learning rate for model finetuning
                   decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
                                epochs in finetuning.
    """ # 1. Instantiates Data Pipeline for evaluation and training # 2. Loads the pretrained resnet50 keras model # 3. Calculates floating point accuracy # 4. Compression # 4.1. Compresses the model using AIMET Spatial SVD # 4.2. Logs the statistics # 4.3. Saves the compressed model # 4.4. Calculates and logs the accuracy of compressed model # 5. Finetuning # 5.1. Finetunes the compressed model # 5.2. Calculates and logs the accuracy of compressed-finetuned model
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
logdir: Path to a directory for logging.
epochs: Number of epochs (type int) for finetuning.
learning_rate: A float type learning rate for model finetuning
decay_steps: A number used to adjust(decay) the learning rate after every decay_steps
epochs in finetuning.
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 logdir : Path to a directory for logging . epochs : Number of epochs ( type int ) for finetuning . learning_rate : A float type learning rate for model finetuning decay_steps : A number used to adjust ( decay ) the learning rate after every decay_steps epochs in finetuning .