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.

DataSetMnist.cs 3.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using NumSharp;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Text;
  17. using Tensorflow;
  18. namespace TensorFlowNET.Examples.Utility
  19. {
  20. public class DataSetMnist : IDataSet
  21. {
  22. public int num_examples { get; }
  23. public int epochs_completed { get; private set; }
  24. public int index_in_epoch { get; private set; }
  25. public NDArray data { get; private set; }
  26. public NDArray labels { get; private set; }
  27. public DataSetMnist(NDArray images, NDArray labels, TF_DataType dtype, bool reshape)
  28. {
  29. num_examples = images.shape[0];
  30. images = images.reshape(images.shape[0], images.shape[1] * images.shape[2]);
  31. images.astype(dtype.as_numpy_datatype());
  32. images = np.multiply(images, 1.0f / 255.0f);
  33. labels.astype(dtype.as_numpy_datatype());
  34. data = images;
  35. this.labels = labels;
  36. epochs_completed = 0;
  37. index_in_epoch = 0;
  38. }
  39. public (NDArray, NDArray) next_batch(int batch_size, bool fake_data = false, bool shuffle = true)
  40. {
  41. var start = index_in_epoch;
  42. // Shuffle for the first epoch
  43. if(epochs_completed == 0 && start == 0 && shuffle)
  44. {
  45. var perm0 = np.arange(num_examples);
  46. np.random.shuffle(perm0);
  47. data = data[perm0];
  48. labels = labels[perm0];
  49. }
  50. // Go to the next epoch
  51. if (start + batch_size > num_examples)
  52. {
  53. // Finished epoch
  54. epochs_completed += 1;
  55. // Get the rest examples in this epoch
  56. var rest_num_examples = num_examples - start;
  57. //var images_rest_part = _images[np.arange(start, _num_examples)];
  58. //var labels_rest_part = _labels[np.arange(start, _num_examples)];
  59. // Shuffle the data
  60. if (shuffle)
  61. {
  62. var perm = np.arange(num_examples);
  63. np.random.shuffle(perm);
  64. data = data[perm];
  65. labels = labels[perm];
  66. }
  67. start = 0;
  68. index_in_epoch = batch_size - rest_num_examples;
  69. var end = index_in_epoch;
  70. var images_new_part = data[np.arange(start, end)];
  71. var labels_new_part = labels[np.arange(start, end)];
  72. /*return (np.concatenate(new float[][] { images_rest_part.Data<float>(), images_new_part.Data<float>() }, axis: 0),
  73. np.concatenate(new float[][] { labels_rest_part.Data<float>(), labels_new_part.Data<float>() }, axis: 0));*/
  74. return (images_new_part, labels_new_part);
  75. }
  76. else
  77. {
  78. index_in_epoch += batch_size;
  79. var end = index_in_epoch;
  80. return (data[np.arange(start, end)], labels[np.arange(start, end)]);
  81. }
  82. }
  83. }
  84. }