<div align=right>
  <a href="https://hits.seeyoufarm.com"><img src="https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FNota-NetsPresso%2Fnetspresso-python&count_bg=%2323E7E7E7&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=hits&edge_flat=false"/></a>
</div>

# PyNetsPresso

<div align="center">
    <img src="https://netspresso-docs-imgs.s3.ap-northeast-2.amazonaws.com/imgs/banner/pynp_main.png"/>
</div>
</br>

<div align="center">
      <a href="https://github.com/Nota-NetsPresso/ModelZoo-YOLOFastest-for-ARM-U55-M85"> YOLO Fastest </a>
    | <a href="https://github.com/Nota-NetsPresso/yolox_nota"> YOLOX </a>
    | <a href="https://github.com/Nota-NetsPresso/ultralytics_nota"> YOLOv8 </a> 
    | <a href="https://github.com/Nota-NetsPresso/ModelZoo-YOLOv7"> YOLOv7 </a> 
    | <a href="https://github.com/Nota-NetsPresso/yolov5_nota"> YOLOv5 </a> 
    | <a href="https://github.com/Nota-NetsPresso/PIDNet_nota"> PIDNet </a>     
    | <a href="https://github.com/Nota-NetsPresso/pytorch-cifar-models_nota"> PyTorch-CIFAR-Models</a>
</div>
</br>

<div align="center">
    <p align="center">
        <a href="https://www.python.org/downloads/" target="_blank"><img src="https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10-blue" />
        <a href="https://www.tensorflow.org/install/pip" target="_blank"><img src="https://img.shields.io/badge/TensorFlow-2.3.x ~ 2.8.x.-FF6F00?style=flat&logo=tensorflow&logoColor=#FF6F00&link=https://www.tensorflow.org/install/pip"/></a>
        <a href="https://pytorch.org/" target="_blank"><img src="https://img.shields.io/badge/PyTorch-1.11.x ~ 1.13.x.-EE4C2C?style=flat&logo=pytorch&logoColor=#EE4C2C"/></a>
        <br>
        <a href="https://netspresso.ai?utm_source=git&utm_medium=text_np&utm_campaign=py_launch"><img src="https://img.shields.io/badge/NetsPresso-Open in Website-1BD2EB?style=flat&link=https://netspresso.ai/"/></a>
        <a href="https://github.com/Nota-NetsPresso/NetsPresso-Model-Compressor-ModelZoo"><img src="https://img.shields.io/badge/ModelZoo-Open in Github-181717?style=flat&logo=github&logoColor=#181717"/></a>
        <a href="https://github.com/Nota-NetsPresso/NetsPresso-Model-Compressor-ModelZoo/tree/main/best_practices"><img src="https://img.shields.io/badge/Best Practice-Open in Colab-F9AB00?style=flat&logo=googlecolab&logoColor=#F9AB00"/></a>
    </p>
</div>
</br>

Use **PyNetsPresso** for a seamless model optimization process. 
PyNetsPresso resolves AI-related constraints in business use cases and enables cost-efficiency and enhanced performance by removing the requirement for high-spec servers and network connectivity and preventing high latency and personal data breaches.

The **PyNetsPresso** is a python interface with the NetsPresso web application and REST API.

Easily compress various models with our resources. Please browse the [Docs] for details, and join our [Discussion Forum] for providing feedback or sharing your use cases.

To get started with the PyNetsPresso, you will need to sign up either at [NetsPresso] or [PyNetsPresso].</a>
</br>
</br>

<div align="center">
  <a href="https://github.com/Nota-NetsPresso" style="text-decoration:none;">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/github_white.png">
      <source media="(prefers-color-scheme: light)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/github.png">
      <img alt="github" src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/github.png" width="3%">
    </picture>
  </a>
  <img src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/logo-transparent.png" width="3%" alt="" />
  <a href="https://www.facebook.com/NotaAI" style="text-decoration:none;">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/facebook_white.png">
      <source media="(prefers-color-scheme: light)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/facebook.png">
      <img alt="facebook" src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/facebook.png" width="3%">
    </picture>
  </a>
  <img src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/logo-transparent.png" width="3%" alt="" />
  <a href="https://twitter.com/nota_ai" style="text-decoration:none;">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/twitter_white.png">
      <source media="(prefers-color-scheme: light)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/twitter.png">
      <img alt="twitter" src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/twitter.png" width="3%">
    </picture>
  </a>
  <img src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/logo-transparent.png" width="3%" alt="" />
  <a href="https://www.youtube.com/channel/UCeewYFAqb2EqwEXZCfH9DVQ" style="text-decoration:none;">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/youtube_white.png">
      <source media="(prefers-color-scheme: light)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/youtube.png">
      <img alt="youtube" src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/youtube.png" width="3%">
    </picture>
  </a>
  <img src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/logo-transparent.png" width="3%" alt="" />
  <a href="https://www.linkedin.com/company/nota-incorporated" style="text-decoration:none;">
    <picture>
      <source media="(prefers-color-scheme: dark)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/linkedin_white.png">
      <source media="(prefers-color-scheme: light)" srcset="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/linkedin.png">
      <img alt="youtube" src="https://github.com/Nota-NetsPresso/NetsPresso-Compatible-Models/blob/main/imgs/common/linkedin.png" width="3%">
    </picture>
  </a>
</div>
</br>
</br>

<div align="center">
    <img width="100%" src="https://netspresso-docs-imgs.s3.ap-northeast-2.amazonaws.com/imgs/banner/workflow_banner.png">
</div>
</br>

<div width="9%" align="center">
    <table width="90%" align="center">
        <tr>
            <td width="30%" align="center">Steps</td>
            <td width="30%" align="center">Types</td>
            <td width="40%" align="center">Description</td>
        </tr>
        <tr>
            <td width="30%" align="center">
                Train
                <br>
                (Model Zoo)
            </td>
            <td width="30%" align="center">
                <details open>
                    <summary>Image Classification</summary>
                    <a href="https://github.com/Nota-NetsPresso/pytorch-cifar-models_nota">PyTorch-CIFAR-Models</a>
                </details>
                <details open>
                    <summary>Object Detection</summary>
                    <a href="https://github.com/Nota-NetsPresso/ModelZoo-YOLOFastest-for-ARM-U55-M85">YOLO Fastest</a><br>
                    <a href="https://github.com/Nota-NetsPresso/yolox_nota">YOLOX</a><br>
                    <a href="https://github.com/Nota-NetsPresso/yolov5_nota">YOLOv5</a><br>
                    <a href="https://github.com/Nota-NetsPresso/ModelZoo-YOLOv7">YOLOv7</a>
                </details>
                <details open>
                    <summary>Semantic Segmentation</summary>
                    <a href="https://github.com/Nota-NetsPresso/PIDNet_nota">PIDNet</a>
                </details>
                <details open>
                    <summary>Pose Estimation</summary>
                    <a href="https://github.com/Nota-NetsPresso/ultralytics_nota">YOLOv8</a>
                </details>
            </td>
            <td width="40%" align="center">Build and train models.</td>
        </tr>
        <tr>
            <td width="30%" align="center">Compress</td>
            <td width="30%" align="center">np.compressor</td>
            <td width="40%" align="center">Compress and optimize the user’s pre-trained model.</td>
        </tr>
        <tr>
            <td width="30%" align="center">Convert</td>
            <td width="30%" align="center">np.launcher</td>
            <td width="40%" align="center">Convert AI models to run efficiently on the desired hardware and provide easy installation for seamless usage of the converted AI models.</td>
        </tr>
    </table>
</div>


## Installation

There are two ways you can install the PyNetsPresso: using pip or manually through our project GitHub repository.

To install this package, please use Python 3.8 or higher.

From PyPI (Recommended)
```bash
pip install netspresso
```

From Github
```bash
git clone https://github.com/nota-netspresso/pynetspresso.git
cd pynetspresso
pip install -e .
```


## Quick Start

### Login

To use the PyNetsPresso, please enter the email and password registered in NetsPresso.

```python
from netspresso.client import SessionClient
from netspresso.compressor import ModelCompressor

session = SessionClient(email='YOUR_EMAIL', password='YOUR_PASSWORD')
compressor = ModelCompressor(user_session=session)
```

### Upload Model

To upload your trained model, simply enter the required information. 

When a model is successfully uploaded, a unique 'model.model_id' is generated to allow repeated use of the uploaded model.

```python
from netspresso.compressor import Task, Framework

model = compressor.upload_model(
    model_name="YOUR_MODEL_NAME",
    task=Task.IMAGE_CLASSIFICATION,
    framework=Framework.TENSORFLOW_KERAS,
    file_path="YOUR_MODEL_PATH", # ex) ./model.h5
    input_shapes="YOUR_MODEL_INPUT_SHAPES",  # ex) [{"batch": 1, "channel": 3, "dimension": [32, 32]}]
)
```

### Automatic Compression

Automatically compress the model by setting the compression ratio for the model.

Enter the ID of the uploaded model, the name and storage path of the compressed model, and the compression ratio.

```python
compressed_model = compressor.automatic_compression(
    model_id=model.model_id,
    model_name="YOUR_COMPRESSED_MODEL_NAME",
    output_path="OUTPUT_PATH",  # ex) ./compressed_model.h5
    compression_ratio=0.5,
)
```

### Convert Model and Benchmark the Converted Model
Convert an ONNX model into a TensorRT model, and benchmark the TensorRT model on the Jetson Nano.

```python
from loguru import logger
from netspresso.launcher import ModelConverter, ModelBenchmarker, ModelFramework, TaskStatus, DeviceName, SoftwareVersion

converter = ModelConverter(user_session=session)

model = converter.upload_model("./examples/sample_models/test.onnx")


conversion_task = converter.convert_model(
    model=model,
    input_shape=model.input_shape,
    target_framework=ModelFramework.TENSORRT,
    target_device_name=DeviceName.JETSON_AGX_ORIN,
    target_software_version=SoftwareVersion.JETPACK_5_0_1,
    wait_until_done=True
)

logger.info(conversion_task)

CONVERTED_MODEL_PATH = "converted_model.trt"
converter.download_converted_model(conversion_task, dst=CONVERTED_MODEL_PATH)


benchmarker = ModelBenchmarker(user_session=session)
benchmark_model = benchmarker.upload_model(CONVERTED_MODEL_PATH)
benchmark_task = benchmarker.benchmark_model(
    model=benchmark_model,
    target_device_name=DeviceName.JETSON_AGX_ORIN,
    target_software_version=SoftwareVersion.JETPACK_5_0_1,
    wait_until_done=True
)
logger.info(f"model inference latency: {benchmark_task.latency} ms")
logger.info(f"model gpu memory footprint: {benchmark_task.memory_footprint_gpu} MB")
logger.info(f"model cpu memory footprint: {benchmark_task.memory_footprint_cpu} MB")
```

## Available Options for Launcher (Convert, Benchmark)

### Available Target Frameworks for Conversion with Source Models

| Target / Source Model | ONNX | TENSORFLOW_KERAS | TENSORFLOW |
| :-------------------- | :--: | :--------------: | :--------: |
| TENSORRT              |  ✔️  |                  |            |
| DRPAI                 |  ✔️  |                  |            |
| OPENVINO              |  ✔️  |                  |            |
| TENSORFLOW_LITE       |  ✔️  |        ✔️        |     ✔️     |


### Available Devices for Framework

| Device / Framework   | ONNX | TENSORRT | TENSORFLOW_LITE | DRPAI | OPENVINO |
| :------------------- | :--: | :------: | :-------------: | :---: | :------: |
| RASPBERRY_PI_4B      |  ✔️  |          |       ✔️        |       |          |
| RASPBERRY_PI_3B_PLUS |  ✔️  |          |       ✔️        |       |          |
| RASPBERRY_PI_ZERO_W  |  ✔️  |          |       ✔️        |       |          |
| RASPBERRY_PI_ZERO_2W |  ✔️  |          |       ✔️        |       |          |
| RENESAS_RZ_V2L       |  ✔️  |          |                 |  ✔️   |          |
| RENESAS_RZ_V2M       |  ✔️  |          |                 |  ✔️   |          |
| RENESAS_RA8D1       |      |          |        ✔️(only INT8)        |       |          |
| ALIF_ENSEMBLE_E7_DEVKIT_GEN2       |     |          |        ✔️(only INT8)       |       |          |
| JETSON_NANO          |  ✔️  |    ✔️    |                 |       |          |
| JETSON_TX2           |  ✔️  |    ✔️    |                 |       |          |
| JETSON_XAVIER        |  ✔️  |    ✔️    |                 |       |          |
| JETSON_NX            |  ✔️  |    ✔️    |                 |       |          |
| JETSON_AGX_ORIN      |  ✔️  |    ✔️    |                 |       |          |
| AWS_T4               |  ✔️  |    ✔️    |                 |       |          |
| Intel_XEON_W_2233    |      |          |                 |       |    ✔️    |


### Available Software Versions for Jetson Devices

Software Versions requires only Jetson Device. If you are using a different device, you do not need to enter it.

| Software Version / Device | JETSON_NANO | JETSON_TX2 | JETSON_XAVIER | JETSON_NX | JETSON_AGX_ORIN |
| :------------------------ | :---------: | :--------: | :-----------: | :-------: | :-------------: |
| JETPACK_4_4_1             |     ✔️      |            |               |           |                 |
| JETPACK_4_6               |     ✔️      |     ✔️     |      ✔️       |    ✔️     |                 |
| JETPACK_5_0_1             |             |            |               |           |       ✔️        |
| JETPACK_5_0_2             |             |            |               |    ✔️     |                 |


## NetsPresso Model Compressor Best Practice

If you want to experience Model Compressor online without any installation, please refer to the [NetsPresso-Model-Compressor-ModelZoo] repo that runs on Google Colab.

## Contact

Join our [Discussion Forum] for providing feedback or sharing your use cases, and if you want to talk more with Nota, please contact us [here].</br>
Or you can also do it via email([netspresso@nota.ai]) or phone(+82 2-555-8659)!


[Docs]: https://nota-netspresso.github.io/PyNetsPresso-docs
[Discussion Forum]: https://github.com/orgs/Nota-NetsPresso/discussions
[NetsPresso]: https://netspresso.ai?utm_source=git_comp&utm_medium=text_np&utm_campaign=py_launch
[PyNetsPresso]: https://py.netspresso.ai/?utm_source=git_comp&utm_medium=text_py&utm_campaign=py_launch
[here]: https://www.nota.ai/contact-us
[netspresso@nota.ai]: mailto:netspresso@nota.ai
[NetsPresso-Model-Compressor-ModelZoo]: https://github.com/Nota-NetsPresso/NetsPresso-Model-Compressor-ModelZoo
