mnistreader.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 teach
  26. import (
  27. "encoding/binary"
  28. "fmt"
  29. "io"
  30. "log"
  31. "os"
  32. mat "gonum.org/v1/gonum/mat"
  33. )
  34. type MNISTReader struct {
  35. file *os.File
  36. resultsFile *os.File
  37. size int
  38. imageSize int
  39. buffered *mat.Dense
  40. resultsBuffered *mat.Dense
  41. }
  42. func NewMNISTReader(dataFilename string, resultsFilename string) (r *MNISTReader) {
  43. r = &MNISTReader{}
  44. var err error
  45. r.file, err = os.Open(dataFilename)
  46. if err != nil {
  47. return nil
  48. }
  49. r.resultsFile, err = os.Open(resultsFilename)
  50. if err != nil {
  51. return nil
  52. }
  53. buffer := make([]byte, 16)
  54. r.file.Read(buffer)
  55. header := binary.BigEndian.Uint32(buffer[:4])
  56. if header != 0x00000803 {
  57. return nil
  58. }
  59. r.size = int(binary.BigEndian.Uint32(buffer[4:8]))
  60. r.imageSize = int(binary.BigEndian.Uint32(buffer[8:12])) * int(binary.BigEndian.Uint32(buffer[12:16]))
  61. fmt.Printf("Image size: %v\n", r.imageSize)
  62. buffer = make([]byte, 8)
  63. r.resultsFile.Read(buffer)
  64. header = binary.BigEndian.Uint32(buffer[0:4])
  65. if header != 0x00000801 {
  66. return nil
  67. }
  68. resultsSize := int(binary.BigEndian.Uint32(buffer[4:8]))
  69. if resultsSize != r.size {
  70. return nil
  71. }
  72. return
  73. }
  74. func (r *MNISTReader) GetData() *mat.Dense {
  75. return r.buffered
  76. }
  77. func (r *MNISTReader) GetExpect() *mat.Dense {
  78. return r.resultsBuffered
  79. }
  80. func (r *MNISTReader) Next() bool {
  81. buffer := make([]byte, r.imageSize)
  82. _, err := r.file.Read(buffer)
  83. if err == io.EOF {
  84. r.file.Seek(16, 0)
  85. r.resultsFile.Seek(8, 0)
  86. return false
  87. } else if err != nil {
  88. log.Fatal("File read error\n")
  89. }
  90. values := make([]float64, r.imageSize)
  91. for i, v := range buffer {
  92. values[i] = float64(v) / 255.0
  93. }
  94. r.buffered = mat.NewDense(r.imageSize, 1, values)
  95. // values = make([]float64, len(values))
  96. // for i, v := range buffer {
  97. // if v > 0 {
  98. // values[i] = 1
  99. // } else {
  100. // values[i] = 0
  101. // }
  102. // }
  103. // squareDense := mat.NewDense(28, 28, values)
  104. // fmt.Printf("r.buffered:\n%v\n\n", mat.Formatted(squareDense, mat.Prefix(""), mat.Excerpt(0), mat.Squeeze()))
  105. buffer = make([]byte, 1)
  106. _, err = r.resultsFile.Read(buffer)
  107. if err != nil {
  108. log.Fatal("Result file read error\n")
  109. }
  110. num := int(buffer[0])
  111. r.resultsBuffered = mat.NewDense(10, 1, nil)
  112. r.resultsBuffered.Set(num, 0, 1.0)
  113. // fmt.Printf("r.resultsBuffered:\n%v\n\n", mat.Formatted(r.resultsBuffered, mat.Prefix(""), mat.Excerpt(0)))
  114. return true
  115. }
  116. func (r *MNISTReader) Reset() {
  117. r.file.Seek(16, 0)
  118. r.resultsFile.Seek(8, 0)
  119. }