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

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

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