コード例 #1
0
        private static TFSession LoadTFSession(IHostEnvironment env, string exportDirSavedModel)
        {
            Contracts.Check(env != null, nameof(env));
            env.CheckValue(exportDirSavedModel, nameof(exportDirSavedModel));
            var sessionOptions = new TFSessionOptions();
            var tags           = new string[] { "serve" };
            var graph          = new TFGraph();
            var metaGraphDef   = new TFBuffer();

            return(TFSession.FromSavedModel(sessionOptions, null, exportDirSavedModel, tags, graph, metaGraphDef));
        }
コード例 #2
0
        public static void Main(string [] args)
        {
            TFSessionOptions options = new TFSessionOptions();

            unsafe
            {
                //byte[] PUConfig = new byte[] { 0x32, 0x05, 0x20, 0x01, 0x2a, 0x01, 0x30, 0x38, 0x01 }; //gpu
                byte[] PUConfig = new byte[] { 0x0a, 0x07, 0x0a, 0x03, 0x67, 0x70, 0x75, 0x10, 0x00 }; //cpu
                fixed(void *ptr = &PUConfig[0])
                {
                    options.SetConfig(new IntPtr(ptr), PUConfig.Length);
                }
            }
            TFSession session;
            var       graph = new TFGraph();

            using (TFSession sess = new TFSession(graph, options))
                using (var metaGraphUnused = new TFBuffer())
                {
                    session = sess.FromSavedModel(options, null, "tzb", new[] { "serve" }, graph, metaGraphUnused);
                    IEnumerable <TensorFlow.DeviceAttributes> iem = session.ListDevices();
                    foreach (object obj in iem)
                    {
                        Console.WriteLine(((DeviceAttributes)obj).Name);
                    }
                    var labels = File.ReadAllLines("tzb/label.txt");
                    //打印节点名称

                    /*IEnumerable<TensorFlow.TFOperation> iem = graph.GetEnumerator();
                     * foreach (object obj in iem)
                     * {
                     *  Console.WriteLine(((TFOperation)obj).Name);
                     * }*/
                    //while(true)
                    float[] eimg = new float[224 * 224];
                    for (int i = 0; i < 224 * 224; i++)
                    {
                        eimg[i] = 0;
                    }
                    TFTensor ten = TFTensor.FromBuffer(tfs, eimg, 0, 224 * 224 * 1);
                    for (int j = 0; j < 3; j++)
                    {
                        var runner = session.GetRunner();
                        runner.AddInput(graph["images"][0], ten).Fetch(graph["classes"].Name);
                        var output = runner.Run();
                    }
                    ten.Dispose();
                    string[] files = Directory.GetFiles("tzb/images/defect", "*.*");
                    //while(true)
                    foreach (string file in files)
                    {
                        DateTime bft = DateTime.Now;
                        //var tensor = Image2Tensor(file);
                        //break;
                        var tensor = ImageUtil.CreateTensorFromImageFile(file);
                        //TFTensor tensor = TFTensor.FromBuffer(tfs, eimg, 0, 224 * 224);
                        var runner = session.GetRunner();
                        runner.AddInput(graph["images"][0], tensor).Fetch(graph["classes"].Name);
                        var      output = runner.Run();
                        DateTime aft    = DateTime.Now;
                        TimeSpan ts     = aft.Subtract(bft);
                        System.Threading.Thread.Sleep(50);
                        var result = output[0];
                        int class_ = ((int[])result.GetValue(jagged: true))[0];
                        Console.WriteLine(file + " best_match: " + class_ + " " + labels[class_] + " time: " + ts.TotalMilliseconds);
                    }
                }
        }