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,))])

Convert to TFLite format

tflite_model = convert_to_tflite(model)

Convert PyTorch Model to ONNX

import torch from model_conversion import convert_to_onnx

Create a simple model

model = torch.nn.Linear(5, 10) dummy_input = torch.randn(1, 5)

Convert to ONNX format

onnx_path = convert_to_onnx(model, dummy_input, "model.onnx")

Model Optimization

Distillation in TensorFlow

from model_conversion import distill_model_tflite

Load teacher model and create student model

teacher_model = tf.keras.models.load_model("teacher_model.h5") student_model = tf.keras.models.Sequential([tf.keras.layers.Dense(10, input_shape=(5,))])

Training data

train_data, train_labels = ... # Your training data

Perform distillation

student_model = distill_model_tflite(teacher_model, student_model, train_data, train_labels)

Distillation in PyTorch

from model_conversion import distill_model_pytorch

Load teacher model and create student model

teacher_model = torch.load("teacher_model.pt") student_model = torch.nn.Linear(5, 10)

Training data loader and optimizer

train_loader = ... # Your training data loader optimizer = torch.optim.Adam(student_model.parameters())

Perform distillation

student_model = distill_model_pytorch(teacher_model, student_model, train_loader, optimizer)

Pruning in TensorFlow

from model_conversion import prune_model_tflite

Create a simple model

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

Prune the model

pruned_model = prune_model_tflite(model, sparsity=0.5)

Pruning in PyTorch

from model_conversion import prune_model_pytorch

Create a simple model

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

Prune the model

pruned_model = prune_model_pytorch(model, amount=0.3)

Quantization in TensorFlow

from model_conversion import quantize_model_tflite

Create a simple model

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

Quantize the model

quantized_model = quantize_model_tflite(model)

Quantization in PyTorch

from model_conversion import quantize_model_pytorch

Create a simple model

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

Quantize the model

quantized_model = quantize_model_pytorch(model)

Model Serialization

Save a Model

from model_conversion import save_model

TensorFlow 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.0.tar.gz (8.2 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.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tinyml_compress-1.0.0.tar.gz
  • Upload date:
  • Size: 8.2 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.0.tar.gz
Algorithm Hash digest
SHA256 4f27d055e3227e8a47ea4e0853575554e640aba09c22c9dc8635bffebe4ed387
MD5 0266f77843c931cc031af76678dbd69e
BLAKE2b-256 0281b6213eef7f45d17f200b84d8ebb57626d4e1a3cb129884c9e4387f9f0818

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tinyml_compress-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e08cc9883e1fb7bc17d7faa6944e5fadd481825c0c59bdadd78757f57e515b58
MD5 8b1ffa7206ac02cd8b6d0cef1f0a313a
BLAKE2b-256 3ce80c21341bff8c511df1cfec5a12914a380303d4bcb51d69021070e603cd6c

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