Recommendation Compression
- recommendation_compression(self, compression_method: ~netspresso.enums.compression.CompressionMethod, recommendation_method: ~netspresso.enums.compression.RecommendationMethod, recommendation_ratio: float, input_model_path: str, output_dir: str, input_shapes: ~typing.List[~typing.Dict[str, int]], framework: ~netspresso.enums.model.Framework = Framework.PYTORCH, options: ~netspresso.clients.compressor.v2.schemas.compression.base.RecommendationOptions = RecommendationOptions(reshape_channel_axis=-1, policy=<Policy.AVERAGE: 'average'>, layer_norm=<LayerNorm.STANDARD_SCORE: 'standard_score'>, group_policy=<GroupPolicy.AVERAGE: 'average'>, step_size=2, step_op=<StepOp.ROUND: 'round'>, reverse=False, min_num_of_value=8), dataset_path: str | None = None) CompressorMetadata
Compress a recommendation-based model using the given compression and recommendation methods.
- Parameters:
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.
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.
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:
Compress metadata.
- Return type:
CompressorMetadata
Details of Parameters
Compression Method
- class CompressionMethod(value)[source]
An enumeration.
Available Compression Method
Name |
Description |
---|---|
PR_L2 |
L2 Norm Pruning |
PR_GM |
GM Pruning |
PR_NN |
Nuclear Norm Pruning |
PR_SNP |
Structured Neuron-level Pruning |
FD_TK |
Tucker Decomposition |
FD_SVD |
Singular Value Decomposition |
Example
from netspresso.enums import CompressionMethod
COMPRESSION_METHOD = CompressionMethod.PR_L2
Warning
Nuclear Norm is only supported in the Tensorflow-Keras framework.
Structured Neuron-level is only supported in the PyTorch and ONNX frameworks.
Note
Click on the link to learn more about the information. (Compression Method)
Recommendation Method
- class RecommendationMethod(value)[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.enums import RecommendationMethod
RECOMMENDATION_METHOD = RecommendationMethod.SLAMP
Note
- If you selected PR_L2, PR_GM, PR_NN, PR_SNP 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.
Available ranges
Click the link for more information. (VBMF)
Options
Example
from netspresso.enums import Policy, LayerNorm, GroupPolicy
from netspresso.clients.compressor.v2.schemas import Options
OPTIONS = Options(
policy=Policy.AVERAGE,
layer_norm=LayerNorm.TSS_NORM,
group_policy=GroupPolicy.COUNT,
reshape_channel_axis=-1
)
Note
Click the link for more information. (Pruning Options)
Note
This parameter applies only to the Pruning Method (PR_L2, PR_GM, PR_NN, PR_SNP).
Example
from netspresso import NetsPresso
from netspresso.enums import CompressionMethod, RecommendationMethod
netspresso = NetsPresso(email="YOUR_EMAIL", password="YOUR_PASSWORD")
compressor = netspresso.compressor_v2()
compressed_model = compressor.recommendation_compression(
compression_method=CompressionMethod.PR_L2,
recommendation_method=RecommendationMethod.SLAMP,
recommendation_ratio=0.5,
input_model_path="./examples/sample_models/graphmodule.pt",
output_dir="./outputs/compressed/graphmodule_recommend",
input_shapes=[{"batch": 1, "channel": 3, "dimension": [224, 224]}],
)