-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldg_v2_gen.py
173 lines (138 loc) · 5.79 KB
/
ldg_v2_gen.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
from keras.layers import Lambda, Input, Dense, Flatten, Conv2D, Conv2DTranspose
from keras.layers import Activation, BatchNormalization, Reshape, Concatenate
from keras.models import Model
from keras.utils import to_categorical
from keras import backend as K
from keras import initializers
import numpy as np
import matplotlib.pyplot as plt
import os
import argparse
label_str = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
lebel_dict = {i: v for i, v in enumerate(label_str)}
n_label = len(label_str)
img_rows = 28
img_cols = 28
img_channel = 1
orig_dimension = img_rows * img_cols
image_shape = (img_rows, img_cols, img_channel)
latent_dim = 10
def sampling(arg):
arg = [z_mean, z_log_var]
dim = K.int_shape(z_mean)[1]
# reparameterization trick
epsilon = K.random_normal(
shape=(K.shape(z_mean)[0], dim), mean=0.0, stddev=1.0)
return z_mean + K.exp(0.5 * z_log_var) * epsilon
w_init = initializers.random_normal(stddev=0.02)
gamma_init = initializers.random_normal(mean=1.0, stddev=0.02)
# encoder
img_inputs = Input(shape=(orig_dimension,), name='image_input')
label_inputs = Input(shape=(n_label,), name='label_input')
encoder_inputs = Concatenate()([img_inputs, label_inputs])
x = Dense(orig_dimension, kernel_initializer=w_init,
activation='relu')(encoder_inputs)
x = Reshape(image_shape)(x)
x = Conv2D(16, 3, strides=1, padding='same', kernel_initializer=w_init)(x)
x = BatchNormalization(gamma_initializer=gamma_init)(x)
x = Activation('relu')(x)
x = Conv2D(32, 3, strides=1, padding='same', kernel_initializer=w_init)(x)
x = BatchNormalization(gamma_initializer=gamma_init)(x)
x = Activation('relu')(x)
x = Conv2D(64, 3, strides=2, padding='same', kernel_initializer=w_init)(x)
x = BatchNormalization(gamma_initializer=gamma_init)(x)
x = Activation('relu')(x)
before_flatten_shape = K.int_shape(x)
x = Flatten()(x)
x = Dense(128, kernel_initializer=w_init, activation='relu')(x)
z_mean = Dense(latent_dim, name='z_mean')(x)
z_log_var = Dense(latent_dim, name='z_log_var')(x)
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])
encoder = Model([img_inputs, label_inputs], [
z_mean, z_log_var, z], name='encoder')
# decoder
latent_inputs = Input(shape=(latent_dim,), name='latent_inputs')
decoder_inputs = Concatenate()([latent_inputs, label_inputs])
x = Dense(128, kernel_initializer=w_init, activation='relu')(decoder_inputs)
x = Dense(before_flatten_shape[1] * before_flatten_shape[2] *
before_flatten_shape[3], activation='relu', kernel_initializer=w_init)(x)
x = Reshape(
(before_flatten_shape[1], before_flatten_shape[2], before_flatten_shape[3]))(x)
x = Conv2DTranspose(64, 3, strides=1, padding='same',
kernel_initializer=w_init)(x)
x = BatchNormalization(gamma_initializer=gamma_init)(x)
x = Activation('relu')(x)
x = Conv2DTranspose(32, 3, strides=2, padding='same',
kernel_initializer=w_init)(x)
x = BatchNormalization(gamma_initializer=gamma_init)(x)
x = Activation('relu')(x)
x = Conv2DTranspose(16, 3, strides=1, padding='same',
kernel_initializer=w_init)(x)
x = BatchNormalization(gamma_initializer=gamma_init)(x)
x = Activation('relu')(x)
x = Conv2DTranspose(img_channel, 3, activation='tanh',
padding='same', kernel_initializer=w_init)(x)
outputs = Flatten()(x)
# instantiate decoder model
decoder = Model([latent_inputs, label_inputs], outputs, name='decoder')
# VAE
outputs = decoder([encoder([img_inputs, label_inputs])[2], label_inputs])
vae = Model([img_inputs, label_inputs], outputs)
def load_weights_vae(weight_name=None):
# load all the weights for encoder and decoder when loading for vae
vae.load_weights(os.path.join(saving_folder, weight_name))
def find_idx_from_label_dict(search_str):
for i, val in lebel_dict.items():
if val == search_str:
return i
def letter_digit_gen(input_str, th=None):
gap = 20
img = np.zeros((img_cols, gap * len(input_str) + (img_cols - gap)))
for idx, l in enumerate(input_str):
if l == ' ':
img[:, gap * idx:gap * idx +
img_cols] += np.zeros((img_cols, img_cols))
elif l not in label_str:
pass
else:
cls_idx = find_idx_from_label_dict(l)
latent = np.random.randn(latent_dim)
latent = np.expand_dims(np.random.randn(latent_dim), 0)
generated = decoder.predict(
[latent, np.expand_dims(to_categorical(cls_idx, n_label), 0)])
generated = (0.5 * generated) + 0.5
generated = generated.reshape(img_rows, img_cols)
generated = np.transpose(generated)
img[:, gap * idx:gap * idx + img_cols] += generated
if th != None:
for i, v in np.ndenumerate(img):
if v >= th:
img[i] = 1
else:
img[i] = 0
return img
def restricted_float(x):
x = float(x)
if x < 0.0 or x > 1.0:
raise argparse.ArgumentTypeError(
'{} is not in range [0.0, 1.0]'.format(x))
return x
parser = argparse.ArgumentParser()
parser.add_argument('input_str', help='input string to be converted')
parser.add_argument('-t', '--threshold', type=restricted_float,
help='binary threshold: float[0-1]')
parser.add_argument('-s', '--save',
help='save the image in .png')
args = parser.parse_args()
input_str = args.input_str
saving_folder = 'best_weight_ldg_v2_conv-cvae'
load_weights_vae(
weight_name='ldg_v2_conv-cvae-best-wiehgts-050-131.521-131.939.h5')
img = letter_digit_gen(input_str, th=args.threshold)
plt.figure(figsize=(5, 5))
plt.axis('off')
plt.imshow(1 - img, cmap='gray')
if args.save:
os.makedirs('generated_images', exist_ok=True)
plt.savefig('generated_images/{}.png'.format(args.save))
plt.show()