YoloSharp icon indicating copy to clipboard operation
YoloSharp copied to clipboard

进行图像分割预测,出现错误。

Open lijianxin520 opened this issue 1 year ago • 9 comments

Image

lijianxin520 avatar Mar 13 '25 08:03 lijianxin520

Your machine does't support current TorchSharp-Cuda environment. Please check your environment or change TorchSharp env support for your machine.

IntptrMax avatar Mar 13 '25 09:03 IntptrMax

我在Segmenter种设置了deviceType为CPU了,为何会提示使用CUDA呢?

lijianxin520 avatar Mar 14 '25 00:03 lijianxin520

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.

IntptrMax avatar Mar 14 '25 02:03 IntptrMax

Image 这是我安装的包,是还有缺少包吗?

lijianxin520 avatar Mar 14 '25 07:03 lijianxin520

Have it worked?

IntptrMax avatar Mar 16 '26 15:03 IntptrMax

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:

Image Image Image

lijianxin520 avatar Mar 17 '26 01:03 lijianxin520

There is something wrong with device=CPU in torchsharp 0.106.0, please use 0.105.2 instead.

IntptrMax avatar Mar 17 '26 02:03 IntptrMax

Sorry, there's still an error. Are there any examples of Segmentation? Image

Image

lijianxin520 avatar Mar 17 '26 03:03 lijianxin520

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");
}
Image

IntptrMax avatar Mar 21 '26 08:03 IntptrMax