Beispiel #1
0
        // Convert the image in filename to a Tensor suitable as input to the Inception model.
        public static TFTensor CreateTensorFromImageFile(string file, TFDataType destinationDataType = TFDataType.Float)
        {
            var contents = File.ReadAllBytes(file);

            // DecodeJpeg uses a scalar String-valued tensor as input.
            using (var tensor = TFTensor.CreateString(contents))
                // Construct a graph to normalize the image
                using (var graph = ConstructGraphToNormalizeImage(out TFOutput input, out TFOutput output, destinationDataType))
                    // Execute that graph to normalize this one image
                    using (var session = new TFSession(graph))
                    {
                        var normalized = session.Run(
                            inputs: new[] { input },
                            inputValues: new[] { tensor },
                            outputs: new[] { output });
                        Console.WriteLine(normalized.Length);
                        return(normalized[0]);
                    }
        }
Beispiel #2
0
        /// <summary>
        /// Create a constant tensor based on a shape
        /// Used by Zeros and Ones
        /// </summary>
        /// <param name="value">Value for tensor</param>
        /// <param name="tfshape">Shape of the tensor</param>
        /// <param name="dtype">Optional Type of the Zero value. Default: Double</param>
        /// <param name="operName">Operation name, optional.</param>
        /// <returns></returns>
        /// see https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/python/framework/constant_op.py
        public TFOutput Constant(object value, TFShape tfshape, TFDataType dtype = TFDataType.Double, string operName = null)
        {
            if (tfshape.NumDimensions <= 0)
            {
                TFTensor tensor = TFTensor.Create1DTensor(dtype, value);
                return(Const(tensor, tensor.TensorType, operName));
            }
            //convert the .net type to relevant tensorflow type
            object dtvalue = TFTensor.FetchSimple(dtype, value);

            var shape = tfshape.ToArray();
            var idx   = new int [shape.Length];

            for (int i = 0; i < shape.Length; i++)
            {
                if (shape [i] > Int32.MaxValue)
                {
                    throw new ArgumentOutOfRangeException("Shape can not be longer than 32 bits");
                }
            }

            Array data = null;

            if (tfshape.IsLongArray)
            {
                data = Array.CreateInstance(dtvalue.GetType(), tfshape.ToArray());
            }
            else
            {
                data = Array.CreateInstance(dtvalue.GetType(), tfshape.ToIntArray());
            }

            TFTensor.Set(data, dtype, shape, idx, 0, value);

            TFTensor tensor_value = new TFTensor(data);

            return(Const(tensor_value, tensor_value.TensorType, operName));
        }
Beispiel #3
0
        // Additional pointers for using TensorFlow & CustomVision together
        // Python: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/label_image.py
        // C++: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/label_image/main.cc
        // Java: https://github.com/Azure-Samples/cognitive-services-android-customvision-sample/blob/master/app/src/main/java/demo/tensorflow/org/customvision_sample/MSCognitiveServicesClassifier.java
        private static TFGraph ConstructGraphToNormalizeImage(out TFOutput input, out TFOutput output, TFDataType destinationDataType = TFDataType.Float)
        {
            const int   W     = 227;
            const int   H     = 227;
            const float Scale = 1;

            // Depending on your CustomVision.ai Domain - set appropriate Mean Values (RGB)
            // https://github.com/Azure-Samples/cognitive-services-android-customvision-sample for RGB values (in BGR order)
            var bgrValues = new TFTensor(new float[] { 104.0f, 117.0f, 123.0f }); // General (Compact) & Landmark (Compact)
            //var bgrValues = new TFTensor(0f); // Retail (Compact)

            var graph = new TFGraph();

            input = graph.Placeholder(TFDataType.String);

            var caster        = graph.Cast(graph.DecodeJpeg(contents: input, channels: 3), DstT: TFDataType.Float);
            var dims_expander = graph.ExpandDims(caster, graph.Const(0, "batch"));
            var resized       = graph.ResizeBilinear(dims_expander, graph.Const(new int[] { H, W }, "size"));
            var resized_mean  = graph.Sub(resized, graph.Const(bgrValues, "mean"));
            var normalised    = graph.Div(resized_mean, graph.Const(Scale));

            output = normalised;
            return(graph);
        }
Beispiel #4
0
 public static TensorFlow.TFOutput Const(this TFGraph graph, TensorFlow.TFTensor value, TensorFlow.TFDataType dtype, string operName = null)
 {
     return(graph.Const(value, dtype, operName));
 }
Beispiel #5
0
 /// <summary>
 /// Creates a constant operation from a TFTensor or constant
 /// </summary>
 /// <param name="value">Value.</param>
 /// <param name="operName">Oper name.</param>
 /// <remarks>
 /// Since TFTensor have implicit conversion operators, you can call this method with
 /// a constant like this: graph.Const (23)
 /// </remarks>
 public TFOutput Const(TFTensor value, string operName = null)
 {
     return(Const(value, value.TensorType, operName));
 }
Beispiel #6
0
 public static TensorFlow.TFOutput ImageSummary(this TFGraph graph, TensorFlow.TFOutput tag, TensorFlow.TFOutput tensor, long?max_images = null, TensorFlow.TFTensor bad_color = null, string operName = null)
 {
     return(graph.ImageSummary(tag, tensor, max_images, bad_color, operName));
 }