Salted fish K210 experience notes-model training
This tutorial can be completed in Win: making a data set, training yolo, and converting it into a Kmodel file for k210
Model training
1. Install Anaconda3
Anaconda3 address and installation instructions
Remember to check [Add Anaconda3 to the PATH environment variable] during installation
2. Download tool
To download this project, extract [train_ann.zip] and [train_img.zip] to the current folder in the project root directory.
Download
[ncc_0.1_win.zip] to the project root directory and extract to the current folder
3. Prepare the environment
First install Anaconda in your system. You can open the command line input to conda -V
verify whether it is installed and the current version of conda.
1. New environment: conda create -n yolo python=3.6
Enter y and wait for the download to complete
2. Activate the environment: conda activate yolo
3. Install the necessary software packages:pip install -r requirements.txt
The reason for the error: the file is not in the root directory, so it cannot be opened.
Try again in the past.
Wait for QAQ (it takes a little longer ... be patient)
4. Modify the parameters
In [configs.json], modify the network type, the lable tag (such as raccoon), and other parameters. Note the folder name examlpe for storing pictures (train_img) and comments (train_ann)
5. Import pictures
First put the picture in the train_img folder
Open labelImg.exe
Open Dir—> Select the folder to store the image (train_img)
Change Save Dir—> Select the folder to store the annotation (train_ann)
Create RectBox—> Select the object to be marked and enter lable, the same as the configs above (such as raccoon )
After
saving , click Next (Next Image) to automatically generate an xml file of the marked target location and save it in the comment folder
6. Start training
input the command
python train.py -c configs.json
If the cv2 is lost, it is that the requirements.txt file is not finished, and it can be solved by retrying again: pip install -r requirements.txt
PS: There are too many training pictures, which leads to a huge training time. . When I do it myself, the pictures are less. (I waited for my flowers) The
download is complete
After the training is completed, a time-named folder will appear. The tflite file inside is the trained model.
Rename it (eg test.tflite) and copy it to the project root
7. Convert to Kmodel
ncc_0.1_win\ncc test.tflite test.kmodel -i tflite -o k210model --dataset train_img
After conversion, test.kmodel will appear in the root directory, you can burn it into k210 to run
test program
import sensor,image,lcd,time
import KPU as kpu
lcd.init(freq=15000000)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((224, 224))
sensor.set_brightness(2)
sensor.run(1)
clock = time.clock()
classes = ['class_1']
task = kpu.load(0x300000)
anchor = (1, 1.2, 2, 3, 4, 3, 6, 4, 5, 6.5)
a = kpu.init_yolo2(task, 0.17, 0.3, 5, anchor)
while(True):
clock.tick()
img = sensor.snapshot()
code = kpu.run_yolo2(task, img)
print(clock.fps())
if code:
for i in code:
a=img.draw_rectangle(i.rect())
a = lcd.display(img)
print(i.classid(),i.value())
for i in code:
lcd.draw_string(i.x(), i.y(), classes[i.classid()], lcd.RED, lcd.WHITE)
lcd.draw_string(i.x(), i.y()+12, '%f1.3'%i.value(), lcd.RED, lcd.WHITE)
else:
a = lcd.display(img)
a = kpu.deinit(task)
Reference address: https://github.com/TonyZ1Min/yolo-for-k210