Skip to main content

A library for converting, optimizing, and saving/loading machine learning models.

Project description

Model Conversion and Optimization Library (tinyml_compress)

This library provides functions to convert, save, load, distill, prune, and quantize machine learning models for both TensorFlow and PyTorch. It supports model conversion to TFLite and ONNX formats, as well as optimization techniques like distillation, pruning, and quantization.

Features

  • Model Conversion:

    • Convert TensorFlow models to TFLite format.
    • Convert PyTorch models to ONNX format.
  • Model Optimization:

    • Distillation:
      • Perform knowledge distillation between a teacher and a student model for both TensorFlow and PyTorch.
    • Pruning:
      • Apply pruning to TensorFlow and PyTorch models to reduce the number of parameters.
    • Quantization:
      • Apply quantization to TensorFlow models to reduce model size and improve inference speed.
      • Apply dynamic quantization to PyTorch models.
  • Model Serialization:

    • Save and load TensorFlow and PyTorch models to and from disk.

Installation

To install this library, you can use the following pip command:

pip install tinyML

Install required dependencies:

pip install -r requirements.txt

Usage

Model Conversion

Convert TensorFlow Model to TFLite

import tensorflow as tf from model_conversion import convert_to_tflite

Create a simple model model = tf.keras.Sequential([tf.keras.layers.Dense(10, input_shape=(5,))]) tflite_model = convert_to_tflite(model)

Convert PyTorch Model to ONNX

import torch from model_conversion import convert_to_onnx

model = torch.nn.Linear(5, 10) dummy_input = torch.randn(1, 5) onnx_path = convert_to_onnx(model, dummy_input, "model.onnx")

Model Optimization

Distillation in TensorFlow

from model_conversion import distill_model_tflite

teacher_model = tf.keras.models.load_model("teacher_model.h5") student_model = tf.keras.models.Sequential([tf.keras.layers.Dense(10, input_shape=(5,))]) train_data, train_labels = ... # Your training data student_model = distill_model_tflite(teacher_model, student_model, train_data, train_labels)

Distillation in PyTorch

from model_conversion import distill_model_pytorch

teacher_model = torch.load("teacher_model.pt") student_model = torch.nn.Linear(5, 10) train_loader = ... # Your training data loader optimizer = torch.optim.Adam(student_model.parameters()) student_model = distill_model_pytorch(teacher_model, student_model, train_loader, optimizer)

Pruning in TensorFlow

from model_conversion import prune_model_tflite

model = tf.keras.Sequential([tf.keras.layers.Dense(10, input_shape=(5,))]) pruned_model = prune_model_tflite(model, sparsity=0.5)

Pruning in PyTorch

from model_conversion import prune_model_pytorch model = torch.nn.Linear(5, 10) pruned_model = prune_model_pytorch(model, amount=0.3)

Quantization in TensorFlow

from model_conversion import quantize_model_tflite

model = tf.keras.Sequential([tf.keras.layers.Dense(10, input_shape=(5,))]) quantized_model = quantize_model_tflite(model)

Quantization in PyTorch

from model_conversion import quantize_model_pytorch

model = torch.nn.Linear(5, 10) quantized_model = quantize_model_pytorch(model)

Model Serialization

Save a Model

from model_conversion import save_model

model = tf.keras.Sequential([tf.keras.layers.Dense(10, input_shape=(5,))]) save_model(model, "model.h5")

PyTorch model

import torch model = torch.nn.Linear(5, 10) save_model(model, "model.pt")

Load a Model

from model_conversion import load_model

Load TensorFlow model

tensorflow_model = load_model("model.h5", model_type="tensorflow")

Load PyTorch model

pytorch_model = load_model("model.pt", model_type="pytorch", input_shape=(5,))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tinyml_compress-1.0.1.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tinyml_compress-1.0.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file tinyml_compress-1.0.1.tar.gz.

File metadata

  • Download URL: tinyml_compress-1.0.1.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for tinyml_compress-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f612272a425a9feeb8314aba88cb460a875f43b7e603eb94b7cfe2f465ab00e8
MD5 741d91b21f294918e52ec4c9da7edbaf
BLAKE2b-256 e662f73072b610d7a8a73d89aa42fa29f7bec58969de6ad3b46f65ba86a4f11b

See more details on using hashes here.

File details

Details for the file tinyml_compress-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tinyml_compress-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c3efd5ed8336d16e57961ffcd38030b0ab3d6b53ea33f7ba44e351eae032468a
MD5 09f2263f73ee8eaa3bb2919b88f3690b
BLAKE2b-256 64d6bc905d67d69dec5dfd56bc0c1f1a481fc8e1ab712e4f35c08babbfb507cc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page