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.

InceptionArchGoogLeNet.cs 4.5 kB

6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using NumSharp;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using Tensorflow;
  6. using static Tensorflow.Python;
  7. namespace TensorFlowNET.Examples
  8. {
  9. /// <summary>
  10. /// Inception Architecture for Computer Vision
  11. /// Port from tensorflow\examples\label_image\label_image.py
  12. /// </summary>
  13. public class InceptionArchGoogLeNet : IExample
  14. {
  15. public bool Enabled { get; set; } = false;
  16. public string Name => "Inception Arch GoogLeNet";
  17. public bool IsImportingGraph { get; set; } = false;
  18. string dir = "label_image_data";
  19. string pbFile = "inception_v3_2016_08_28_frozen.pb";
  20. string labelFile = "imagenet_slim_labels.txt";
  21. string picFile = "grace_hopper.jpg";
  22. int input_height = 299;
  23. int input_width = 299;
  24. int input_mean = 0;
  25. int input_std = 255;
  26. string input_name = "import/input";
  27. string output_name = "import/InceptionV3/Predictions/Reshape_1";
  28. public bool Run()
  29. {
  30. PrepareData();
  31. var labels = File.ReadAllLines(Path.Join(dir, labelFile));
  32. var nd = ReadTensorFromImageFile(Path.Join(dir, picFile),
  33. input_height: input_height,
  34. input_width: input_width,
  35. input_mean: input_mean,
  36. input_std: input_std);
  37. var graph = Graph.ImportFromPB(Path.Join(dir, pbFile));
  38. var input_operation = graph.get_operation_by_name(input_name);
  39. var output_operation = graph.get_operation_by_name(output_name);
  40. var results = with(tf.Session(graph),
  41. sess => sess.run(output_operation.outputs[0],
  42. new FeedItem(input_operation.outputs[0], nd)));
  43. results = np.squeeze(results);
  44. var argsort = results.argsort<float>();
  45. var top_k = argsort.Data<float>()
  46. .Skip(results.size - 5)
  47. .Reverse()
  48. .ToArray();
  49. foreach (float idx in top_k)
  50. Console.WriteLine($"{picFile}: {idx} {labels[(int)idx]}, {results[(int)idx]}");
  51. return true;
  52. }
  53. private NDArray ReadTensorFromImageFile(string file_name,
  54. int input_height = 299,
  55. int input_width = 299,
  56. int input_mean = 0,
  57. int input_std = 255)
  58. {
  59. return with(tf.Graph().as_default(), graph =>
  60. {
  61. var file_reader = tf.read_file(file_name, "file_reader");
  62. var image_reader = tf.image.decode_jpeg(file_reader, channels: 3, name: "jpeg_reader");
  63. var caster = tf.cast(image_reader, tf.float32);
  64. var dims_expander = tf.expand_dims(caster, 0);
  65. var resize = tf.constant(new int[] { input_height, input_width });
  66. var bilinear = tf.image.resize_bilinear(dims_expander, resize);
  67. var sub = tf.subtract(bilinear, new float[] { input_mean });
  68. var normalized = tf.divide(sub, new float[] { input_std });
  69. return with(tf.Session(graph), sess => sess.run(normalized));
  70. });
  71. }
  72. public void PrepareData()
  73. {
  74. Directory.CreateDirectory(dir);
  75. // get model file
  76. string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz";
  77. Utility.Web.Download(url, dir, $"{pbFile}.tar.gz");
  78. Utility.Compress.ExtractTGZ(Path.Join(dir, $"{pbFile}.tar.gz"), dir);
  79. // download sample picture
  80. string pic = "grace_hopper.jpg";
  81. url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/{pic}";
  82. Utility.Web.Download(url, dir, pic);
  83. }
  84. public Graph ImportGraph()
  85. {
  86. throw new NotImplementedException();
  87. }
  88. public Graph BuildGraph()
  89. {
  90. throw new NotImplementedException();
  91. }
  92. public void Train(Session sess)
  93. {
  94. throw new NotImplementedException();
  95. }
  96. public void Predict(Session sess)
  97. {
  98. throw new NotImplementedException();
  99. }
  100. public void Test(Session sess)
  101. {
  102. throw new NotImplementedException();
  103. }
  104. }
  105. }