private void Form1_Load(object sender, EventArgs e) { saveFileDialog.FilterIndex = 2; saveFileDialog.RestoreDirectory = true; saveFileDialog.Filter = "BMP|*.bmp|" + "JPEG|*.jpg; *.jpeg|" + "PNG|*.png|" + "GIF|*.gif|" + "TIFF|*.tiff"; deepPersonLab = new DeepPersonLab(@"..\..\..\deeplabv3_mnv2_pascal_train_aug_2018_01_29\deeplabv3_mnv2_pascal_train_aug.onnx"); portraitModeFilter = new PortraitModeFilter(0.0); }
/// <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)); }