forked from chen0040/keras-text-to-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcgan_v3.py
226 lines (177 loc) · 10.5 KB
/
dcgan_v3.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
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
from keras.models import Model, Sequential
from keras.layers import Input, Dense, Reshape, concatenate
from keras.layers.core import Activation, Flatten
from keras.layers.normalization import BatchNormalization
from keras.layers.convolutional import UpSampling2D, Conv2D, MaxPooling2D
from keras.optimizers import SGD
from keras_text_to_image.library.utility.image_utils import combine_normalized_images, img_from_normalized_img
from keras import backend as K
import numpy as np
from PIL import Image
import os
import matplotlib.pyplot as plt
#from keras_text_to_image.library.utility.glove_loader import GloveModel
class DCGanV3(object):
model_name = 'dc-gan-v3'
def __init__(self):
K.set_image_dim_ordering('tf')
self.generator = None
self.discriminator = None
self.model = None
self.img_width = 64
self.img_height = 64
self.img_channels = 3
self.random_input_dim = 100
self.text_input_dim = 4800
self.config = None
#self.glove_source_dir_path = './very_large_data'
#self.glove_model = GloveModel()
@staticmethod
def get_config_file_path(model_dir_path):
return os.path.join(model_dir_path, DCGanV3.model_name + '-config.npy')
@staticmethod
def get_weight_file_path(model_dir_path, model_type):
return os.path.join(model_dir_path, DCGanV3.model_name + '-' + model_type + '-weights.h5')
def create_model(self):
init_img_width = self.img_width // 4
init_img_height = self.img_height // 4
random_input = Input(shape=(self.random_input_dim,))
text_input1 = Input(shape=(self.text_input_dim,))
random_dense = Dense(self.random_input_dim)(random_input)
text_layer1 = Dense(1024)(text_input1)
merged = concatenate([random_dense, text_layer1])
generator_layer = Activation('tanh')(merged)
generator_layer = Dense(128 * init_img_width * init_img_height)(generator_layer)
generator_layer = BatchNormalization()(generator_layer)
generator_layer = Activation('tanh')(generator_layer)
generator_layer = Reshape((init_img_width, init_img_height, 128),
input_shape=(128 * init_img_width * init_img_height,))(generator_layer)
generator_layer = UpSampling2D(size=(2, 2))(generator_layer)
generator_layer = Conv2D(64, kernel_size=5, padding='same')(generator_layer)
generator_layer = Activation('tanh')(generator_layer)
generator_layer = UpSampling2D(size=(2, 2))(generator_layer)
generator_layer = Conv2D(self.img_channels, kernel_size=5, padding='same')(generator_layer)
generator_output = Activation('tanh')(generator_layer)
self.generator = Model([random_input, text_input1], generator_output)
self.generator.compile(loss='mean_squared_error', optimizer="SGD")
print('generator: ', self.generator.summary())
text_input2 = Input(shape=(self.text_input_dim,))
text_layer2 = Dense(1024)(text_input2)
img_input2 = Input(shape=(self.img_width, self.img_height, self.img_channels))
img_layer2 = Conv2D(64, kernel_size=(5, 5), padding='same')(
img_input2)
img_layer2 = Activation('tanh')(img_layer2)
img_layer2 = MaxPooling2D(pool_size=(2, 2))(img_layer2)
img_layer2 = Conv2D(128, kernel_size=5)(img_layer2)
img_layer2 = Activation('tanh')(img_layer2)
img_layer2 = MaxPooling2D(pool_size=(2, 2))(img_layer2)
img_layer2 = Flatten()(img_layer2)
img_layer2 = Dense(1024)(img_layer2)
merged = concatenate([img_layer2, text_layer2])
discriminator_layer = Activation('tanh')(merged)
discriminator_layer = Dense(1)(discriminator_layer)
discriminator_output = Activation('sigmoid')(discriminator_layer)
self.discriminator = Model([img_input2, text_input2], discriminator_output)
d_optim = SGD(lr=0.0005, momentum=0.9, nesterov=True)
self.discriminator.compile(loss='binary_crossentropy', optimizer=d_optim)
print('discriminator: ', self.discriminator.summary())
model_output = self.discriminator([self.generator.output, text_input1])
self.model = Model([random_input, text_input1], model_output)
self.discriminator.trainable = False
g_optim = SGD(lr=0.0005, momentum=0.9, nesterov=True)
self.model.compile(loss='binary_crossentropy', optimizer=g_optim)
print('generator-discriminator: ', self.model.summary())
def load_model(self, model_dir_path):
config_file_path = DCGanV3.get_config_file_path(model_dir_path)
self.config = np.load(config_file_path).item()
self.img_width = self.config['img_width']
self.img_height = self.config['img_height']
self.img_channels = self.config['img_channels']
self.random_input_dim = self.config['random_input_dim']
self.text_input_dim = self.config['text_input_dim']
#self.glove_source_dir_path = self.config['glove_source_dir_path']
self.create_model()
#self.glove_model.load(self.glove_source_dir_path, embedding_dim=self.text_input_dim)
self.generator.load_weights(DCGanV3.get_weight_file_path(model_dir_path, 'generator'))
self.discriminator.load_weights(DCGanV3.get_weight_file_path(model_dir_path, 'discriminator'))
def fit(self, model_dir_path, image_label_pairs, epochs=None, batch_size=None, snapshot_dir_path=None,
snapshot_interval=None):
if epochs is None:
epochs = 100
if batch_size is None:
batch_size = 128
if snapshot_interval is None:
snapshot_interval = 20
self.config = dict()
self.config['img_width'] = self.img_width
self.config['img_height'] = self.img_height
self.config['random_input_dim'] = self.random_input_dim
self.config['text_input_dim'] = self.text_input_dim
self.config['img_channels'] = self.img_channels
#self.config['glove_source_dir_path'] = self.glove_source_dir_path
#self.glove_model.load(data_dir_path=self.glove_source_dir_path, embedding_dim=self.text_input_dim)
config_file_path = DCGanV3.get_config_file_path(model_dir_path)
np.save(config_file_path, self.config)
noise = np.zeros((batch_size, self.random_input_dim))
text_batch = np.zeros((batch_size, self.text_input_dim))
self.create_model()
desc_plot = []
gen_plot = []
for epoch in range(epochs):
print("Epoch is", epoch)
batch_count = int(image_label_pairs.shape[0] / batch_size)
print("Number of batches", batch_count)
for batch_index in range(batch_count):
# Step 1: train the discriminator
image_label_pair_batch = image_label_pairs[batch_index * batch_size:(batch_index + 1) * batch_size]
image_batch = []
for index in range(batch_size):
image_label_pair = image_label_pair_batch[index]
normalized_img = image_label_pair[0]
#text = image_label_pair[1]
image_batch.append(normalized_img)
text_batch[index, :] = image_label_pair[1] #self.glove_model.encode_doc(text, self.text_input_dim)
noise[index, :] = np.random.uniform(-1, 1, self.random_input_dim)
image_batch = np.array(image_batch)
# image_batch = np.transpose(image_batch, (0, 2, 3, 1))
generated_images = self.generator.predict([noise, text_batch], verbose=0)
if (epoch * batch_size + batch_index) % snapshot_interval == 0 and snapshot_dir_path is not None:
self.save_snapshots(generated_images, snapshot_dir_path=snapshot_dir_path,
epoch=epoch, batch_index=batch_index)
self.discriminator.trainable = True
d_loss = self.discriminator.train_on_batch([np.concatenate((image_batch, generated_images)),
np.concatenate((text_batch, text_batch))],
np.array([1] * batch_size + [0] * batch_size))
#print("Epoch %d batch %d d_loss : %f" % (epoch, batch_index, d_loss))
# Step 2: train the generator
for index in range(batch_size):
noise[index, :] = np.random.uniform(-1, 1, self.random_input_dim)
self.discriminator.trainable = False
g_loss = self.model.train_on_batch([noise, text_batch], np.array([1] * batch_size))
print("Epoch %d batch %d/%d\ng_loss : %f, d_loss: %f" % (epoch, batch_index, batch_count-1, g_loss, d_loss))
desc_plot.append(d_loss)
gen_plot.append(g_loss)
if (epoch * batch_size + batch_index) % 10 == 9:
self.generator.save_weights(DCGanV3.get_weight_file_path(model_dir_path, 'generator'), True)
self.discriminator.save_weights(DCGanV3.get_weight_file_path(model_dir_path, 'discriminator'), True)
plt.plot(desc_plot)
plt.plot(gen_plot)
plt.title('Generator and Descriminator losses in epoch ' + str(epoch))
plt.legend(['desc','gen'],loc= 'upper left')
plt.savefig('data/plot')
plt.close()
self.generator.save_weights(DCGanV3.get_weight_file_path(model_dir_path, 'generator'), True)
self.discriminator.save_weights(DCGanV3.get_weight_file_path(model_dir_path, 'discriminator'), True)
def generate_image_from_text(self, text):
noise = np.zeros(shape=(1, self.random_input_dim))
encoded_text = np.zeros(shape=(1, self.text_input_dim))
encoded_text[0, :] = text #self.glove_model.encode_doc(text)
noise[0, :] = np.random.uniform(-1, 1, self.random_input_dim)
generated_images = self.generator.predict([noise, encoded_text], verbose=0)
generated_image = generated_images[0]
generated_image = generated_image * 127.5 + 127.5
return Image.fromarray(generated_image.astype(np.uint8))
def save_snapshots(self, generated_images, snapshot_dir_path, epoch, batch_index):
image = combine_normalized_images(generated_images)
img_from_normalized_img(image).save(
os.path.join(snapshot_dir_path, DCGanV3.model_name + '-' + str(epoch) + "-" + str(batch_index) + ".png"))