YoloSharp allows you to define a training and inference pipeline using a Config object and a YoloTask instance.
- 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. - Initialize Task: Create a
YoloTask passing the configuration. - Load Model (Optional): Use
yoloTask.LoadModel() to load pretrained weights. - Train: Call
yoloTask.Train() to start the training process. - 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);