-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathmain.swift
184 lines (152 loc) · 5.96 KB
/
main.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
// Copyright 2019 The TensorFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
import Datasets
import Foundation
import ModelSupport
import TensorFlow
let epochCount = 10
let batchSize = 32
let outputFolder = "./output/"
let imageHeight = 28
let imageWidth = 28
let imageSize = imageHeight * imageWidth
let latentSize = 64
// Models
struct Generator: Layer {
var dense1 = Dense<Float>(
inputSize: latentSize, outputSize: latentSize * 2,
activation: { leakyRelu($0) })
var dense2 = Dense<Float>(
inputSize: latentSize * 2, outputSize: latentSize * 4,
activation: { leakyRelu($0) })
var dense3 = Dense<Float>(
inputSize: latentSize * 4, outputSize: latentSize * 8,
activation: { leakyRelu($0) })
var dense4 = Dense<Float>(
inputSize: latentSize * 8, outputSize: imageSize,
activation: tanh)
var batchnorm1 = BatchNorm<Float>(featureCount: latentSize * 2)
var batchnorm2 = BatchNorm<Float>(featureCount: latentSize * 4)
var batchnorm3 = BatchNorm<Float>(featureCount: latentSize * 8)
@differentiable
func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
let x1 = batchnorm1(dense1(input))
let x2 = batchnorm2(dense2(x1))
let x3 = batchnorm3(dense3(x2))
return dense4(x3)
}
}
struct Discriminator: Layer {
var dense1 = Dense<Float>(
inputSize: imageSize, outputSize: 256,
activation: { leakyRelu($0) })
var dense2 = Dense<Float>(
inputSize: 256, outputSize: 64,
activation: { leakyRelu($0) })
var dense3 = Dense<Float>(
inputSize: 64, outputSize: 16,
activation: { leakyRelu($0) })
var dense4 = Dense<Float>(
inputSize: 16, outputSize: 1,
activation: identity)
@differentiable
func callAsFunction(_ input: Tensor<Float>) -> Tensor<Float> {
input.sequenced(through: dense1, dense2, dense3, dense4)
}
}
// Loss functions
@differentiable
func generatorLoss(fakeLogits: Tensor<Float>) -> Tensor<Float> {
sigmoidCrossEntropy(
logits: fakeLogits,
labels: Tensor(ones: fakeLogits.shape))
}
@differentiable
func discriminatorLoss(realLogits: Tensor<Float>, fakeLogits: Tensor<Float>) -> Tensor<Float> {
let realLoss = sigmoidCrossEntropy(
logits: realLogits,
labels: Tensor(ones: realLogits.shape))
let fakeLoss = sigmoidCrossEntropy(
logits: fakeLogits,
labels: Tensor(zeros: fakeLogits.shape))
return realLoss + fakeLoss
}
/// Returns `size` samples of noise vector.
func sampleVector(size: Int) -> Tensor<Float> {
Tensor(randomNormal: [size, latentSize])
}
let dataset = MNIST(batchSize: batchSize, device: Device.default,
entropy: SystemRandomNumberGenerator(), flattening: true, normalizing: true)
var generator = Generator()
var discriminator = Discriminator()
let optG = Adam(for: generator, learningRate: 2e-4, beta1: 0.5)
let optD = Adam(for: discriminator, learningRate: 2e-4, beta1: 0.5)
// Noise vectors and plot function for testing
let testImageGridSize = 4
let testVector = sampleVector(size: testImageGridSize * testImageGridSize)
func saveImageGrid(_ testImage: Tensor<Float>, name: String) throws {
var gridImage = testImage.reshaped(
to: [
testImageGridSize, testImageGridSize,
imageHeight, imageWidth,
])
// Add padding.
gridImage = gridImage.padded(forSizes: [(0, 0), (0, 0), (1, 1), (1, 1)], with: 1)
// Transpose to create single image.
gridImage = gridImage.transposed(permutation: [0, 2, 1, 3])
gridImage = gridImage.reshaped(
to: [(imageHeight + 2) * testImageGridSize, (imageWidth + 2) * testImageGridSize, 1])
// Convert [-1, 1] range to [0, 255] range.
gridImage = ((gridImage + 1) / 2) * 255
try gridImage.saveImage(directory: outputFolder, name: name, format: .png)
}
print("Start training...")
// Start training loop.
for (epoch, epochBatches) in dataset.training.prefix(epochCount).enumerated() {
// Start training phase.
Context.local.learningPhase = .training
for batch in epochBatches {
// Perform alternative update.
// Update generator.
let vec1 = sampleVector(size: batchSize)
let 𝛁generator = TensorFlow.gradient(at: generator) { generator -> Tensor<Float> in
let fakeImages = generator(vec1)
let fakeLogits = discriminator(fakeImages)
let loss = generatorLoss(fakeLogits: fakeLogits)
return loss
}
optG.update(&generator, along: 𝛁generator)
// Update discriminator.
let realImages = batch.data
let vec2 = sampleVector(size: batchSize)
let fakeImages = generator(vec2)
let 𝛁discriminator = TensorFlow.gradient(at: discriminator) { discriminator -> Tensor<Float> in
let realLogits = discriminator(realImages)
let fakeLogits = discriminator(fakeImages)
let loss = discriminatorLoss(realLogits: realLogits, fakeLogits: fakeLogits)
return loss
}
optD.update(&discriminator, along: 𝛁discriminator)
}
// Start inference phase.
Context.local.learningPhase = .inference
let testImage = generator(testVector)
do {
try saveImageGrid(testImage, name: "epoch-\(epoch)-output")
} catch {
print("Could not save image grid with error: \(error)")
}
let lossG = generatorLoss(fakeLogits: testImage)
print("[Epoch: \(epoch)] Loss-G: \(lossG)")
}