# 模型量化

* 模型INT8量化
* 模型图优化

## 模型INT8量化

首先，我们要得到pt模型文件，在“模型训练”中，我讲解了可以通过本地训练和云端训练两种方式得到经过训练后的pt模型文件。

<figure><img src="/files/uB4Xt2ugjrrk0O5Ph1z9" alt=""><figcaption><p>本地</p></figcaption></figure>

<figure><img src="/files/ITBTHfSB6glIXgNrx11e" alt=""><figcaption><p>在线</p></figcaption></figure>

利用anaconda新建一个环境（例如可以命令为petoi\_convert\_local），在新环境中依次执行：

```
pip install ultralytics
pip install tensorflow
```

（！注意：我们在模型训练章节的本地训练部分使用anaconda新建了petoi\_train\_local环境，而本节使用的petoi\_convert\_local环境和petoi\_train\_local环境是两个不同的环境，不可使用petoi\_train\_local进行以下操作。）

下面，我们需要对模型进行量化，执行：

```
yolo export model=${your model path} format=tflite imgsz=192 int8
```

你将会在当前文件夹下看到一个 `yolov8n_saved_model` 文件夹，其中包含 `yolov8n_full_integer_quant.tflite` 模型文件。

<figure><img src="/files/yF1FlV8qbKRh9SgTRWua" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/MIfkZ5gn0p7scFiPmgZ9" alt=""><figcaption></figcaption></figure>

## 图优化

下面进行图优化。如果您是windows电脑，您需要安装 Microsoft C++ Build Tools。如果您是Mac用户或者Linux用户，则不需要安装。

{% embed url="<https://visualstudio.microsoft.com/zh-hans/visual-cpp-build-tools/>" %}

执行

```
pip3 install ethos-u-vela

```

创建vela\_config.ini,将以下内容复制到vela\_config.ini

```
; file: my_vela_cfg.ini ; ----------------------------------------------------------------------------- 
; Vela configuration file ; ----------------------------------------------------------------------------- 
; System Configuration 

; My_Sys_Cfg 
[System_Config.My_Sys_Cfg] 
core_clock=400e6 
axi0_port=Sram 
axi1_port=OffChipFlash 
Sram_clock_scale=1.0 
Sram_burst_length=32 
Sram_read_latency=16 
Sram_write_latency=16 
Dram_clock_scale=0.75 
Dram_burst_length=128 
Dram_read_latency=500 
Dram_write_latency=250 
OnChipFlash_clock_scale=0.25 
OffChipFlash_clock_scale=0.015625 
OffChipFlash_burst_length=32 
OffChipFlash_read_latency=64 
OffChipFlash_write_latency=64 
; ----------------------------------------------------------------------------- 
; Memory Mode 
; My_Mem_Mode_Parent 
[Memory_Mode.My_Mem_Mode_Parent] 
const_mem_area=Axi1 
arena_mem_area=Axi0 
cache_mem_area=Axi0
```

在终端执行

```
vela --accelerator-config ethos-u55-64 --config vela_config.ini --system-config My_Sys_Cfg --memory-mode My_Mem_Mode_Parent --output-dir ${Save path of the optimized model} ${The path of the tflite model that needs to be optimized}
```

&#x5C06;***${Save path of the optimized model}替换为你想要的输出目录***

&#x5C06;***${The path of the tflite model that needs to be optimized}***&#x66FF;换为你刚才得到的经过量化后的模型文件

然后你就会得到以“\_vela”结尾的模型，这个模型是可以用于部署在Grove Vision V2上的模型文件。

<figure><img src="/files/68v46DrefSf1h91sWW28" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.petoi.com/chinese/kuo-zhan-mo-kuai/shen-du-kai-fa-ying-yong-ai-shi-jue-mo-kuai/mo-xing-liang-hua.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
