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

            // DecodeJpeg uses a scalar String-valued tensor as input.
            var tensor = TFTensor.CreateString(contents);

            TFGraph  graph;
            TFOutput input, output;

            // Construct a graph to normalize the image
            ConstructGraphToNormalizeImage(out graph, out input, out output);

            // 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 });

                return(normalized[0]);
            }
        }
    // Convert the image in filename to a Tensor suitable as input to the Inception model.
    public static TFTensor CreateTensorFromImageFile(byte[] imageTex, TFDataType destinationDataType = TFDataType.Float)
    {
        var contents = imageTex;

        // DecodeJpeg uses a scalar String-valued tensor as input.
        var tensor = TFTensor.CreateString(contents);

        TFOutput input, output;

        // Construct a graph to normalize the image
        using (var graph = ConstructGraphToNormalizeImage(out input, out 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 });

                return(normalized[0]);
            }
        }
    }
示例#3
0
        // Convert the image in filename to a Tensor suitable as input to the Inception model.
        public static TFTensor CreateTensorFromImageFile(string file, int targetWidth, int targetHeight, TFDataType destinationDataType = TFDataType.Float)
        {
            var contents = File.ReadAllBytes(file);

            // DecodeJpeg uses a scalar String-valued tensor as input.
            var tensor = TFTensor.CreateString(contents);

            TFOutput input, output;

            //// Construct a graph to normalize the image
            //using (var graph = ConstructGraphToNormalizeImage (out input, out output, targetWidth, targetHeight, 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 });

            //		return normalized [0];
            //	}
            //}

            using (var graph = ConstructGraphToNormalizeImage(out input, out 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 });

                    return(normalized[0]);
                }
            }
        }
示例#4
0
        private static async Task <TFTensor> ReadTensorStringFromFile(string fileName, CancellationToken ct)
        {
            var content = await File.ReadAllBytesAsync(fileName, ct).ConfigureAwait(false);

            return(TFTensor.CreateString(content));
        }
        public TFTensor 转换方法()
        {
            //if (b)
            //{
            //    contents = File.ReadAllBytes(file);

            //    // DecodeJpeg uses a scalar String-valued tensor as input.

            //    tensor = TFTensor.CreateString(contents);
            //    b = false;
            //}



            contents = File.ReadAllBytes(file);

            //// DecodeJpeg uses a scalar String-valued tensor as input.

            tensor = TFTensor.CreateString(contents);



            //contents  = null;

            // Construct a graph to normalize the image 归一化

            // Execute that graph to normalize this one image 执行图规范化这个形象
            //using (var session = new TFSession(graph1))
            //{
            //    var normalized = session.Run(
            //        inputs: new[] { input },
            //        inputValues: new[] { tensor },
            //        outputs: new[] { output });
            //    //tensor = null;
            //    return normalized[0];
            //}



            //using (var session = new TFSession(graph1))
            //{
            //var session = new TFSession(graph1);
            //    var runner = session.GetRunner();

            //    runner.AddInput(input, tensor);
            //    runner.Fetch(output);
            //    normalized = runner.Run();

            //    //session.CloseSession();
            //    session.DeleteSession();
            //    runner = null;
            //    //return normalized[0];

            ////}
            //return 1;



            using (var session = new TFSession(graph1))
            {
                try
                {
                    var runner = session.GetRunner();

                    runner.AddInput(input, tensor);
                    runner.Fetch(output);
                    normalized = runner.Run();

                    return(normalized[0]);
                }
                catch (Exception e)
                {
                    //Console.WriteLine(e);
                    throw;
                }
                finally
                {
                    session.Dispose();
                    //session.CloseSession();
                    //session.DeleteSession();
                }
            }

            //normalized = session.Run(
            //        inputs: new[] { input },
            //        inputValues: new[] { tensor },
            //        outputs: new[] { output });

            //tensor = null;

            //return normalized[0];
        }
示例#6
0
        public void Export(string newModelDir)
        {
            if (newModelDir.Last() != '/' && newModelDir.Last() != '\\')
            {
                newModelDir += "/";
            }

            TFOutput NodeSaver, NodeSaverPath;

            //if (!ForTraining)
            {
                NodeSaver     = Graph["save_1/control_dependency"][0];
                NodeSaverPath = Graph["save_1/Const"][0];
            }
            //else
            //{
            //    NodeSaver = Graph["save_2/control_dependency"][0];
            //    NodeSaverPath = Graph["save_2/Const"][0];
            //}

            Directory.CreateDirectory(newModelDir);
            if (Directory.Exists(newModelDir + "variables"))
            {
                Directory.Delete(newModelDir + "variables", true);
            }
            Directory.CreateDirectory(newModelDir + "variables");

            foreach (var fileName in Directory.EnumerateFiles(ModelDir))
            {
                string Source      = fileName;
                string Destination = newModelDir + Helper.PathToNameWithExtension(fileName);

                bool AreSame = false;
                try
                {
                    AreSame = Helper.NormalizePath(Source) == Helper.NormalizePath(Destination);
                }
                catch { }

                if (!AreSame)
                {
                    File.Copy(fileName, newModelDir + Helper.PathToNameWithExtension(fileName), true);
                }
            }

            TFTensor TensorPath = TFTensor.CreateString(Encoding.ASCII.GetBytes(newModelDir + "variables/variables"));
            var      Runner     = Session.GetRunner().AddInput(NodeSaverPath, TensorPath);

            Runner.Run(NodeSaver);

            if (Directory.EnumerateDirectories(newModelDir + "variables", "variables_temp*").Count() > 0)
            {
                string TempName = Directory.EnumerateDirectories(newModelDir + "variables", "variables_temp*").First();
                foreach (var oldPath in Directory.EnumerateFiles(TempName))
                {
                    string OldName = Helper.PathToNameWithExtension(oldPath);
                    string NewName = "variables" + OldName.Substring(OldName.IndexOf("."));
                    string NewPath = newModelDir + "variables/" + NewName;

                    File.Move(oldPath, NewPath);
                }
                Directory.Delete(TempName, true);
            }

            TensorPath.Dispose();
        }