-
Notifications
You must be signed in to change notification settings - Fork 51
/
cifar.sc
163 lines (142 loc) · 4.34 KB
/
cifar.sc
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
{
import _root_.ammonite.ops._
import _root_.io.github.tailhq.dynaml.pipes.DataPipe
import _root_.io.github.tailhq.dynaml.tensorflow.{
dtflearn,
dtfutils,
dtfdata,
dtfpipe
}
import _root_.org.platanios.tensorflow.api._
import _root_.org.platanios.tensorflow.api.learn.layers.Activation
import _root_.org.platanios.tensorflow.data.image.CIFARLoader
import _root_.java.nio.file.Paths
val tempdir = home / "tmp"
val dataSet =
CIFARLoader.load(Paths.get(tempdir.toString()), CIFARLoader.CIFAR_10)
val dtf_cifar_data = dtfdata.tf_dataset(
dtfdata.supervised_dataset(
dataSet.trainImages.unstack(axis = 0),
dataSet.trainLabels.castTo[Long].unstack(axis = -1)
),
dtfdata.supervised_dataset(
dataSet.testImages.unstack(axis = 0),
dataSet.testLabels.castTo[Long].unstack(axis = -1)
)
)
println("Building the model.")
val relu_act =
DataPipe[String, Activation[Float]]((x: String) => tf.learn.ReLU[Float](x))
val architecture =
tf.learn.Cast[UByte, Float]("Input/Cast") >>
dtflearn.inception_unit[Float](channels = 3, Seq.fill(4)(10), relu_act)(
layer_index = 1
) >>
dtflearn.inception_unit[Float](channels = 40, Seq.fill(4)(5), relu_act)(
layer_index = 2
) >>
tf.learn.Flatten[Float]("Layer_3/Flatten") >>
dtflearn.feedforward[Float](256)(id = 4) >>
tf.learn.ReLU[Float]("Layer_4/ReLU", 0.1f) >>
dtflearn.feedforward[Float](10)(id = 5)
val loss = tf.learn.SparseSoftmaxCrossEntropy[Float, Long, Float](
"Loss/CrossEntropy"
) >>
tf.learn.Mean("Loss/Mean") >>
tf.learn.ScalarSummary("Loss/Summary", "Loss")
val optimizer = tf.train.Adam(0.1f)
val cifar_model =
dtflearn.model[
Output[UByte], Output[Long], Output[Float], Float,
Tensor[UByte], UINT8, Shape,
Tensor[Long], INT64, Shape,
Tensor[Float], FLOAT32, Shape](
architecture,
(UINT8, dataSet.trainImages.shape(1 ::)),
(INT64, Shape()),
loss
)
val data_ops = dtflearn.model.data_ops[(Output[UByte], Output[Long])](
shuffleBuffer = 5000,
batchSize = 128,
prefetchSize = 10
)
val train_config = dtflearn.model.trainConfig(
tempdir / "cifar_summaries",
data_ops,
optimizer,
dtflearn.rel_loss_change_stop(0.05, 500),
Some(
dtflearn.model._train_hooks(
tempdir / "cifar_summaries",
stepRateFreq = 100,
summarySaveFreq = 100,
checkPointFreq = 100
)
)
)
val pattern_to_tensor =
DataPipe[Seq[(Tensor[UByte], Tensor[Long])], (Tensor[UByte], Tensor[Long])](
ds => {
val (xs, ys) = ds.unzip
(
dtfpipe.EagerStack[UByte](axis = 0).run(xs),
dtfpipe.EagerStack[Long](axis = 0).run(ys)
)
}
)
val data_handle_ops = dtflearn.model.tf_data_handle_ops[
(Tensor[UByte], Tensor[Long]),
(Tensor[UByte], Tensor[Long]),
Tensor[Float],
(Output[UByte], Output[Long])
](
bufferSize = 500,
patternToTensor = Some(pattern_to_tensor),
concatOpO = Some(dtfpipe.EagerConcatenate[Float]())
)
val data_handle_ops_infer =
dtflearn.model.tf_data_handle_ops[Tensor[UByte], Tensor[UByte], Tensor[
Float
], Output[UByte]](
bufferSize = 1000,
patternToTensor = Some(dtfpipe.EagerStack[UByte](axis = 0)),
concatOpO = Some(dtfpipe.EagerConcatenate[Float]())
)
cifar_model.train(
dtf_cifar_data.training_dataset,
train_config,
data_handle_ops
)
def accuracy(predictions: Tensor[Long], labels: Tensor[Long]): Float =
tfi
.equal(predictions.argmax[Long](1), labels)
.castTo[Float]
.mean()
.scalar
.asInstanceOf[Float]
val (trainingPreds, testPreds): (Tensor[Float], Tensor[Float]) = (
cifar_model
.infer_batch(
dtf_cifar_data.training_dataset.map(p => p._1),
data_handle_ops_infer
)
.left
.get,
cifar_model
.infer_batch(
dtf_cifar_data.test_dataset.map(p => p._1),
data_handle_ops_infer
)
.left
.get
)
val (trainAccuracy, testAccuracy) = (
accuracy(trainingPreds.castTo[Long], dataSet.trainLabels.castTo[Long]),
accuracy(testPreds.castTo[Long], dataSet.testLabels.castTo[Long])
)
print("Train accuracy = ")
pprint.pprintln(trainAccuracy)
print("Test accuracy = ")
pprint.pprintln(testAccuracy)
}