# 8. Online Model Training
## 8.1 Training Data Preparation The [**7. Touch Control Course - Touch Photography**](https://docs.hiwonder.com/projects/CanMV-K230/en/latest/docs/7_Touch_Control_Course.html#touch-photography) routine is utilized to collect the corresponding dataset. Refer to the 7.3 Touch Photography section of the **7. Touch Control Course** for specific procedures on capturing and saving the dataset images. ## 8.2 Online Training Platform Registration Online training model address: https://www.kendryte.com/zh/training/dataset 1. Click the `X` button to cancel login.
2. Click the top right corner to switch to English.
3. After switching, click the **Sign in** or **Sign up** option in the top right corner.
4. Accessing the KENDRYTE allows for either registration or login, and registration is demonstrated here as an example.
5. Click the **Register** button to begin registration.
6. Fill in the corresponding personal information and click **Register**.
## 8.3 Initiating Online Model Training
1. After successful registration, the account logs in automatically and then clicks **Create Dataset** on the right.
2. Taking `Image Detection` as an example to create a dataset, enter the dataset name and select the corresponding recognition and annotation types.
3. Click the **Configuration** button in the top-right corner.
4. Click the `Upload Image` button to upload the corresponding dataset to the cloud platform.
5. Upload the prepared dataset to the model training cloud platform.
6. Click the **Annotate** button after the image upload is complete.
7. Click `New Tag` to add labels for the corresponding categories.
8. Add `green`, `red`, and `yellow` labels for traffic light recognition.
9. Select the corresponding color tag, using green as an example, and drag the mouse to annotate the target object.
10. Apply corresponding annotations to all images.
11. Click the return button upon completion of all annotations.
12. Next, click the **Train** button to start model training.
13. Fill in the corresponding training parameters.
```
* Platform: Select K230
* nncase Version: The latest version is selected.
* Iterations: This value is adjusted based on the dataset, typically set around 200. The system automatically ceases training when the loss remains stable, and change < 0.05 for 30~50 consecutive epochs.
* Batch Size: Select 8.
* Learning Rate: Enter 0.001.
* Label Box Limit: Enter 5, which indicates the maximum number of target boxes allowed per image.
```
14. Click **Confirm** to automatically redirect to the training interface and wait for the training. Note that queuing may be required during busy periods.
15. As training occurs in the cloud, the webpage or browser may be closed without affecting the process.
16. During the process, hover the mouse over the training result chart to view real-time loss data.
17. Upon training completion, the registered email address receives a notification indicating the end of model training.
## 8.4 Model Download and Deployment
1. First, click `Records` on the left, then click `Download` on the right to save the trained model and deployment materials locally.
2. Extract the downloaded compressed package locally.
3. The detailed directory structure of the archive is as follows.
```
**_image_1_2_2.py # 1.2.2 firmware image inference script, full code
**_video_1_2_2.py # 1.2.2 firmware video inference script, full code
**_image_1_3.py # 1.3 firmware image inference script, highly encapsulated
**_video_1_3.py # 1.3 firmware video inference script, highly encapsulated
README.pdf # Official tutorial document
det_results # Verification test results
mp_deployment_source # micropython deployment resource directory
|- *.kmodel # kmodel file
|- deploy_config.json # Deployment configuration file
```
4. Extract the downloaded archive to a local directory. Focus exclusively on the sample program, as well as the model and configuration files located within the `mp_deployment_source` directory.
5. Copy the entire `mp_deployment_source` folder to the `This PC\CanMV\sdcard` directory on the K230 development board SD card.
6. Using the video inference program as an example, drag the decompressed `det_image_1_2_2.py` into the CanMV IDE.
7. The program requires minor modification regarding the corresponding path.
Slightly modify the import path in the `det_image_1_2_2.py` program.
Original program:
```python
root_path = "/sdcard/mp_deployment_source/"
config_path = root_path + "deploy_config.json"
deploy_conf = {}
debug_mode = 1
```
Modified to:
```py
root_path = "/sdcard/mp_deployment_source/"
config_path = "/sdcard/mp_deployment_source/deploy_config.json"
deploy_conf = {}
debug_mode = 1
```
8. The display mode can also be selected.
Select the display mode by slightly modifying the display section of the `det_image_1_2_2.py` program.
LCD display mode:
```py
display_mode = "lcd"
if display_mode == "lcd":
DISPLAY_WIDTH = ALIGN_UP(800, 16)
DISPLAY_HEIGHT = 480
else:
DISPLAY_WIDTH = ALIGN_UP(1920, 16)
DISPLAY_HEIGHT = 1080
OUT_RGB888P_WIDTH = ALIGN_UP(640, 16)
OUT_RGB888P_HEIGH = 360
```
HDMI display mode:
```py
display_mode = "hdmi"
if display_mode == "lcd":
DISPLAY_WIDTH = ALIGN_UP(800, 16)
DISPLAY_HEIGHT = 480
else:
DISPLAY_WIDTH = ALIGN_UP(1920, 16)
DISPLAY_HEIGHT = 1080
OUT_RGB888P_WIDTH = ALIGN_UP(640, 16)
OUT_RGB888P_HEIGH = 360
```
9. The achieved effect is shown in the figure below.