进行图像分割预测,出现错误。
Your machine does't support current TorchSharp-Cuda environment. Please check your environment or change TorchSharp env support for your machine.
我在Segmenter种设置了deviceType为CPU了,为何会提示使用CUDA呢?
Check your nuget packages in YoloSharpDemo. If you didn't change the package, YoloSharp will use TorchSharp-cuda-windows 0.105.0 . This pack depends on CUDA 12.1 . If you want to use CPU only, please change it to TorchSharp-cpu pack.
这是我安装的包,是还有缺少包吗?
Have it worked?
Sorry, I have updated all the packages to the latest version, but the program is still giving an error. The detailed error message is as follows:
There is something wrong with device=CPU in torchsharp 0.106.0, please use 0.105.2 instead.
Sorry, there's still an error. Are there any examples of Segmentation?
Maybe you can try like the code below COCO128-seg dataset is already in the project. The NumberClass have to match the number of class in your own dataset.
static void Main(string[] args)
{
string preTrainedModelPath = @"..\..\..\Assets\PreTrainedModels\yolov8n-seg.bin"; // Pretrained model path.
string predictImagePath = @"..\..\..\Assets\TestImage\bus.jpg";
Mat predictImage = Cv2.ImRead(predictImagePath);
// Create a Yolo config
Config config = new Config
{
DeviceType = DeviceType.CUDA,
ScalarType = ScalarType.Float16,
RootPath = @"..\..\..\Assets\DataSets\coco128-seg",
TrainDataPath = "train.txt",
ValDataPath = "val.txt",
YoloType = YoloType.Yolov8,
YoloSize = YoloSize.n,
TaskType = TaskType.Segmentation,
ImageProcessType = ImageProcessType.Letterbox,
ImageSize = 640,
BatchSize = 16,
NumberClass = 80,
PredictThreshold = 0.3f,
IouThreshold = 0.7f,
Workers = 4,
Epochs = 100,
};
// Create a yolo task.
YoloTask yoloTask = new YoloTask(config);
// Load pre-trained model. If you don't want to use pre-trained model, skip the step.
yoloTask.LoadModel(preTrainedModelPath, skipNcNotEqualLayers: true);
// Train model
yoloTask.Train();
// Predict image, if the model is not trained or loaded, it will use random weight to predict.
List<YoloResult> predictResult = yoloTask.ImagePredict(predictImage);
// Rand for mask color.
Random rand = new Random(1024);
// Draw results
foreach (YoloResult result in predictResult)
{
float[] cxcywhr = new float[] { result.CenterX, result.CenterY, result.Width, result.Height, result.Radian };
float[] points = cxcywhr2xyxyxyxy(cxcywhr);
Point[] pts = new Point[4]
{
new Point(points[0], points[1]),
new Point(points[2], points[3]),
new Point(points[4], points[5]),
new Point(points[6], points[7]),
};
Cv2.Polylines(predictImage, new Point[][] { pts }, true, OpenCvSharp.Scalar.Red, 2);
string label = string.Format("{0}:{1:F1}%", result.ClassID, result.Score * 100);
Size textSize = Cv2.GetTextSize(label, HersheyFonts.HersheySimplex, 0.5, 1, out int baseline);
Cv2.Rectangle(predictImage, new Rect(new Point(result.X, result.Y - textSize.Height - baseline), new Size(textSize.Width, textSize.Height + baseline)), OpenCvSharp.Scalar.White, Cv2.FILLED);
Cv2.PutText(predictImage, label, new Point(result.X, result.Y - baseline), HersheyFonts.HersheySimplex, 0.5, OpenCvSharp.Scalar.Black, 1);
Console.WriteLine(string.Format("LabelID:{0}, Score:{1:F1}%, CenterX:{2}, CenterY:{3}, Width:{4}, Height:{5}, R:{6:F3} rnd", result.ClassID, result.Score * 100, result.CenterX, result.CenterY, result.Width, result.Height, result.Radian));
// Draw mask
if (result.Mask is not null)
{
Mat maskMat = Mat.FromArray<byte>(result.Mask);
maskMat = maskMat * 255;
maskMat = maskMat.Transpose();
// Create random color
int R = rand.Next(0, 255);
int G = rand.Next(0, 255);
int B = rand.Next(0, 255);
OpenCvSharp.Scalar color = new OpenCvSharp.Scalar(R, G, B, 200);
Mat backColor = new Mat(maskMat.Rows, maskMat.Cols, MatType.CV_8UC3, color);
Cv2.Add(predictImage, backColor, predictImage, maskMat);
}
if (result.KeyPoints is not null)
{
foreach (YoloSharp.Types.KeyPoint keyPoint in result.KeyPoints)
{
Cv2.Circle(predictImage, (int)keyPoint.X, (int)keyPoint.Y, 3, OpenCvSharp.Scalar.Blue);
}
}
}
predictImage.SaveImage("result.jpg");
Console.WriteLine();
Console.WriteLine("Image Predict done");
}