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

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

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