Automatic Compression¶
Description¶
            netspresso.compressor.v2.compressor.CompressorV2.automatic_compression(input_model_path, output_dir, input_shapes, framework=Framework.PYTORCH, compression_ratio=0.5)
¶
    Compress a model automatically based on the given compression ratio.
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                input_model_path
             | 
            
                  str
             | 
            
               The file path where the model is located.  | 
            required | 
                output_dir
             | 
            
                  str
             | 
            
               The local path to save the compressed model.  | 
            required | 
                input_shapes
             | 
            
                  List[Dict[str, int]]
             | 
            
               Input shapes of the model.  | 
            required | 
                framework
             | 
            
                  Framework
             | 
            
               The framework of the model.  | 
            
                  PYTORCH
             | 
          
                compression_ratio
             | 
            
                  float
             | 
            
               The compression ratio for automatic compression. Defaults to 0.5.  | 
            
                  0.5
             | 
          
Raises:
| Type | Description | 
|---|---|
                  e
             | 
            
               If an error occurs while performing automatic compression.  | 
          
Returns:
| Name | Type | Description | 
|---|---|---|
CompressorMetadata |             
                  CompressorMetadata
             | 
            
               Compress metadata.  | 
          
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.
 
\[0 < \text{ratio} < 1\]
Example¶
from netspresso import NetsPresso
# Login with API key (recommended)
# Get your API token from: https://account.netspresso.ai/api-token
netspresso = NetsPresso(api_key="YOUR_API_KEY")
# Note: Email/password login will be deprecated soon
# 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,
)