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.

ImageRecognition.cs 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using NumSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.IO.Compression;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using Tensorflow;
  10. namespace TensorFlowNET.Examples
  11. {
  12. public class ImageRecognition : Python, IExample
  13. {
  14. public int Priority => 7;
  15. public bool Enabled { get; set; } = true;
  16. public string Name => "Image Recognition";
  17. public bool ImportGraph { get; set; } = false;
  18. string dir = "ImageRecognition";
  19. string pbFile = "tensorflow_inception_graph.pb";
  20. string labelFile = "imagenet_comp_graph_label_strings.txt";
  21. string picFile = "grace_hopper.jpg";
  22. public bool Run()
  23. {
  24. PrepareData();
  25. var labels = File.ReadAllLines(Path.Join(dir, labelFile));
  26. var files = Directory.GetFiles(Path.Join(dir, "img"));
  27. foreach (var file in files)
  28. {
  29. var tensor = ReadTensorFromImageFile(file);
  30. var graph = new Graph().as_default();
  31. //import GraphDef from pb file
  32. graph.Import(Path.Join(dir, pbFile));
  33. var input_name = "input";
  34. var output_name = "output";
  35. var input_operation = graph.OperationByName(input_name);
  36. var output_operation = graph.OperationByName(output_name);
  37. var idx = 0;
  38. float propability = 0;
  39. with(tf.Session(graph), sess =>
  40. {
  41. var results = sess.run(output_operation.outputs[0], new FeedItem(input_operation.outputs[0], tensor));
  42. var probabilities = results.Data<float>();
  43. for (int i = 0; i < probabilities.Length; i++)
  44. {
  45. if (probabilities[i] > propability)
  46. {
  47. idx = i;
  48. propability = probabilities[i];
  49. }
  50. }
  51. });
  52. Console.WriteLine($"{picFile}: {labels[idx]} {propability}");
  53. return labels[idx].Equals("military uniform");
  54. }
  55. return false;
  56. }
  57. private NDArray ReadTensorFromImageFile(string file_name,
  58. int input_height = 224,
  59. int input_width = 224,
  60. int input_mean = 117,
  61. int input_std = 1)
  62. {
  63. return with(tf.Graph().as_default(), graph =>
  64. {
  65. var file_reader = tf.read_file(file_name, "file_reader");
  66. var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg");
  67. var cast = tf.cast(decodeJpeg, tf.float32);
  68. var dims_expander = tf.expand_dims(cast, 0);
  69. var resize = tf.constant(new int[] { input_height, input_width });
  70. var bilinear = tf.image.resize_bilinear(dims_expander, resize);
  71. var sub = tf.subtract(bilinear, new float[] { input_mean });
  72. var normalized = tf.divide(sub, new float[] { input_std });
  73. return with(tf.Session(graph), sess => sess.run(normalized));
  74. });
  75. }
  76. public void PrepareData()
  77. {
  78. Directory.CreateDirectory(dir);
  79. // get model file
  80. string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip";
  81. Utility.Web.Download(url, dir, "inception5h.zip");
  82. Utility.Compress.UnZip(Path.Join(dir, "inception5h.zip"), dir);
  83. // download sample picture
  84. Directory.CreateDirectory(Path.Join(dir, "img"));
  85. url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/grace_hopper.jpg";
  86. Utility.Web.Download(url, Path.Join(dir, "img"), "grace_hopper.jpg");
  87. }
  88. }
  89. }

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