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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using NumSharp;
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using Tensorflow;
  6. using static Tensorflow.Binding;
  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 = new Graph();
  38. graph.Import(Path.Join(dir, pbFile));
  39. var input_operation = graph.get_operation_by_name(input_name);
  40. var output_operation = graph.get_operation_by_name(output_name);
  41. NDArray results;
  42. using (var sess = tf.Session(graph))
  43. {
  44. results = sess.run(output_operation.outputs[0],
  45. new FeedItem(input_operation.outputs[0], nd));
  46. }
  47. results = np.squeeze(results);
  48. var argsort = results.argsort<float>();
  49. var top_k = argsort.Data<float>()
  50. .Skip(results.size - 5)
  51. .Reverse()
  52. .ToArray();
  53. foreach (float idx in top_k)
  54. Console.WriteLine($"{picFile}: {idx} {labels[(int)idx]}, {results[(int)idx]}");
  55. return true;
  56. }
  57. private NDArray ReadTensorFromImageFile(string file_name,
  58. int input_height = 299,
  59. int input_width = 299,
  60. int input_mean = 0,
  61. int input_std = 255)
  62. {
  63. var graph = tf.Graph().as_default();
  64. var file_reader = tf.read_file(file_name, "file_reader");
  65. var image_reader = tf.image.decode_jpeg(file_reader, channels: 3, name: "jpeg_reader");
  66. var caster = tf.cast(image_reader, tf.float32);
  67. var dims_expander = tf.expand_dims(caster, 0);
  68. var resize = tf.constant(new int[] { input_height, input_width });
  69. var bilinear = tf.image.resize_bilinear(dims_expander, resize);
  70. var sub = tf.subtract(bilinear, new float[] { input_mean });
  71. var normalized = tf.divide(sub, new float[] { input_std });
  72. using (var sess = tf.Session(graph))
  73. return sess.run(normalized);
  74. }
  75. public void PrepareData()
  76. {
  77. Directory.CreateDirectory(dir);
  78. // get model file
  79. string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception_v3_2016_08_28_frozen.pb.tar.gz";
  80. Utility.Web.Download(url, dir, $"{pbFile}.tar.gz");
  81. Utility.Compress.ExtractTGZ(Path.Join(dir, $"{pbFile}.tar.gz"), dir);
  82. // download sample picture
  83. string pic = "grace_hopper.jpg";
  84. url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/{pic}";
  85. Utility.Web.Download(url, dir, pic);
  86. }
  87. public Graph ImportGraph()
  88. {
  89. throw new NotImplementedException();
  90. }
  91. public Graph BuildGraph()
  92. {
  93. throw new NotImplementedException();
  94. }
  95. public void Train(Session sess)
  96. {
  97. throw new NotImplementedException();
  98. }
  99. public void Predict(Session sess)
  100. {
  101. throw new NotImplementedException();
  102. }
  103. public void Test(Session sess)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. }
  108. }