Automatic Compression

automatic_compression(self, model_id: str, model_name: str, output_path: str, compression_ratio: float = 0.5) → netspresso.compressor.core.model.CompressedModel

Compress a model automatically based on the given compression ratio.

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

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

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

  • compression_ratio (float) – The compression ratio for automatic compression. Defaults to 0.5.

Raises

e – If an error occurs while performing automatic compression.

Returns

The compressed model.

Return type

CompressedModel

Details of Parameters

Compression Ratio

Note

  • As the compression ratio increases, you can get more lighter and faster compressed models, but with greater lost of accuracy.

  • Therefore, it is necessary to find an appropriate ratio for your requirements. It might require a few trials and errors.

  • The range of available values is as follows.

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]

Bases: netspresso.compressor.core.model.Model

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.automatic_compression(
    model_id="YOUR_UPLOADED_MODEL_ID",
    model_name="YOUR_COMPRESSED_MODEL_NAME",
    output_path="OUTPUT_PATH",  # ex) ./compressed_model.h5
    compression_ratio=0.5,
)

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"
)