Training and Deployment of a Self-Trained Model Using Code with Huskylens 2
1.1 Project Overview
If you want HUSKYLENS 2 to recognize objects of your own, you first need to train a dedicated recognition model for it.
All training in this tutorial is done on your own computer — no account registration and no website is needed. The training tool is Ultralytics, the official Python library for the YOLO family of models: once it is installed, you only need to write a little code and it will handle model training, model conversion and file export for you. The apple photos, the trained model and the training results are all stored on your own computer and are never uploaded to any platform.
Models trained this way have a wide range of uses: fruit sorting, parts recognition, robot obstacle avoidance and maker education demos can all be applied directly. Switching to a different object is just as easy: prepare the corresponding photos and retrain once. Follow the workflow in this document and you will have your own trained model running on HUSKYLENS 2.
1.2 Demo Video
2 How It Works
Deploying a self-trained model consists of 5 steps: first prepare the dataset by collecting and organizing the videos/images of the objects to be recognized and generating a dataset in the required format; then train the model on the computer; next convert the trained model file to ONNX format; then use the official "Model Installation Package Generator" to package the ONNX model into an installation package usable on HUSKYLENS 2; finally copy the package to HUSKYLENS 2 to complete the installation. The complete workflow is shown in the figure below, where dataset preparation and installation/deployment are done on HUSKYLENS 2, training and export are done on the computer, and package generation is handled by the Package Generator.

3 Hardware and Software Preparation
3.1 Hardware List and Connections

3.2 Software Platform Preparation

3.2.1 Python and VSCode Environment Setup
Download and install Visual Studio Code from the official website.
Next, install or update the Ultralytics package in an environment with Python>=3.8 and PyTorch>=1.8:
pip install -U ultralytics
After the installation is complete, check whether it was installed successfully:
ultralytics version

If you encounter problems, please refer to: quick start
3.2.2 Microsoft .NET SDK 7.0 Environment
Download the Microsoft .NET SDK 7.0 environment.
The DFRobot Generator requires the .NET 7.0 SDK to run. Download SDK 7.0.410 (Windows x64) from the official Microsoft website.

Run the installer and install with the default settings.
Check whether the installation succeeded: since .NET has no graphical interface, you can verify it through the command prompt:
Press Win + R, type cmd, then press Enter.
Enter the following command and press Enter:
dotnet --version

If a version number is returned, the installation succeeded.
3.2.3 HUSKYLENS 2 Package Generator
The Generator is used to package the ONNX model and the dataset into a .zip file for HUSKYLENS 2.
Download HUSKYLENS2_Package_Generator.zip from the official DFRobot website.
Extract the .zip file to a local folder.
4 Dataset Preparation
This document uses a dataset in the standard YOLO format:
Images are placed in the images folder, and each image has a label file with the same name in the labels folder; the class names and splits are written in data.yaml.
your_dataset/
├── data.yaml
├── train/
│ ├── images/
│ └── labels/
├── valid/
│ ├── images/
│ └── labels/
└── test/ # optional
├── images/
└── labels/
If you do not have a dataset yet, you can prepare one with the Roboflow platform.
The Roboflow platform link is: Roboflow
When shooting footage, it is recommended to take more shots in different scenes and at different distances, and to include some clutter on the desk in the frame — this makes recognition more stable.
When exporting the dataset, it is recommended to resize the images to 640×640 during preprocessing. HUSKYLENS 2 supports only three sizes when generating an installation package — 224, 320 and 640 — and 640 gives the best recognition results.
The example in this project uses apples; you can use any object you need to recognize instead, and the workflow is exactly the same.
5 Model Training
5.1 Starting the Training
5.1.1 Open the Project
File → Open Folder, then create or select your project directory, for example D:\yolo_demo.

5.1.2 Training Code
Create a new file train.py and paste the following code:
from ultralytics import YOLO
if name == "main":
# Load a pretrained model: only "yolov8n.pt" or "yolo11n.pt" is supported
model = YOLO("yolo11n.pt")
# Start training
results = model.train(
data=r"D:\yolo_demo\Dataset\your_dataset_name\data.yaml", # dataset config file
epochs=20, # number of training epochs
imgsz=640, # input size, only 224 / 320 / 640
batch=16, # images per batch, reduce if out of memory
device="cpu",
workers=0,
lr0=0.001,
cos_lr=True,
patience=0,
freeze=10,
cache="ram",
project=r"D:\yolo_demo\runs", # output directory
name="apple_v3", # name of this training run
)The code files are listed in the attachment list.
5.1.3 Run
Press Ctrl + F5, or click the ▷ button in the top-right corner to run. The training process is displayed in the terminal window below, with no need to type commands manually.
Requirements of HUSKYLENS 2 for the model:
Training architecture: yolov8n or yolo11n; models trained with other variants cannot be installed
Image size (imgsz): 224, 320 or 640, and it must match the dataset export size
Training time: On CPU, training on about 50 images for 20 epochs takes roughly 10 minutes.
When training succeeds, the result is as follows:

5.2 View the Training Results
After training ends, you can view the generated best.pt model file in the specified path:

6 Export the Model
Create a new file export.py, paste the following code (change the model path to your own best.pt path), save it and then run it:
from ultralytics import YOLO
if __name__ == "__main__":
# Load the trained model
model = YOLO(r"D:\yolo_demo\runs\apple_v3\weights\best.pt")
# Export to ONNX; best.onnx is saved in the same folder
model.export(
format="onnx",
imgsz=640, # must match the training input size
nms=False, # no NMS inside the model
simplify=True, # simplify the model graph
)Verify: the terminal shows ONNX: export complete, and best.onnx is generated in the weights directory.
7 Generate and Install the Package
7.1 Generate the Package
First package with the Generator: select the dataset folder and the model file, select the model version, and set the app name.

If it reports "Training image directory not found:", the paths in data.yaml may not match the actual folders. The correct data.yaml format is as follows:


Check the data.yaml in the dataset against this format. Here path is the dataset root directory (./ means the same folder as data.yaml), and train is the training image directory under it.
After a successful conversion, the .zip installation package appears in the folder:

Note: do not rename or modify the generated .zip file! Keep the original name unchanged, otherwise HUSKYLENS 2 will not be able to recognize it.
7.2 Copy the Package to the Device
Copy the generated .zip file to the HUSKYLENS\storage\installation_package directory.

Then on HUSKYLENS 2, open the device screen, go to Model Installation → Local Installation → select the package, and wait for completion.



Once installed, you can find your own trained object detection model in the function list. To delete the model, find the deployed self-trained model on the screen, long-press the app icon, and confirm as prompted.
8 Verifying the Results
Result demonstration:

If there are many false recognitions in actual use, you can raise the confidence threshold on the device appropriately; if the results are still unsatisfactory, add images with different backgrounds and blank images without the target, and then retrain.
9 Attachment List
· Apple-YOLO11n-det-640.a87b.zip - Apple-YOLO11n-det-640.a87b.zip
· HUSKYLENS_test.v1i.yolov11.zip - Dataset file
· Ultralytics test.zip - code file










