Welcome to K210 Module repository
16. Autonomous training model
16. Train the model yourself and use it
16. Train the model yourself and use it16.1. Experimental objectives16.2. Experimental process16.2.1. Train the model16.2.2, call the model16.3、Experimental results16.4、Experimental Summary
16.1. Experimental objectives
This lesson mainly teaches you to train the model yourself and run it on K210
16.2. Experimental process
16.2.1. Train the model
- Before training, make sure that you are using the factory firmware of Yabo, otherwise the trained model cannot be used.
- Go to the model training website : https://www.kendryte.com/en/products
- Register your own account

- After logging in, click Model Training


- Click Dataset -->Create Dataset --> Enter the dataset name --> Select the annotation type: Image Detection --> Click Submit. The model we trained in this chapter is used to detect three types of transportation: [airplanes, cars and ships]

- Select the dataset we just created

- Click Upload Image, upload the image and give the corresponding label

Click the "Label" button to enter the labeling interface and label the image with the corresponding label


Use the mouse to frame the object and click Save.


- Find the created dataset in the "Dataset" column and click the "Training button".

You can use the default parameters on the training parameter page or adjust them yourself

- Waiting for training

Note: The training progress may be stuck at 0% here, please wait patiently
- Download the training model

*.kmodel file is the trained model, det.py file is an example of using the model

- Upload kmodel to the SD card. The upload location of the model trained in this example is [/sd/KPU/train_model_and_use/det.kmodel]
Note: If the final model effect is not very good, you can try to unify the size of the images in the training set first
16.2.2, call the model
Open the det.py file in the compressed package downloaded in the previous section in the IDE
Modify the path in kpu.load_kmodel(/sd/det.kmodel') to the model path: kpu.load_kmodel('/sd/KPU/train_model_and_use/det.kmodel')
Modify the content of the labels array to the labels in the model you trained (you can see it in the label.txt file)
# 类名称,按照label.txt顺序填写 Class name, fill in the order according to label.txtlabels = ["plane", "ship", "car"]# anchors,使用anchor.txt中第二行的值 anchors, use the value of the second line in anchor.txtanchor = (9.50, 4.00, 7.97, 5.06, 9.69, 4.81, 7.59, 6.58, 9.36, 6.02)Initialize the KPU object
xxxxxxxxxxkpu = KPU()# 从sd或flash加载模型 Load model from sd or flashkpu.load_kmodel('/sd/KPU/train_model_and_use/det.kmodel')#kpu.load_kmodel(0x300000, 584744)kpu.init_yolo2(anchor, anchor_num=(int)(len(anchor)/2), img_w=320, img_h=240, net_w=320 , net_h=240 ,layer_w=10 ,layer_h=8, threshold=0.1, nms_value=0.3, classes=len(labels))Target detection main function
xxxxxxxxxxwhile(True):gc.collect()clock.tick()img = sensor.snapshot()kpu.run_with_output(img)dect = kpu.regionlayer_yolo2()fps = clock.fps()if len(dect) > 0:for l in dect :a = img.draw_rectangle(l[0],l[1],l[2],l[3],color=(0,255,0))info = "%s %.3f" % (labels[l[4]], l[5])a = img.draw_string(l[0],l[1],info,color=(255,0,0),scale=2.0)print(info)del infoa = img.draw_string(0, 0, "%2.1ffps" %(fps),color=(0,60,255),scale=2.0)lcd.display(img)
16.3、Experimental results
Connect the K210 module to the computer via a microUSB cable, click the Connect button in CanMV IDE, and click the Run button to run the example code. You can also download the code as main.py to the K210 module and run it.
After the system is initialized, the LCD displays the camera image and marks the "ship", "plane" and "car" in the image. The specific effect is shown in the figure



16.4、Experimental Summary
In this section, we learned how to use the online cloud training platform on the Canaan official website to train our own model and use the model in the K210 vision module. After training, please import the model file into the memory card and then insert the memory card into the memory card slot of the K210 module. If the model file in the memory card cannot be read, an error will be reported. Due to the size of the model, the accuracy of recognition may be affected. You can train a better model yourself.