public static TensorProto CreateTensorFromImage(int[][] imageData, float revertsBits)
        {
            var imageFeatureShape = new TensorShapeProto();

            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = 1
            });
            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = imageData.Length * imageData.Length
            });

            var imageTensorBuilder = new TensorProto();

            imageTensorBuilder.Dtype       = DataType.DtFloat;
            imageTensorBuilder.TensorShape = imageFeatureShape;

            for (int i = 0; i < imageData.Length; ++i)
            {
                for (int j = 0; j < imageData.Length; ++j)
                {
                    imageTensorBuilder.FloatVal.Add(imageData[i][j] / revertsBits);
                }
            }

            return(imageTensorBuilder);
        }
Example #2
0
        private TensorProto GetTensorProto(byte[] byteArray)
        {
            ByteString imageData = ByteString.CopyFrom(byteArray);

            TensorProto tensorProto = new TensorProto();

            Dim dimBatch = new Dim()
            {
                Name = "batch", Size = 1
            };
            Dim dimData = new Dim()
            {
                Name = "data", Size = 1
            };
            RepeatedField <Dim> repeatedField = new RepeatedField <Dim>();

            repeatedField.Add(dimBatch);
            repeatedField.Add(dimData);

            TensorShapeProto tensorShape = new TensorShapeProto();

            tensorShape.Dim = repeatedField;

            tensorProto.TensorShape = tensorShape;

            tensorProto.Dtype = DataType.DtString;
            tensorProto.StringVal.Add(imageData);

            return(tensorProto);
        }
Example #3
0
        public static int[] ConvertSymbolicShapeToBarracuda(TensorShapeProto shape, string onnxLayout)
        {
            // TODO: use dimension denotation from TensorShapeProto to figure, if this particular tensor has specific data layout
            // https://github.com/onnx/onnx/blob/master/docs/DimensionDenotation.md
            var onnxShape = shape.AsInts();

            return(ConvertSymbolicShapeToBarracuda(onnxShape, onnxLayout));
        }
Example #4
0
        public static int[] ConvertSymbolicShapeToBarracuda(TensorShapeProto shape, string onnxLayout)
        {
            // TODO: use dimension denotation from TensorShapeProto to figure, if this particular tensor has specific data layout
            // https://github.com/onnx/onnx/blob/master/docs/DimensionDenotation.md
            var onnxShape = shape.Dim.Select(v => v.DimValue <int.MinValue?int.MinValue : v.DimValue> int.MaxValue ? int.MaxValue : (int)v.DimValue).ToArray();

            return(ConvertSymbolicShapeToBarracuda(onnxShape, onnxLayout));
        }
Example #5
0
        public async Task <IEnumerable <Detection> > Predict(Bitmap bmp)
        {
            if (_client == null)
            {
                throw new ApplicationException(nameof(_client));
            }

            // Desired image format
            const int         channels = 3;
            const int         width    = 300;
            const int         height   = 300;
            const PixelFormat format   = PixelFormat.Format24bppRgb;

            var shape = new TensorShapeProto
            {
                Dim = { new []
                        {
                            new TensorShapeProto.Types.Dim {
                                Name = "", Size = 1
                            },
                            new TensorShapeProto.Types.Dim {
                                Name = nameof(height), Size = height
                            },
                            new TensorShapeProto.Types.Dim {
                                Name = nameof(width), Size = width
                            },
                            new TensorShapeProto.Types.Dim {
                                Name = nameof(channels), Size = channels
                            },
                        } }
            };

            var proto = new TensorProto
            {
                TensorShape   = shape,
                Dtype         = Tensorflow.DataType.DtUint8,
                TensorContent = ToByteString(bmp, channels, width, height, format)
            };

            var request = new PredictRequest
            {
                ModelSpec = new ModelSpec {
                    Name = _model
                }
            };

            request.Inputs.Add("data", proto);

            // Send requenst for inference
            PredictResponse response = await _client.PredictAsync(request);

            return(ToDetections(response));
        }
        public static TensorProto TensorProtoFromImage(byte[] imageData)
        {
            var imageFeatureShape = new TensorShapeProto();

            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = 1
            });

            var imageTensorBuilder = new TensorProto();

            imageTensorBuilder.Dtype       = DataType.DtFloat;
            imageTensorBuilder.TensorShape = imageFeatureShape;
            imageTensorBuilder.StringVal.Add(Google.Protobuf.ByteString.CopyFrom(imageData));
            imageTensorBuilder.Dtype = DataType.DtString;

            return(imageTensorBuilder);
        }
Example #7
0
    public static TensorProto CreateTensorFromBuffer(IntPtr pixelBuffer, int height, int width, int channels)
    {
        var imageFeatureShape = new TensorShapeProto();

        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = 1
        });
        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = height
        });
        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = width
        });
        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = channels
        });

        var imageTensorBuilder = new TensorProto();

        imageTensorBuilder.Dtype       = DataType.DtUint8;
        imageTensorBuilder.TensorShape = imageFeatureShape;


        unsafe
        {
            for (int i = 0; i < height; i++)
            {
                byte *pI = (byte *)pixelBuffer.ToPointer() + i * width * channels; //pointer to start of row
                for (int j = 0; j < width; j++)
                {
                    imageTensorBuilder.IntVal.Add(pI[j * channels]);
                    imageTensorBuilder.IntVal.Add(pI[j * channels + 1]);
                    imageTensorBuilder.IntVal.Add(pI[j * channels + 2]);
                }
            }
        }

        return(imageTensorBuilder);
    }
Example #8
0
    public static TensorProto CreateTensorFromImage(uint[] pixels, int height, int width, int channels)
    {
        var imageFeatureShape = new TensorShapeProto();

        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = 1
        });
        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = width
        });
        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = height
        });
        imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
        {
            Size = channels
        });

        var imageTensorBuilder = new TensorProto();

        imageTensorBuilder.Dtype       = DataType.DtUint8;
        imageTensorBuilder.TensorShape = imageFeatureShape;

        var px = 0;

        for (int i = 0; i < width; ++i)
        {
            for (int j = 0; j < height; ++j)
            {
                var color = pixels[i + (j * width)];
                imageTensorBuilder.IntVal.Add((byte)(0xFF & (color >> 16)));
                imageTensorBuilder.IntVal.Add((byte)(0xFF & (color >> 8)));
                imageTensorBuilder.IntVal.Add((byte)(0xFF & (color >> 0)));
            }
        }

        return(imageTensorBuilder);
    }
Example #9
0
        public static TensorProto CreateTensorFromImage(int[][][] dimArray, float revertsBits, int height, int width, int channels)
        {
            var imageFeatureShape = new TensorShapeProto();

            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = 1
            });
            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = height
            });
            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = width
            });
            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = channels
            });

            var imageTensorBuilder = new TensorProto();

            imageTensorBuilder.Dtype       = DataType.DtUint8;
            imageTensorBuilder.TensorShape = imageFeatureShape;

            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    for (int c = 0; c < channels; c++)
                    {
                        //imageTensorBuilder.FloatVal.Add(dimArray[i][j][c] / revertsBits);
                        imageTensorBuilder.IntVal.Add(dimArray[i][j][c]);
                    }
                }
            }

            return(imageTensorBuilder);
        }
Example #10
0
 public static int[] ConvertSymbolicShapeToBarracuda(TensorShapeProto shape, string onnxLayout)
 {
     // TODO: use dimension denotation from TensorShapeProto to figure, if this particular tensor has specific data layout
     // https://github.com/onnx/onnx/blob/master/docs/DimensionDenotation.md
     return(ConvertSymbolicShapeToBarracuda(shape.Dim.Select(d => d.DimValue).ToArray(), onnxLayout));
 }
Example #11
0
        public static PredictResponse ImageDetectionRequest(string image_url, out int height, out int width)
        {
            //Create gRPC Channel
            var channel = new Channel(scoringServer, ChannelCredentials.Insecure,
                                      new List <Grpc.Core.ChannelOption> {
                new ChannelOption(ChannelOptions.MaxReceiveMessageLength, int.MaxValue),
                new ChannelOption(ChannelOptions.MaxSendMessageLength, int.MaxValue)
            });
            var client = new PredictionService.PredictionServiceClient(channel);

            //Create prediction request
            var request = new PredictRequest()
            {
                ModelSpec = new ModelSpec()
                {
                    Name = "ssd", SignatureName = "serving_default"
                }
            };

            //Add image tensor
            WebClient wc = new WebClient();

            byte[] data     = wc.DownloadData(image_url);
            Stream stream   = new MemoryStream(wc.DownloadData(image_url));
            var    dimArray = ImageUtils.ConvertImageStreamToDimArrays(stream);

            height = dimArray.Length;
            width  = dimArray[0].Length;
            var channels = dimArray[0][0].Length;

            var imageTensorBuilder = new TensorProto();
            var imageFeatureShape  = new TensorShapeProto();

            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = 1
            });
            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = height
            });
            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = width
            });
            imageFeatureShape.Dim.Add(new TensorShapeProto.Types.Dim()
            {
                Size = channels
            });

            imageTensorBuilder.Dtype       = DataType.DtUint8;
            imageTensorBuilder.TensorShape = imageFeatureShape;
            for (int i = 0; i < height; ++i)
            {
                for (int j = 0; j < width; ++j)
                {
                    for (int c = 0; c < channels; c++)
                    {
                        //imageTensorBuilder.FloatVal.Add(dimArray[i][j][c] / revertsBits);
                        imageTensorBuilder.IntVal.Add(dimArray[i][j][c]);
                    }
                }
            }
            request.Inputs.Add("inputs", imageTensorBuilder);

            //using (Stream stream = new MemoryStream(wc.DownloadData(image_url)))
            //{
            //    request.Inputs.Add("inputs", TensorBuilder.CreateTensorFromImage(stream, 1.0f));
            //}

            // Run the prediction
            var predictResponse = client.Predict(request);

            //Console.WriteLine(predictResponse.Outputs["detection_classes"]);
            //Console.WriteLine(predictResponse.Outputs["detection_boxes"]);
            //Console.WriteLine(predictResponse.Outputs["detection_scores"]);
            return(predictResponse);
        }
Example #12
0
 public static int[] AsInts(this TensorShapeProto shape)
 {
     return(shape.Dim.Select(v => v.DimValue <int.MinValue?int.MinValue : v.DimValue> int.MaxValue ? int.MaxValue : (int)v.DimValue).ToArray());
 }