You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 4.3 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.Drawing;
  17. using System.Linq;
  18. using System.Reflection;
  19. using Tensorflow;
  20. using Console = Colorful.Console;
  21. using static Tensorflow.Binding;
  22. namespace TensorFlowNET.Examples
  23. {
  24. class Program
  25. {
  26. static void Main(string[] args)
  27. {
  28. int finished = 0;
  29. var errors = new List<string>();
  30. var success = new List<string>();
  31. var parsedArgs = ParseArgs(args);
  32. var examples = Assembly.GetEntryAssembly().GetTypes()
  33. .Where(x => x.GetInterfaces().Contains(typeof(IExample)))
  34. .Select(x => (IExample)Activator.CreateInstance(x))
  35. .Where(x => x.Enabled)
  36. .OrderBy(x => x.Name)
  37. .ToArray();
  38. if (parsedArgs.ContainsKey("ex"))
  39. examples = examples.Where(x => x.Name == parsedArgs["ex"]).ToArray();
  40. Console.WriteLine(Environment.OSVersion.ToString(), Color.Yellow);
  41. Console.WriteLine($"TensorFlow Binary v{tf.VERSION}", Color.Yellow);
  42. Console.WriteLine($"TensorFlow.NET v{Assembly.GetAssembly(typeof(TF_DataType)).GetName().Version}", Color.Yellow);
  43. for (var i = 0; i < examples.Length; i++)
  44. Console.WriteLine($"[{i}]: {examples[i].Name}");
  45. var key = "0";
  46. if (examples.Length > 1)
  47. {
  48. Console.Write($"Choose one example to run, hit [Enter] to run all: ", Color.Yellow);
  49. key = Console.ReadLine();
  50. }
  51. var sw = new Stopwatch();
  52. for (var i = 0; i < examples.Length; i++)
  53. {
  54. if (i.ToString() != key && key != "") continue;
  55. var example = examples[i];
  56. Console.WriteLine($"{DateTime.UtcNow} Starting {example.Name}", Color.White);
  57. try
  58. {
  59. sw.Restart();
  60. bool isSuccess = example.Run();
  61. sw.Stop();
  62. if (isSuccess)
  63. success.Add($"Example: {example.Name} in {sw.Elapsed.TotalSeconds}s");
  64. else
  65. errors.Add($"Example: {example.Name} in {sw.Elapsed.TotalSeconds}s");
  66. }
  67. catch (Exception ex)
  68. {
  69. errors.Add($"Example: {example.Name}");
  70. Console.WriteLine(ex);
  71. }
  72. finished++;
  73. Console.WriteLine($"{DateTime.UtcNow} Completed {example.Name}", Color.White);
  74. }
  75. success.ForEach(x => Console.WriteLine($"{x} is OK!", Color.Green));
  76. errors.ForEach(x => Console.WriteLine($"{x} is Failed!", Color.Red));
  77. Console.WriteLine($"{finished} of {examples.Length} example(s) are completed.");
  78. Console.ReadLine();
  79. }
  80. private static Dictionary<string, string> ParseArgs(string[] args)
  81. {
  82. var parsed = new Dictionary<string, string>();
  83. for (int i = 0; i < args.Length; i++)
  84. {
  85. string key = args[i].Substring(1);
  86. switch (key)
  87. {
  88. case "ex":
  89. parsed.Add(key, args[++i]);
  90. break;
  91. default:
  92. break;
  93. }
  94. }
  95. return parsed;
  96. }
  97. }
  98. }