Ejemplo n.º 1
0
        // Make string scalar in ONNX from native C# number
        public static TensorProto MakeString(string name, string value)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = TensorProto.Types.DataType.String;
            tensor.StringData.Add(StringToByteString(value));
            return(tensor);
        }
Ejemplo n.º 2
0
        // Make ulong and uint integer types scalar in ONNX from native C# number
        public static TensorProto MakeUInt(string name, bool isUint64, ulong value)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = (int)ConvertToTensorProtoType(isUint64 ? typeof(ulong) : typeof(uint));
            tensor.Uint64Data.Add(value);
            return(tensor);
        }
Ejemplo n.º 3
0
        // Make long scalar in ONNX from native C# number
        public static TensorProto MakeInt64(string name, long value)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = TensorProto.Types.DataType.Int64;
            tensor.Int64Data.Add(value);
            return(tensor);
        }
Ejemplo n.º 4
0
        // Make int32 and smaller integer types scalar in ONNX from native C# number
        public static TensorProto MakeInt32(string name, Type type, int value)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = (int)ConvertToTensorProtoType(type);
            tensor.Int32Data.Add(value);
            return(tensor);
        }
Ejemplo n.º 5
0
        // Make float scalar in ONNX from native C# number
        public static TensorProto MakeFloat(string name, float value)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = TensorProto.Types.DataType.Float;
            tensor.FloatData.Add(value);
            return(tensor);
        }
Ejemplo n.º 6
0
        // Make int32 and smaller integer types scalar in ONNX from native C# number
        public static TensorProto MakeDouble(string name, double value)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = (int)TensorProto.Types.DataType.Double;
            tensor.DoubleData.Add(value);
            return(tensor);
        }
Ejemplo n.º 7
0
                public PrositPrecursorOutput(TensorProto tensor, ref int index)
                {
                    Intensities = new FragmentIonIntensity[PrositConstants.PEPTIDE_SEQ_LEN - 1];

                    // Copy blocks of intensities for each ion
                    for (var i = 0; i < PrositConstants.PEPTIDE_SEQ_LEN - 1; ++i)
                    {
                        Intensities[i] = new FragmentIonIntensity(tensor, ref index);
                    }
                }
Ejemplo n.º 8
0
        public PredictionResult Predict(byte[] byteArray)
        {
            TensorProto    tensorProto = this.GetTensorProto(byteArray);
            PredictRequest request     = this.GetPredictRequest(tensorProto);

            DateTime deadline = DateTime.UtcNow.AddSeconds(TIMEOUT);

            PredictResponse response = _client.Predict(request, new CallOptions(deadline: deadline));

            return(this.ParseResponse(response));
        }
Ejemplo n.º 9
0
        public void GetProto()
        {
            int[] data = {-1, 0, 1, int.MaxValue, int.MinValue};
            int[] shape = {5};
            Syft.Tensor.IntTensor t = ctrl.intTensorFactory.Create(_data: data, _shape: shape);

            TensorProto message = t.GetProto();
            byte[] messageAsByte = message.ToByteArray();
            TensorProto message2 = TensorProto.Parser.ParseFrom(messageAsByte);

            Assert.AreEqual(message, message2);
        }
Ejemplo n.º 10
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));
        }
Ejemplo n.º 11
0
        private PredictRequest GetPredictRequest(TensorProto tensorProto)
        {
            PredictRequest request = new PredictRequest();

            request.ModelSpec = new ModelSpec()
            {
                Name = _modelSpec, SignatureName = _signatureName
            };
            request.Inputs.Add(_inputsKey, tensorProto);

            return(request);
        }
Ejemplo n.º 12
0
 public static void RemoveExternalDataField(TensorProto tensor, string field_key)
 {
     foreach (var _tup_1 in tensor.ExternalData.Select((_p_1, _p_2) => Tuple.Create(_p_2, _p_1)))
     {
         var i     = _tup_1.Item1;
         var field = _tup_1.Item2;
         if (field.Key == field_key)
         {
             tensor.ExternalData.Remove(field);
         }
     }
 }
Ejemplo n.º 13
0
 public ExternalDataInfo(TensorProto tensor)
 {
     this.location = "";
     this.offset   = null;
     this.length   = null;
     this.checksum = null;
     this.basepath = "";
     attributes    = new Dictionary <string, string>();
     foreach (var entry in tensor.ExternalData)
     {
         attributes.Add(entry.Key, entry.Value);
     }
 }
Ejemplo n.º 14
0
        private static TensorProto makeProto(Tuple <float[], int[]> input)
        {
            var proto = new TensorProto {
                Dtype = DataType.DtFloat
            };

            proto.FloatVal.AddRange(input.Item1);
            var dims = input.Item2.Select(dim => new TensorShapeProto.Types.Dim {
                Size = dim
            });

            proto.TensorShape.Dim.AddRange(dims);
            return(proto);
        }
 private void GetData(Bitmap myBitmap, TensorProto proto)
 {
     // Important : load data column by column
     for (var i = 0; i < myBitmap.Width; i++)
     {
         for (var j = 0; j < myBitmap.Height; j++)
         {
             Color pixelColor = myBitmap.GetPixel(j, i);
             proto.FloatVal.Add(AdjustPixel(pixelColor.R));
             proto.FloatVal.Add(AdjustPixel(pixelColor.G));
             proto.FloatVal.Add(AdjustPixel(pixelColor.B));
         }
     }
 }
        public static TensorProto CreateTensor(float value)
        {
            var tensor = new TensorProto();

            tensor.FloatVal.Add(value);
            tensor.TensorShape = new TensorShapeProto();
            tensor.Dtype       = DataType.DtFloat;
            var dim = new TensorShapeProto.Types.Dim();

            dim.Size = 1;
            tensor.TensorShape.Dim.Add(dim);

            return(tensor);
        }
Ejemplo n.º 17
0
                public FragmentIonIntensity(TensorProto tensor, ref int index)
                {
                    if (index + PrositConstants.PRECURSOR_CHARGES > tensor.FloatVal.Count)
                    {
                        throw new ArgumentException();
                    }

                    Intensities = new float[PrositConstants.PRECURSOR_CHARGES];
                    // Copy intensities
                    for (var i = 0; i < PrositConstants.PRECURSOR_CHARGES; ++i)
                    {
                        Intensities[i] = PrositHelpers.ReLU(tensor.FloatVal[index++]);
                    }
                }
Ejemplo n.º 18
0
        public ImageRequest(params Stream[] images)
        {
            _modelSpec = new ModelSpec();
            _proto     = new TensorProto {
                Dtype = DataType.DtString
            };

            var bytes = images.Select(ByteString.FromStream);

            _proto.StringVal.AddRange(bytes);
            _proto.TensorShape = new TensorShapeProto();
            _proto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim());
            _proto.TensorShape.Dim[0].Size = images.Length;
        }
Ejemplo n.º 19
0
        public async Task Retries_transient_exceptions(StatusCode transientStatusCode)
        {
            var exception = new RpcException(new Status(transientStatusCode, string.Empty));

            var expectedResultValues = new[] { 1f, 2f, 3f };

            var outputTensorProto = new TensorProto {
                Dtype = DataType.DtFloat
            };

            outputTensorProto.FloatVal.Add(expectedResultValues);

            outputTensorProto.TensorShape = new TensorShapeProto();
            outputTensorProto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim());
            outputTensorProto.TensorShape.Dim[0].Size = 3;

            var predictRequest  = new PredictRequest();
            var predictResponse = new PredictResponse();

            predictResponse.Outputs.Add("output_alias", outputTensorProto);

            var predictionServiceClientMock = new Mock <IPredictionServiceClient>();

            predictionServiceClientMock.Setup(x => x.PredictAsync(predictRequest)).Returns(async() =>
            {
                await Task.CompletedTask;

                if (exception != null)
                {
                    var x     = exception;
                    exception = null;
                    throw x;
                }

                return(predictResponse);
            }).Verifiable();

            var scoringRequestMock = new Mock <IScoringRequest>();

            scoringRequestMock.Setup(x => x.MakePredictRequest()).Returns(() => predictRequest);

            var scoringClient = new ScoringClient(predictionServiceClientMock.Object);
            var result        = await scoringClient.ScoreAsync(scoringRequestMock.Object);

            Assert.Equal(expectedResultValues, result);

            scoringRequestMock.Verify(x => x.MakePredictRequest(), Times.Exactly(1));
            predictionServiceClientMock.Verify(x => x.PredictAsync(predictRequest), Times.Exactly(2));
        }
Ejemplo n.º 20
0
        public float PredictIdentity(float attribute1, float attribute2)
        {
            Channel channel = new Channel("localhost:7000", ChannelCredentials.Insecure);

            try
            {
                var client  = new PredictionService.PredictionServiceClient(channel);
                var request = new PredictRequest()
                {
                    ModelSpec = new ModelSpec()
                    {
                        Name          = "Sample2",
                        Version       = 1,
                        SignatureName = "Predict"
                    }
                };


                var proto = new TensorProto
                {
                    TensorShape = new TensorShapeProto(),
                    Dtype       = DataType.DtFloat
                };

                // one sample in batch, first dimension is always number of samples submitted for prediction
                proto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim()
                {
                    Size = 1
                });
                proto.TensorShape.Dim.Add(new TensorShapeProto.Types.Dim()
                {
                    Size = 2
                });

                proto.FloatVal.Add(attribute1);
                proto.FloatVal.Add(attribute2);

                request.Inputs.Add("inputs", proto);
                var result   = client.Predict(request);
                var response = JsonConvert.DeserializeObject <ServingResponse>(result.Outputs.ToString());

                return((float)response.outputs.floatVal[0]);
            }
            finally
            {
                channel.ShutdownAsync().Wait();
            }
        }
Ejemplo n.º 21
0
        public static void SaveExternalData(TensorProto tensor, string base_path)
        {
            var info = new ExternalDataInfo(tensor);
            var external_data_file_path = Path.Combine(base_path, info.location);

            // Retrieve the tensor's data from raw_data or load external file
            if (tensor.RawData == null)
            {
                throw new Exception("raw_data field doesn't exist.");
            }

            // Create file if it doesn't exist
            if (!File.Exists(external_data_file_path))
            {
                using (var f = File.CreateText(external_data_file_path))
                {
                    f.Write("ab");
                }
            }

            // Open file for reading and writing at random locations ('r+b')
            using (var data_file = File.OpenRead(external_data_file_path))
            {
                data_file.Seek(0, SeekOrigin.End);
                if (info.offset != null)
                {
                    // Pad file to required offset if needed
                    var file_size = data_file.Length;
                    if (info.offset > file_size)
                    {
                        List <byte> writeBytes = new List <byte>();
                        for (int i = 0; i < (info.offset - file_size); i++)
                        {
                            writeBytes.AddRange(Encoding.UTF8.GetBytes("\0"));
                        }

                        data_file.Write(writeBytes.ToArray());
                    }

                    data_file.Seek(info.offset.Value, SeekOrigin.Begin);
                }

                var offset = (int)data_file.Length;
                data_file.Write(tensor.RawData.ToByteArray());
                SetExternalData(tensor, info.location, offset, (int)data_file.Length - offset);
            }
        }
Ejemplo n.º 22
0
        // Make ulong and uint integer vector (i.e., 1-D tensor) with dims=null. Otherwise, dims is used as the shape of the produced tensor.
        public static TensorProto MakeUInts(string name, bool isUint64, IEnumerable <ulong> values, IEnumerable <long> dims = null)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = (int)ConvertToTensorProtoType(isUint64 ? typeof(ulong) : typeof(uint));
            tensor.Uint64Data.AddRange(values);
            if (dims != null)
            {
                tensor.Dims.AddRange(dims);
            }
            else
            {
                tensor.Dims.Add(values.Count());
            }
            return(tensor);
        }
Ejemplo n.º 23
0
        // Make string vector (i.e., 1-D tensor) with dims=null. Otherwise, dims is used as the shape of the produced tensor.
        public static TensorProto MakeStrings(string name, IEnumerable <string> values, IEnumerable <long> dims = null)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = TensorProto.Types.DataType.String;
            tensor.StringData.AddRange(StringToByteString(values));
            if (dims != null)
            {
                tensor.Dims.AddRange(dims);
            }
            else
            {
                tensor.Dims.Add(values.Count());
            }
            return(tensor);
        }
Ejemplo n.º 24
0
        // Make float vector (i.e., 1-D tensor) with dims=null. Otherwise, dims is used as the shape of the produced tensor.
        public static TensorProto MakeFloats(string name, IEnumerable <float> values, IEnumerable <long> dims = null)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = TensorProto.Types.DataType.Float;
            tensor.FloatData.AddRange(values);
            if (dims != null)
            {
                tensor.Dims.AddRange(dims);
            }
            else
            {
                tensor.Dims.Add(values.Count());
            }
            return(tensor);
        }
Ejemplo n.º 25
0
        // Make int32 and smaller integer types vector (i.e., 1-D tensor) with dims=null. Otherwise, dims is used as the shape of the produced tensor.
        public static TensorProto MakeInt32s(string name, Type type, IEnumerable <int> values, IEnumerable <long> dims = null)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = (int)ConvertToTensorProtoType(type);
            tensor.Int32Data.AddRange(values);
            if (dims != null)
            {
                tensor.Dims.AddRange(dims);
            }
            else
            {
                tensor.Dims.Add(values.Count());
            }
            return(tensor);
        }
Ejemplo n.º 26
0
        // Make double vector (i.e., 1-D tensor) with dims=null. Otherwise, dims is used as the shape of the produced tensor.
        public static TensorProto MakeDoubles(string name, IEnumerable <double> values, IEnumerable <long> dims = null)
        {
            var tensor = new TensorProto();

            tensor.Name     = name;
            tensor.DataType = (int)TensorProto.Types.DataType.Double;
            tensor.DoubleData.AddRange(values);
            if (dims != null)
            {
                tensor.Dims.AddRange(dims);
            }
            else
            {
                tensor.Dims.Add(values.Count());
            }
            return(tensor);
        }
        public static T Convert <T>(this TensorProto tensor) where T : class
        {
            var shape    = tensor.TensorShape;
            var dimCount = shape.Dim.Count;

            var resultType = typeof(T);

            if (!resultType.IsArray)
            {
                throw new Exception("Unable to convert tensor into scalar type");
            }

            var arrayRank = typeof(T).GetArrayRank();

            if (arrayRank != dimCount)
            {
                throw new Exception($"result tensor was not the expected rank {arrayRank} - was rank {dimCount}");
            }

            var elementType = resultType.GetElementType();

            Func <TensorProto, int, object> getItemFunc = null;

            if (elementType == typeof(float))
            {
                getItemFunc = (t, i) => t.FloatVal[i];
            }

            if (getItemFunc == null)
            {
                throw new Exception($"Don't know how to handle type {elementType}");
            }

            var dimSizes    = shape.Dim.Select(d => (int)d.Size).ToArray();
            var sysArray    = Array.CreateInstance(elementType, dimSizes);
            var tensorIndex = 0;

            foreach (var dimArray in GetPermutations(dimSizes))
            {
                sysArray.SetValue(getItemFunc(tensor, tensorIndex), dimArray);
                tensorIndex++;
            }

            return(sysArray as T);
        }
Ejemplo n.º 28
0
        /// <summary>
        /// Decodes "Prosit-encoded" peptide sequences from a tensor. Only used in testing
        /// </summary>
        /// <param name="tensor">Int tensor of shape n x Constants.PEPTIDE_SEQ_LEN</param>
        /// <returns>A list of modified sequence objects representing the decoded sequences</returns>
        public static ModifiedSequence[] DecodeSequences2(TensorProto tensor)
        {
            var n           = tensor.TensorShape.Dim[0].Size;
            var result      = new ModifiedSequence[n];
            var encodedSeqs = tensor.IntVal.ToArray();

            var seq = new StringBuilder(PrositConstants.PEPTIDE_SEQ_LEN);

            for (var i = 0; i < n; ++i)
            {
                var explicitMods = new List <ExplicitMod>();

                var idx = i * PrositConstants.PEPTIDE_SEQ_LEN;
                for (var j = 0; j < PrositConstants.PEPTIDE_SEQ_LEN; ++j)
                {
                    if (encodedSeqs[idx + j] == 0) // Essentially a null terminator
                    {
                        break;
                    }
                    // This essentially prioritizes unmodified AA over modified AA (e.g. unmodified C over Carbamidomethyl C)
                    else if (PrositConstants.AMINO_ACIDS_REVERSE.TryGetValue(encodedSeqs[idx + j], out var prositAA))
                    {
                        seq.Append(prositAA.AA);
                    }
                    // Here a single "first" modification is given precedence over all others for any given AA
                    else if (PrositConstants.MODIFICATIONS_REVERSE.TryGetValue(encodedSeqs[idx + j], out var prositAAMods))
                    {
                        var prositAAMod = prositAAMods[0];
                        explicitMods.Add(new ExplicitMod(j, prositAAMod.Mod));
                        seq.Append(prositAAMod.AA);
                    }
                    else
                    {
                        throw new PrositException(string.Format(@"Unknown Prosit AA index {0}", encodedSeqs[idx + j]));
                    }
                }

                var unmodSeq = seq.ToString();
                var mods     = explicitMods.Select(mod => ModifiedSequence.MakeModification(unmodSeq, mod));
                result[i] = new ModifiedSequence(seq.ToString(), mods, MassType.Monoisotopic);
                seq.Clear();
            }

            return(result);
        }
Ejemplo n.º 29
0
        /// <summary>
        /// Helper function for creating input tensors.
        /// type should match T.
        /// </summary>
        public static TensorProto Create2dTensor <T>(DataType type, Func <TensorProto, RepeatedField <T> > getVal,
                                                     ICollection <T> inputs, params long[] dimensions)
        {
            // Construct Tensor
            var tp = new TensorProto {
                Dtype = type
            };

            // Populate with data
            getVal(tp).AddRange(inputs);
            tp.TensorShape = new TensorShapeProto();
            Assume.AreEqual(dimensions.Aggregate(1L, (a, b) => a * b), (long)inputs.Count);
            tp.TensorShape.Dim.AddRange(dimensions.Select(d => new TensorShapeProto.Types.Dim {
                Size = d
            }));

            return(tp);
        }
        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);
        }