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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. string dir = "ImageRecognition";
  18. string pbFile = "tensorflow_inception_graph.pb";
  19. string labelFile = "imagenet_comp_graph_label_strings.txt";
  20. string picFile = "grace_hopper.jpg";
  21. public bool Run()
  22. {
  23. PrepareData();
  24. var labels = File.ReadAllLines(Path.Join(dir, labelFile));
  25. var files = Directory.GetFiles(Path.Join(dir, "img"));
  26. foreach (var file in files)
  27. {
  28. var tensor = ReadTensorFromImageFile(file);
  29. var graph = new Graph().as_default();
  30. //import GraphDef from pb file
  31. graph.Import(Path.Join(dir, pbFile));
  32. var input_name = "input";
  33. var output_name = "output";
  34. var input_operation = graph.OperationByName(input_name);
  35. var output_operation = graph.OperationByName(output_name);
  36. var idx = 0;
  37. float propability = 0;
  38. with(tf.Session(graph), sess =>
  39. {
  40. var results = sess.run(output_operation.outputs[0], new FeedItem(input_operation.outputs[0], tensor));
  41. var probabilities = results.Data<float>();
  42. for (int i = 0; i < probabilities.Length; i++)
  43. {
  44. if (probabilities[i] > propability)
  45. {
  46. idx = i;
  47. propability = probabilities[i];
  48. }
  49. }
  50. });
  51. Console.WriteLine($"{picFile}: {labels[idx]} {propability}");
  52. return labels[idx].Equals("military uniform");
  53. }
  54. return false;
  55. }
  56. private NDArray ReadTensorFromImageFile(string file_name,
  57. int input_height = 224,
  58. int input_width = 224,
  59. int input_mean = 117,
  60. int input_std = 1)
  61. {
  62. return with(tf.Graph().as_default(), graph =>
  63. {
  64. var file_reader = tf.read_file(file_name, "file_reader");
  65. var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg");
  66. var cast = tf.cast(decodeJpeg, tf.float32);
  67. var dims_expander = tf.expand_dims(cast, 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. return with(tf.Session(graph), sess => sess.run(normalized));
  73. });
  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/inception5h.zip";
  80. Utility.Web.Download(url, dir, "inception5h.zip");
  81. Utility.Compress.UnZip(Path.Join(dir, "inception5h.zip"), dir);
  82. // download sample picture
  83. Directory.CreateDirectory(Path.Join(dir, "img"));
  84. url = $"https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/examples/label_image/data/grace_hopper.jpg";
  85. Utility.Web.Download(url, Path.Join(dir, "img"), "grace_hopper.jpg");
  86. }
  87. }
  88. }

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