Recommendation Compression

recommendation_compression(self, model_id: str, model_name: str, compression_method: netspresso.compressor.client.utils.enum.CompressionMethod, recommendation_method: netspresso.compressor.client.utils.enum.RecommendationMethod, recommendation_ratio: float, output_path: str, options: netspresso.compressor.client.schemas.compression.Options = Options(reshape_channel_axis=-1, policy=<Policy.AVERAGE: 'average'>, layer_norm=<LayerNorm.STANDARD_SCORE: 'standard_score'>, group_policy=<GroupPolicy.AVERAGE: 'average'>), dataset_path: str = None) → netspresso.compressor.core.model.CompressedModel

Compress a recommendation-based model using the given compression and recommendation methods.

Parameters
  • model_id (str) – The ID of the model.

  • model_name (str) – The name of the compressed model.

  • compression_method (CompressionMethod) – The selected compression method.

  • recommendation_method (RecommendationMethod) – The selected recommendation method.

  • recommendation_ratio (float) – The compression ratio recommended by the recommendation method.

  • output_path (str) – The local path to save the compressed model.

  • options (Options, optional) – The options for pruning method.

  • dataset_path (str, optional) – The path of the dataset used for nuclear norm compression method. Default is None.

Raises

e – If an error occurs while performing recommendation compression.

Returns

The compressed model.

Return type

CompressedModel

Details of Parameters

Compression Method

class CompressionMethod[source]

An enumeration.

Available Compression Method

Name

Description

PR_L2

L2 Norm Pruning

PR_GM

GM Pruning

PR_NN

Nuclear Norm Pruning

FD_TK

Tucker Decomposition

FD_SVD

Singular Value Decomposition

Example

from netspresso.compressor import CompressionMethod

COMPRESSION_METHOD = CompressionMethod.PR_L2

Warning

  • Nuclear Norm is only supported in the Tensorflow-Keras Framework.

Note

Recommendation Method

class RecommendationMethod[source]

An enumeration.

Available Recommendation Method

Name

Description

SLAMP

Structured Layer-adaptive Sparsity for the Magnitude-based Pruning

VBMF

Variational Bayesian Matrix Factorization

Example

from netspresso.compressor import RecommendationMethod

RECOMMENDATION_METHOD = RecommendationMethod.SLAMP

Note

  • If you selected PR_L2, PR_GM, PR_NN for compression_method
    • The recommended_method available is SLAMP.

  • If you selected FD_TK, FD_SVD for compression_method
    • The recommended_method available is VBMF.

Recommendation Ratio

Note

  • SLAMP (Pruning ratio)

    • Remove corresponding amounts of the filters. (e.g. 0.2 removes 20% of the filters in each layer)

    • Available ranges

    • Click the link for more information. (SLAMP)

  • VBMF (Calibration ratio)

    • This function control compression level of model if the result of recommendation doesn’t meet the compression level user wants. Remained rank add or subtract (removed rank x calibration ratio) according to calibration ratio range.

      ../../../../../_images/calibration_ratio.png
    • Available ranges

    • Click the link for more information. (VBMF)

Options

Example

from netspresso.compressor import Policy, LayerNorm, GroupPolicy, Options

OPTIONS = Options(
    policy=Policy.AVERAGE,
    layer_norm=LayerNorm.TSS_NORM,
    group_policy=GroupPolicy.COUNT,
    reshape_channel_axis=-1
)

Note

Note

  • This parameter applies only to the Pruning Method (PR_L2, PR_GM, PR_NN).

Details of Returns

class CompressedModel(model_id: str, model_name: str, task: str, framework: str, model_size: float, flops: float, trainable_parameters: float, non_trainable_parameters: float, number_of_layers: int, input_shapes: List[netspresso.compressor.core.model.InputShape] = <factory>, compression_id: str = '', original_model_id: str = '')[source]

Represents a compressed model.

compression_id

The ID of the compression.

Type

str

original_model_id

The ID of the uploaded model.

Type

str

Example

from netspresso.compressor import ModelCompressor


compressor = ModelCompressor(email="YOUR_EMAIL", password="YOUR_PASSWORD")
compressed_model = compressor.recommendation_compression(
    model_id="YOUR_UPLOADED_MODEL_ID",
    model_name="YOUR_COMPRESSED_MODEL_NAME",
    compression_method=CompressionMethod.PR_L2,
    recommendation_method=RecommendationMethod.SLAMP,
    recommendation_ratio=0.5,
    output_path="OUTPUT_PATH",  # ex) ./compressed_model.h5
    options=Options(
        policy=Policy.AVERAGE,
        layer_norm=LayerNorm.TSS_NORM,
        group_policy=GroupPolicy.COUNT,
        reshape_channel_axis=-1
    )
)

Output

>>> compressed_model
CompressedModel(
    model_id="78f65510-1f99-4856-99d9-60902373bd1d",
    model_name="YOUR_COMPRESSED_MODEL_NAME",
    task="image_classification",
    framework="tensorflow_keras",
    input_shapes=[InputShape(batch=1, channel=3, dimension=[32, 32])],
    model_size=2.9439,
    flops=24.1811,
    trainable_parameters=0.6933,
    non_trainable_parameters=0.01,
    number_of_layers=0,
    compression_id="b9feccee-d69e-4074-a225-5417d41aa572",
    original_model_id="YOUR_UPLOADED_MODEL_ID"
)