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 3.9 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using ICSharpCode.SharpZipLib.GZip;
  2. using ICSharpCode.SharpZipLib.Tar;
  3. using NumSharp.Core;
  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. namespace TensorFlowNET.Examples
  14. {
  15. /// <summary>
  16. /// Inception Architecture for Computer Vision
  17. /// Port from tensorflow\examples\label_image\label_image.py
  18. /// </summary>
  19. public class InceptionArchGoogLeNet : Python, IExample
  20. {
  21. string dir = "label_image_data";
  22. string pbFile = "inception_v3_2016_08_28_frozen.pb";
  23. string labelFile = "imagenet_slim_labels.txt";
  24. string picFile = "grace_hopper.jpg";
  25. int input_height = 299;
  26. int input_width = 299;
  27. int input_mean = 0;
  28. int input_std = 255;
  29. string input_name = "import/input";
  30. string output_name = "import/InceptionV3/Predictions/Reshape_1";
  31. public void Run()
  32. {
  33. PrepareData();
  34. var labels = File.ReadAllLines(Path.Join(dir, labelFile));
  35. var nd = ReadTensorFromImageFile(Path.Join(dir, picFile),
  36. input_height: input_height,
  37. input_width: input_width,
  38. input_mean: input_mean,
  39. input_std: input_std);
  40. var graph = Graph.ImportFromPB(Path.Join(dir, pbFile));
  41. var input_operation = graph.get_operation_by_name(input_name);
  42. var output_operation = graph.get_operation_by_name(output_name);
  43. var results = with(tf.Session(graph),
  44. sess => sess.run(output_operation.outputs[0],
  45. new FeedItem(input_operation.outputs[0], nd)));
  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. }
  55. private NDArray ReadTensorFromImageFile(string file_name,
  56. int input_height = 299,
  57. int input_width = 299,
  58. int input_mean = 0,
  59. int input_std = 255)
  60. {
  61. return with(tf.Graph().as_default(), graph =>
  62. {
  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. return with(tf.Session(graph), sess => sess.run(normalized));
  72. });
  73. }
  74. private 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. }
  87. }

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