Training a Cat Face Recognition Model from Scratch and Deploying It to a K230 Board
·(edited)· / 0
AI TranslationSimplified ChineseEnglish
Key Insights
The author documents the process of training a cat face recognition model from scratch and deploying it to a K230 development board. The project uses the CatFLW cat face dataset, which already includes cat face bounding boxes and keypoints, so only the bounding boxes need to be converted into YOLO annotations to create a YOLO dataset with training, validation, and test sets, plus the cat_face class. Ultralytics YOLO is then installed in a Conda environment, PyTorch is configured depending on whether an NVIDIA GPU is available, and training is performed with YOLOv8n and a 320 input size suitable for K230. After training, the results can be verified with test images, a GUI, and a computer webcam before exporting to ONNX. Before deployment, nncase matching the K230 firmware must be installed, and the ONNX model is converted to kmodel using the official detection script. Finally, the kmodel is copied to CanMV/K230, and deployment is completed by modifying the model path, class name, and confidence threshold based on the official YOLOv8 example.
Training a Cat Face Recognition Model from Scratch and Deploying It to a K230 Board
Preface
As everyone famously doesn’t know, I ended up getting into Hohai University’s Automation program with a score of 635. Sadly, I didn’t manage to coast into Xidian’s Management program and use it as a stepping stone.
But since things turned out this way, I might as well keep playing with some new toys (
I happened to get my hands on a LCSC K230 development board. After trying a few of its demos, I wanted to train a model of my own.
This dataset contains about 2,000 cat face images, each with one cat face bounding box and 48 keypoints, so the usual upfront data annotation work is already done. All we need to do is convert bounding_boxes into YOLO YOLO annotations before training.
Note: the official example’s test_yolov8\detect\to_kmodel.py is for object detection conversion script. Don’t use the script from the classify directory.
Training a Cat Face Recognition Model from Scratch and Deploying It to a K230 Board
Preface
As everyone famously doesn’t know, I ended up getting into Hohai University’s Automation program with a score of 635. Sadly, I didn’t manage to coast into Xidian’s Management program and use it as a stepping stone.
But since things turned out this way, I might as well keep playing with some new toys (
I happened to get my hands on a LCSC K230 development board. After trying a few of its demos, I wanted to train a model of my own.
So, why not train a cat face recognition model?
Getting the Cat Face Dataset
For this training run, I used the https://www.kaggle.com/datasets/georgemartvel/catflw dataset (CC BY-NC 4.0, so it can’t be used directly in commercial projects).
https://github.com/martvelge/CatFLW
Processing the Dataset
This dataset contains about 2,000 cat face images, each with one cat face bounding box and 48 keypoints, so the usual upfront data annotation work is already done. All we need to do is convert bounding_boxes into YOLO YOLO annotations before training.
Use the following code to perform the conversion:
This will generate the YOLO dataset: catflw_yolo
Training YOLO
Installing the Training Environment
I recommend using Conda:
Check the installation:
If you have an NVIDIA GPU, install the PyTorch build that matches your CUDA version. You can also train without a GPU, but it will be much slower.
Starting YOLOv8n Training
Run this in the current directory:
Parameter
Meaning
yolo
Invokes the Ultralytics YOLO command-line tool
detect
Uses the object detection task
train
Runs training
data=catflw_yolo\data.yaml
Path to the dataset configuration file
model=yolov8n.pt
Uses the YOLOv8 Nano pretrained model, which is small, fast, and suitable for the K230
epochs=100
Trains for 100 epochs over the full training set
imgsz=320
Resizes images to about 320×320, affecting both speed and accuracy
batch=8
Processes 8 images at a time; if you run out of VRAM, change it to 4 or 2
device=0
Uses GPU 0, i.e. my RTX 5070 Laptop GPU
project=runs
Saves training results to the runs directory
name=catface_yolov8n
Name of this training experiment
workers=0
Uses 0 data loader workers, which is more stable on Windows
Parameter notes:
After training finishes, you’ll find the model at:
Validate the Model with YOLO
Check the result with a test image:
The results will be saved to a path like:
Validating with a GUI and a webcam
Exporting to ONNX
For the K230, conversion usually goes through ONNX -> kmodel:
This generates:
It’s a good idea to use Netron to inspect the ONNX input. It should usually be:
Installing nncase
For the K230, the nncase version must match the development board firmware. According to the official documentation, Windows requires:
Then go to nncase Releases and download the corresponding version:
Install it:
Official K230 documentation:
Important: don’t just use the latest nncase. First confirm whether your K230 is the CanMV version or another variant.
Converting ONNX to KModel
Run:
Extract it:
Check whether the detection conversion script exists:
If it returns:
that means the script is ready.
Then install the conversion script dependencies:
Confirm that the current model and calibration set exist:
Both should return:
Note: the official example’s test_yolov8\detect\to_kmodel.py is for object detection conversion script. Don’t use the script from the classify directory.
After that, run:
Run this from the project root directory:
Parameter meanings:
The conversion may take a few minutes. If it succeeds, it will usually generate the following in the ONNX directory:
Deploying to the K230
If you are using CanMV:
1. Copy the catface_yolov8n.kmodel to the development board’s SD card.
2. Follow the official YOLOv8 K230 example.
3. Modify the model path in the example code.
4. Change the class names to:
5. Adjust the confidence threshold:
6. Example code: