训练和导出模型请参考
https://github.com/bubbliiiing/deeplabv3-plus-pytorch
使用OpenCVSharp
using System;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System.Collections.Generic;
namespace Opencvsharpdeploy
{
class Program
{
static void Main(string[] args)
{
Mat image = Cv2.ImRead("demo.jpg");
Net net = CvDnn.ReadNet("deeplab.onnx");
Mat resize_image = new Mat();
Cv2.Resize(image, resize_image, new OpenCvSharp.Size(512, 512), 0, 0, InterpolationFlags.Linear);
Mat blob = CvDnn.BlobFromImage(resize_image, 1.0 / 255.0, new OpenCvSharp.Size(512, 512), new Scalar(0, 0, 0), true, false);
net.SetInput(blob);
Mat probs;
probs = net.Forward();
int batch = probs.Size(0);
int cls = probs.Size(1);
int width = probs.Size(2);
int height = probs.Size(3);
List<Mat> masks = new List<Mat>();
for (int b = 0; b < batch; b++)
{
for (int c = 0; c < cls; c++)
{
Mat mask = Mat.Zeros(new Size(height, width), MatType.CV_8UC1);
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
float value = probs.At<float>(b, c, h, w);
if (value > 0.9)
{
mask.At<int>(h, w) = 255;
}
else
{
mask.At<int>(h, w) = 0;
}
}
}
masks.Add(mask);
}
}
Mat masked_img = new Mat();
Mat rgb_mask = Mat.Zeros(new Size(image.Rows, image.Cols), MatType.CV_8UC3);
Console.WriteLine(masks.Count);
for (int i = 0; i < masks.Count; i++)
{
Random rd = new Random();
Cv2.Resize(masks[i], masks[i], new OpenCvSharp.Size(image.Rows, image.Cols));
if (i == 0)
{
Cv2.Add(rgb_mask, new Scalar(0, 0, 0), rgb_mask, masks[i]);
}
else
{
Cv2.Add(rgb_mask, new Scalar(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255)), rgb_mask, masks[i]);
}
}
Cv2.AddWeighted(image, 0.5, rgb_mask.Clone(), 0.5, 0, masked_img);
Cv2.NamedWindow("Mask.jpg", WindowFlags.Normal);
Cv2.ImShow("Mask.jpg", masked_img);
Cv2.WaitKey(0);
}
}
}
使用Microsoft.ML.OnnxRuntime
using System;
using System.Collections.Generic;
using System.Linq;
using OpenCvSharp;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
namespace Onnxruntimedeploy
{
class Program
{
static void Main(string[] args)
{
string model_path = "deeplab.onnx";
string image_path = "demo.jpg";
Mat masked_img = new Mat();
List<NamedOnnxValue> input_ontainer;
SessionOptions options;
InferenceSession onnx_session;
Tensor<float> input_tensor;
IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
DisposableNamedOnnxValue[] results_onnxvalue;
options = new SessionOptions();
options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
options.AppendExecutionProvider_CPU(0);
onnx_session = new InferenceSession(model_path, options);
input_ontainer = new List<NamedOnnxValue>();
Mat image = Cv2.ImRead(image_path);
Mat resize_image = new Mat();
float[] result_array = new float[1 * 3 * 512 * 512];
Cv2.Resize(image, resize_image, new OpenCvSharp.Size(512, 512));
input_tensor = new DenseTensor<float>(new[] { 1, 3, 512, 512 });
for (int y = 0; y < resize_image.Height; y++)
{
for (int x = 0; x < resize_image.Width; x++)
{
input_tensor[0, 0, y, x] = resize_image.At<Vec3b>(y, x)[0] / 255f;
input_tensor[0, 1, y, x] = resize_image.At<Vec3b>(y, x)[1] / 255f;
input_tensor[0, 2, y, x] = resize_image.At<Vec3b>(y, x)[2] / 255f;
}
}
input_ontainer.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));
result_infer = onnx_session.Run(input_ontainer);
results_onnxvalue = result_infer.ToArray();
List<Mat> masks = new List<Mat>();
var pred_value = results_onnxvalue[0].AsEnumerable<float>().ToArray();
var pred_dim = results_onnxvalue[0].AsTensor<float>().Dimensions.ToArray();
for (int b = 0; b < pred_dim[0]; b++)
{
for (int c = 0; c < pred_dim[1]; c++)
{
Mat mask = new Mat(pred_dim[2], pred_dim[3], MatType.CV_8UC1);
for (int h = 0; h < pred_dim[2]; h++)
{
for (int w = 0; w < pred_dim[3]; w++)
{
int idx = (b * pred_dim[1] * pred_dim[2] * pred_dim[3]) +
(c * pred_dim[2] * pred_dim[3]) + (h * pred_dim[3]) + w;
float value = pred_value[idx];
if (pred_value[idx] > 0.9)
{
mask.At<int>(h, w) = 255;
}
else
{
mask.At<int>(h, w) = 0;
}
}
}
masks.Add(mask);
}
}
Mat rgb_mask = Mat.Zeros(new Size(image.Rows, image.Cols), MatType.CV_8UC3);
for (int i = 0; i < masks.Count; i++)
{
Random rd = new Random();
Cv2.Resize(masks[i], masks[i], new OpenCvSharp.Size(image.Rows, image.Cols));
if (i == 0)
{
Cv2.Add(rgb_mask, new Scalar(0, 0, 0), rgb_mask, masks[i]);
}
else
{
Cv2.Add(rgb_mask, new Scalar(rd.Next(0, 255), rd.Next(0, 255), rd.Next(0, 255)), rgb_mask, masks[i]);
}
}
Cv2.AddWeighted(image, 0.5, rgb_mask.Clone(), 0.5, 0, masked_img);
Cv2.NamedWindow("Mask.jpg", WindowFlags.Normal);
Cv2.ImShow("Mask.jpg", masked_img);
Cv2.WaitKey(0);
}
}
}