-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathmain.go
164 lines (131 loc) · 3.9 KB
/
main.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
package main
// Training various models on the CIFAR-10 dataset.
//
// The dataset can be downloaded from https:www.cs.toronto.edu/~kriz/cifar.html, files
// should be placed in the data/ directory.
//
// The resnet model reaches 95.4% accuracy.
import (
"fmt"
"log"
"time"
"github.com/sugarme/gotch"
"github.com/sugarme/gotch/nn"
"github.com/sugarme/gotch/ts"
"github.com/sugarme/gotch/vision"
)
func convBn(p *nn.Path, cIn, cOut int64) *nn.SequentialT {
config := nn.DefaultConv2DConfig()
config.Padding = []int64{1, 1}
config.Bias = false
seq := nn.SeqT()
seq.Add(nn.NewConv2D(p, cIn, cOut, 3, config))
seq.Add(nn.BatchNorm2D(p, cOut, nn.DefaultBatchNormConfig()))
seq.AddFn(nn.NewFunc(func(xs *ts.Tensor) *ts.Tensor {
return xs.MustRelu(false)
}))
return seq
}
func layer(p *nn.Path, cIn, cOut int64) nn.FuncT {
pre := convBn(p.Sub("pre"), cIn, cOut)
block1 := convBn(p.Sub("b1"), cOut, cOut)
block2 := convBn(p.Sub("b2"), cOut, cOut)
return nn.NewFuncT(func(xs *ts.Tensor, train bool) *ts.Tensor {
tmp1 := xs.ApplyT(pre, train)
preTs := tmp1.MaxPool2DDefault(2, true)
tmp2 := preTs.ApplyT(block1, train)
ys := tmp2.ApplyT(block2, train)
tmp2.MustDrop()
res := preTs.MustAdd(ys, true)
ys.MustDrop()
return res
})
}
func fastResnet(p *nn.Path) *nn.SequentialT {
seq := nn.SeqT()
seq.Add(convBn(p.Sub("pre"), 3, 64))
seq.Add(layer(p.Sub("layer1"), 64, 128))
seq.Add(convBn(p.Sub("inter"), 128, 256))
seq.AddFn(nn.NewFunc(func(xs *ts.Tensor) *ts.Tensor {
return xs.MaxPool2DDefault(2, false)
}))
seq.Add(layer(p.Sub("layer2"), 256, 512))
seq.AddFn(nn.NewFunc(func(xs *ts.Tensor) *ts.Tensor {
tmp := xs.MaxPool2DDefault(4, false)
res := tmp.FlatView()
tmp.MustDrop()
return res
}))
seq.Add(nn.NewLinear(p.Sub("linear"), 512, 10, nn.DefaultLinearConfig()))
seq.AddFn(nn.NewFunc(func(xs *ts.Tensor) *ts.Tensor {
return xs.MustMulScalar(ts.FloatScalar(0.125), false)
}))
return seq
}
func main() {
dir := "../../data/cifar10"
ds := vision.CFLoadDir(dir)
fmt.Printf("TrainImages shape: %v\n", ds.TrainImages.MustSize())
fmt.Printf("TrainLabel shape: %v\n", ds.TrainLabels.MustSize())
fmt.Printf("TestImages shape: %v\n", ds.TestImages.MustSize())
fmt.Printf("TestLabel shape: %v\n", ds.TestLabels.MustSize())
fmt.Printf("Number of labels: %v\n", ds.Labels)
// device := gotch.CPU
device := gotch.NewCuda().CudaIfAvailable()
vs := nn.NewVarStore(device)
net := fastResnet(vs.Root())
var lossVal float64
startTime := time.Now()
var bestAccuracy float64
for epoch := 0; epoch < 150; epoch++ {
optConfig := nn.NewSGDConfig(0.9, 0.0, 5e-4, true)
var (
opt *nn.Optimizer
err error
)
switch {
case epoch < 50:
opt, err = optConfig.Build(vs, 0.1)
if err != nil {
log.Fatal(err)
}
case epoch < 100:
opt, err = optConfig.Build(vs, 0.01)
if err != nil {
log.Fatal(err)
}
case epoch >= 100:
opt, err = optConfig.Build(vs, 0.001)
if err != nil {
log.Fatal(err)
}
}
iter := ts.MustNewIter2(ds.TrainImages, ds.TrainLabels, int64(64))
iter.Shuffle()
for {
item, ok := iter.Next()
if !ok {
break
}
devicedData := item.Data.MustTo(vs.Device(), true)
devicedLabel := item.Label.MustTo(vs.Device(), true)
bimages := vision.Augmentation(devicedData, true, 4, 8)
logits := net.ForwardT(bimages, true)
loss := logits.CrossEntropyForLogits(devicedLabel)
opt.BackwardStep(loss)
lossVal = loss.Float64Values()[0]
devicedData.MustDrop()
devicedLabel.MustDrop()
bimages.MustDrop()
loss.MustDrop()
}
testAcc := nn.BatchAccuracyForLogits(vs, net, ds.TestImages, ds.TestLabels, vs.Device(), 512)
fmt.Printf("Epoch:\t %v\t Loss: \t %.3f \tAcc: %10.2f%%\n", epoch, lossVal, testAcc*100.0)
if testAcc > bestAccuracy {
bestAccuracy = testAcc
}
iter.Drop()
}
fmt.Printf("Best Accuracy: %10.2f%%\n", bestAccuracy*100.0)
fmt.Printf("Taken time:\t%.2f mins\n", time.Since(startTime).Minutes())
}