> For the complete documentation index, see [llms.txt](https://guide.petoi.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://guide.petoi.com/extensible-modules/petoi-ai-vision-module/custom-petoi-vision-model/steps-on-how-to-get-started/step-2-train-model/train-model-on-the-cloud.md).

# Train model on the cloud

{% hint style="info" %}
Reminder: All code and scripts for this project are executed in the cloud. Open the [Google Colab Notebook](https://colab.research.google.com/drive/1OPTGxdn03eJU69hFMEPp0hzXAPZfEDNZ?usp=sharing) and select Runtime > Run all to execute the setup.
{% endhint %}

Deep learning networks excel at automatically extracting features from large datasets, making them highly effective for tasks like object detection and image segmentation. To apply deep learning to your specific use case, you first need to understand how to train a model using known data.

#### Training models in the cloud

Use this workflow to train a YOLOv8 model in Ultralytics, export it to TFLite, optimize it with Vela, and download the final file.

{% stepper %}
{% step %}

### Create an Ultralytics account

Open the Ultralytics platform and create an account.

[![Logo](https://docs-example.gitbook.io/petoi-ai-vision/~gitbook/image?url=https%3A%2F%2Fplatform.ultralytics.com%2Ficon.svg%3Ficon.23bfd74n-o_b0.svg\&width=20\&dpr=3\&quality=100\&sign=5a0868a6\&sv=2)Annotate, Train, and Deploy Vision AIUltralytics Platform](https://platform.ultralytics.com/)
{% endstep %}

{% step %}

### Create a new project

Open **Example project**.

Select **New model**.

Change the model to `yolov8n`. On the right side there will be a dataset called COCO8, which is used for object detection to detect people, things, animals, etc.

![](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/eyUsfdT0oB5jhJ2aw3H5/image)

In the same window, switch to **Local Training** at the bottom.
{% endstep %}

{% step %}

### Open Google Colab

In Google Drive, go to **New → More → Google Colaboratory**.

![](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/uWhnWqoNSXmBi1Grydjx/image)
{% endstep %}

{% step %}

### Install Ultralytics and set your API key

Paste this into the first cell and run it.

```python
import os

# Install ultralytics to get the 'yolo' command 
!pip install ultralytics

# Set your API key for this notebook session 
os.environ['ULTRALYTICS_API_KEY'] = '(YOUR_API_KEY)'
```

![](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/Es5HEfQYt9JX4QMXZVAW/image)
{% endstep %}

{% step %}

### Start training

Paste this into a new cell.

If the original command uses `export`, replace it with `os.environ[...]`.

Also add `!` before `yolo`.

```python
import os

# Install ultralytics to get the 'yolo' command 
!pip install ultralytics

# Set the API key using os.environ for persistence within the Python process 
os.environ['ULTRALYTICS_API_KEY'] = '(YOUR_API_KEY)' 

!yolo train \ 
  model="ul://ultralytics/yolov8/yolov8n" \ 
  data="ul://ultralytics/datasets/coco8" \ 
  task="detect" \ 
  epochs=100 \ 
  batch=-1 \ 
  imgsz=640 \ 
  project="(your-name)/example-project"
```

For faster training, change the Colab runtime to **T4 GPU**.

![](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/iXmOLeyiUduglsUZ3rci/image)

After you verify the command, run the cell and wait for training to finish.
{% endstep %}

{% step %}

### Export the best model to TFLite

Run this cell after training completes.

Replace `(your-name)` with your Ultralytics username.

```python
from ultralytics import YOLO

# Load the best trained model 
model = YOLO('runs/detect/(your-name)/example-project/train/weights/best.pt')

# Export the model to TFLite format with int8 quantization 
model.export( 
    format='tflite', 
    imgsz=192, 
    int8=True 
) 

print("Model exported to TFLite (int8) successfully!") 
print("The exported model should be in the 'runs/detect/train/weights' directory.")
```

{% endstep %}

{% step %}

### Install Vela

Run this cell to install Arm Vela.

```python
!pip3 install ethos-u-vela
```

{% endstep %}

{% step %}

### Create the Vela config

Paste this into a new cell and run it.

```ini
%%writefile 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
```

{% endstep %}

{% step %}

### Optimize the TFLite model with Vela

Run this command in a new cell.

Replace `(your-name)` with your Ultralytics username.

```python
!vela --accelerator-config ethos-u55-64 --config vela_config.ini --system-config My_Sys_Cfg --memory-mode My_Mem_Mode_Parent --output-dir /content /content/runs/detect/(your-name)/example-project/train/weights/best_int8.tflite
```

{% endstep %}

{% step %}

### Download the final model

Run this cell to download the optimized model.

```python
from google.colab import files 

files.download('/content/best_int8_vela.tflite')
```

{% endstep %}
{% endstepper %}

#### Result

After you complete the workflow, you should have an exported and Vela-optimized model ready to download and deploy.

Your final output should be `best_int8_vela.tflite`.

#### Examples:

![](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/l3Ao6dwENWVN2riS4IVv/image)

*Example exported model output in Colab.*

![](https://content.gitbook.com/content/XLpFdG7mubqEHWylLK3w/blobs/rxq6EXPQHcK8IvwDuEdM/image)

*Example optimized model file ready for download.*


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://guide.petoi.com/extensible-modules/petoi-ai-vision-module/custom-petoi-vision-model/steps-on-how-to-get-started/step-2-train-model/train-model-on-the-cloud.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
