-
Notifications
You must be signed in to change notification settings - Fork 1
/
mnist.go
223 lines (191 loc) · 7.41 KB
/
mnist.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package main
import (
"fmt"
"image"
"math/rand"
"github.com/dereklstinson/gocunets/devices/gpu/nvidia/cudnn"
"github.com/dereklstinson/gocunets/layers"
"github.com/dereklstinson/gocunets/loss"
"github.com/dereklstinson/gocunets/testing/mnist/dfuncs"
"github.com/dereklstinson/gocunets/testing/mnistdcauto/dcnetworks"
"github.com/dereklstinson/gocunets/utils"
"github.com/dereklstinson/gocunets/utils/filing"
"github.com/dereklstinson/gocunets/utils/imaging"
gocudnn "github.com/dereklstinson/gocudnn"
"github.com/nfnt/resize"
)
func main() {
network()
}
func network() {
//Setting up GPU and Handles and Steams
gocudnn.Cuda{}.LockHostThread()
devs, err := gocudnn.Cuda{}.GetDeviceList()
utils.CheckError(err)
utils.CheckError(devs[0].Set())
handle := cudnn.CreateHandler(devs[0], "/home/derek/go/src/github.com/dereklstinson/gocudnn/kernels/")
//stream, err := gocudnn.Cuda{}.CreateBlockingStream()
// utils.CheckError(handles.SetStream(stream))
stream, err := gocudnn.Cuda{}.CreateBlockingStream()
utils.CheckError(err)
utils.CheckError(handle.SetStream(stream))
//Flag managers
var dataflag cudnn.DataTypeFlag
var convflag gocudnn.ConvolutionFlags
var fflag cudnn.TensorFormatFlag
//Data Locations
const filedirectory = "../mnist/files/"
const mnistfilelabel = "train-labels.idx1-ubyte"
const mnistimage = "train-images.idx3-ubyte"
const imagesave = "/home/derek/Desktop/DCAutoEncoderGif/"
//Load the mnist data
mnistdata, err := dfuncs.LoadMNIST(filedirectory, mnistfilelabel, mnistimage)
utils.CheckError(err)
//Normalize The Data
avg := dfuncs.FindAverage(mnistdata)
mnistdata = dfuncs.NormalizeData(mnistdata, avg)
//Organize the batches into batches of 0 to 9 so that batchsize will be 10
sectioned := makenumbers(mnistdata)
//Make the batch up the batches. this would be number of runs for an epoc
batchesofinputbatches := makeinputbatches(sectioned)
fmt.Println("Number of Runs: ", len(batchesofinputbatches))
//Make Autoencoder network
AutoEncoder := dcnetworks.DCAutoReverse(handle, fflag.NCHW(), dataflag.Float(), convflag.Mode.CrossCorrelation(), true, 10)
//Set the AutoEncoderNetwork hidden layer algo
utils.CheckError(AutoEncoder.DynamicHidden())
//Load the batches into gpu mem this is basically the Arabic numbers are place in arabicoutput.T() and arabicnums.DeltaT()
arabicoutput, arabicnums := putintogpumem(batchesofinputbatches, fflag.NCHW(), dataflag.Float(), []int32{10, 1, 28, 28}, true)
//Make an imager so we can visually see the progress
imager, err := imaging.MakeImager(handle)
utils.CheckError(err)
//set the number of epocs
epocs := 100
snapshotsize := 100
//Set the Loss Calculator. This is Mean Square Error
MSE, err := loss.CreateMSECalculatorGPU(handle, true)
utils.CheckError(err)
//Need this memory as an inbetween for the Autoencoder and Loss Function so that it can return the errors to the autoencoder
fconout, err := layers.BuildIO(fflag.NCHW(), dataflag.Float(), []int32{10, 1, 28, 28}, true)
utils.CheckError(err)
//Need this to reshape the output of the autoencoder into something the imager can use to make an image.Image
imagerlayer, err := layers.BuildIO(fflag.NCHW(), dataflag.Float(), []int32{10, 1, 28, 28}, true)
utils.CheckError(err)
workspace, err := gocudnn.UnifiedMangedGlobal(gocudnn.SizeT(1000 * 1000 * 50))
utils.CheckError(err)
totalrunimage := make([]image.Image, 0)
for i := 0; i < epocs; i++ {
giffer := imaging.NewGiffer(0, 1) //giffer stacks a bunch of images and puts them into a gif
images := make([]image.Image, 0)
//Making a lossaray to calculate the loss per batch
epocloss := float32(0)
for j := range arabicnums {
stream.Sync()
utils.CheckError(AutoEncoder.ForwardProp(handle, workspace, arabicnums[j], arabicoutput[j]))
stream.Sync()
//Load the outputs from autoencoder into fconout
fconout.LoadTValues(arabicoutput[j].T().Memer())
stream.Sync()
//arabicout contains the the output of the autoencoder in its T() and target values in its DeltaT() fconout will get the errors from the loss function in its DeltaT()
MSE.ErrorGPU(handle, fconout, arabicoutput[j])
stream.Sync()
//MSE.Loss() just returns the loss calculated in MSE.ErrorGPU. MSE.ErrorGPU doesn't return return the loss it just stores it.
epocloss += MSE.Loss()
utils.CheckError(err)
stream.Sync()
//BackProp those errors put into fconout back through the auto encoder
utils.CheckError(AutoEncoder.BackPropFilterData(handle, workspace, arabicnums[j], fconout))
stream.Sync()
//Update the weights
utils.CheckError(AutoEncoder.UpdateWeights(handle, 10))
stream.Sync()
if j%snapshotsize == 0 {
utils.CheckError(AutoEncoder.ForwardProp(handle, workspace, arabicnums[0], arabicoutput[0]))
imagerlayer.LoadTValues(arabicoutput[0].T().Memer())
stream.Sync()
outputimage, err := imager.TileBatches(handle, imagerlayer, 2, 5, 28, 28)
utils.CheckError(err)
images = append(images, outputimage)
// fmt.Println("Grabbing Image:", j)
stream.Sync()
}
}
somenewimages := make([]image.Image, len(images))
for j := range images {
somenewimages[j] = resize.Resize(0, 280, images[j], resize.NearestNeighbor)
}
totalrunimage = append(totalrunimage, somenewimages...)
epocloss /= float32(len(arabicnums))
stream.Sync()
fmt.Println("At Epoc: ", i, "Loss is :", epocloss)
if epocloss <= 10.5 || i >= 10 {
fmt.Println("HIT 11.5 Loss")
giffer.MakeGrayGif(totalrunimage)
fmt.Println("Writing GIF")
utils.CheckError(filing.WritetoHD(imagesave, "AutoDCresize0", giffer))
// utils.CheckError(filing.WriteImage(imagesave, "AutoEncoder"+number, outputimage))
fmt.Println("Done Writing GIF")
devs[0].Reset()
return
}
// shuffle(arabicnums, arabicoutput)
}
devs[0].Reset()
}
func putintogpumem(arabic [][]float32, frmt cudnn.TensorFormat, dtype cudnn.DataType, dimsarabic []int32, memmanaged bool) (output, runs []*layers.IO) {
var err error
runs = make([]*layers.IO, len(arabic))
output = make([]*layers.IO, len(arabic))
for i := range arabic {
runs[i], err = layers.BuildNetworkInputIO(frmt, dtype, dimsarabic, memmanaged)
utils.CheckError(err)
ptr, err := gocudnn.MakeGoPointer(arabic[i])
utils.CheckError(err)
utils.CheckError(runs[i].LoadTValues(ptr))
output[i], err = layers.BuildIO(frmt, dtype, dimsarabic, memmanaged)
utils.CheckError(err)
utils.CheckError(output[i].LoadDeltaTValues(ptr))
}
return output, runs
}
func shuffle(runs, outputs []*layers.IO) {
rand.Shuffle(len(runs), func(i, j int) {
runs[i], runs[j] = runs[j], runs[i]
outputs[i], outputs[j] = outputs[j], outputs[i]
})
}
func makeinputbatches(sections []number) [][]float32 {
min := int(9999999)
for i := range sections {
if min > len(sections[i].mnist) {
min = len(sections[i].mnist)
}
}
numofbatches := min
fmt.Println(numofbatches)
numinbatches := len(sections)
fmt.Println(numinbatches)
batches := make([][]float32, numofbatches)
imgsize := 28 * 28
for i := range batches {
batches[i] = make([]float32, numinbatches*imgsize)
}
for i := range sections {
for j := 0; j < numofbatches; j++ {
for k := range sections[i].mnist[j].Data {
batches[j][i*imgsize+k] = sections[i].mnist[j].Data[k]
}
}
}
return batches
}
func makenumbers(mnist []dfuncs.LabeledData) []number {
sections := make([]number, 10)
for i := range mnist {
nmbr := mnist[i].Number
sections[nmbr].mnist = append(sections[nmbr].mnist, mnist[i])
}
return sections
}
type number struct {
mnist []dfuncs.LabeledData
}