Spaces:
Build error
Build error
brayden-gg
commited on
Commit
•
3d3e7ab
1
Parent(s):
f6d8e1b
imprived speed of mdn sampling
Browse files- app.py +75 -76
- convenience.py +39 -62
app.py
CHANGED
@@ -30,18 +30,22 @@ for writer_id in [120, 80]:
|
|
30 |
all_loaded_data.append(loaded_data)
|
31 |
|
32 |
default_loaded_data = all_loaded_data[-1]
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# data for writer interpolation
|
35 |
-
|
36 |
writer_mean_Ws = []
|
37 |
all_word_writer_Ws = []
|
38 |
all_word_writer_Cs = []
|
39 |
weight = 0.7
|
40 |
|
41 |
def update_target_word(target_word):
|
42 |
-
|
43 |
for word in target_word.split(" "):
|
44 |
-
|
45 |
|
46 |
writer_mean_Ws.clear()
|
47 |
for loaded_data in all_loaded_data:
|
@@ -50,7 +54,7 @@ def update_target_word(target_word):
|
|
50 |
|
51 |
all_word_writer_Ws.clear()
|
52 |
all_word_writer_Cs.clear()
|
53 |
-
for word in
|
54 |
all_writer_Ws, all_writer_Cs = convenience.get_DSD(net, word, writer_mean_Ws, all_loaded_data, device)
|
55 |
all_word_writer_Ws.append(all_writer_Ws)
|
56 |
all_word_writer_Cs.append(all_writer_Cs)
|
@@ -62,20 +66,8 @@ def update_target_word(target_word):
|
|
62 |
def update_writer_slider(val):
|
63 |
global weight
|
64 |
weight = val
|
65 |
-
|
66 |
-
|
67 |
-
width = 50
|
68 |
-
|
69 |
-
for word, all_writer_Ws, all_writer_Cs in zip(words, all_word_writer_Ws, all_word_writer_Cs):
|
70 |
-
all_W_c = convenience.get_writer_blend_W_c([1 - weight, weight], all_writer_Ws, all_writer_Cs)
|
71 |
-
all_commands = convenience.get_commands(net, word, all_W_c)
|
72 |
-
|
73 |
-
for [x, y, t] in all_commands:
|
74 |
-
if t == 0:
|
75 |
-
dr.line((px+width, py, x+width, y), 255, 1)
|
76 |
-
px, py = x, y
|
77 |
-
width += np.max(all_commands[:, 0]) + 25
|
78 |
-
|
79 |
return im.convert("RGB")
|
80 |
|
81 |
def update_chosen_writers(writer1, writer2):
|
@@ -87,37 +79,77 @@ def update_chosen_writers(writer1, writer2):
|
|
87 |
all_loaded_data.append(loaded_data)
|
88 |
|
89 |
return gr.Slider.update(label=f"{writer1} vs. {writer2}"), update_writer_slider(weight)
|
90 |
-
'''
|
91 |
-
def choose_writer(writ, c1, c2, c3, c4, val):
|
92 |
-
all_loaded_data.clear()
|
93 |
-
w = int(writ.split(" ")[1])
|
94 |
-
loaded_data = dl.next_batch(TYPE='TRAIN', uid=w, tids=list(range(num_samples)))
|
95 |
-
all_loaded_data.append(loaded_data)
|
96 |
-
return char_grid(c1, c2, c3, c4, val)
|
97 |
-
'''
|
98 |
|
99 |
# for character blend
|
100 |
def interpolate_chars(c1, c2, weight):
|
|
|
|
|
101 |
net.clamp_mdn = 0
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
|
105 |
def choose_blend_chars(c1, c2):
|
106 |
return gr.Slider.update(label=f"'{c1}' vs. '{c2}'")
|
107 |
|
108 |
# for MDN
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
""
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
update_target_word("hello world")
|
|
|
121 |
|
122 |
with gr.Blocks() as demo:
|
123 |
with gr.Tabs():
|
@@ -163,39 +195,6 @@ with gr.Blocks() as demo:
|
|
163 |
char1.change(fn=choose_blend_chars, inputs=[char1, char2], outputs=[char_slider])
|
164 |
char2.change(fn=choose_blend_chars, inputs=[char1, char2], outputs=[char_slider])
|
165 |
|
166 |
-
"""
|
167 |
-
with gr.TabItem("Character Grid"): #slow
|
168 |
-
with gr.Row():
|
169 |
-
with gr.Column():
|
170 |
-
char1 = gr.Dropdown(choices=avail_char_list, value="y", label="Character 1")
|
171 |
-
with gr.Column():
|
172 |
-
char2 = gr.Dropdown(choices=avail_char_list, value="s", label="Character 2")
|
173 |
-
with gr.Column():
|
174 |
-
char3 = gr.Dropdown(choices=avail_char_list, value="u", label="Character 3")
|
175 |
-
with gr.Column():
|
176 |
-
char4 = gr.Dropdown(choices=avail_char_list, value="n", label="Character 4")
|
177 |
-
with gr.Row():
|
178 |
-
submit_button = gr.Button(value="Blend y, s, u, and n!")
|
179 |
-
'''
|
180 |
-
with gr.Row():
|
181 |
-
radio_options2 = ["Writer " + str(n) for n in writer_options]
|
182 |
-
writer = gr.Radio(radio_options2, value="Writer 80", label="Style for Writer")
|
183 |
-
writer.change(fn=choose_writer, inputs=[writer, char1, char2, char3, char4, slider2], outputs=[output])
|
184 |
-
'''
|
185 |
-
#slider2 = gr.Slider(2, 20, value=10, label="Grid Size", step=1)
|
186 |
-
|
187 |
-
default_image = convenience.sample_character_grid(['y', 's', 'u', 'n'], 10, net, [default_loaded_data], device).convert("RGB")
|
188 |
-
output = gr.Image(default_image)
|
189 |
-
|
190 |
-
char1.change(fn=choose_grid_chars, inputs=[char1, char2, char3, char4], outputs=[submit_button])
|
191 |
-
char2.change(fn=choose_grid_chars, inputs=[char1, char2, char3, char4], outputs=[submit_button])
|
192 |
-
char3.change(fn=choose_grid_chars, inputs=[char1, char2, char3, char4], outputs=[submit_button])
|
193 |
-
char4.change(fn=choose_grid_chars, inputs=[char1, char2, char3, char4], outputs=[submit_button])
|
194 |
-
|
195 |
-
|
196 |
-
#slider2.change(fn=char_grid, inputs=[char1, char2, char3, char4, slider2], outputs=[output])
|
197 |
-
submit_button.click(fn=char_grid, inputs=[char1, char2, char3, char4], outputs=[output])
|
198 |
-
"""
|
199 |
|
200 |
with gr.TabItem("Add Randomness"):
|
201 |
mdn_word = gr.Textbox(label="Target Word", value="hello world", max_lines=1)
|
@@ -211,14 +210,14 @@ with gr.Blocks() as demo:
|
|
211 |
with gr.Column():
|
212 |
scale_rand = gr.Slider(0, 3, value=0.5, label="Scale of Randomness")
|
213 |
with gr.Row():
|
214 |
-
|
215 |
with gr.Row():
|
216 |
default_im = convenience.mdn_single_sample("hello world", 0.5, 1, net, [default_loaded_data], device).convert('RGB')
|
217 |
mdn_output = gr.Image(default_im)
|
218 |
|
219 |
-
max_rand.change(fn=
|
220 |
-
scale_rand.change(fn=
|
221 |
-
|
222 |
-
mdn_word.submit(fn=
|
223 |
|
224 |
demo.launch()
|
|
|
30 |
all_loaded_data.append(loaded_data)
|
31 |
|
32 |
default_loaded_data = all_loaded_data[-1]
|
33 |
+
mdn_words = []
|
34 |
+
mdn_mean_Ws = []
|
35 |
+
all_word_mdn_Ws = []
|
36 |
+
all_word_mdn_Cs = []
|
37 |
|
38 |
# data for writer interpolation
|
39 |
+
writer_words = []
|
40 |
writer_mean_Ws = []
|
41 |
all_word_writer_Ws = []
|
42 |
all_word_writer_Cs = []
|
43 |
weight = 0.7
|
44 |
|
45 |
def update_target_word(target_word):
|
46 |
+
writer_words.clear()
|
47 |
for word in target_word.split(" "):
|
48 |
+
writer_words.append(word)
|
49 |
|
50 |
writer_mean_Ws.clear()
|
51 |
for loaded_data in all_loaded_data:
|
|
|
54 |
|
55 |
all_word_writer_Ws.clear()
|
56 |
all_word_writer_Cs.clear()
|
57 |
+
for word in writer_words:
|
58 |
all_writer_Ws, all_writer_Cs = convenience.get_DSD(net, word, writer_mean_Ws, all_loaded_data, device)
|
59 |
all_word_writer_Ws.append(all_writer_Ws)
|
60 |
all_word_writer_Cs.append(all_writer_Cs)
|
|
|
66 |
def update_writer_slider(val):
|
67 |
global weight
|
68 |
weight = val
|
69 |
+
net.clamp_mdn = 0
|
70 |
+
im = convenience.draw_words(writer_words, all_word_writer_Ws, all_word_writer_Cs, [1 - weight, weight], net)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
return im.convert("RGB")
|
72 |
|
73 |
def update_chosen_writers(writer1, writer2):
|
|
|
79 |
all_loaded_data.append(loaded_data)
|
80 |
|
81 |
return gr.Slider.update(label=f"{writer1} vs. {writer2}"), update_writer_slider(weight)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
|
83 |
# for character blend
|
84 |
def interpolate_chars(c1, c2, weight):
|
85 |
+
"""Generates an image of handwritten text based on target_sentence"""
|
86 |
+
|
87 |
net.clamp_mdn = 0
|
88 |
+
|
89 |
+
letters = [c1, c2]
|
90 |
+
character_weights = [1 - weight, weight]
|
91 |
+
M = len(letters)
|
92 |
+
mean_global_W = convenience.get_mean_global_W(net, all_loaded_data[0], device)
|
93 |
+
|
94 |
+
all_Cs = torch.zeros(1, M, convenience.L, convenience.L)
|
95 |
+
for i in range(M): # get corners of grid
|
96 |
+
W_vector, char_matrix = convenience.get_DSD(net, letters[i], [mean_global_W], [default_loaded_data], device)
|
97 |
+
all_Cs[:, i, :, :] = char_matrix
|
98 |
+
|
99 |
+
all_Ws = mean_global_W.reshape(1, 1, convenience.L)
|
100 |
+
|
101 |
+
all_W_c = convenience.get_character_blend_W_c(character_weights, all_Ws, all_Cs)
|
102 |
+
all_commands = convenience.get_commands(net, letters[0], all_W_c)
|
103 |
+
|
104 |
+
width = 60
|
105 |
+
x_offset = 325
|
106 |
+
im = Image.fromarray(np.zeros([160, 750]))
|
107 |
+
dr = ImageDraw.Draw(im)
|
108 |
+
for [x, y, t] in all_commands:
|
109 |
+
if t == 0:
|
110 |
+
dr.line((
|
111 |
+
px + width/2 + x_offset,
|
112 |
+
py - width/2, # letters are shifted down for some reason
|
113 |
+
x + width/2 + + x_offset,
|
114 |
+
y - width/2), 255, 1)
|
115 |
+
px, py = x, y
|
116 |
+
|
117 |
+
|
118 |
+
return im.convert("RGB")
|
119 |
|
120 |
def choose_blend_chars(c1, c2):
|
121 |
return gr.Slider.update(label=f"'{c1}' vs. '{c2}'")
|
122 |
|
123 |
# for MDN
|
124 |
+
|
125 |
+
def update_mdn_word(target_word):
|
126 |
+
mdn_words.clear()
|
127 |
+
for word in target_word.split(" "):
|
128 |
+
mdn_words.append(word)
|
129 |
+
|
130 |
+
mdn_mean_Ws.clear()
|
131 |
+
mean_global_W = convenience.get_mean_global_W(net, default_loaded_data, device)
|
132 |
+
mdn_mean_Ws.append(mean_global_W)
|
133 |
+
|
134 |
+
all_word_mdn_Ws.clear()
|
135 |
+
all_word_mdn_Cs.clear()
|
136 |
+
for word in mdn_words:
|
137 |
+
all_writer_Ws, all_writer_Cs = convenience.get_DSD(net, word, mdn_mean_Ws, [default_loaded_data], device)
|
138 |
+
all_word_mdn_Ws.append(all_writer_Ws)
|
139 |
+
all_word_mdn_Cs.append(all_writer_Cs)
|
140 |
+
|
141 |
+
return sample_mdn(net.scale_sd, net.clamp_mdn)
|
142 |
+
|
143 |
+
|
144 |
+
def sample_mdn(maxs, maxr):
|
145 |
+
net.clamp_mdn = maxr
|
146 |
+
net.scale_sd = maxs
|
147 |
+
im = convenience.draw_words(mdn_words, all_word_mdn_Ws, all_word_mdn_Cs, [1], net)
|
148 |
+
return im.convert("RGB")
|
149 |
+
|
150 |
|
151 |
update_target_word("hello world")
|
152 |
+
update_mdn_word("hello world")
|
153 |
|
154 |
with gr.Blocks() as demo:
|
155 |
with gr.Tabs():
|
|
|
195 |
char1.change(fn=choose_blend_chars, inputs=[char1, char2], outputs=[char_slider])
|
196 |
char2.change(fn=choose_blend_chars, inputs=[char1, char2], outputs=[char_slider])
|
197 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
198 |
|
199 |
with gr.TabItem("Add Randomness"):
|
200 |
mdn_word = gr.Textbox(label="Target Word", value="hello world", max_lines=1)
|
|
|
210 |
with gr.Column():
|
211 |
scale_rand = gr.Slider(0, 3, value=0.5, label="Scale of Randomness")
|
212 |
with gr.Row():
|
213 |
+
mdn_sample_button = gr.Button(value="Resample!")
|
214 |
with gr.Row():
|
215 |
default_im = convenience.mdn_single_sample("hello world", 0.5, 1, net, [default_loaded_data], device).convert('RGB')
|
216 |
mdn_output = gr.Image(default_im)
|
217 |
|
218 |
+
max_rand.change(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output])
|
219 |
+
scale_rand.change(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output])
|
220 |
+
mdn_sample_button.click(fn=sample_mdn, inputs=[scale_rand, max_rand], outputs=[mdn_output])
|
221 |
+
mdn_word.submit(fn=update_mdn_word, inputs=[mdn_word], outputs=[mdn_output])
|
222 |
|
223 |
demo.launch()
|
convenience.py
CHANGED
@@ -295,26 +295,19 @@ def mdn_video(target_word, num_samples, scale_sd, clamp_mdn, net, all_loaded_dat
|
|
295 |
us_target_word = re.sub(r"\s+", '_', target_word)
|
296 |
os.makedirs(f"./results/{us_target_word}_mdn_samples", exist_ok=True)
|
297 |
for i in range(num_samples):
|
298 |
-
im = Image.fromarray(np.zeros([160, 750]))
|
299 |
-
dr = ImageDraw.Draw(im)
|
300 |
-
width = 50
|
301 |
-
|
302 |
net.scale_sd = scale_sd
|
303 |
net.clamp_mdn = clamp_mdn
|
304 |
|
305 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
306 |
|
|
|
|
|
307 |
for word in words:
|
308 |
writer_Ws, writer_Cs = get_DSD(net, word, [mean_global_W], [all_loaded_data[0]], device)
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
if t == 0:
|
314 |
-
dr.line((px+width, py, x+width, y), 255, 1)
|
315 |
-
px, py = x, y
|
316 |
-
width += np.max(all_commands[:, 0]) + 25
|
317 |
-
|
318 |
im.convert("RGB").save(f'results/{us_target_word}_mdn_samples/sample_{i}.png')
|
319 |
# Convert fromes to video using ffmpeg
|
320 |
photos = ffmpeg.input(f'results/{us_target_word}_mdn_samples/sample_*.png', pattern_type='glob', framerate=10)
|
@@ -325,27 +318,19 @@ def sample_blended_writers(writer_weights, target_sentence, net, all_loaded_data
|
|
325 |
"""Generates an image of handwritten text based on target_sentence"""
|
326 |
words = target_sentence.split(' ')
|
327 |
|
328 |
-
im = Image.fromarray(np.zeros([160, 750]))
|
329 |
-
dr = ImageDraw.Draw(im)
|
330 |
-
width = 50
|
331 |
-
|
332 |
writer_mean_Ws = []
|
333 |
for loaded_data in all_loaded_data:
|
334 |
mean_global_W = get_mean_global_W(net, loaded_data, device)
|
335 |
writer_mean_Ws.append(mean_global_W)
|
336 |
|
|
|
|
|
337 |
for word in words:
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
if t == 0:
|
344 |
-
dr.line((px+width, py, x+width, y), 255, 1)
|
345 |
-
px, py = x, y
|
346 |
-
width += np.max(all_commands[:, 0]) + 25
|
347 |
-
|
348 |
-
return im
|
349 |
|
350 |
|
351 |
def sample_character_grid(letters, grid_size, net, all_loaded_data, device="cpu"):
|
@@ -418,26 +403,12 @@ def writer_interpolation_video(target_sentence, transition_time, net, all_loaded
|
|
418 |
|
419 |
for i in range(n - 1):
|
420 |
for j in range(transition_time):
|
421 |
-
im = Image.fromarray(np.zeros([160, 750]))
|
422 |
-
dr = ImageDraw.Draw(im)
|
423 |
-
width = 50
|
424 |
-
|
425 |
completion = j/(transition_time)
|
426 |
|
427 |
individual_weights = [1 - completion, completion]
|
428 |
writer_weights = [0] * i + individual_weights + [0] * (n - 2 - i)
|
429 |
|
430 |
-
|
431 |
-
all_writer_Ws, all_writer_Cs = word_Ws[k], word_Cs[k]
|
432 |
-
all_W_c = get_writer_blend_W_c(writer_weights, all_writer_Ws, all_writer_Cs)
|
433 |
-
all_commands = get_commands(net, word, all_W_c)
|
434 |
-
|
435 |
-
for [x, y, t] in all_commands:
|
436 |
-
if t == 0:
|
437 |
-
dr.line((px+width, py, x+width, y), 255, 1)
|
438 |
-
px, py = x, y
|
439 |
-
width += np.max(all_commands[:, 0]) + 25
|
440 |
-
|
441 |
im.convert("RGB").save(f"./results/{target_sentence}_blend_frames/frame_{str(i * transition_time + j).zfill(3)}.png")
|
442 |
|
443 |
# Convert fromes to video using ffmpeg
|
@@ -452,35 +423,25 @@ def mdn_single_sample(target_word, scale_sd, clamp_mdn, net, all_loaded_data, de
|
|
452 |
max_scale: the maximum value used to scale SD while sampling (increment is based on num samples)
|
453 |
'''
|
454 |
words = target_word.split(' ')
|
455 |
-
im = Image.fromarray(np.zeros([160, 750]))
|
456 |
-
dr = ImageDraw.Draw(im)
|
457 |
-
width = 50
|
458 |
|
459 |
net.scale_sd = scale_sd
|
460 |
net.clamp_mdn = clamp_mdn
|
461 |
|
462 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
463 |
|
|
|
|
|
464 |
for word in words:
|
465 |
writer_Ws, writer_Cs = get_DSD(net, word, [mean_global_W], [all_loaded_data[0]], device)
|
466 |
-
|
467 |
-
|
468 |
|
469 |
-
|
470 |
-
if t == 0:
|
471 |
-
dr.line((px+width, py, x+width, y), 255, 1)
|
472 |
-
px, py = x, y
|
473 |
-
width += np.max(all_commands[:, 0]) + 25
|
474 |
|
475 |
-
return im
|
476 |
|
477 |
def sample_blended_chars(character_weights, letters, net, all_loaded_data, device="cpu"):
|
478 |
"""Generates an image of handwritten text based on target_sentence"""
|
479 |
|
480 |
-
width = 60
|
481 |
-
im = Image.fromarray(np.zeros([100, 100]))
|
482 |
-
dr = ImageDraw.Draw(im)
|
483 |
-
|
484 |
M = len(letters)
|
485 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
486 |
|
@@ -494,6 +455,9 @@ def sample_blended_chars(character_weights, letters, net, all_loaded_data, devic
|
|
494 |
all_W_c = get_character_blend_W_c(character_weights, all_Ws, all_Cs)
|
495 |
all_commands = get_commands(net, letters[0], all_W_c)
|
496 |
|
|
|
|
|
|
|
497 |
for [x, y, t] in all_commands:
|
498 |
if t == 0:
|
499 |
dr.line((
|
@@ -512,8 +476,6 @@ def char_interpolation_video(letters, transition_time, net, all_loaded_data, dev
|
|
512 |
|
513 |
os.makedirs(f"./results/{''.join(letters)}_frames", exist_ok=True) # make a folder for the frames
|
514 |
|
515 |
-
width = 50
|
516 |
-
|
517 |
M = len(letters)
|
518 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
519 |
|
@@ -530,11 +492,11 @@ def char_interpolation_video(letters, transition_time, net, all_loaded_data, dev
|
|
530 |
individual_weights = [1 - completion, completion]
|
531 |
character_weights = [0] * i + individual_weights + [0] * (M - 2 - i)
|
532 |
all_W_c = get_character_blend_W_c(character_weights, all_Ws, all_Cs)
|
533 |
-
all_commands = get_commands(net,
|
534 |
|
535 |
im = Image.fromarray(np.zeros([100, 100]))
|
536 |
dr = ImageDraw.Draw(im)
|
537 |
-
|
538 |
for [x, y, t] in all_commands:
|
539 |
if t == 0:
|
540 |
dr.line((
|
@@ -553,3 +515,18 @@ def char_interpolation_video(letters, transition_time, net, all_loaded_data, dev
|
|
553 |
videos.run(overwrite_output=True)
|
554 |
|
555 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
295 |
us_target_word = re.sub(r"\s+", '_', target_word)
|
296 |
os.makedirs(f"./results/{us_target_word}_mdn_samples", exist_ok=True)
|
297 |
for i in range(num_samples):
|
|
|
|
|
|
|
|
|
298 |
net.scale_sd = scale_sd
|
299 |
net.clamp_mdn = clamp_mdn
|
300 |
|
301 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
302 |
|
303 |
+
word_Ws = []
|
304 |
+
word_Cs = []
|
305 |
for word in words:
|
306 |
writer_Ws, writer_Cs = get_DSD(net, word, [mean_global_W], [all_loaded_data[0]], device)
|
307 |
+
word_Ws.append(writer_Ws)
|
308 |
+
word_Cs.append(writer_Cs)
|
309 |
+
|
310 |
+
im = draw_words(words, word_Ws, word_Cs, [1], net)
|
|
|
|
|
|
|
|
|
|
|
311 |
im.convert("RGB").save(f'results/{us_target_word}_mdn_samples/sample_{i}.png')
|
312 |
# Convert fromes to video using ffmpeg
|
313 |
photos = ffmpeg.input(f'results/{us_target_word}_mdn_samples/sample_*.png', pattern_type='glob', framerate=10)
|
|
|
318 |
"""Generates an image of handwritten text based on target_sentence"""
|
319 |
words = target_sentence.split(' ')
|
320 |
|
|
|
|
|
|
|
|
|
321 |
writer_mean_Ws = []
|
322 |
for loaded_data in all_loaded_data:
|
323 |
mean_global_W = get_mean_global_W(net, loaded_data, device)
|
324 |
writer_mean_Ws.append(mean_global_W)
|
325 |
|
326 |
+
word_Ws = []
|
327 |
+
word_Cs = []
|
328 |
for word in words:
|
329 |
+
writer_Ws, writer_Cs = get_DSD(net, word, writer_mean_Ws, all_loaded_data, device)
|
330 |
+
word_Ws.append(writer_Ws)
|
331 |
+
word_Cs.append(writer_Cs)
|
332 |
+
|
333 |
+
return draw_words(words, word_Ws, word_Cs, writer_weights, net)
|
|
|
|
|
|
|
|
|
|
|
|
|
334 |
|
335 |
|
336 |
def sample_character_grid(letters, grid_size, net, all_loaded_data, device="cpu"):
|
|
|
403 |
|
404 |
for i in range(n - 1):
|
405 |
for j in range(transition_time):
|
|
|
|
|
|
|
|
|
406 |
completion = j/(transition_time)
|
407 |
|
408 |
individual_weights = [1 - completion, completion]
|
409 |
writer_weights = [0] * i + individual_weights + [0] * (n - 2 - i)
|
410 |
|
411 |
+
im = draw_words(words, word_Ws, word_Cs, writer_weights, net)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
412 |
im.convert("RGB").save(f"./results/{target_sentence}_blend_frames/frame_{str(i * transition_time + j).zfill(3)}.png")
|
413 |
|
414 |
# Convert fromes to video using ffmpeg
|
|
|
423 |
max_scale: the maximum value used to scale SD while sampling (increment is based on num samples)
|
424 |
'''
|
425 |
words = target_word.split(' ')
|
|
|
|
|
|
|
426 |
|
427 |
net.scale_sd = scale_sd
|
428 |
net.clamp_mdn = clamp_mdn
|
429 |
|
430 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
431 |
|
432 |
+
word_Ws = []
|
433 |
+
word_Cs = []
|
434 |
for word in words:
|
435 |
writer_Ws, writer_Cs = get_DSD(net, word, [mean_global_W], [all_loaded_data[0]], device)
|
436 |
+
word_Ws.append(writer_Ws)
|
437 |
+
word_Cs.append(writer_Cs)
|
438 |
|
439 |
+
return draw_words(words, word_Ws, word_Cs, [1], net)
|
|
|
|
|
|
|
|
|
440 |
|
|
|
441 |
|
442 |
def sample_blended_chars(character_weights, letters, net, all_loaded_data, device="cpu"):
|
443 |
"""Generates an image of handwritten text based on target_sentence"""
|
444 |
|
|
|
|
|
|
|
|
|
445 |
M = len(letters)
|
446 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
447 |
|
|
|
455 |
all_W_c = get_character_blend_W_c(character_weights, all_Ws, all_Cs)
|
456 |
all_commands = get_commands(net, letters[0], all_W_c)
|
457 |
|
458 |
+
width = 60
|
459 |
+
im = Image.fromarray(np.zeros([100, 100]))
|
460 |
+
dr = ImageDraw.Draw(im)
|
461 |
for [x, y, t] in all_commands:
|
462 |
if t == 0:
|
463 |
dr.line((
|
|
|
476 |
|
477 |
os.makedirs(f"./results/{''.join(letters)}_frames", exist_ok=True) # make a folder for the frames
|
478 |
|
|
|
|
|
479 |
M = len(letters)
|
480 |
mean_global_W = get_mean_global_W(net, all_loaded_data[0], device)
|
481 |
|
|
|
492 |
individual_weights = [1 - completion, completion]
|
493 |
character_weights = [0] * i + individual_weights + [0] * (M - 2 - i)
|
494 |
all_W_c = get_character_blend_W_c(character_weights, all_Ws, all_Cs)
|
495 |
+
all_commands = get_commands(net, letters[i], all_W_c)
|
496 |
|
497 |
im = Image.fromarray(np.zeros([100, 100]))
|
498 |
dr = ImageDraw.Draw(im)
|
499 |
+
width = 50
|
500 |
for [x, y, t] in all_commands:
|
501 |
if t == 0:
|
502 |
dr.line((
|
|
|
515 |
videos.run(overwrite_output=True)
|
516 |
|
517 |
|
518 |
+
def draw_words(words, word_Ws, word_Cs, writer_weights, net):
|
519 |
+
im = Image.fromarray(np.zeros([160, 750]))
|
520 |
+
dr = ImageDraw.Draw(im)
|
521 |
+
width = 50
|
522 |
+
for word, all_writer_Ws, all_writer_Cs in zip(words, word_Ws, word_Cs):
|
523 |
+
all_W_c = get_writer_blend_W_c(writer_weights, all_writer_Ws, all_writer_Cs)
|
524 |
+
all_commands = get_commands(net, word, all_W_c)
|
525 |
+
|
526 |
+
for [x, y, t] in all_commands:
|
527 |
+
if t == 0:
|
528 |
+
dr.line((px+width, py, x+width, y), 255, 1)
|
529 |
+
px, py = x, y
|
530 |
+
width += np.max(all_commands[:, 0]) + 25
|
531 |
+
|
532 |
+
return im
|