-
Notifications
You must be signed in to change notification settings - Fork 0
/
char_gen.py
439 lines (409 loc) · 13.9 KB
/
char_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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# example of loading the generator model and generating images
print(' Character Generator',
'\n[===================]\n')
from numpy import asarray
from numpy import array
from numpy import zeros
from numpy import ones
from numpy import transpose
from numpy import float32
from numpy import append
from numpy import full
from numpy import savetxt
from numpy import arange
from numpy.linalg import norm
from numpy.random import randn
from numpy.random import randint
from tensorflow.keras.models import load_model
from matplotlib import pyplot
from sys import argv
from time import sleep
def plot_euclidean_distance(examples, n_cl):
n_ex = len(examples)
ex_cl = n_ex // n_cl
ed_cl = ex_cl*(ex_cl-1)//2
result = array(zeros((n_cl, ed_cl)), float32)
# all same class
for i in range(n_ex):
for j in range((i//n_cl) + 1, n_ex//n_cl):
i_cl = i // n_cl
index = (j-i_cl -1) + (ed_cl - ((ex_cl-i_cl)*(ex_cl-i_cl-1)//2))
#print('x:', i%n_cl, '\ty:', index, '\tj: ', j, '\tx: ', ex_cl, '\ted: ', ed_cl, '\ti_cl: ', i_cl, '\th(x-i_cl): ', ((ex_cl-i_cl)*(ex_cl-i_cl-1)//2))
result[i % n_cl, index] = (norm(examples[i, :, :, 0] - examples[((j*n_cl)+(i%n_cl)), :, :, 0]))
result = transpose(result)
if (input("save? y/n: ") == 'y'):
savetxt('eucl_dist_%dsamples.txt' % ex_cl, result, delimiter=',')
else:
pyplot.figure(figsize=(12, 5))
pyplot.boxplot(result)
pyplot.xlabel("class")
pyplot.ylabel("euclidean distance")
pyplot.tight_layout()
pyplot.ylim(bottom=0)
pyplot.show()
label_arr = ( '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A','B',('C','c'),'D','E','F','G','H',('I','i'),('J','j'),('K','k'),('L', 'l'),('M', 'm'),'N',('O', 'o'),('P','p'),'Q','R',('S', 's'),'T',('U', 'u'),('V', 'v'),('W', 'w'),('X', 'x'),('Y', 'y'),('Z', 'z'),
'a','b','d','e','f','g','h','n','q','r','t')
def to_label(in_lbl):
for i,a in enumerate(label_arr):
if (type(a) is tuple):
for b in a:
if (in_lbl == b):
return i
elif (in_lbl == a):
return i
return -1
def placeTextInArray(textList, textWidth = -1):
n_cols = 0
for word in textList:
if (len(word) > n_cols):
n_cols = len(word)
if (n_cols < textWidth):
n_cols = textWidth
i = 0
change = False
while (i != -1 and len(textList) > 1):
if (len(textList[i]) + len(textList[i+1]) < n_cols):
textList[i] = textList[i] + " " + textList[i+1]
textList.pop(i+1)
change = True
i = (i+1)
if (i >= len(textList)-1 and change):
i = 0
change = False
elif (i >= len(textList)-1 and not change):
i = -1
space_map = zeros((len(textList), n_cols), dtype=bool)
for i, _ in enumerate(space_map):
for j, _ in enumerate(space_map[i]):
try:
if (textList[i][j] == ' '):
space_map[i, j] = True
except IndexError:
space_map[i, j] = True
except Exception as e:
print(e)
quit()
return textList, space_map
class n_lat_pt_exception(Exception):
def __init__(self, got, expected):
self.got = got
self.expected = expected
def __str__(self):
return 'Incorrect number of latent point dimensions!\nGot: ' + str(self.got) + '\nExpected: ' + str(self.expected)
def load_latent_point(latent_dim, n_samples, filename=""):
in_lpt = zeros((n_samples, latent_dim), dtype='float32')
if (filename != ""):
with open(filename) as lpt_file:
i=0
for line in lpt_file:
if (line[0] == '#'):
try:
n_lptf = int(line[1:])
if (n_lptf != latent_dim):
raise n_lat_pt_exception(n_lptf, latent_dim)
except Exception as ex:
print(ex)
quit()
else:
for pt in in_lpt:
pt[i] = float(line)
i += 1
return in_lpt
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples, lptf_base_fname=""):
if (lptf_base_fname != ""):
return load_latent_point(latent_dim, n_samples, lptf_base_fname)
# generate points in the latent space
x_input = randn(latent_dim * n_samples)
# reshape into a batch of inputs for the network
z_input = x_input.reshape(n_samples, latent_dim)
return z_input
def generate_latent_points_similar(latent_dim, n_samples, lptf_base_fname="", variation=0.4):
pt = generate_latent_points(latent_dim, 1, lptf_base_fname)
return (generate_latent_points(latent_dim, n_samples) * variation) + full((n_samples, latent_dim), pt[0])
def latent_map_step(cur, end, lat_range, exp):
step = (cur + 0.5 - end*0.5)
max_step = (end-1)/2
y_max = lat_range/2
try:
if (exp):
if (step>0):
return y_max * (step*step) / (max_step*max_step)
else:
return -y_max * (step*step) / (max_step*max_step)
else:
return step * lat_range / (end-1)
except ZeroDivisionError:
return 0.0
def generate_latent_map_points(latent_dim, rows, cols, map_range, vec, map_dim = [-1, -1], lptf_base_fname="", exponential=False):
lps = load_latent_point(latent_dim, rows*cols, lptf_base_fname)
lps = lps.reshape(rows, cols, latent_dim)
for i in range(rows):
val_i = latent_map_step(i, rows, map_range, exponential)
for j in range(cols):
val_j = latent_map_step(j, cols, map_range, exponential)
for k in range(latent_dim):
if (map_dim[0] == -1 and map_dim[1] == -1):
delta = (vec[k] * ((k%2 or rows<=1) * val_i + ((k+1)%2 or cols<=1) * val_j))# + (lps[i,j,k] / 10) * (val_i+3)/(i + 0.5)
lps[i, j, k] = lps[i, j, k] + delta
elif (k == map_dim[0]): # dim 0 means val_i for dim k==dim[0]
delta = (vec[k] * (val_i))# + (lps[i,j,k] / 10) * (val_i+3)/(i + 0.5)
lps[i, j, k] = lps[i, j, k] + delta
elif (k == map_dim[1]): # dim 1 means val_j for dim k==dim[1]
delta = (vec[k] * (val_j))# + (lps[i,j,k] / 10) * (val_i+3)/(i + 0.5)
lps[i, j, k] = lps[i, j, k] + delta
else:
lps[i, j, k] = lps[i, j, k]#(lps[i,j,k] / 10) * (val_i+3)/(i + 0.5)
return lps.reshape(cols*rows, latent_dim)
def ascii_print(out, rows, cols):
for i in range(rows):
chars = out[i*cols:(i+1)*cols]
for y in range(28):
for c in chars:
for x in range(28):
px = c[y, x]
if (px > 0.9):
print(chr(0x2593), end='')
elif (px > 0.5):
print(chr(0x2592), end='')
elif (px > 0.1):
print(chr(0x2591), end='')
else:
print(' ', end='')
print('')
sleep(0.3)
# create and save a plot of generated images
def save_plot(examples, rows, cols, outfile=""):
fig = pyplot.figure(figsize=(cols * (28/96), rows * (28/96)))
# plot images
for i in range(rows * cols):
# define subplot
pyplot.subplot(rows, cols, 1 + i)
# turn off axis
pyplot.axis('off')
# plot raw pixel data
pyplot.imshow(examples[i, :, :, 0], cmap='gray_r')
pyplot.subplots_adjust(wspace=0, hspace=0, left=0, right=1, bottom=0, top=1)
if (outfile != ""):
fig.savefig(outfile)
quit()
pyplot.show()
eucl = False
f_name = ""
n_classes = 0
rows = 10
in_text = False
lat_map_range = 0
in_char_id = None
text_width = -1
in_dim = [-1, -1]
latent_dim = 100
ascii_out = False
lptf_name = ""
text_var = 0.4
save_name = ""
lat_map_exp = False
lptf_name_vector = ""
# load model
if (len(argv) > 1):
for i, arg in enumerate(argv):
if (arg[0] == '-'):
for j, opt in enumerate(arg):
if (opt == 'r'):
try:
rows = int(argv[i+1])
except:
print('Invalid number of rows!\nusage:\n\t"python gen_cGAN.py -r <number of rows>"')
elif (opt == 'e'):
eucl = True
elif (opt == 'c'):
try:
n_classes = int(argv[i+1])
except:
print('Invalid number of classes!\nusage:\n\t"python gen_cGAN.py -c <n_classes>"')
n_classes = 0
elif (opt == 'f'):
try:
f_name = argv[i+1]
open(f_name)
except:
print('Invalid file name!\nusage:\n\t"python gen_cGAN.py -f <f_name>"')
f_name = ""
elif (opt == 'x'):
ascii_out = True
elif (opt == 't'):
in_text = True
elif (opt == 'v'):
try:
text_var = float(argv[i+1])
except:
print('Invalid text variation level specification!\nusage:\n\t" -t <variation level> "')
elif (opt == 'p'):
try:
lptf_name = argv[i+1]
except:
print('Could not load latent point from file!\nusage:\n\t"python gen_cGAN.py -p <path to .lptf file> "')
elif (opt == 'V'):
try:
lptf_name_vector = argv[i+1]
except:
print('Could not load latent point from file!\nusage:\n\t"python gen_cGAN.py -p <path to .lptf file> "')
elif (opt == 'w'):
try:
text_width = int(argv[i+1])
except:
print('Invalid text width!\nusage:\n\t"python gen_cGAN.py -w <text width>"')
elif (opt == 'L'):
try:
lat_map_range = float(argv[i+1])*2
except:
print('Invalid latent map range!\nusage:\n\t"python gen_cGAN.py -L <lat_map_range>"')
elif (opt == 'l'):
lat_map_exp = True
elif (opt == 'd'):
try:
if (arg[j+1] == 'x'):
in_dim[1] = int(argv[i+1])
elif (arg[j+1] == 'y'):
in_dim[0] = int(argv[i+1])
else:
raise Exception
except:
print('Invalid latent dim specification!\nusage:\n\t" -dx <latent space dim> " or " -dy <latent space dim> "')
elif (opt == 's'):
try:
save_name = argv[i+1]
except:
print('Invalid save name!\nusage:\n\t" -s <save f_name>"')
quit()
elif (opt == 'C'):
try:
in_char_id = int(argv[i+1])
except:
print('Invalid char ID!\nusage:\n\t"python gen_cGAN.py -C <char ID>"')
elif (opt == 'H'):
print( '" -f <model.h5> ":\t- Load generator from file "model.h5"',
'" -c <n_classes> ":\t- Integer, number of data classes',
'" -C <n_classes> ":\t- Integer, character class',
'" -r <n_rows> ":\t\t- Integer, number of rows to be generated',
'" -t ":\t\t\t- Text generation mode',
'" -v <variation level> ":\t- Float, variation level in text generation mode',
'" -w <width>":\t\t- Integer, row width for text generation mode',
'" -L <latent_map_range> ":\t- Float, latent space map range (-latent_map_range to +latent_map_range)',
'" -l ":\t\t\t- Latent space map exponential mode, less extreme characters',
'" -V <.lptf file> ":\t- load a latent point from .lptf file to be use as a vector for the latent map',
'" -dx <latent_dim> ":\t- Integer, specifies latent dimension for map dimension x',
'" -dy <latent_dim> ":\t- Integer, specifies latent dimension for map dimension y',
'" -e ":\t\t\t- Euclidean Box-Plot mode, calculates and shows euclidean distance in the generated images',
'" -x ":\t\t\t- ASCII output mode',
'" -p <.lptf file> ":\t- load a latent point from .lptf file',
'" -s <save f_name>":\t- save output to file (only in image mode)',
sep='\n')
quit()
print('Use "python char_gen.py -H" for a list of all options\n')
if (f_name == ""):
f_name = input('Enter generator file name: ')
if (n_classes == 0):
n_classes=int(input('Enter number of classes: '))
model = load_model(f_name)
# This *should* work, if not -> comment line and use '-D'-option
latent_dim = model.layers[1].input_shape[0][1]
stop = False
while(not in_text):
if (in_char_id == None):
try:
in_char = input('Enter char ID: ')
char = int(in_char)
except ValueError:
char = to_label(in_char)
print("Using char ID:", char, end='')
if (char >= 0):
print("(", label_arr[char], ")")
else:
print('')
except Exception as e:
print(e)
quit()
elif (not stop):
char = in_char_id
stop = True
else: # only do once when -C
quit()
# generate images
latent_points = generate_latent_points(latent_dim, rows*n_classes, lptf_base_fname=lptf_name)
latent_map_vector = []
if (lptf_name_vector != ""):
latent_map_vector = load_latent_point(latent_dim, 1, lptf_name_vector)[0]
else:
latent_map_vector = ones([latent_dim], dtype='float32')
if (lat_map_range != 0):
if (char == -1):
latent_points = latent_points.reshape((rows, n_classes, latent_dim))
for c in range(n_classes):
pts = generate_latent_map_points(latent_dim, rows, 1, lat_map_range, latent_map_vector, map_dim=in_dim, lptf_base_fname=lptf_name, exponential=lat_map_exp)
for r in range(rows):
latent_points[r, c] = pts[r]
latent_points = latent_points.reshape((n_classes*rows, latent_dim))
else:
latent_points = generate_latent_map_points(latent_dim, rows, n_classes, lat_map_range, latent_map_vector, map_dim=in_dim, lptf_base_fname=lptf_name, exponential=lat_map_exp)
# specify labels
labels = zeros(n_classes*rows)
# generate images
# test = asarray([17, 40, 45, 19, 50, 49, 36, 55, 36, 49])
labels = zeros(rows*n_classes)
for i in range(rows*n_classes):
if (char >= 0 and char < n_classes):
labels[i] = char
e_classes = 1
elif (char == -1):
labels[i] = i % n_classes
e_classes = n_classes
elif (char >= -8):
for j in range(7):
if ((-2)-j == char):
labels[i] = ((i + j*100) // 10) % n_classes
# if (char == 980311):
# for i in range(10):
# labels[i*10:(i+1)*10] = test
out = model.predict([latent_points, labels])
out = (out + 1) / 2.0
if (eucl):
plot_euclidean_distance(out, e_classes)
else:
if (not ascii_out):
save_plot(out, rows, n_classes, save_name)
else:
ascii_print(out, rows, n_classes)
text = ['this', 'is', 'placeholder', 'text', 'this text is unneccessarily spaced ']
# text string is not None
while (n_classes == 47):
new_text = input("Enter text here: ").split()
if (new_text != []):
text = new_text
print("Using text: ", text)
text, space_arr = placeTextInArray(text, text_width)
# text to int array
labels = zeros((space_arr.shape), dtype=int)
for i, _ in enumerate(labels):
for j, _ in enumerate(labels[i]):
try:
labels[i,j] = to_label(text[i][j])
except:
labels[i,j] = 0
if (labels[i,j] == -1):
labels[i,j] = 0
space_arr[i,j] = True
height, width = labels.shape[0], labels.shape[1]
lat_pts = generate_latent_points_similar(latent_dim, height*width, lptf_name, variation=text_var)
out = model.predict([lat_pts, labels.reshape(height*width)])
out = (out +1) / 2.0
for h in range(height):
for w in range(width):
if (space_arr[h, w]):
out[h*width+w] = zeros((28, 28, 1), dtype=float32)
if (not ascii_out):
save_plot(out, height, width)
else:
ascii_print(out, height, width)
input('Sorry, "-t" is only available for 47-class models\n')