YoloSharp

repository·master·Indexed 42 days ago

https://github.com/intptrmax/yolosharp

A 100% C# implementation for training and running YOLO models using TorchSharp. It enables the entire computer vision pipeline—including training, validation, and inference—within the .NET ecosystem, eliminating the need for a Python environment. Supports YOLOv5, YOLOv8, YOLOv11, and YOLOv12 checkpoints across various tasks such as Detection, Segmentation, Pose, and Obb.

Tokens
1.3K
Snippets
4
Records
6
Agent score
4%

What's inside YoloSharp

  1. Overview of the Ultralytics COCO8-pose Dataset

    master

    The Ultralytics COCO8-pose dataset is a small, versatile pose detection dataset designed for testing, debugging, and sanity-checking object detection models. It consists of the first 8 images from the COCO train 2017 set, split into 4 images for training and 4 images for validation.

    Because of its small size, it is ideal for verifying training pipelines and experimenting with new detection approaches before committing to larger datasets. This dataset is specifically intended for use with Ultralytics YOLOv8.

  2. Install YoloSharp via NuGet

    master

    To use YoloSharp in your .NET project, add the IntptrMax.YoloSharp package via NuGet.

    Important Dependencies: You must also manually add one of the following LibTorch packages (version 2.5.1.0) and OpenCvSharp4.runtime to your project to ensure the underlying engine works:

    • libtorch-cpu
    • libtorch-cuda-12.1
    • libtorch-cuda-12.1-win-x64
    • libtorch-cuda-12.1-linux-x64
    dotnet add package IntptrMax.YoloSharp
  3. Basic Usage: Train and Predict with YoloSharp

    master

    YoloSharp allows you to define a training and inference pipeline using a Config object and a YoloTask instance.

    1. Configure: Define your DeviceType (e.g., CUDA), YoloType (e.g., Yolov8), TaskType (e.g., Obb, Detection, Segmentation, Pose), and dataset paths in a Config object.
    2. Initialize Task: Create a YoloTask passing the configuration.
    3. Load Model (Optional): Use yoloTask.LoadModel() to load pretrained weights.
    4. Train: Call yoloTask.Train() to start the training process.
    5. Predict: Use yoloTask.ImagePredict(Mat image) to perform inference on an OpenCV Mat object.
    string preTrainedModelPath = @"..\..\..\Assets\PreTrainedModels\yolov8n-obb.bin";
    string predictImagePath = @"..\..\..\Assets\TestImage\trucks.jpg";
    string dataRootPath = @"..\..\..\Assets\datasets\dotav1";
    
    string trainDataPath = "train.txt";
    string valDataPath = "val.txt";
    
    Mat predictImage = Cv2.ImRead(predictImagePath);
    
    // Create a Yolo config
    Config config = new Config
    {
        DeviceType = DeviceType.CUDA,
        ScalarType = ScalarType.BFloat16,
        RootPath = dataRootPath,
        TrainDataPath = trainDataPath,
        ValDataPath = valDataPath,
        YoloType = YoloType.Yolov8,
        YoloSize = YoloSize.n,
        TaskType = TaskType.Obb,
        ImageProcessType = ImageProcessType.Mosiac,
        ImageSize = 640,
        BatchSize = 16,
        NumberClass = 15,
        PredictThreshold = 0.3f,
        IouThreshold = 0.7f,
        Workers = 4,
        Epochs = 100,
        LearningRate = 1e-4f,
        Patience = 50,
        KeyPoint_Num = 21,
        KeyPoint_Dim = 3,
    };
    
    // Create a yolo task.
    YoloTask yoloTask = new YoloTask(config);
    
    // Load pre-trained model.
    yoloTask.LoadModel(preTrainedModelPath, skipNcNotEqualLayers: true);
    
    // Train model
    yoloTask.Train();
    
    // Predict image
    List<YoloResult> predictResult = yoloTask.ImagePredict(predictImage);
  4. Download Pretrained YOLO Checkpoints

    master

    YoloSharp supports loading pretrained weights from Ultralytics YOLO (v5/v8/v11) and converted YOLOv12 checkpoints. Checkpoints are available for various model sizes: n (nano), s (small), m (medium), l (large), and x (extra large).

    Prediction Checkpoints

    Available for: yolov5, yolov5u, yolov8, yolov11.

    Segmentation Checkpoints

    Available for: yolov8, yolov11.