To compress a model using a Python script it is possible to import aiminify and call the minify(...) function. As shown in the example below:
from aiminify import minify
from your_project import model
compressed_model, _ = minify(model)
Depending on the type of model you are compressing it can be stored using the appropriate methods. As aiminify supports PyTorch and Tensorflow as backends, the different storing methods are implemented in the save_model() function.
from aiminify import minify
from aiminify import save_model
from your_project import model
compressed_model, _ = minify(model)
save_model(compressed_model, "./compressed-pytorch-model.pt", input_shape)
compressed_model, _ = minify(model, quantization=false)
save_model(compressed_model, "./compressed-pytorch-model.pt")
from aiminify import minify
from aiminify import save_model
from your_project import model
compressed_model, _ = minify(model)
save_model(compressed_model, "./compressed-tensorflow-model.keras")
Compression strength, setting the strength of the pruner
Training set used for fine tuning
Validation set used for fine tuning
Optimizer used for fine tuning
Loss function used for fine tuning
Fine tune the model after compression
Precision for fine tuning. Other option is mixed
Number of training steps to accumulate gradients before a backward update
Use a smart strategy for determining the amount of filters to prune instead of flat pruning x % of all filters.
The return value of minify is (compressed_model, feedback_dictionary). compressed_model is, as the name would suggest, your compressed model using the same backend as the input (Tensorflow, PyTorch). feedback_dictionary contains logs and miscellaneous messages from the compression algorithm.
training_generator, validation_generator, loss_function can be specific to the backend you’re using. For example when using pytorch the training_generator and validation_generator need to be a subclass of torch.utils.data.Dataset. For Tensorflow these can be a subclass of tensorflow.data.Dataset. Similar with loss_function for PyTorch this can be any member of torch.nn.modules.loss and for Tensorflow this can be any implementation of tf.keras.losses.Loss.