Examples/tensorflow/compression/spatial_svd.py
def aimet_spatial_svd(sess: tf.Session, input_op_names: List[str], output_op_names: List[str],
                      evaluator: aimet_common_defs.EvalFunction, working_dir: str) -> Tuple[tf.Session,
                                                                                            aimet_common_defs.CompressionStats]:
    """
    Compresses the model using AIMET's Tensorflow Spatial SVD auto mode compression scheme.

    :param sess: The sess graph to compress
    :param input_op_names: The list of input op name of the sess.graph
    :param output_op_names: The list of output op name of the sess.graph
    :param evaluator: Evaluator used during compression
    :param working_dir: Dir path to save compressed TensorFlow meta file
    :return: A tuple of compressed sess graph and its statistics
    """

    # Please refer to the API documentation for more details.

    # Desired target compression ratio using Spatial SVD
    # This value denotes the desired compression % of the original model.
    # To compress the model to 20% of original model, use 0.2. This would
    # compress the model by 80%.
    # We are compressing the model by 50% here.
    target_comp_ratio = Decimal(0.5)

    # Number of compression ratio used by the API at each layer
    # API will evaluate 0.1, 0.2, ..., 0.9, 1.0 ratio (total 10 candidates)
    # at each layer
    num_comp_ratio_candidates = 10

    # Creating Greedy selection parameters:
    greedy_params = aimet_common_defs.GreedySelectionParameters(target_comp_ratio=target_comp_ratio,
                                                                num_comp_ratio_candidates=num_comp_ratio_candidates)

    # Ignoring first convolutional layer of the model for compression
    modules_to_ignore = [sess.graph.get_operation_by_name('conv1_conv/Conv2D')]

    # Creating Auto mode Parameters:
    auto_params = aimet_tensorflow_defs.SpatialSvdParameters.AutoModeParams(greedy_select_params=greedy_params,
                                                                            modules_to_ignore=modules_to_ignore)

    # Creating Spatial SVD parameters with Auto Mode:
    params = aimet_tensorflow_defs.SpatialSvdParameters(input_op_names=input_op_names,
                                                        output_op_names=output_op_names,
                                                        mode=aimet_tensorflow_defs.SpatialSvdParameters.Mode.auto,
                                                        params=auto_params)

    # Scheme is Spatial SVD:
    scheme = aimet_common_defs.CompressionScheme.spatial_svd

    # Cost metric is MAC, it can be MAC or Memory
    cost_metric = aimet_common_defs.CostMetric.mac

    # Input image shape
    image_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'], image_net_config.dataset['image_height'])

    # Calling model compression using Spatial SVD:
    # Here evaluator is passed which is used by the API to evaluate the
    # accuracy for various compression ratio of each layer. To speed up
    # the process, only 10 batches of data is being used inside evaluator
    # (by passing eval_iterations=10) instead of running evaluation on
    # complete dataset.
    results = ModelCompressor.compress_model(sess=sess,
                                             working_dir=working_dir,
                                             eval_callback=evaluator,
                                             eval_iterations=10,
                                             input_shape=image_shape,
                                             compress_scheme=scheme,
                                             cost_metric=cost_metric,
                                             parameters=params)

    return results

    Compresses the model using AIMET's Tensorflow Spatial SVD auto mode compression scheme.

    :param sess: The sess graph to compress
    :param input_op_names: The list of input op name of the sess.graph
    :param output_op_names: The list of output op name of the sess.graph
    :param evaluator: Evaluator used during compression
    :param working_dir: Dir path to save compressed TensorFlow meta file
    :return: A tuple of compressed sess graph and its statistics
    
Compresses the model using AIMET's Tensorflow Spatial SVD auto mode compression scheme.
Compresses the model using AIMET ' s Tensorflow Spatial SVD auto mode compression scheme .
def aimet_spatial_svd(sess: tf.Session, input_op_names: List[str], output_op_names: List[str],
                      evaluator: aimet_common_defs.EvalFunction, working_dir: str) -> Tuple[tf.Session,
                                                                                            aimet_common_defs.CompressionStats]:
    target_comp_ratio = Decimal(0.5)
    num_comp_ratio_candidates = 10
    greedy_params = aimet_common_defs.GreedySelectionParameters(target_comp_ratio=target_comp_ratio,
                                                                num_comp_ratio_candidates=num_comp_ratio_candidates)
    modules_to_ignore = [sess.graph.get_operation_by_name('conv1_conv/Conv2D')]
    auto_params = aimet_tensorflow_defs.SpatialSvdParameters.AutoModeParams(greedy_select_params=greedy_params,
                                                                            modules_to_ignore=modules_to_ignore)
    params = aimet_tensorflow_defs.SpatialSvdParameters(input_op_names=input_op_names,
                                                        output_op_names=output_op_names,
                                                        mode=aimet_tensorflow_defs.SpatialSvdParameters.Mode.auto,
                                                        params=auto_params)
    scheme = aimet_common_defs.CompressionScheme.spatial_svd
    cost_metric = aimet_common_defs.CostMetric.mac
    image_shape = (1, image_net_config.dataset['image_channels'],
                   image_net_config.dataset['image_width'], image_net_config.dataset['image_height'])
    results = ModelCompressor.compress_model(sess=sess,
                                             working_dir=working_dir,
                                             eval_callback=evaluator,
                                             eval_iterations=10,
                                             input_shape=image_shape,
                                             compress_scheme=scheme,
                                             cost_metric=cost_metric,
                                             parameters=params)
    return results
def aimet_spatial_svd ( sess : tf . Session , input_op_names : List [ str ] , output_op_names : List [ str ] , evaluator : aimet_common_defs . EvalFunction , working_dir : str ) -> Tuple [ tf . Session , aimet_common_defs . CompressionStats ] : target_comp_ratio = Decimal ( 0.5 ) num_comp_ratio_candidates = 10 greedy_params = aimet_common_defs . GreedySelectionParameters ( target_comp_ratio = target_comp_ratio , num_comp_ratio_candidates = num_comp_ratio_candidates ) modules_to_ignore = [ sess . graph . get_operation_by_name ( 'conv1_conv/Conv2D' ) ] auto_params = aimet_tensorflow_defs . SpatialSvdParameters . AutoModeParams ( greedy_select_params = greedy_params , modules_to_ignore = modules_to_ignore ) params = aimet_tensorflow_defs . SpatialSvdParameters ( input_op_names = input_op_names , output_op_names = output_op_names , mode = aimet_tensorflow_defs . SpatialSvdParameters . Mode . auto , params = auto_params ) scheme = aimet_common_defs . CompressionScheme . spatial_svd cost_metric = aimet_common_defs . CostMetric . mac image_shape = ( 1 , image_net_config . dataset [ 'image_channels' ] , image_net_config . dataset [ 'image_width' ] , image_net_config . dataset [ 'image_height' ] ) results = ModelCompressor . compress_model ( sess = sess , working_dir = working_dir , eval_callback = evaluator , eval_iterations = 10 , input_shape = image_shape , compress_scheme = scheme , cost_metric = cost_metric , parameters = params ) return results
Compresses the model using AIMET's Tensorflow Spatial SVD auto mode compression scheme.
Compresses the model using AIMET ' s Tensorflow Spatial SVD auto mode compression scheme .
"""
    Compresses the model using AIMET's Tensorflow Spatial SVD auto mode compression scheme.

    :param sess: The sess graph to compress
    :param input_op_names: The list of input op name of the sess.graph
    :param output_op_names: The list of output op name of the sess.graph
    :param evaluator: Evaluator used during compression
    :param working_dir: Dir path to save compressed TensorFlow meta file
    :return: A tuple of compressed sess graph and its statistics
    """ # Please refer to the API documentation for more details. # Desired target compression ratio using Spatial SVD # This value denotes the desired compression % of the original model. # To compress the model to 20% of original model, use 0.2. This would # compress the model by 80%. # We are compressing the model by 50% here. # Number of compression ratio used by the API at each layer # API will evaluate 0.1, 0.2, ..., 0.9, 1.0 ratio (total 10 candidates) # at each layer # Creating Greedy selection parameters: # Ignoring first convolutional layer of the model for compression # Creating Auto mode Parameters: # Creating Spatial SVD parameters with Auto Mode: # Scheme is Spatial SVD: # Cost metric is MAC, it can be MAC or Memory # Input image shape # Calling model compression using Spatial SVD: # Here evaluator is passed which is used by the API to evaluate the # accuracy for various compression ratio of each layer. To speed up # the process, only 10 batches of data is being used inside evaluator # (by passing eval_iterations=10) instead of running evaluation on # complete dataset.
A tuple of compressed sess graph and its statistics
A tuple of compressed sess graph and its statistics
sess
tf.Session
The sess graph to compress
The sess graph to compress
input_op_names
List[str]
The list of input op name of the sess.graph
The list of input op name of the sess . graph
output_op_names
List[str]
The list of output op name of the sess.graph
The list of output op name of the sess . graph
evaluator
aimet_common_defs.EvalFunction
Evaluator used during compression
Evaluator used during compression
working_dir
str
Dir path to save compressed TensorFlow meta file
Dir path to save compressed TensorFlow meta file