-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscriminator.py
159 lines (139 loc) · 6.57 KB
/
Discriminator.py
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
from pathlib import Path
from Layers import Conv2DSN, DenseSN
import numpy as np
import tensorflow as tf
import tensorflow.keras as ks
import tensorflow.keras.layers as lr
class Discriminator:
def __init__(self,
batchSize: int = 32,
imShape: (int, int, int) = (64, 64, 3),
initWeights: ks.initializers.Initializer = ks.initializers.RandomUniform(-1, 1)):
self.mBatchSize = batchSize
self.mImHeight = imShape[0]
self.mImWidth = imShape[1]
self.mImChannels = imShape[2]
self.mInitWeights = initWeights
return
def load(self, checkpoint, dire=""):
if checkpoint == "initial":
self.mModel = self.createModel()
self.setOptimizer()
elif checkpoint == "latest":
self.mModel = self.createModel()
self.setOptimizer()
checkpoint = tf.train.Checkpoint(discriminatorOptimizer=self.mOptimizer,
discriminator=self.mModel)
checkpoint.restore(tf.train.latest_checkpoint(dire / self.mSaveDir))
else:
# @todo: implement method for loading model at specific epochs
...
return
def save(self, dire: Path):
checkpoint = tf.train.Checkpoint(discriminatorOptimizer=self.mOptimizer,
discriminator=self.mModel)
checkpoint.save(dire / self.mSavePre)
return
def createModel(self, spectralNorm: bool = True):
model = ks.Sequential()
if spectralNorm:
model.add(Conv2DSN.Conv2DSN(64, (5, 5), strides=2, padding='same',
input_shape=[self.mImHeight, self.mImWidth, self.mImChannels],
kernel_initializer=self.mInitWeights))
# model.add(lr.BatchNormalization())
model.add(lr.LeakyReLU(alpha=0.2))
model = self.sConvReLU(model, output=64, shape=(4, 4), stride=2)
model = self.sConvReLU(model, output=128, shape=(4, 4), stride=2)
model = self.sConvReLU(model, output=256, shape=(4, 4), stride=2)
model.add(lr.Flatten())
model.add(DenseSN.DenseSN(1, activation='sigmoid'))
else:
model.add(lr.Conv2D(64, (5, 5), strides=2, padding='same',
input_shape=[self.mImHeight, self.mImWidth, self.mImChannels],
kernel_initializer=self.mInitWeights))
model.add(lr.BatchNormalization())
model.add(lr.LeakyReLU(alpha=0.2))
model = self.convReLU(model, output=64, shape=(4, 4), stride=2)
model = self.convReLU(model, output=128, shape=(4, 4), stride=2)
model = self.convReLU(model, output=256, shape=(4, 4), stride=2)
model.add(lr.Flatten())
model.add(lr.Dense(1, activation='sigmoid'))
return model
def sConvReLU(self, model: ks.Sequential, output: int, shape: (int, int),
stride: int, padding: str = "same", useBias: bool = False,
slope: float = 0.2) -> ks.Sequential:
model.add(Conv2DSN.Conv2DSN(output, shape, strides=(stride, stride), padding=padding, use_bias=useBias,
kernel_initializer=self.mInitWeights))
model.add(lr.BatchNormalization())
model.add(lr.LeakyReLU(alpha=slope))
return model
def convReLU(self, model: ks.Sequential, output: int, shape: (int, int),
stride: int, padding: str = "same", useBias: bool = False,
slope: float = 0.2) -> ks.Sequential:
model.add(lr.Conv2D(output, shape, strides=(stride, stride), padding=padding, use_bias=useBias,
kernel_initializer=self.mInitWeights))
model.add(lr.BatchNormalization())
model.add(lr.LeakyReLU(alpha=slope))
return model
def setOptimizer(self, learning = 0.0002, b1=0.5):
self.mOptimizer = ks.optimizers.Adam(learning_rate=learning, beta_1=b1)
return
@tf.function
def loss(self, realOutput: tf.Tensor, fakeOutput: tf.Tensor,
lossFunc: str = "gan", labelSmoothing: bool = True,
noise: bool = True):
def addGaussianNoise(image):
noise = tf.random.normal(shape=realOutput.shape, mean=0, stddev=50/255, dtype=tf.float32)
image = image+noise
image = tf.clip_by_value(image, 0.0, 1.0)
return image
# Add noise to images
if noise:
tf.map_fn(addGaussianNoise, realOutput, parallel_iterations=10)
tf.map_fn(addGaussianNoise, fakeOutput, parallel_iterations=10)
# Create labels for real and fake images
realLabels = tf.ones_like(realOutput)
fakeLabels = tf.zeros_like(fakeOutput)
# Apply smoothing to the labels to help stop the discriminator becoming to overconfident/underconfident about
# its predictions. So we use the ranges [0~0.3], [0.7~1]
if labelSmoothing:
realLabels = realLabels - 0.3 + (np.random.random(realLabels.shape)*0.5)
fakeLabels = fakeLabels + np.random.random(fakeLabels.shape)*0.3
# This returns a helper function to compute the cross entropy loss
crossEntropy = tf.keras.losses.BinaryCrossentropy(from_logits=False)
# Now apply the correct loss functions
if lossFunc == "gan":
realLoss = crossEntropy(realLabels, realOutput)
fakeLoss = crossEntropy(fakeLabels, fakeOutput)
return realLoss + fakeLoss
elif lossFunc == "ralsgan":
return (tf.reduce_mean(tf.square(realOutput - tf.reduce_mean(fakeLabels) - tf.ones_like(realLabels)))
+ tf.reduce_mean(tf.square(fakeOutput - tf.reduce_mean(realLabels) + tf.ones_like(fakeLabels)))) / 2.
else:
raise ValueError("Loss function in the Discriminator class cannot be found.")
def fit(self):
return
def evaluate(self):
return
def predict(self):
return
mBatchSize = None
mLoad = False
mImHeight = None
mImWidth = None
mImChannels = None
mInitWeights = None
mModel: ks.Model = None
mOptimizer = None
mSaveDir = Path("Discriminator")
mSavePre = mSaveDir / Path("ckpt")
if __name__ == "__main__":
# from InputPipe import InputPipe
# io = InputPipe()
# io.loadAllImages()
disc = Discriminator(imShape=(64, 64, 3),
initWeights=ks.initializers.TruncatedNormal(stddev=0.02, mean=0))
disc.load(checkpoint="initial")
mod = disc.mModel
mod.summary()
print(mod.output_shape)