-
Notifications
You must be signed in to change notification settings - Fork 2
/
VGGExample.swift
521 lines (441 loc) · 16.4 KB
/
VGGExample.swift
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
//
// VGGExample.swift
// GrAIExamples
//
// Created by Jean-François Reboud on 17/10/2022.
//
import XCTest
import GrAIdient
/// Train a simple VGG model on the CIFAR dataset.
final class VGGExample: XCTestCase
{
/// Directory to dump outputs from the tests.
let _outputDir = NSTemporaryDirectory()
/// Batch size of data.
let _batchSize = 64
/// Size of one image (height and width are the same).
let _size = 32
/// Mean of the preprocessing to apply to data.
let _mean: (Float, Float, Float) = (123.675, 116.28, 103.53)
/// Deviation of the preprocessing to apply to data.
let _std: (Float, Float, Float) = (58.395, 57.12, 57.375)
/// Initialize test.
override func setUp()
{
setPythonLib()
_ = MetalKernel.get
GrAI.Opti.GPU = true
GrAI.Precision.float = true
}
///
/// Get optimizer parameters for model training.
///
/// - Parameter nbLoops: Number of steps per epoch.
/// - Returns: The optimizer parameters.
///
func _getOptimizerParams(nbLoops: Int) -> GrAI.Optimizer.Params
{
var optimizerParams = GrAI.Optimizer.Params()
optimizerParams.nbLoops = nbLoops
// Simple optimizer scheduler: always the same optimizer during
// the training.
optimizerParams.optimizer = ConstEpochsScheduler(
GrAI.Optimizer.Class.AdamRectified
)
// Simple variable scheduler: always the same variable during
// the training.
optimizerParams.variables["alpha"] = ConstEpochsVar(
value: ConstVal(1e-3)
)
optimizerParams.variables["lambda"] = ConstEpochsVar(
value: ConstVal(1e-6)
)
// Other schedulers can be built thanks to `GrAI.Optimizer.Params`.
return optimizerParams
}
///
/// Build a simple model.
///
/// - Parameter bn: Whether to use batch normalization or not.
/// - Returns: The model built.
///
func _buildModel(bn: Bool) -> Model
{
// Create the context to build a graph of layers where
// there is no previous model dependency: layer id starts at 0.
let context = ModelContext(name: "VGG", models: [])
let params = GrAI.Model.Params(context: context)
var layer: Layer2D
layer = Input2D(
nbChannels: 3,
width: _size, height: _size,
params: params
)
layer = MaxPool2D(layerPrev: layer, size: 2, stride: 2, params: params)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
layer = MaxPool2D(layerPrev: layer, size: 2, stride: 2, params: params)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
layer = MaxPool2D(layerPrev: layer, size: 2, stride: 2, params: params)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
layer = MaxPool2D(layerPrev: layer, size: 2, stride: 2, params: params)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
layer = Convolution2D(
layerPrev: layer, size: 3, nbChannels: 8, stride: 1,
activation: ReLU.str, biases: true, bn: bn,
params: params
)
var head: Layer1D = AvgPool2D(layerPrev: layer, params: params)
head = try! FullyConnected(
layerPrev: head, nbNeurons: 1,
activation: ReLU.str, biases: true, params: params
)
_ = MSE1D(layerPrev: head, params: params)
// Retrieve base model in the context and initialize a
// real model (with `layerPrev` links updated).
return Model(model: context.model, modelsPrev: [])
}
///
/// Load a model from the disk.
///
/// - Parameter modelPath: The model path on the disk.
/// - Returns: the model loaded.
///
func _loadModel(_ modelPath: String) -> Model
{
// Load model from the disk.
let data = try! Data(
contentsOf: URL(fileURLWithPath: modelPath)
)
// Decode it as a base model
// (model where `layerPrev` links are not initialized).
let baseModel = try! PropertyListDecoder().decode(
BaseModel.self,
from: data
)
// Create a model with initialized links
// with no previous model dependencies.
let vgg = Model(model: baseModel, modelsPrev: [])
return vgg
}
///
/// Evaluate a model on the testing CIFAR dataset.
///
/// - Parameter model: The model to evaluate.
/// - Returns: The ratio (in percent) of good predictions.
///
func _evaluateModel(_ model: Model) -> Int
{
let cifar8 = CIFAR.loadDataset(
datasetPath: _outputDir + "/datasetTest8",
size: _size
)
let cifar5 = CIFAR.loadDataset(
datasetPath: _outputDir + "/datasetTest5",
size: _size
)
cifar8.initSamples(batchSize: _batchSize)
cifar5.initSamples(batchSize: _batchSize)
// We keep a subset of the dataset to have a quicker evaluation.
cifar8.keep(100)
cifar5.keep(100)
let firstLayer: Input2D = model.layers.first as! Input2D
let lastLayer: MSE1D = model.layers.last as! MSE1D
var nbRight = 0
var nbTotal = 0
var sampler: CIFAR = cifar8
var samples = sampler.getSamples()
for label in 0...1
{
if samples == nil
{
sampler = cifar5
samples = sampler.getSamples()
}
while samples != nil
{
// Pre processing.
let data = preprocess(
samples!,
height: _size,
width: _size,
mean: _mean,
std: _std,
imageFormat: .Neuron
)
// Update internal batch size.
model.updateKernel(batchSize: samples!.count)
// Set data.
try! firstLayer.setDataGPU(
data,
batchSize: samples!.count,
nbChannels: 3, height: _size, width: _size,
format: .Neuron
)
// Forward.
try! model.forward()
for elem in 0..<samples!.count
{
// Get result: 1 neuron.
let result: Float = lastLayer.getOutsGPU(elem: elem)[0]
if label == 0 && result < 0.5
{
nbRight += 1
}
else if label == 1 && result >= 0.5
{
nbRight += 1
}
nbTotal += 1
}
samples = sampler.getSamples()
}
}
let ratio = Int(Double(nbRight) / Double(nbTotal) * 100)
return ratio
}
/// Test1: dump CIFAR train and test datasets for labels 8 and 5.
func test1_DumpDataset()
{
CIFAR.dumpTrain(
datasetPath: _outputDir + "/datasetTrain8",
label: 8,
size: _size
)
CIFAR.dumpTrain(
datasetPath: _outputDir + "/datasetTrain5",
label: 5,
size: _size
)
CIFAR.dumpTest(
datasetPath: _outputDir + "/datasetTest8",
label: 8,
size: _size
)
CIFAR.dumpTest(
datasetPath: _outputDir + "/datasetTest5",
label: 5,
size: _size
)
}
/// Test2: dump CIFAR images for labels 8 and 5.
func test2_DumpImages()
{
let batchSize = 16
let cifar8 = CIFAR.loadDataset(
datasetPath: _outputDir + "/datasetTest8",
size: _size
)
let cifar5 = CIFAR.loadDataset(
datasetPath: _outputDir + "/datasetTest5",
size: _size
)
cifar8.initSamples(batchSize: batchSize)
cifar5.initSamples(batchSize: batchSize)
let samples8 = cifar8.getSamples()!
let samples5 = cifar5.getSamples()!
let pixels8 = Image.toRGB(samples8, width: _size, height: _size)
let pixels5 = Image.toRGB(samples5, width: _size, height: _size)
for elem in 0..<batchSize
{
var image = Image.buildImage(
pixels: pixels8[elem], width: _size, height: _size
)
try! image.save(
url: URL(fileURLWithPath: _outputDir + "CIFAR8_\(elem).png")
)
image = Image.buildImage(
pixels: pixels5[elem], width: _size, height: _size
)
try! image.save(
url: URL(fileURLWithPath: _outputDir + "CIFAR5_\(elem).png")
)
}
}
/// Test3: test that an untrained model makes bad predictions.
func test3_UntrainedModel()
{
// Build a model with randomly initialized weights.
let vgg = _buildModel(bn: true)
// Initialize for inference.
vgg.initKernel(phase: .Inference)
// Evaluate model on CIFAR testing dataset.
let ratio = _evaluateModel(vgg)
print(
"Ratio of good predictions: \(ratio)%."
)
// Encode the model.
let encoder = PropertyListEncoder()
let data = try! encoder.encode(vgg)
// Save it to the disk.
try! data.write(
to: URL(fileURLWithPath: _outputDir + "/vgg1.plist")
)
}
/// Test4: train a simple model.
func test4_TrainVGG()
{
let cifar8 = CIFAR.loadDataset(
datasetPath: _outputDir + "/datasetTrain8",
size: _size
)
let cifar5 = CIFAR.loadDataset(
datasetPath: _outputDir + "/datasetTrain5",
size: _size
)
// Get optimizer parameters for iterating over batch size elements.
let params = _getOptimizerParams(nbLoops: _batchSize)
// A batch will in fact be composed of half elements coming from
// cifar8 (ships => label: 0) and half elements coming from
// cifar5 (dogs => label: 1).
cifar8.initSamples(batchSize: _batchSize / 2)
cifar5.initSamples(batchSize: _batchSize / 2)
// Keep a subset of the dataset to have a quicker training.
cifar8.keep(500)
cifar5.keep(500)
// Small trick to force full batches throughout the training:
// this enables us to set the ground truth once and for all.
let nbWholeBatches =
cifar8.nbSamples / cifar8.batchSize * cifar8.batchSize
cifar8.keep(nbWholeBatches)
cifar5.keep(nbWholeBatches)
// Load previous model from the disk.
let vgg = _loadModel(_outputDir + "vgg1.plist")
// Initialize for training.
vgg.initialize(params: params, phase: .Training)
let firstLayer: Input2D = vgg.layers.first as! Input2D
let lastLayer: MSE1D = vgg.layers.last as! MSE1D
// Initialize the ground truth once and for all.
let groundTruth = FloatBuffer(
nbElems: _batchSize, deviceID: 0, shared: true
)
var gtBuffer = [Float](repeating: 0.0, count: _batchSize)
for elem in 0..<_batchSize / 2
{
gtBuffer[elem] = 0.0
}
for elem in _batchSize / 2..<_batchSize
{
gtBuffer[elem] = 1.0
}
groundTruth.initialize(array: >Buffer)
let nbEpochs = 5
for epoch in 0..<nbEpochs
{
print("EPOCH \(epoch + 1)/\(nbEpochs).")
cifar8.shuffle()
cifar5.shuffle()
for step in 0..<cifar8.nbLoops
{
let samples8 = cifar8.getSamples()!
let samples5 = cifar5.getSamples()!
let samples = samples8 + samples5
if samples.count != _batchSize
{
fatalError("Unreachable.")
}
// Pre processing.
let data = preprocess(
samples,
height: _size,
width: _size,
mean: _mean,
std: _std,
imageFormat: .Neuron
)
// Reset gradient validity for backward pass
// and update the batch size (although here it stays the same).
vgg.updateKernel(batchSize: _batchSize)
// Set data.
try! firstLayer.setDataGPU(
data,
batchSize: _batchSize,
nbChannels: 3, height: _size, width: _size,
format: .Neuron
)
// Forward.
try! vgg.forward()
// Apply loss derivative.
try! lastLayer.lossDerivativeGPU(
groundTruth,
batchSize: _batchSize,
nbNeurons: 1
)
// Backward.
try! vgg.backward()
// Update weights.
try! vgg.update()
// Get loss result.
// Note that backward is explicitly
// enabled by `applyGradient` whereas `getLoss` is
// just an indicator.
let loss = try! lastLayer.getLossGPU(
groundTruth,
batchSize: _batchSize,
nbNeurons: 1
)
print("Step \(step + 1)/\(cifar8.nbLoops): \(sqrt(loss)).")
// Update internal step.
// This is not mandatory except if we used another
// optimizer scheduler: see `_getOptimizerParams`.
vgg.incStep()
}
}
// Encode the trained model.
let encoder = PropertyListEncoder()
let data = try! encoder.encode(vgg)
// Save it to the disk.
try! data.write(
to: URL(fileURLWithPath: _outputDir + "/vgg2.plist")
)
}
/// Test5: test that the previous trained model makes better predictions than the untrained model.
func test5_CompareModels()
{
// Load previous model from the disk.
let vgg1 = _loadModel(_outputDir + "/vgg1.plist")
let vgg2 = _loadModel(_outputDir + "/vgg2.plist")
// Initialize for inference.
vgg1.initKernel(phase: .Inference)
vgg2.initKernel(phase: .Inference)
// Evaluate model on CIFAR testing dataset.
let ratio1 = _evaluateModel(vgg1)
let ratio2 = _evaluateModel(vgg2)
print(
"Ratio of good predictions before training: \(ratio1)%."
)
print(
"Ratio of good predictions after training: \(ratio2)%."
)
}
}