public void ConstructMultiDimIntArrayTensor()
        {
            var array = new [, ] {
                { 123, 456 }
            };

            using (var tensor = new TFTensor(array))
            {
                Assert.Equal(TFDataType.Int32, tensor.TensorType);
                Assert.Equal(array.Rank, tensor.NumDims);
                Assert.Equal(array.GetLength(0), tensor.GetTensorDimension(0));
                Assert.Equal(array.GetLength(1), tensor.GetTensorDimension(1));
                Assert.Equal(new long [] { array.GetLength(0), array.GetLength(1) }, tensor.Shape);
                Assert.Equal((uint)sizeof(int) * array.Length, tensor.TensorByteSize.ToUInt32());
                Assert.Equal(array, tensor.GetValue());
            }
        }
        public void ConstructDoubleArrayTensor()
        {
            var array = new [] { 123.456, 234.567 };

            using (var tensor = new TFTensor(array))
            {
                Assert.Equal(TFDataType.Double, tensor.TensorType);
                Assert.Equal(array.Rank, tensor.NumDims);
                Assert.Equal(array.Length, tensor.GetTensorDimension(0));
                Assert.Equal(new long [] { array.Length }, tensor.Shape);
                Assert.Equal((uint)sizeof(double) * array.Length, tensor.TensorByteSize.ToUInt32());
                Assert.Equal(array, tensor.GetValue());
            }
        }
        public void ConstrucComplexArrayTensor()
        {
            var array = new [] { new Complex(1, 2), new Complex(2, -1) };

            using (var tensor = new TFTensor(array))
            {
                Assert.Equal(TFDataType.Complex128, tensor.TensorType);
                Assert.Equal(array.Rank, tensor.NumDims);
                Assert.Equal(array.Length, tensor.GetTensorDimension(0));
                Assert.Equal(new long [] { array.Length }, tensor.Shape);
                Assert.Equal(16u * array.Length, tensor.TensorByteSize.ToUInt32());
                Assert.Equal(array, tensor.GetValue());
            }
        }
        public void ConstructUnsignedShortArrayTensor()
        {
            var array = new ushort [] { 123, 234 };

            using (var tensor = new TFTensor(array))
            {
                Assert.Equal(TFDataType.UInt16, tensor.TensorType);
                Assert.Equal(array.Rank, tensor.NumDims);
                Assert.Equal(array.Length, tensor.GetTensorDimension(0));
                Assert.Equal(new long [] { array.Length }, tensor.Shape);
                Assert.Equal((uint)sizeof(ushort) * array.Length, tensor.TensorByteSize.ToUInt32());
                Assert.Equal(array, tensor.GetValue());
            }
        }
        public void ConstructSignedByteArrayTensor()
        {
            var array = new sbyte [] { 123, -123 };

            using (var tensor = new TFTensor(array))
            {
                Assert.Equal(TFDataType.Int8, tensor.TensorType);
                Assert.Equal(array.Rank, tensor.NumDims);
                Assert.Equal(array.Length, tensor.GetTensorDimension(0));
                Assert.Equal(new long [] { array.Length }, tensor.Shape);
                Assert.Equal((uint)sizeof(sbyte) * array.Length, tensor.TensorByteSize.ToUInt32());
                Assert.Equal(array, tensor.GetValue());
            }
        }
        public void ConstructBoolArrayTensor()
        {
            var array = new [] { true, false };

            using (var tensor = new TFTensor(array))
            {
                Assert.Equal(TFDataType.Bool, tensor.TensorType);
                Assert.Equal(array.Rank, tensor.NumDims);
                Assert.Equal(array.Length, tensor.GetTensorDimension(0));
                Assert.Equal(new long [] { array.Length }, tensor.Shape);
                Assert.Equal((uint)sizeof(bool) * array.Length, tensor.TensorByteSize.ToUInt32());
                Assert.Equal(array, tensor.GetValue());
            }
        }
Beispiel #7
0
        static void Main(string[] args)
        {
            // 整数张量
            var tensor = new TFTensor(1);

            Console.WriteLine("Value:" + tensor.GetValue());

            // 矩阵张量
            tensor = new TFTensor(new int[, ]
            {
                { 1, 2, 3 },
                { 4, 5, 6 },
            });
            Console.WriteLine("TensorType: {0}", tensor.TensorType.ToString());
            Console.WriteLine("NumDims: " + tensor.NumDims);
            Console.WriteLine("Shape", string.Join(",", tensor.Shape));
            for (var i = 0; i < tensor.NumDims; i++)
            {
                var dim = tensor.GetTensorDimension(i);
                Console.WriteLine("DimIndex: {0}, Dim: {1}", i, dim);
            }

            // 创建图
            var g = new TFGraph();

            // 创建字符串张量
            tensor = new TFTensor("Hello, world!".Select(o => (sbyte)o).ToArray());
            var hello = g.Const(tensor);

            // 创建会话
            var sess = new TFSession(g);

            // 进行计算
            var result = sess.GetRunner().Run(hello).GetValue();

            // 输出计算结果
            Console.WriteLine(string.Join("", ((sbyte[])result).Select(o => (char)o)));
        }
Beispiel #8
0
        public override IObservable <Pose> Process(IObservable <IplImage> source)
        {
            return(Observable.Defer(() =>
            {
                TFSessionOptions options = new TFSessionOptions();
                unsafe
                {
                    byte[] GPUConfig = new byte[] { 0x32, 0x02, 0x20, 0x01 };
                    fixed(void *ptr = &GPUConfig[0])
                    {
                        options.SetConfig(new IntPtr(ptr), GPUConfig.Length);
                    }
                }

                var graph = new TFGraph();
                var session = new TFSession(graph, options, null);
                var bytes = File.ReadAllBytes(ModelFileName);
                graph.Import(bytes);

                IplImage temp = null;
                TFTensor tensor = null;
                TFSession.Runner runner = null;
                var config = ConfigHelper.PoseConfig(PoseConfigFileName);
                return source.Select(input =>
                {
                    var poseScale = 1.0;
                    const int TensorChannels = 3;
                    var frameSize = input.Size;
                    var scaleFactor = ScaleFactor;
                    if (scaleFactor.HasValue)
                    {
                        poseScale = scaleFactor.Value;
                        frameSize.Width = (int)(frameSize.Width * poseScale);
                        frameSize.Height = (int)(frameSize.Height * poseScale);
                        poseScale = 1.0 / poseScale;
                    }

                    if (tensor == null || tensor.GetTensorDimension(1) != frameSize.Height || tensor.GetTensorDimension(2) != frameSize.Width)
                    {
                        tensor = new TFTensor(
                            TFDataType.Float,
                            new long[] { 1, frameSize.Height, frameSize.Width, TensorChannels },
                            frameSize.Width * frameSize.Height * TensorChannels * sizeof(float));
                        runner = session.GetRunner();
                        runner.AddInput(graph["Placeholder"][0], tensor);
                        runner.Fetch(graph["concat_1"][0]);
                    }

                    var frame = input;
                    if (frameSize != input.Size)
                    {
                        if (temp == null || temp.Size != frameSize)
                        {
                            temp = new IplImage(frameSize, input.Depth, input.Channels);
                        }

                        CV.Resize(input, temp);
                        frame = temp;
                    }

                    using (var image = new IplImage(frameSize, IplDepth.F32, TensorChannels, tensor.Data))
                    {
                        CV.Convert(frame, image);
                    }

                    // Run the model
                    var output = runner.Run();

                    // Fetch the results from output:
                    var poseTensor = output[0];
                    var pose = new Mat((int)poseTensor.Shape[0], (int)poseTensor.Shape[1], Depth.F32, 1, poseTensor.Data);
                    var result = new Pose(input);
                    var threshold = MinConfidence;
                    for (int i = 0; i < pose.Rows; i++)
                    {
                        BodyPart bodyPart;
                        bodyPart.Name = config[i];
                        bodyPart.Confidence = (float)pose.GetReal(i, 2);
                        if (bodyPart.Confidence < threshold)
                        {
                            bodyPart.Position = new Point2f(float.NaN, float.NaN);
                        }
                        else
                        {
                            bodyPart.Position.X = (float)(pose.GetReal(i, 1) * poseScale);
                            bodyPart.Position.Y = (float)(pose.GetReal(i, 0) * poseScale);
                        }
                        result.Add(bodyPart);
                    }
                    return result;
                });
            }));
        }
Beispiel #9
0
        public override IObservable <Pose> Process(IObservable <IplImage> source)
        {
            return(Observable.Defer(() =>
            {
                TFSessionOptions options = new TFSessionOptions();
                unsafe
                {
                    byte[] GPUConfig = new byte[] { 0x32, 0x02, 0x20, 0x01 };
                    fixed(void *ptr = &GPUConfig[0])
                    {
                        options.SetConfig(new IntPtr(ptr), GPUConfig.Length);
                    }
                }

                var graph = new TFGraph();
                var session = new TFSession(graph, options, null);
                var bytes = File.ReadAllBytes(ModelFileName);
                graph.Import(bytes);

                TFTensor tensor = null;
                var config = ConfigHelper.PoseConfig(PoseConfigFileName);
                return source.Select(input =>
                {
                    if (tensor == null || tensor.GetTensorDimension(1) != input.Height || tensor.GetTensorDimension(2) != input.Width)
                    {
                        tensor = new TFTensor(
                            TFDataType.Float,
                            new long[] { 1, input.Height, input.Width, 3 },
                            input.WidthStep * input.Height * 4);
                    }

                    using (var image = new IplImage(input.Size, IplDepth.F32, 3, tensor.Data))
                    {
                        CV.Convert(input, image);
                    }

                    var runner = session.GetRunner();
                    runner.AddInput(graph["Placeholder"][0], tensor);
                    runner.Fetch(graph["concat_1"][0]);

                    // Run the model
                    var output = runner.Run();

                    // Fetch the results from output:
                    var poseTensor = output[0];
                    var pose = new Mat((int)poseTensor.Shape[0], (int)poseTensor.Shape[1], Depth.F32, 1, poseTensor.Data);
                    var result = new Pose(input);
                    var threshold = MinConfidence;
                    for (int i = 0; i < pose.Rows; i++)
                    {
                        BodyPart bodyPart;
                        bodyPart.Name = config[i];
                        bodyPart.Confidence = (float)pose.GetReal(i, 2);
                        if (bodyPart.Confidence < threshold)
                        {
                            bodyPart.Position = new Point2f(float.NaN, float.NaN);
                        }
                        else
                        {
                            bodyPart.Position.X = (float)pose.GetReal(i, 1);
                            bodyPart.Position.Y = (float)pose.GetReal(i, 0);
                        }
                        result.Add(bodyPart);
                    }
                    return result;
                });
            }));
        }