/
cifar.go
96 lines (82 loc) · 2.21 KB
/
cifar.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cifar
import (
"io/ioutil"
"log"
"math"
"os"
"path/filepath"
"gorgonia.org/tensor"
)
const numLabels = 10
const pixelRange = 255
func pixelWeight(px byte) float64 {
retVal := float64(px)/pixelRange*0.9 + 0.1
if retVal == 1.0 {
return 0.999
}
return retVal
}
func reversePixelWeight(px float64) byte {
// return byte((pixelRange*px - pixelRange) / 0.9)
return byte(pixelRange*math.Min(0.99, math.Max(0.01, px)) - pixelRange)
}
// Load function for cifar
// typ can be "train" or "test"
// loc should be where the CIFAR-10 files can be found
func Load(typ, loc string) (inputs, targets tensor.Tensor, err error) {
// cifar-10 comes in 6 separate binary files
var arrayFiles []string
switch typ {
case "train":
arrayFiles = []string{
"train/data_batch_1.bin",
"train/data_batch_2.bin",
"train/data_batch_3.bin",
"train/data_batch_4.bin",
"train/data_batch_5.bin",
}
case "test":
arrayFiles = []string{
"test/test_batch.bin",
}
}
// create slices to store our data
var labelSlice []uint8
var imageSlice []float64
// each binary file comes formatted in 3073 byte groups
// 1 byte for the class
// 32 by 32 bytes for each of the red, green and blue pixel colour values
for _, targetFile := range arrayFiles {
f, err := os.Open(filepath.Join(loc, targetFile))
if err != nil {
log.Fatal(err)
}
defer f.Close()
cifar, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
for index, element := range cifar {
if index%3073 == 0 {
labelSlice = append(labelSlice, uint8(element))
} else {
imageSlice = append(imageSlice, pixelWeight(element))
}
}
}
// transform label slice into the necessary format
labelBacking := make([]float64, len(labelSlice)*numLabels, len(labelSlice)*numLabels)
labelBacking = labelBacking[:0]
for i := 0; i < len(labelSlice); i++ {
for j := 0; j < numLabels; j++ {
if j == int(labelSlice[i]) {
labelBacking = append(labelBacking, 0.9)
} else {
labelBacking = append(labelBacking, 0.1)
}
}
}
inputs = tensor.New(tensor.WithShape(len(labelSlice), 3, 32, 32), tensor.WithBacking(imageSlice))
targets = tensor.New(tensor.WithShape(len(labelSlice), numLabels), tensor.WithBacking(labelBacking))
return
}