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.

MnistDataSet.cs 5.0 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using ICSharpCode.SharpZipLib.Core;
  2. using ICSharpCode.SharpZipLib.GZip;
  3. using NumSharp.Core;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using Tensorflow;
  10. namespace TensorFlowNET.Examples.Utility
  11. {
  12. public class MnistDataSet
  13. {
  14. private const string DEFAULT_SOURCE_URL = "https://storage.googleapis.com/cvdf-datasets/mnist/";
  15. private const string TRAIN_IMAGES = "train-images-idx3-ubyte.gz";
  16. private const string TRAIN_LABELS = "train-labels-idx1-ubyte.gz";
  17. private const string TEST_IMAGES = "t10k-images-idx3-ubyte.gz";
  18. private const string TEST_LABELS = "t10k-labels-idx1-ubyte.gz";
  19. public static Datasets read_data_sets(string train_dir,
  20. bool one_hot = false,
  21. TF_DataType dtype = TF_DataType.TF_FLOAT,
  22. bool reshape = true,
  23. int validation_size = 5000,
  24. string source_url = DEFAULT_SOURCE_URL)
  25. {
  26. Web.Download(source_url + TRAIN_IMAGES, train_dir, TRAIN_IMAGES);
  27. Compress.ExtractGZip(Path.Join(train_dir, TRAIN_IMAGES), train_dir);
  28. var train_images = extract_images(Path.Join(train_dir, TRAIN_IMAGES.Split('.')[0]));
  29. Web.Download(source_url + TRAIN_LABELS, train_dir, TRAIN_LABELS);
  30. Compress.ExtractGZip(Path.Join(train_dir, TRAIN_LABELS), train_dir);
  31. var train_labels = extract_labels(Path.Join(train_dir, TRAIN_LABELS.Split('.')[0]), one_hot: one_hot);
  32. Web.Download(source_url + TEST_IMAGES, train_dir, TEST_IMAGES);
  33. Compress.ExtractGZip(Path.Join(train_dir, TEST_IMAGES), train_dir);
  34. var test_images = extract_images(Path.Join(train_dir, TEST_IMAGES.Split('.')[0]));
  35. Web.Download(source_url + TEST_LABELS, train_dir, TEST_LABELS);
  36. Compress.ExtractGZip(Path.Join(train_dir, TEST_LABELS), train_dir);
  37. var test_labels = extract_labels(Path.Join(train_dir, TEST_LABELS.Split('.')[0]), one_hot: one_hot);
  38. int end = train_images.shape[0];
  39. var validation_images = train_images[np.arange(validation_size)];
  40. var validation_labels = train_labels[np.arange(validation_size)];
  41. train_images = train_images[np.arange(validation_size, end)];
  42. train_labels = train_labels[np.arange(validation_size, end)];
  43. var train = new DataSet(train_images, train_labels, dtype, reshape);
  44. var validation = new DataSet(validation_images, validation_labels, dtype, reshape);
  45. var test = new DataSet(test_images, test_labels, dtype, reshape);
  46. return new Datasets(train, validation, test);
  47. }
  48. public static NDArray extract_images(string file)
  49. {
  50. using (var bytestream = new FileStream(file, FileMode.Open))
  51. {
  52. var magic = _read32(bytestream);
  53. if (magic != 2051)
  54. throw new ValueError($"Invalid magic number {magic} in MNIST image file: {file}");
  55. var num_images = _read32(bytestream);
  56. var rows = _read32(bytestream);
  57. var cols = _read32(bytestream);
  58. var buf = new byte[rows * cols * num_images];
  59. bytestream.Read(buf, 0, buf.Length);
  60. var data = np.frombuffer(buf, np.uint8);
  61. data = data.reshape((int)num_images, (int)rows, (int)cols, 1);
  62. return data;
  63. }
  64. }
  65. public static NDArray extract_labels(string file, bool one_hot = false, int num_classes = 10)
  66. {
  67. using (var bytestream = new FileStream(file, FileMode.Open))
  68. {
  69. var magic = _read32(bytestream);
  70. if (magic != 2049)
  71. throw new ValueError($"Invalid magic number {magic} in MNIST label file: {file}");
  72. var num_items = _read32(bytestream);
  73. var buf = new byte[num_items];
  74. bytestream.Read(buf, 0, buf.Length);
  75. var labels = np.frombuffer(buf, np.uint8);
  76. if (one_hot)
  77. return dense_to_one_hot(labels, num_classes);
  78. return labels;
  79. }
  80. }
  81. private static NDArray dense_to_one_hot(NDArray labels_dense, int num_classes)
  82. {
  83. var num_labels = labels_dense.shape[0];
  84. var index_offset = np.arange(num_labels) * num_classes;
  85. var labels_one_hot = np.zeros(num_labels, num_classes);
  86. for(int row = 0; row < num_labels; row++)
  87. {
  88. var col = labels_dense.Data<byte>(row);
  89. labels_one_hot[row, col] = 1;
  90. }
  91. return labels_one_hot;
  92. }
  93. private static uint _read32(FileStream bytestream)
  94. {
  95. var buffer = new byte[sizeof(uint)];
  96. var count = bytestream.Read(buffer, 0, 4);
  97. return np.frombuffer(buffer, ">u4").Data<uint>(0);
  98. }
  99. }
  100. }

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