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.2 kB

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

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。