Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            //string dir_path = "D:\\c#\\7_sem\\1_task\\models\\mnist\\mnist\\data\\data\testing\\9\\";
            string dir_path = Console.ReadLine();
            CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
            CancellationToken       token             = cancelTokenSource.Token;

            string model      = "mnist";
            string model_path = "D:\\c#\\7_sem\\1_task\\models\\" + model + "\\" + model + "\\model.onnx";

            Inferencer inferencer = new Inferencer(dir_path, model_path, token);

            int num_files   = new DirectoryInfo(dir_path).GetFiles().Length;
            var listen_task = new Task(() =>
            {
                while (inferencer.resultCollection.Count != num_files)
                {
                    string mes = Console.ReadLine();
                    if (mes == "End")
                    {
                        cancelTokenSource.Cancel();
                        Console.WriteLine("Cancel token");
                        Thread.Sleep(1000);
                        break;
                    }
                }
            });

            listen_task.Start();

            var po = new ParallelOptions {
                CancellationToken = token
            };
            var tasks = new Task[num_files];

            try
            {
                for (int i = 0; i < num_files; i++)
                {
                    tasks[i] = Task.Factory.StartNew(pi =>
                    {
                        int idx = (int)pi;
                        inferencer.CalcInference(idx);
                    }, i);
                }
            }
            catch (OperationCanceledException ex)
            {
                Console.WriteLine("Операция прервана");
            }
            finally
            {
                Task.WaitAll(tasks);
                Task.WaitAll(listen_task);
                cancelTokenSource.Dispose();

                foreach (string res in inferencer.resultCollection)
                {
                    Console.WriteLine(res + "\n");
                }
                //Console.ReadLine();
            }
        }