public bool Run()
        {
            PrepareData();

            var graph = new Graph();

            //import GraphDef from pb file
            graph.Import(Path.Join(Config.Args.ModelFilePath));

            // モデルに応じて決まる
            var input_name  = graph.First().name;
            var output_name = graph.Last().name;

            var input_operation  = graph.OperationByName(input_name);
            var output_operation = graph.OperationByName(output_name);

            var labels        = File.ReadAllLines(Path.Join(Config.Args.LabelFilePath));
            var result_labels = new List <string>();
            var sw            = new Stopwatch();

            var predicts = new PredictResult()
            {
                Items = new List <PredictResultItem>(),
            };

            using (var sess = tf.Session(graph))
            {
                //foreach (var nd in file_ndarrays)
                for (int i = 0; i < file_ndarrays.Count; i++)
                {
                    var nd    = file_ndarrays[i];
                    var files = this.imagePathList[i];

                    sw.Restart();

                    var results = sess.run(output_operation.outputs[0], (input_operation.outputs[0], nd));
                    results = np.squeeze(results);
                    var singleNd = results.ToArray <float>();

                    var idxs = np.argsort <double>(results)["::-1"]; // high confident order
                    var idx  = idxs[0];                              // prediction result

                    Console.WriteLine($"{files[0]} in {sw.ElapsedMilliseconds}ms", Color.Tan);
                    for (int j = 0; j < idxs.Shape[0]; j++)
                    {
                        Console.WriteLine($"  {idxs[j]}) {labels[idxs[j]]} {singleNd[idxs[j]]}", Color.Tan);
                    }
                    result_labels.Add(labels[idx]);

                    var resultArr = results.ToArray <float>();

                    predicts.Items.Add(new PredictResultItem()
                    {
                        PathList = new List <string>(files),
                        Predicts = new List <float>(resultArr),
                        TopIndex = idx,
                    });;
                }
            }

            // output file
            predicts.WriteFile(Config.Args.OutputPath, labels);

            return(predicts.Items.Count > 0);
        }
Ejemplo n.º 2
0
        public bool Run()
        {
            var graph = new Graph();

            //import GraphDef from pb file
            graph.Import(Path.Join(Config.Args.ModelFilePath));

            // モデルに応じて決まる
            var input_name  = graph.First().name;
            var output_name = graph.Last().name;

            var input_operation  = graph.OperationByName(input_name);
            var output_operation = graph.OperationByName(output_name);

            var labels        = File.ReadAllLines(Path.Join(Config.Args.LabelFilePath));
            var result_labels = new List <string>();
            var sw            = new Stopwatch();

            sw.Start();

            var predicts = new PredictResult()
            {
                Items = new List <PredictResultItem>(),
            };

            using (var sess = tf.Session(graph))
            {
                Console.WriteLine($"Initialized in {sw.ElapsedMilliseconds}ms", Color.Tan);
                sw.Restart();

                // mini batch
                foreach (BatchContainer bc in PrepareData(Config.Args.BatchSize))
                {
                    // source
                    var files = new List <string[]>(bc.Files);
                    var nd    = bc.Nd;

                    // prediction process
                    var results = sess.run(output_operation.outputs[0], (input_operation.outputs[0], nd));

                    // per file pair
                    double elapsed = (double)sw.ElapsedMilliseconds / files.Count;

                    // output
                    for (int i = 0; i < files.Count; i++)
                    {
                        var singleResults = results[i];
                        var singleNd      = singleResults.ToArray <float>();
                        var singleFiles   = files[i];

                        var idxs = np.argsort <double>(singleResults)["::-1"]; // high confident order
                        var idx  = idxs[0];                                    // prediction result

                        Console.WriteLine($"{singleFiles[0]} in {elapsed}ms", Color.Tan);
                        for (int j = 0; j < idxs.Shape[0]; j++)
                        {
                            Console.WriteLine($"  {idxs[j]}) {labels[idxs[j]]} {singleNd[idxs[j]]}", Color.Tan);
                        }
                        result_labels.Add(labels[idx]);

                        predicts.Items.Add(new PredictResultItem()
                        {
                            PathList = new List <string>(singleFiles),
                            Predicts = new List <float>(singleNd),
                            TopIndex = idx,
                        });
                    }

                    sw.Restart();
                }
            }

            // output file
            predicts.WriteFile(Config.Args.OutputPath, labels);

            return(predicts.Items.Count > 0);
        }