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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Tensorflow;
  15. namespace TensorFlowNET.Examples.Utility
  16. {
  17. public class DataSetMnist : IDataSet
  18. {
  19. public int num_examples { get; }
  20. public int epochs_completed { get; private set; }
  21. public int index_in_epoch { get; private set; }
  22. public NDArray data { get; private set; }
  23. public NDArray labels { get; private set; }
  24. public DataSetMnist(NDArray images, NDArray labels, TF_DataType dtype, bool reshape)
  25. {
  26. num_examples = images.shape[0];
  27. images = images.reshape(images.shape[0], images.shape[1] * images.shape[2]);
  28. images.astype(dtype.as_numpy_datatype());
  29. images = np.multiply(images, 1.0f / 255.0f);
  30. labels.astype(dtype.as_numpy_datatype());
  31. data = images;
  32. this.labels = labels;
  33. epochs_completed = 0;
  34. index_in_epoch = 0;
  35. }
  36. public (NDArray, NDArray) next_batch(int batch_size, bool fake_data = false, bool shuffle = true)
  37. {
  38. var start = index_in_epoch;
  39. // Shuffle for the first epoch
  40. if(epochs_completed == 0 && start == 0 && shuffle)
  41. {
  42. var perm0 = np.arange(num_examples);
  43. np.random.shuffle(perm0);
  44. data = data[perm0];
  45. labels = labels[perm0];
  46. }
  47. // Go to the next epoch
  48. if (start + batch_size > num_examples)
  49. {
  50. // Finished epoch
  51. epochs_completed += 1;
  52. // Get the rest examples in this epoch
  53. var rest_num_examples = num_examples - start;
  54. //var images_rest_part = _images[np.arange(start, _num_examples)];
  55. //var labels_rest_part = _labels[np.arange(start, _num_examples)];
  56. // Shuffle the data
  57. if (shuffle)
  58. {
  59. var perm = np.arange(num_examples);
  60. np.random.shuffle(perm);
  61. data = data[perm];
  62. labels = labels[perm];
  63. }
  64. start = 0;
  65. index_in_epoch = batch_size - rest_num_examples;
  66. var end = index_in_epoch;
  67. var images_new_part = data[np.arange(start, end)];
  68. var labels_new_part = labels[np.arange(start, end)];
  69. /*return (np.concatenate(new float[][] { images_rest_part.Data<float>(), images_new_part.Data<float>() }, axis: 0),
  70. np.concatenate(new float[][] { labels_rest_part.Data<float>(), labels_new_part.Data<float>() }, axis: 0));*/
  71. return (images_new_part, labels_new_part);
  72. }
  73. else
  74. {
  75. index_in_epoch += batch_size;
  76. var end = index_in_epoch;
  77. return (data[np.arange(start, end)], labels[np.arange(start, end)]);
  78. }
  79. }
  80. }
  81. }