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