-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdata.go
More file actions
76 lines (68 loc) · 1.94 KB
/
Copy pathdata.go
File metadata and controls
76 lines (68 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
//
import (
"os"
"fmt"
"io"
"encoding/binary"
)
//
//read mnist labels from byte file
func DataLoadLabels(r io.Reader) ([]byte) {
header := [2]int32{}
binary.Read(r, binary.BigEndian, &header)
labels := make([]byte, header[1])
r.Read(labels)
return labels
}
//read mnist images from byte file
func DataLoadImages(r io.Reader) ([][]byte, int, int) {
header := [4]int32{}
binary.Read(r, binary.BigEndian, &header)
images := make([][]byte, header[1])
width, height := int(header[2]), int(header[3])
for i := 0; i < len(images); i++ {
images[i] = make([]byte, width * height)
r.Read(images[i])
}
return images, width, height
}
func OpenFile( path string ) *os.File {
file, err := os.Open(path)
if (err != nil) {
fmt.Println(err)
os.Exit(-1)
}
return file
}
func PreprocessGlobalContrastStandardisation( M [][]byte, scalar float64 ) [][]float64 {
// Needs to be rewritten to take global mean.
fmt.Println("\tPreprocessing data: Global Contrast Normalisation")
rows := len(M)
output := make([][]float64,rows)
for i:=0; i<rows; i++ {
output[i] = make([]float64,len(M[i]))
for j:=0; j<len(M[i]); j++ {
output[i][j] = float64(M[i][j]) / scalar
}
}
return output
}
func PreprocessLabelsToVector( labels []byte, nClasses int ) [][]float64 {
// Labels is a vector holding the actual digit-labels.
// this is specifically for the MNIST data
nRows := len(labels)
output := make([][]float64,nRows)
for i:=0; i<nRows; i++ {
//create the array for the array 'result' to hold
tmp := make([]float64,nClasses)
// Now set the actual index to 1.
if labels[i] == 10 { // Backup in case you have the dataset where 0 are encoded by 10s.
tmp[0] = 1.0
} else {
tmp[labels[i]] = 1.0
}
output[i] = tmp
}
return output
}