Train Yolo model in C# with TorchSharp.
With the help of this project you won't have to transform .pt model to onnx, and can train your own model in C# and don't have to install python.
- Written in C# only, don't have to install python.
- Train and predict your own model.
- Support Yolov5, Yolov5u, Yolov8, Yolov11 and Yolov12 now.
- Support Predict, Segment, Obb, Pose(Key Points) and Classification now.
- Support n/s/m/l/x size.
- Support LetterBox and Mosaic4 method for preprocessing images.
- Support NMS with GPU.
- Support Load PreTrained models from ultralytics yolov5/yolov8/yolo11 and yolov12(converted).
- Support .Net6 or higher.
You can download yolo pre-trained models here.
Prediction Checkpoints
| model | n | s | m | l | x |
|---|---|---|---|---|---|
| yolov5 | yolov5n | yolov5s | yolov5m | yolov5l | yolov5x |
| yolov5 | yolov5nu | yolov5su | yolov5mu | yolov5lu | yolov5xu |
| yolov8 | yolov8n | yolov8s | yolov8m | yolov8l | yolov8x |
| yolov11 | yolov11n | yolov11s | yolov11m | yolov11l | yolov11x |
Segmention Checkpoints
| model | n | s | m | l | x |
|---|---|---|---|---|---|
| yolov8 | yolov8n | yolov8s | yolov8m | yolov8l | yolov8x |
| yolov11 | yolov11n | yolov11s | yolov11m | yolov11l | yolov11x |
You can download the code or add it from nuget.
dotnet add package IntptrMax.YoloSharp
Note
Please add one of libtorch-cpu, libtorch-cuda-12.1, libtorch-cuda-12.1-win-x64 or libtorch-cuda-12.1-linux-x64 version 2.5.1.0 and OpenCvSharp4.runtime to execute.
You can use it with the code below:
// Create a yolo task.
YoloTask yoloTask = new YoloTask(taskType, numberClass, yoloType: yoloType, deviceType: deviceType, yoloSize: yoloSize, dtype: dtype, keyPointShape: keyPointShape);
// Load pre-trained model, if you don't want to load the model, you can skip this step.
yoloTask.LoadModel(preTrainedModelPath, skipNcNotEqualLayers: true);
// Train model
yoloTask.Train(rootPath, trainDataPath, valDataPath, outputPath: outputPath, imageSize: imageSize, batchSize: batchSize, epochs: epochs, imageProcessType: imageProcessType);
// Predict image, if the model is not trained or loaded, it will use random weight to predict.
List<YoloResult> predictResult = yoloTask.ImagePredict(predictImage, predictThreshold, iouThreshold);Use yolov8n pre-trained model to detect.
Use yolov8n-seg pre-trained model to detect.
Use yolov8n-obb pre-trained model to detect.
Use yolov8n-pose pre-trained model to detect.



