mnistreader.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. r.dataFilename = dataFilename
  51. r.resultsFilename = resultsFilename
  52. r.validatorFilename = validatorFilename
  53. r.validatorResultsFilename = validatorResultsFilename
  54. if r.dataCount <= 0 || r.imageSize <= 0 || r.validatorCount <= 0 {
  55. return nil
  56. }
  57. return
  58. }
  59. func (r *mnistReader) GetData(i int) (*mat.Dense, *mat.Dense) {
  60. if r.dataCount <= i {
  61. return nil, nil
  62. }
  63. return r.readData(r.dataFilename, r.resultsFilename, i)
  64. }
  65. func (r *mnistReader) DataCount() int {
  66. return r.dataCount
  67. }
  68. func (r *mnistReader) GetValidator(i int) (data *mat.Dense, result *mat.Dense) {
  69. if r.validatorCount <= i {
  70. return nil, nil
  71. }
  72. return r.readData(r.validatorFilename, r.validatorResultsFilename, i)
  73. }
  74. func (r *mnistReader) ValidatorCount() int {
  75. return r.validatorCount
  76. }
  77. func (r *mnistReader) readData(data string, result string, i int) (buffered, resultsBuffered *mat.Dense) {
  78. file, err := os.Open(data)
  79. if err != nil {
  80. log.Fatalf("Could not open data file %v\n", data)
  81. return nil, nil
  82. }
  83. defer file.Close()
  84. resultsFile, err := os.Open(result)
  85. if err != nil {
  86. log.Fatalf("Could not open result file %v\n", result)
  87. return nil, nil
  88. }
  89. defer resultsFile.Close()
  90. file.Seek(16+int64(r.imageSize*i), 0)
  91. resultsFile.Seek(8+int64(i), 0)
  92. buffer := make([]byte, r.imageSize)
  93. _, err = file.Read(buffer)
  94. if err == io.EOF {
  95. log.Fatal("EOF reached but shouldn't\n")
  96. return nil, nil
  97. } else if err != nil {
  98. log.Fatal("File read error\n")
  99. return nil, nil
  100. }
  101. values := make([]float64, r.imageSize)
  102. for i, v := range buffer {
  103. values[i] = float64(v) / 255.0
  104. }
  105. buffered = mat.NewDense(r.imageSize, 1, values)
  106. buffer = make([]byte, 1)
  107. _, err = resultsFile.Read(buffer)
  108. if err != nil {
  109. log.Fatal("Result file read error\n")
  110. }
  111. num := int(buffer[0])
  112. resultsBuffered = mat.NewDense(10, 1, nil)
  113. resultsBuffered.Set(num, 0, 1.0)
  114. return buffered, resultsBuffered
  115. }
  116. func openFileSet(dataFilename string, resultsFilename string) (count int, imageSize int) {
  117. var err error
  118. data, err := os.Open(dataFilename)
  119. if err != nil {
  120. return -1, -1
  121. }
  122. defer data.Close()
  123. result, err := os.Open(resultsFilename)
  124. if err != nil {
  125. return -1, -1
  126. }
  127. defer result.Close()
  128. buffer := make([]byte, 16)
  129. data.Read(buffer)
  130. header := binary.BigEndian.Uint32(buffer[:4])
  131. if header != 0x00000803 {
  132. return -1, -1
  133. }
  134. count = int(binary.BigEndian.Uint32(buffer[4:8]))
  135. imageSize = int(binary.BigEndian.Uint32(buffer[8:12])) * int(binary.BigEndian.Uint32(buffer[12:16]))
  136. buffer = make([]byte, 8)
  137. result.Read(buffer)
  138. header = binary.BigEndian.Uint32(buffer[0:4])
  139. if header != 0x00000801 {
  140. return -1, -1
  141. }
  142. resultsCount := int(binary.BigEndian.Uint32(buffer[4:8]))
  143. if resultsCount != count {
  144. return -1, -1
  145. }
  146. return
  147. }