Manual Compression

Select Compression Method

select_compression_method(self, model_id: str, compression_method: netspresso.compressor.client.utils.enum.CompressionMethod, 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'>)) → netspresso.compressor.core.compression.CompressionInfo

Select a compression method for a model.

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

  • compression_method (CompressionMethod) – The selected compression method.

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

Raises

e – If an error occurs while selecting the compression method.

Returns

The compression information for the selected compression method.

Return type

CompressionInfo

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

PR_ID

Pruning By Index

FD_TK

Tucker Decomposition

FD_SVD

Singular Value Decomposition

FD_CP

CP Decomposition

Example
from netspresso.compressor import CompressionMethod

COMPRESSION_METHOD = CompressionMethod.PR_L2

Warning

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

Note

Options

class Policy[source]

An enumeration.

class LayerNorm[source]

An enumeration.

class GroupPolicy[source]

An enumeration.

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 CompressionInfo(compressed_model_id: str = '', compression_id: str = '', compression_method: str = '', available_layers: List[netspresso.compressor.core.compression.AvailableLayer] = <factory>, original_model_id: str = '', options: netspresso.compressor.core.compression.Options = <factory>)[source]

Represents compression information for a model.

compressed_model_id

The ID of the compressed model.

Type

str

compression_id

The ID of the compression.

Type

str

compression_method

The compression method used.

Type

str

available_layers

The compressible layers information.

AvailableLayer Attributes:
  • name (str): The name of the layer.

  • values (List[Any]): The compression parameters for the layer.

  • channels (List[int]): The channel information for the layer.

Type

List[AvailableLayer]

options

The options for pruning method.

Type

Options, optional

original_model_id

The ID of the original model.

Type

str

Example

from netspresso.compressor import ModelCompressor


compressor = ModelCompressor(email="YOUR_EMAIL", password="YOUR_PASSWORD")
compression_info = compressor.select_compression_method(
    model_id="YOUR_UPLOADED_MODEL_ID",
    compression_method=CompressionMethod.PR_L2,
    options=Options(
        policy=Policy.AVERAGE,
        layer_norm=LayerNorm.TSS_NORM,
        group_policy=GroupPolicy.COUNT,
        reshape_channel_axis=-1
    )
)
Output
>>> compression_info
CompressionInfo(
    compression_method="PR_L2",
    available_layers=[
        AvailableLayer(name='conv1', values=[""], channels=[32]),
        AvailableLayer(name='layers.0.conv2', values=[""], channels=[64]),
        AvailableLayer(name='layers.1.conv2', values=[""], channels=[128]),
        AvailableLayer(name='layers.2.conv2', values=[""], channels=[128]),
        AvailableLayer(name='layers.3.conv2', values=[""], channels=[256]),
        AvailableLayer(name='layers.4.conv2', values=[""], channels=[256]),
        AvailableLayer(name='layers.5.conv2', values=[""], channels=[512]),
        AvailableLayer(name='layers.6.conv2', values=[""], channels=[512]),
        AvailableLayer(name='layers.7.conv2', values=[""], channels=[512]),
        AvailableLayer(name='layers.8.conv2', values=[""], channels=[512]),
        AvailableLayer(name='layers.9.conv2', values=[""], channels=[512]),
        AvailableLayer(name='layers.10.conv2', values=[""], channels=[512]),
        AvailableLayer(name='layers.11.conv2', values=[""], channels=[1024]),
        AvailableLayer(name='layers.12.conv2', values=[""], channels=[1024])
    ],
    options={'reshape_channel_axis': -1, 'policy': 'average', 'layer_norm': 'tss_norm', 'group_policy': 'average'}
    original_model_id="YOUR_UPLOADED_MODEL_ID",
    compressed_model_id="",
    compression_id="",
)

Set Compression Params

Details of Parameters

Values of available layer

Compression Method

Number of Values

Type

Range

PR_L2

1

Float

0.0 < ratio ≤ 1.0

PR_GM

1

Float

0.0 < ratio ≤ 1.0

PR_NN

1

Float

0.0 < ratio ≤ 1.0

PR_ID

(Num of Out Channels - 1)

Int

0 < channels ≤ Num of Out Channels

FD_TK

2

Int

0 < rank ≤ (Num of In Channels or Num of Out Channels)

FD_CP

1

Int

0 < rank ≤ min(Num of In Channels or Num of Out Channels)

FD_SVD

1

Int

0 < rank ≤ min(Num of In Channels or Num of Out Channels)

Example

from netspresso.compressor import ModelCompressor


for available_layer in compression_info.available_layers:
   available_layer.values = [0.2]
Output
>>> compression_info
CompressionInfo(
   compression_method="PR_L2",
   available_layers=[
      AvailableLayer(name='conv1', values=[0.2], channels=[32]),
      AvailableLayer(name='layers.0.conv2', values=[0.2], channels=[64]),
      AvailableLayer(name='layers.1.conv2', values=[0.2], channels=[128]),
      AvailableLayer(name='layers.2.conv2', values=[0.2], channels=[128]),
      AvailableLayer(name='layers.3.conv2', values=[0.2], channels=[256]),
      AvailableLayer(name='layers.4.conv2', values=[0.2], channels=[256]),
      AvailableLayer(name='layers.5.conv2', values=[0.2], channels=[512]),
      AvailableLayer(name='layers.6.conv2', values=[0.2], channels=[512]),
      AvailableLayer(name='layers.7.conv2', values=[0.2], channels=[512]),
      AvailableLayer(name='layers.8.conv2', values=[0.2], channels=[512]),
      AvailableLayer(name='layers.9.conv2', values=[0.2], channels=[512]),
      AvailableLayer(name='layers.10.conv2', values=[0.2], channels=[512]),
      AvailableLayer(name='layers.11.conv2', values=[0.2], channels=[1024]),
      AvailableLayer(name='layers.12.conv2', values=[0.2], channels=[1024])
   ],
   options={'reshape_channel_axis': -1, 'policy': 'average', 'layer_norm': 'tss_norm', 'group_policy': 'average'}
   original_model_id="YOUR_UPLOADED_MODEL_ID",
   compressed_model_id="",
   compression_id="",
)

Compress Model

compress_model(self, compression: netspresso.compressor.core.compression.CompressionInfo, model_name: str, output_path: str, dataset_path: str = None) → netspresso.compressor.core.model.CompressedModel

Compress a model using the provided compression information.

Parameters
  • compression (CompressionInfo) – The information about the compression.

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

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

  • 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 compressing the model.

Returns

The compressed model.

Return type

CompressedModel

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")
compress_model = compressor.compress_model(
    compression=compression_info,
    model_name="YOUR_COMPRESSED_MODEL_NAME",
    output_path="OUTPUT_PATH",  # ex) ./compressed_model.h5
)
Output
>>> compress_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"
)