Exemplo n.º 1
0
        /// <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);
        }
Exemplo n.º 2
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));
        }