mnistreader.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package training
  26. import (
  27. "encoding/binary"
  28. "io"
  29. "log"
  30. "os"
  31. mat "gonum.org/v1/gonum/mat"
  32. )
  33. type mnistReader struct {
  34. dataFilename string
  35. resultsFilename string
  36. validatorFilename string
  37. validatorResultsFilename string
  38. dataCount int
  39. validatorCount int
  40. imageSize int
  41. buffered *mat.Dense
  42. resultsBuffered *mat.Dense
  43. bufferedValidation *mat.Dense
  44. resultsBufferedValidation *mat.Dense
  45. }
  46. func NewMNISTReader(dataFilename string, resultsFilename string, validatorFilename string, validatorResultsFilename string) (r *mnistReader) {
  47. r = &mnistReader{}
  48. r.dataCount, r.imageSize = openFileSet(dataFilename, resultsFilename)
  49. r.validatorCount, _ = openFileSet(validatorFilename, validatorResultsFilename)
  50. if r.dataCount <= 0 || r.imageSize <= 0 || r.validatorCount <= 0 {
  51. return nil
  52. }
  53. return
  54. }
  55. func (r *mnistReader) GetData(i int) (*mat.Dense, *mat.Dense) {
  56. if r.dataCount <= i {
  57. return nil, nil
  58. }
  59. return r.readData(r.dataFilename, r.resultsFilename, i)
  60. }
  61. func (r *mnistReader) DataCount() int {
  62. return r.dataCount
  63. }
  64. func (r *mnistReader) GetValidator(i int) (data *mat.Dense, result *mat.Dense) {
  65. if r.validatorCount <= i {
  66. return nil, nil
  67. }
  68. return r.readData(r.validatorFilename, r.validatorResultsFilename, i)
  69. }
  70. func (r *mnistReader) ValidatorCount() int {
  71. return r.validatorCount
  72. }
  73. func (r *mnistReader) readData(data string, result string, i int) (buffered, resultsBuffered *mat.Dense) {
  74. file, err := os.Open(data)
  75. if err != nil {
  76. return nil, nil
  77. }
  78. defer file.Close()
  79. resultsFile, err := os.Open(result)
  80. if err != nil {
  81. return nil, nil
  82. }
  83. defer resultsFile.Close()
  84. file.Seek(16+int64(r.imageSize*i), 0)
  85. resultsFile.Seek(8+int64(i), 0)
  86. buffer := make([]byte, r.imageSize)
  87. _, err = file.Read(buffer)
  88. if err == io.EOF {
  89. return nil, nil
  90. } else if err != nil {
  91. log.Fatal("File read error\n")
  92. }
  93. values := make([]float64, r.imageSize)
  94. for i, v := range buffer {
  95. values[i] = float64(v) / 255.0
  96. }
  97. buffered = mat.NewDense(r.imageSize, 1, values)
  98. buffer = make([]byte, 1)
  99. _, err = resultsFile.Read(buffer)
  100. if err != nil {
  101. log.Fatal("Result file read error\n")
  102. }
  103. num := int(buffer[0])
  104. resultsBuffered = mat.NewDense(10, 1, nil)
  105. resultsBuffered.Set(num, 0, 1.0)
  106. return buffered, resultsBuffered
  107. }
  108. func openFileSet(dataFilename string, resultsFilename string) (count int, imageSize int) {
  109. var err error
  110. data, err := os.Open(dataFilename)
  111. if err != nil {
  112. return -1, -1
  113. }
  114. defer data.Close()
  115. result, err := os.Open(resultsFilename)
  116. if err != nil {
  117. return -1, -1
  118. }
  119. defer result.Close()
  120. buffer := make([]byte, 16)
  121. data.Read(buffer)
  122. header := binary.BigEndian.Uint32(buffer[:4])
  123. if header != 0x00000803 {
  124. return -1, -1
  125. }
  126. count = int(binary.BigEndian.Uint32(buffer[4:8]))
  127. imageSize = int(binary.BigEndian.Uint32(buffer[8:12])) * int(binary.BigEndian.Uint32(buffer[12:16]))
  128. buffer = make([]byte, 8)
  129. result.Read(buffer)
  130. header = binary.BigEndian.Uint32(buffer[0:4])
  131. if header != 0x00000801 {
  132. return -1, -1
  133. }
  134. resultsCount := int(binary.BigEndian.Uint32(buffer[4:8]))
  135. if resultsCount != count {
  136. return -1, -1
  137. }
  138. return
  139. }