/// <summary> /// Converts a Bitmap to an RGB tensor array. /// </summary> /// <param name="Data">Bitmap</param> /// <returns>RGB tensor array</returns> public static byte[] ToTensor(this Bitmap Data) { BitmapData bmData = Onnx.Lock24bpp(Data); byte[] rgb = Onnx.ToTensor(bmData); Onnx.Unlock(Data, bmData); return(rgb); }
/// <summary> /// Converts an RGB tensor array to a color image. /// </summary> /// <param name="tensor">RGBA tensor array</param> /// <param name="width">Bitmap width</param> /// <param name="height">Bitmap height</param> /// <param name="Data">Bitmap</param> public static void FromSegmentationMap(long[] tensor, int width, int height, Bitmap Data) { BitmapData bmData = Onnx.Lock24bpp(Data); FromSegmentationMap(tensor, width, height, bmData); Onnx.Unlock(Data, bmData); return; }
/// <summary> /// Converts an RGB tensor array to a color image. /// </summary> /// <param name="tensor">RGBA tensor array</param> /// <param name="width">Bitmap width</param> /// <param name="height">Bitmap height</param> /// <param name="Data">Bitmap</param> public static void FromTensor(this byte[] tensor, int width, int height, Bitmap Data) { BitmapData bmData = Onnx.Lock24bpp(Data); FromTensor(tensor, width, height, bmData); Onnx.Unlock(Data, bmData); return; }
/// <summary> /// Returns segmentation mask. /// </summary> /// <param name="image">Input image</param> /// <returns>Segmentation mask</returns> public Bitmap Fit(Bitmap image) { // scaling image var width = image.Width; var height = image.Height; var ratio = 1.0f * _size / Math.Max(width, height); var size = new Size( (int)(ratio * width), (int)(ratio * height)); var resized = new Bitmap(image, size); // creating tensor Console.WriteLine("Creating image tensor..."); var tic = Environment.TickCount; var inputMeta = _session.InputMetadata; var name = inputMeta.Keys.ToArray()[0]; var dimentions = new int[] { 1, size.Height, size.Width, 3 }; var inputData = Onnx.ToTensor(resized); resized.Dispose(); Console.WriteLine($"Tensor was created in {Environment.TickCount - tic} mls."); // prediction Console.WriteLine("Creating segmentation mask..."); tic = Environment.TickCount; var t1 = new DenseTensor <byte>(inputData, dimentions); var inputs = new List <NamedOnnxValue>() { NamedOnnxValue.CreateFromTensor(name, t1) }; var results = _session.Run(inputs).ToArray(); var map = results[0].AsTensor <long>().ToArray(); var mask = DeepPersonLab.FromSegmentationMap(map, size.Width, size.Height); Console.WriteLine($"Segmentation was created in {Environment.TickCount - tic} mls."); // return mask return(new Bitmap(mask, width, height)); }