Automatic Compression

Automatic Compression

automatic_compression(self, input_model_path: str, output_dir: str, input_shapes: List[Dict[str, int]], framework: Framework = Framework.PYTORCH, compression_ratio: float = 0.5) → CompressorMetadata

Compress a model automatically based on the given compression ratio.

  • Parameters:
    • input_model_path (str) – The file path where the model is located.
    • output_dir (str) – The local path to save the compressed model.
    • input_shapes (List *[*Dict *[*str , int ] ]) – Input shapes of the model.
    • framework (Framework , optional) – The framework of the model.
    • compression_ratio (float , optional) – The compression ratio for automatic compression. Defaults to 0.5.
  • Raises:
    e – If an error occurs while performing automatic compression.
  • Returns:
    Compress metadata.
  • Return type:
    CompressorMetadata

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.

Example

from netspresso import NetsPresso


netspresso = NetsPresso(email="YOUR_EMAIL", password="YOUR_PASSWORD")

compressor = netspresso.compressor_v2()
compressed_model = compressor.automatic_compression(
    input_shapes=[{"batch": 1, "channel": 3, "dimension": [224, 224]}],
    input_model_path="./examples/sample_models/graphmodule.pt",
    output_dir="./outputs/compressed/pytorch_automatic_compression_1",
    compression_ratio=0.5,
)