-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgan.py
More file actions
256 lines (214 loc) · 9.74 KB
/
gan.py
File metadata and controls
256 lines (214 loc) · 9.74 KB
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
# CS390-NIP GAN lab
# Max Jacobson / Sri Cherukuri / Anthony Niemiec
# FA2020
# uses Fashion MNIST https://www.kaggle.com/zalando-research/fashionmnist
# uses CIFAR-10 https://www.cs.toronto.edu/~kriz/cifar.html
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Dense, Reshape, Flatten
from tensorflow.keras.layers import BatchNormalization, LeakyReLU
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Conv2DTranspose, UpSampling2D
from tensorflow.keras.optimizers import Adam
#from scipy.misc import imsave
import imageio
import random
random.seed(1618)
np.random.seed(1618)
tf.compat.v1.set_random_seed(1618)
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# NOTE: mnist_d is no credit
# NOTE: cifar_10 is extra credit
#DATASET = "mnist_d"
DATASET = "mnist_f"
#DATASET = "cifar_10"
if DATASET == "mnist_d":
IMAGE_SHAPE = (IH, IW, IZ) = (28, 28, 1)
LABEL = "numbers"
elif DATASET == "mnist_f":
IMAGE_SHAPE = (IH, IW, IZ) = (28, 28, 1)
CLASSLIST = ["top", "trouser", "pullover", "dress", "coat", "sandal", "shirt", "sneaker", "bag", "ankle boot"]
# TODO: choose a label to train on from the CLASSLIST above
#LABEL = "coat"
LABEL = "bag"
elif DATASET == "cifar_10":
IMAGE_SHAPE = (IH, IW, IZ) = (32, 32, 3)
CLASSLIST = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]
LABEL = "airplane"
IMAGE_SIZE = IH*IW*IZ
NOISE_SIZE = 100 # length of noise array
# file prefixes and directory
OUTPUT_NAME = DATASET + "_" + LABEL
OUTPUT_DIR = "./outputs/" + OUTPUT_NAME
# NOTE: switch to True in order to receive debug information
VERBOSE_OUTPUT = False
LOGGING_PATH = "losses.csv"
################################### DATA FUNCTIONS ###################################
# Load in and report the shape of dataset
def getRawData():
if DATASET == "mnist_f":
(xTrain, yTrain), (xTest, yTest) = tf.keras.datasets.fashion_mnist.load_data()
elif DATASET == "cifar_10":
(xTrain, yTrain), (xTest, yTest) = tf.keras.datasets.cifar10.load_data()
elif DATASET == "mnist_d":
(xTrain, yTrain), (xTest, yTest) = tf.keras.datasets.mnist.load_data()
print("Shape of xTrain dataset: %s." % str(xTrain.shape))
print("Shape of yTrain dataset: %s." % str(yTrain.shape))
print("Shape of xTest dataset: %s." % str(xTest.shape))
print("Shape of yTest dataset: %s." % str(yTest.shape))
return ((xTrain, yTrain), (xTest, yTest))
# Filter out the dataset to only include images with our LABEL, meaning we may also discard
# class labels for the images because we know exactly what to expect
def preprocessData(raw):
((xTrain, yTrain), (xTest, yTest)) = raw
if DATASET == "mnist_d":
xP = np.r_[xTrain, xTest]
else:
c = CLASSLIST.index(LABEL)
x = np.r_[xTrain, xTest]
y = np.r_[yTrain, yTest].flatten()
ilist = [i for i in range(y.shape[0]) if y[i] == c]
xP = x[ilist]
# NOTE: Normalize from 0 to 1 or -1 to 1
#xP = xP/255.0
xP = xP/127.5 - 1
print("Shape of Preprocessed dataset: %s." % str(xP.shape))
return xP
################################### CREATING A GAN ###################################
# Model that discriminates between fake and real dataset images
def buildDiscriminator():
model = Sequential()
# TODO: build a discriminator which takes in a (28 x 28 x 1) image - possibly from mnist_f
# and possibly from the generator - and outputs a single digit REAL (1) or FAKE (0)
'''
model.add(Flatten(input_shape = IMAGE_SHAPE))
model.add(Dense(512))
model.add(LeakyReLU(alpha = 0.2))
model.add(Dense(256))
model.add(LeakyReLU(alpha = 0.2))
model.add(Dense(1, activation="sigmoid"))
'''
model.add(Conv2D(64, (3, 3), padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(128, (3, 3), padding="same"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(512))
model.add(LeakyReLU(alpha = 0.2))
model.add(Dense(1, activation="sigmoid"))
# Creating a Keras Model out of the network
inputTensor = Input(shape = IMAGE_SHAPE)
return Model(inputTensor, model(inputTensor))
# Model that generates a fake image from random noise
def buildGenerator():
model = Sequential()
# TODO: build a generator which takes in a (NOISE_SIZE) noise array and outputs a fake
# mnist_f (28 x 28 x 1) image
'''
model.add(Dense(256, input_dim = NOISE_SIZE))
model.add(LeakyReLU(alpha = 0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(512))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(1024))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(IMAGE_SIZE, activation="tanh"))
model.add(Reshape(IMAGE_SHAPE))
'''
LAYER_TRANSITION_SHAPE=(int(IMAGE_SHAPE[0] / 4), int(IMAGE_SHAPE[1] / 4), IMAGE_SHAPE[2] * 128)
print(LAYER_TRANSITION_SHAPE)
LAST_LAYER_SIZE = 1
for num in LAYER_TRANSITION_SHAPE:
LAST_LAYER_SIZE *= num
#convnet, work in progress
model.add(Dense(512, input_dim = NOISE_SIZE))
model.add(LeakyReLU(alpha = 0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Dense(LAST_LAYER_SIZE))
model.add(LeakyReLU(alpha = 0.2))
model.add(BatchNormalization(momentum=0.8))
model.add(Reshape(LAYER_TRANSITION_SHAPE))
#begin convnet
model.add(Conv2DTranspose(IMAGE_SHAPE[2] * 128, (5, 5), padding='same', data_format='channels_last'))
model.add(BatchNormalization(momentum=0.8))
model.add(LeakyReLU(alpha=0.2))
model.add(Conv2DTranspose(IMAGE_SHAPE[2] * 64, (5, 5), strides=(2, 2), padding='same'))
model.add(BatchNormalization(momentum=0.8))
model.add(LeakyReLU(alpha=0.2))
model.add(Conv2DTranspose(IMAGE_SHAPE[2], (5, 5), strides=(2, 2), padding='same'))
print(model.output_shape)
# Creating a Keras Model out of the network
inputTensor = Input(shape = (NOISE_SIZE,))
return Model(inputTensor, model(inputTensor))
def buildGAN(images, epochs = 40000, batchSize = 32, loggingInterval = 0, dis_weight = 1, gen_weight = 1):
# Setup
opt = Adam(lr = 0.0002)
loss = "binary_crossentropy"
# Setup adversary
adversary = buildDiscriminator()
adversary.compile(loss = loss, optimizer = opt, metrics = ["accuracy"])
# Setup generator and GAN
adversary.trainable = False # freeze adversary's weights when training GAN
generator = buildGenerator() # generator is trained within GAN in relation to adversary performance
noise = Input(shape = (NOISE_SIZE,))
gan = Model(noise, adversary(generator(noise))) # GAN feeds generator into adversary
gan.compile(loss = loss, optimizer = opt)
# Training
trueCol = np.ones((batchSize, 1))
falseCol = np.zeros((batchSize, 1))
for epoch in range(epochs):
for run in range(dis_weight):
# Train discriminator with a true and false batch
batch = images[np.random.randint(0, images.shape[0], batchSize)]
noise = np.random.normal(0, 1, (batchSize, NOISE_SIZE))
genImages = generator.predict(noise)
advTrueLoss = adversary.train_on_batch(batch, trueCol)
advFalseLoss = adversary.train_on_batch(genImages, falseCol)
advLoss = np.add(advTrueLoss, advFalseLoss) * 0.5
for run in range(gen_weight):
# Train generator by training GAN while keeping adversary component constant
noise = np.random.normal(0, 1, (batchSize, NOISE_SIZE))
genLoss = gan.train_on_batch(noise, trueCol)
# Logging
if loggingInterval > 0 and epoch % loggingInterval == 0:
print("\tEpoch %d:" % epoch)
print("\t\tDiscriminator loss: %f." % advLoss[0])
print("\t\tDiscriminator accuracy: %.2f%%." % (100 * advLoss[1]))
print("\t\tGenerator loss: %f." % genLoss)
log_file = open(LOGGING_PATH, "a")
#format: (dis loss, dis acc, gen loss)
log_file.write("%s, %s, %s\n" % (advLoss[0], advLoss[1], genLoss))
log_file.close()
runGAN(generator, OUTPUT_DIR + "/" + OUTPUT_NAME + "_test_%d.png" % (epoch / loggingInterval))
return (generator, adversary, gan)
# Generates an image using given generator
def runGAN(generator, outfile):
noise = np.random.normal(0, 1, (1, NOISE_SIZE)) # generate a random noise array
img = generator.predict(noise)[0] # run generator on noise
img = np.squeeze(img) # readjust image shape if needed
img = (0.5*img + 0.5)*255 # adjust values to range from 0 to 255 as needed
#imsave(outfile, img) # store resulting image
imageio.imwrite(outfile, img)
################################### RUNNING THE PIPELINE #############################
def main():
print("Starting %s image generator program." % LABEL)
# Make output directory
if not os.path.exists(OUTPUT_DIR):
os.makedirs(OUTPUT_DIR)
# Receive all of mnist_f
raw = getRawData()
# Filter for just the class we are trying to generate
data = preprocessData(raw)
# Create and train all facets of the GAN
(generator, adv, gan) = buildGAN(data, epochs = 20000, loggingInterval = 1000, gen_weight = 5)
# Utilize our spooky neural net gimmicks to create realistic counterfeit images
for i in range(10):
runGAN(generator, OUTPUT_DIR + "/" + OUTPUT_NAME + "_final_%d.png" % i)
print("Images saved in %s directory." % OUTPUT_DIR)
if __name__ == '__main__':
main()