-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
1270 lines (1115 loc) · 50.4 KB
/
utils.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Some codes from https://github.com/Newmu/dcgan_code
"""
from __future__ import division
import math
import json
import random
import pprint
import scipy.misc
import numpy as np
from time import gmtime, strftime
from six.moves import xrange
import pdb
# MEEE
import json
from colorama import init, Fore, Back, Style
import tensorflow as tf
import tensorflow.contrib.slim as slim
init(autoreset=True)
pp = pprint.PrettyPrinter()
get_stddev = lambda x, k_h, k_w: 1/math.sqrt(k_w*k_h*x.get_shape()[-1])
def show_all_variables():
model_vars = tf.trainable_variables()
slim.model_analyzer.analyze_vars(model_vars, print_info=True)
def get_image(image_path, input_height, input_width,
resize_height=64, resize_width=64,
crop=True, grayscale=False):
image = imread(image_path, grayscale)
return transform(image, input_height, input_width,
resize_height, resize_width, crop)
def save_images(images, size, image_path):
return imsave(inverse_transform(images), size, image_path)
def imread(path, grayscale = False):
if (grayscale):
return scipy.misc.imread(path, flatten = True).astype(np.float)
else:
return scipy.misc.imread(path).astype(np.float)
def merge_images(images, size):
return inverse_transform(images)
def merge(images, size):
h, w = images.shape[1], images.shape[2]
print("MEEE in merge, images shape: " + str(images.shape))
if (images.shape[3] in (3,4)):
c = images.shape[3]
img = np.zeros((h * size[0], w * size[1], c))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[j * h:j * h + h, i * w:i * w + w, :] = image
return img
elif images.shape[3]==1:
img = np.zeros((h * size[0], w * size[1]))
for idx, image in enumerate(images):
i = idx % size[1]
j = idx // size[1]
img[j * h:j * h + h, i * w:i * w + w] = image[:,:,0]
return img
else:
raise ValueError('in merge(images,size) images parameter '
'must have dimensions: HxW or HxWx3 or HxWx4')
def imsave(images, size, path):
image = np.squeeze(merge(images, size))
return scipy.misc.imsave(path, image)
def center_crop(x, crop_h, crop_w,
resize_h=64, resize_w=64):
if crop_w is None:
crop_w = crop_h
h, w = x.shape[:2]
j = int(round((h - crop_h)/2.))
i = int(round((w - crop_w)/2.))
return scipy.misc.imresize(
x[j:j+crop_h, i:i+crop_w], [resize_h, resize_w])
def transform(image, input_height, input_width,
resize_height=64, resize_width=64, crop=True):
if crop:
cropped_image = center_crop(
image, input_height, input_width,
resize_height, resize_width)
else:
cropped_image = scipy.misc.imresize(image, [resize_height, resize_width])
return np.array(cropped_image)/127.5 - 1.
def inverse_transform(images):
return (images+1.)/2.
def to_json(output_path, *layers):
with open(output_path, "w") as layer_f:
lines = ""
for w, b, bn in layers:
layer_idx = w.name.split('/')[0].split('h')[1]
B = b.eval()
if "lin/" in w.name:
W = w.eval()
depth = W.shape[1]
else:
W = np.rollaxis(w.eval(), 2, 0)
depth = W.shape[0]
biases = {"sy": 1, "sx": 1, "depth": depth, "w": ['%.2f' % elem for elem in list(B)]}
if bn != None:
gamma = bn.gamma.eval()
beta = bn.beta.eval()
gamma = {"sy": 1, "sx": 1, "depth": depth, "w": ['%.2f' % elem for elem in list(gamma)]}
beta = {"sy": 1, "sx": 1, "depth": depth, "w": ['%.2f' % elem for elem in list(beta)]}
else:
gamma = {"sy": 1, "sx": 1, "depth": 0, "w": []}
beta = {"sy": 1, "sx": 1, "depth": 0, "w": []}
if "lin/" in w.name:
fs = []
for w in W.T:
fs.append({"sy": 1, "sx": 1, "depth": W.shape[0], "w": ['%.2f' % elem for elem in list(w)]})
lines += """
var layer_%s = {
"layer_type": "fc",
"sy": 1, "sx": 1,
"out_sx": 1, "out_sy": 1,
"stride": 1, "pad": 0,
"out_depth": %s, "in_depth": %s,
"biases": %s,
"gamma": %s,
"beta": %s,
"filters": %s
};""" % (layer_idx.split('_')[0], W.shape[1], W.shape[0], biases, gamma, beta, fs)
else:
fs = []
for w_ in W:
fs.append({"sy": 5, "sx": 5, "depth": W.shape[3], "w": ['%.2f' % elem for elem in list(w_.flatten())]})
lines += """
var layer_%s = {
"layer_type": "deconv",
"sy": 5, "sx": 5,
"out_sx": %s, "out_sy": %s,
"stride": 2, "pad": 1,
"out_depth": %s, "in_depth": %s,
"biases": %s,
"gamma": %s,
"beta": %s,
"filters": %s
};""" % (layer_idx, 2**(int(layer_idx)+2), 2**(int(layer_idx)+2),
W.shape[0], W.shape[3], biases, gamma, beta, fs)
layer_f.write(" ".join(lines.replace("'","").split()))
def make_gif(images, fname, duration=2, true_image=False):
import moviepy.editor as mpy
def make_frame(t):
try:
x = images[int(len(images)/duration*t)]
except:
x = images[-1]
if true_image:
return x.astype(np.uint8)
else:
return ((x+1)/2*255).astype(np.uint8)
clip = mpy.VideoClip(make_frame, duration=duration)
clip.write_gif(fname, fps = len(images) / duration)
def encode(sess, dcgan, config):
idx = 354
batch_files = dcgan.data[idx*config.batch_size:(idx+1)*config.batch_size]
batch = [
get_image(batch_file,
input_height=dcgan.input_height,
input_width=dcgan.input_width,
resize_height=dcgan.output_height,
resize_width=dcgan.output_width,
crop=dcgan.crop,
grayscale=dcgan.grayscale) for batch_file in batch_files]
if dcgan.grayscale:
batch_images = np.array(batch).astype(np.float32)[:, :, :, None]
else:
batch_images = np.array(batch).astype(np.float32)
batch_z = np.random.uniform(-1, 1, [config.batch_size, dcgan.z_dim]) \
.astype(np.float32)
encoded = sess.run(dcgan.D, feed_dict={dcgan.inputs: batch_images})
print("MEEE Encode encoded shape: " + str(encoded.shape))
# sample_files = dcgan.data[0:dcgan.sample_num]
# sample = [
# get_image(sample_file,
# input_height=dcgan.input_height,
# input_width=dcgan.input_width,
# resize_height=dcgan.output_height,
# resize_width=dcgan.output_width,
# crop=dcgan.crop,
# grayscale=dcgan.grayscale) for sample_file in sample_files]
# if (dcgan.grayscale):
# sample_inputs = np.array(sample).astype(np.float32)[:, :, :, None]
# else:
# sample_inputs = np.array(sample).astype(np.float32)
def generate_random_images(sess, dcgan, rand_state, config, base_dir, time_stamp, cut, count):
num_images = cut["total_frame_num"]
# print("MEEE image_frame_dim: " + str(image_frame_dim))
idx = 0
# time_stamp = strftime("%Y%m%d-%H%M%S", gmtime())
while 1:
values = np.arange(0, 1, 1./config.batch_size)
z_sample = rand_state.uniform(-1, 1, size=(config.batch_size , dcgan.z_dim))
print("MEEE first z_sample: " + str(z_sample[0, :5]))
print("MEEE z_sample shape: " + str(z_sample.shape))
# for kdx, z in enumerate(z_sample): # Why many times sess.run(z_sample) ?
# print("MEEE kdx: " + str(kdx) + " z shape: " + str(z.shape))
# z[idx % config.batch_size] = values[kdx]
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
print("MEEE samples shape: " + str(samples.shape))
for n in range(config.batch_size):
if idx + 1 > num_images:
return
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{}_{:05d}'.format(json_file_name, config.dataset, time_stamp , count)
count += 1
img_path = config.sample_dir + "/" + save_name + '.png'
json_path = config.sample_dir + "/" + save_name + '.json'
print("img path rand gen: " + img_path);
# save_images(samples[0, :, :, :], [1, 1], './samples/test_single%s.png' % (0))
scipy.misc.imsave(img_path, samples[n, :, :, :])
rand_seed = z_sample[n, :].tolist()
# Save seed into json
with open(json_path, 'w') as outfile:
json.dump(rand_seed, outfile)
idx += 1
return count
def generate_image_from_seed(sess, dcgan, config):
json_path = config.input_seed_path
json_file_name = json_path.split("/")[-1]
json_file_name = json_file_name.split(".")[0]
seed = []
if json_path:
with open(json_path, 'r') as f:
seed = json.load(f)
print("MEEE seed read: " + str(seed))
else:
print(Fore.RED + "MEEE WARNING: Input seed path is None.")
return
z_sample_list = []
for i in range(config.batch_size):
z_sample_list.append(seed)
z_sample = np.asarray(z_sample_list, dtype=np.float32)
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
save_name = 'GenFrom_{}'.format(json_file_name)
img_path = './samples/' + save_name + '.png'
# save_images(samples[0, :, :, :], [1, 1], './samples/test_single%s.png' % (0))
scipy.misc.imsave(img_path, samples[0, :, :, :])
print(Fore.CYAN + "MEEE seed image generated: " + img_path)
def generate_traverse_all_latent_vectors(sess, dcgan, rand_state, config, base_dir, time_stamp, cut, count):
frames_per_period = cut["frames_per_period"]
start_image_file = cut["start_image"]
stored_images = 0
num_queued_images = 0
base_json_path = base_dir
start_image = []
with open(base_json_path + '/' + start_image_file + ".json", 'r') as f:
start_image = json.load(f)
start_image = np.asarray(start_image)
total_frame_num = frames_per_period * start_image.shape[0]
# total_frame_num = frames_per_period * len(start_image)
curr_seed_idx = 0
while stored_images < total_frame_num:
batch_idx = 0
batch_seeds = np.zeros(shape=(config.batch_size, 100), dtype=np.float32)
while batch_idx < config.batch_size:
if curr_seed_idx >= 100:
break
# Do things
step_idx = num_queued_images % frames_per_period
print("stapidx: " + str(step_idx))
print("currSeedIdx: " + str(curr_seed_idx))
result_z = traverse_latent_vectors_step(start_image, step_idx, curr_seed_idx, cut)
batch_seeds[batch_idx] = np.asarray(result_z)
batch_idx += 1
num_queued_images += 1
# If condition met, increase idx
if num_queued_images % frames_per_period == 0:
curr_seed_idx += 1
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: batch_seeds})
# Naming
for i in range(config.batch_size):
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{:05d}'.format(json_file_name, time_stamp , count)
count += 1
# TODO: Create timestampt dir
img_path = config.sample_dir + "/" + save_name + '.png'
scipy.misc.imsave(img_path, samples[i, :, :, :])
print(Fore.CYAN + "Continuous random interp image generated: " + img_path)
stored_images += 1
if stored_images >= total_frame_num:
return count
return count
def traverse_latent_vectors_step(start_image, step_idx, curr_seed_idx, cut):
frames_per_period = cut["frames_per_period"]
is_wrap = cut["is_wrap"]
is_exp_ease = cut["is_exp_ease"]
traverse_num = start_image[curr_seed_idx]
if is_wrap:
traverse_length = 2.0
else:
traverse_length = 4.0
if is_exp_ease:
ratio = step_idx / frames_per_period
traverse_num += traverse_length * exp_ease_inout(ratio, 0.0, 1.0, cut)
else:
step_size = traverse_length / frames_per_period
traverse_num += step_size * step_idx
if not is_wrap:
if traverse_num > 1.0:
traverse_num = 1.0 - (traverse_num - 1.0)
if traverse_num < -1.0:
traverse_num = -1.0 - (traverse_num + 1.0)
else:
if traverse_num > 1.0:
traverse_num = -1.0 + (traverse_num - 1.0)
result = start_image.copy()
result[curr_seed_idx] = traverse_num
return result
def exp_ease_inout(ratio, low, high, cut):
power = cut["power"]
t = ratio * 2.0
if t < 1.0:
result = ((high-low)/2.0) * np.power(t, power) + low
else:
result = -((high-low)/2.0) * (np.float_power(abs(t-2), power) - 2) + low
return result
def generate_all101(sess, dcgan, rand_state, config, base_dir, time_stamp, cut, count):
allOne = np.ones((100))
allZero = np.zeros((100))
allNeg = allOne.copy() * -1.0
batch_seeds = np.zeros(shape=(config.batch_size, 100), dtype=np.float32)
batch_seeds[0] = allOne
batch_seeds[1] = allZero
batch_seeds[2] = allNeg
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: batch_seeds})
one_save_name = "{}-allOne.png".format(config.dataset)
zero_save_name = "{}-allZero.png".format(config.dataset)
neg_save_name = "{}-allneg.png".format(config.dataset)
one_img_path = config.sample_dir + "/" + one_save_name + '.png'
scipy.misc.imsave(one_img_path, samples[0, :, :, :])
zero_img_path = config.sample_dir + "/" + zero_save_name + '.png'
scipy.misc.imsave(zero_img_path, samples[1, :, :, :])
neg_img_path = config.sample_dir + "/" + neg_save_name + '.png'
scipy.misc.imsave(neg_img_path, samples[2, :, :, :])
return 3
def generate_single_value_changes(sess, dcgan, config, base_dir, time_stamp, cut, count):
change_idx_num = cut["change_idx_num"]
# time_stamp = strftime("%Y%m%d-%H%M%S", gmtime())
starting_image_path = cut["start_image"]
# json_path = config.input_seed_path
seed = [] # Will be reassigned before use
if starting_image_path:
full_starting_image_path = "/".join((base_dir, starting_image_path + ".json"))
with open(full_starting_image_path, 'r') as f:
seed = json.load(f)
print("MEEE seed read: " + str(seed))
else:
print(Fore.RED + "MEEE WARNING: Input seed path is None.")
return
z_sample_list = []
# for i in range(config.batch_size):
step = 0.00001
for i in range(5):
for j in range(10):
z_sample_list.append(seed[:])
for k in range(change_idx_num):
seed[k] += step
print("seed[0]: " + str(seed[0]) + " step: " + str(step))
step *= 10
# Reset json
if starting_image_path:
full_starting_image_path = "/".join((base_dir, starting_image_path + ".json"))
with open(full_starting_image_path, 'r') as f:
seed = json.load(f)
print("MEEE seed read: " + str(seed))
else:
print(Fore.RED + "MEEE WARNING: Input seed path is None.")
# if starting_image_path:
# with open(starting_image_path, 'r') as f:
# seed = json.load(f)
# print("MEEE seed read: " + str(seed))
# else:
# print(Fore.RED + "MEEE WARNING: Input seed path is None.")
while len(z_sample_list) < config.batch_size:
z_sample_list.append(seed)
for i in range(50):
print("double check: " + str(z_sample_list[i][0]))
z_sample = np.asarray(z_sample_list, dtype=np.float32)
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
# Generate batch images
saved_idx = 0
for i in range(5):
for j in range(10):
# save_name = '{}_{}_{}_{:02d}'.format(time_stamp, change_idx_num, str(5-i), j)
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{}_{:05d}'.format(json_file_name, config.dataset, time_stamp , count)
count += 1
img_path = config.sample_dir + "/" + save_name + '.png'
scipy.misc.imsave(img_path, samples[saved_idx, :, :, :])
print(Fore.CYAN + "MEEE mode12 image generated: " + img_path)
saved_idx+=1
return count
def generate_sin_cycle_all_100(sess, dcgan, config, base_dir, time_stamp, cut, count):
# time_stamp = strftime("%Y%m%d-%H%M%S", gmtime())
starting_image_path = cut["start_image"]
# json_path = config.input_seed_path
seed = [] # Will be reassigned before use
if starting_image_path:
full_starting_image_path = "/".join((base_dir, starting_image_path + ".json"))
with open(full_starting_image_path, 'r') as f:
seed = json.load(f)
print("MEEE seed read: " + str(seed))
else:
print(Fore.RED + "MEEE WARNING: Input seed path is None.")
# if starting_image_path:
# with open(starting_image_path, 'r') as f:
# seed = json.load(f)
# print("MEEE seed read: " + str(seed))
# else:
# print(Fore.RED + "MEEE WARNING: Input seed path is None.")
orig_seed = seed[:]
# sin_cycle_json_path = config.sin_cycle_json
# sin_cycle_json_path = cut["params"]
z_sample_list = []
num_cycles = 2
# frames_per_cycle = 30 * 6# PARAM one cycle in 6 seconds
frames_per_cycle = cut["params"]["frames_per_cycle"]
num_total_frames = frames_per_cycle * 100
# num_frames_per_number = frames_per_cycle * num_cycles
sin_step = (2 * math.pi) / frames_per_cycle
saved_frame = 0
curr_frame = 0
while curr_frame < num_total_frames:
z_sample_list = []
for i in range(config.batch_size):
z_sample_list.append(seed[:])
# Update seed with sin
seed = orig_seed[:]
seed_idx = int(int(curr_frame) / int(frames_per_cycle)) % 100 # %100 to prevent going over 100
print("seed_idx: " + str(seed_idx) + " : " + str(curr_frame) + " / " + str(frames_per_cycle) + " % " + str(num_total_frames))
seed[seed_idx] = math.sin((curr_frame % frames_per_cycle) * sin_step)
curr_frame+=1
z_sample = np.asarray(z_sample_list, dtype=np.float32)
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
for i in range(config.batch_size):
# save_name = 'Sin_cycle_all_100_{}_{:05d}'.format(time_stamp, saved_frame)
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{}_{:05d}'.format(json_file_name, config.dataset, time_stamp , count)
img_path = config.sample_dir + "/" + save_name + '.png'
# scipy.misc.imsave(img_path, samples[i, :, :, :])
print(Fore.CYAN + "MEEE sin cycle all 100 image generated: " + img_path)
saved_frame += 1
if saved_frame >= num_total_frames:
return count
return count
def generate_sin_cycle(sess, dcgan, config, base_dir, time_stamp, cut, count):
num_cycles = cut["num_cycles"]
seconds_per_cycle = cut["seconds_per_cycle"]
mode = cut["mode_num"]
starting_image_path = cut["start_image"]
# time_stamp = strftime("%Y%m%d-%H%M%S", gmtime())
# json_path = config.input_seed_path
seed = [] # Will be reassigned before use
if starting_image_path:
full_starting_image_path = "/".join((base_dir, starting_image_path + ".json"))
with open(full_starting_image_path, 'r') as f:
seed = json.load(f)
print("MEEE seed read: " + str(seed))
else:
print(Fore.RED + "MEEE WARNING: Input seed path is None.")
# if starting_image_path:
# with open(starting_image_path, 'r') as f:
# seed = json.load(f)
# print("MEEE seed read: " + str(seed))
# else:
# print(Fore.RED + "MEEE WARNING: Input seed path is None.")
z_sample_list = []
frames_per_cycle = 30 * seconds_per_cycle # PARAM one cycle in 10 seconds
num_total_frames = frames_per_cycle * num_cycles
sin_step = (2 * math.pi) / frames_per_cycle
saved_frame = 0
curr_frame = 0
# sin_cycle_json_path = config.sin_cycle_json
# cycle_data = cut["cycle_data"]
if mode == 14:
# if "cycle_data" in cut:
# # with open(sin_cycle_json_path, 'r') as f:
# # cycle_json = json.load(f)
# print("MEEE cycle data read: " + str(cycle_data))
# else:
# print(Fore.RED + "MEEE WARNING: sin cycle data is None.")
num_total_frames = cut["overall_length"]
cycle_data = cut["data"]
while curr_frame < num_total_frames:
z_sample_list = []
for i in range(config.batch_size):
z_sample_list.append(seed[:])
# Update seed with sin
for sin in cycle_data:
sin_step = (2*math.pi) / sin["framesPerCycle"]
seed[sin["idx"]] = math.sin(curr_frame * sin_step + sin["phaseShift"] * 2 * math.pi)
curr_frame+=1
z_sample = np.asarray(z_sample_list, dtype=np.float32)
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
for i in range(config.batch_size):
# save_name = 'Sin_cycle_withJson_{}_{:05d}'.format(time_stamp, saved_frame)
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{}_{:05d}'.format(json_file_name, config.dataset, time_stamp , count)
count += 1
img_path = config.sample_dir + "/" + save_name + '.png'
scipy.misc.imsave(img_path, samples[i, :, :, :])
print(Fore.CYAN + "MEEE sin cycle image generated: " + img_path)
saved_frame += 1
if saved_frame >= num_total_frames:
return count
return count
# Newer version to match PGAN standard
def generate_random_walk(sess, dcgan, rand_state, config, base_dir, time_stamp, cut, count):
mode_num = cut["mode_num"]
stored_images = 0
num_queued_images = 0
start_image_json = cut["start_image"]
total_frame_num = cut["total_frame_num"]
base_json_path = base_dir
with open("/".join((base_json_path, start_image_json)) + ".json") as f:
start_seed = json.load(f)
start_seed = np.asarray(start_seed, dtype=np.float32)
curr_phases = np.zeros(start_seed.shape, dtype=np.float32)
if mode_num == 3:
s = cut["speed"]
# a = cut["amplitude"]
# amplitudes = (rand_state.random_sample(start_seed.shape) - 0.5) * 2.0 * a
sin_seed = np.zeros(start_seed.shape, dtype=np.float32)
# offset_seed = (rand_state.random_sample(start_seed.shape) - np.float32(0.5)) * np.pi * np.float32(2.0) # [-pi, pi)
curr_speed = rand_state.random_sample(start_seed.shape) * s
offset_seed = (rand_state.rand(start_seed.shape[0]) - np.float32(0.5)) * np.pi * np.float32(2.0) # [-pi, pi)
curr_phases = np.zeros(start_seed.shape, dtype=np.float32)
curr_seed = sin_seed
elif mode_num == 4 or mode_num == 5:
curr_seed = start_seed
while stored_images < total_frame_num:
batch_idx = 0
batch_seeds = np.zeros(shape=(config.batch_size, 100), dtype=np.float32)
while batch_idx < config.batch_size:
batch_seeds[batch_idx] = curr_seed
if mode_num == 3:
sin_seed, curr_phases = sinusoidal_walk(offset_seed, num_queued_images, cut, curr_phases)
curr_seed = start_seed + sin_seed
elif mode_num == 4:
curr_seed = wrap_walk(start_seed, curr_seed, rand_state, cut)
elif mode_num == 5:
curr_seed = clamp_walk(start_seed, curr_seed, rand_state, cut)
batch_idx += 1
num_queued_images += 1
# labels = np.zeros([batch_seeds[0].shape[0]] + Gs.input_shapes[1][1:]) # Dummy data input
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: batch_seeds})
# Save
for i in range(config.batch_size):
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{}_{:05d}'.format(json_file_name, config.dataset, time_stamp , count)
count += 1
img_path = config.sample_dir + "/" + save_name + '.png'
scipy.misc.imsave(img_path, samples[i, :, :, :])
print(Fore.CYAN + "Image saved: " + img_path)
stored_images += 1
if stored_images >= total_frame_num:
print(Fore.CYAN + "Done !")
return count
return count
def legacy_sinusoidal_walk(curr_speed, amplitudes, t, cut):
result = np.zeros(amplitudes.shape, dtype=np.float32)
for i in range(result.shape[0]):
# curr_phases[i] = ((phase_shift[i] - curr_phases[i]) * easing) + curr_phases[i]
result[i] = np.float32(curr_speed[i])*t
result = np.sin(result) * amplitudes[i]
return result
def sinusoidal_walk(phase_shift, t, cut, curr_phases):
amplitude = cut["amplitude"]
s = cut["speed"]
result = np.zeros(phase_shift.shape, dtype=np.float32)
# Mode D vars
easing = cut["easing"]
# if test_mode == "D":
for i in range(result.shape[0]):
# result[i] = np.float32(s)*t + phase_shift[i]
curr_phases[i] = ((phase_shift[i] - curr_phases[i]) * easing) + curr_phases[i]
result[i] = np.float32(s)*t + curr_phases[i]
result = np.sin(result) * amplitude
return result, curr_phases
def clamp_walk(start_seed, walk_seed, rand_state, cut):
max_speed = cut["max_speed"]
clamp_boundary = cut["clamp_boundary"]
random_walk_val = (rand_state.random_sample(walk_seed.shape) - np.float32(0.5)) * np.float32(2.0) * np.float32(max_speed)
walked_seeds = walk_seed + random_walk_val
result = np.zeros(walk_seed.shape, dtype=np.float32)
for i in range(result.shape[0]):
orig_val = start_seed[i]
curr = walked_seeds[i]
upper_bound = min(1.0, orig_val+clamp_boundary)
lower_bound = max(-1.0, orig_val-clamp_boundary)
if i == 13:
print("orig: " + str(orig_val) + " upper: " + str(upper_bound) + " lower: " + str(lower_bound) + " curr_bef: " + str(curr))
if curr > upper_bound:
curr = upper_bound
if curr < lower_bound:
curr = lower_bound
result[i] = curr
if i == 13:
print("curr aft: " + str(curr))
return result
def wrap_walk(start_seed, walk_seed, rand_state, cut):
# wrap_buffer_size = cut["wrap_buffer_size"]
max_speed = cut["max_speed"]
random_walk_val = (rand_state.random_sample(walk_seed.shape) - np.float32(0.5)) * np.float32(2.0) * np.float32(max_speed)
walked_seeds = walk_seed + random_walk_val
result = np.zeros(walk_seed.shape, dtype=np.float32)
for i in range(result.shape[0]):
curr = walked_seeds[i]
if curr > 1.0:
curr = -1.0 + (curr - 1.0)
if curr < -1.0:
curr = 1.0 + (curr - 1.0)
result[i] = curr
# max_boundary = start_seed[i] + wrap_buffer_size
# min_boundary = start_seed[i] - wrap_buffer_size
# result[i] = curr
# if curr > max_boundary:
# result[i] = min_boundary + (curr - max_boundary)
# if curr < min_boundary:
# result[i] = max_boundary + (curr - min_boundary)
return result
# Legacy latent walk modes, has legacy naming convention
def generate_walk_in_latent_space(sess, dcgan, config, base_dir, time_stamp, cut, count):
walk_num = cut["total_frame_num"]
mode = cut["mode_num"]
starting_image_path = cut["start_image"]
max_vector_length = cut["max_vector_length"]
# time_stamp = strftime("%Y%m%d-%H%M%S", gmtime())
# json_path = config.input_seed_path
# json_file_name = starting_image_path.split("/")[-1]
# json_file_name = json_file_name.split(".")[0]
seed = [] # Will be reassigned before use
if starting_image_path:
full_starting_image_path = "/".join((base_dir, starting_image_path + ".json"))
with open(full_starting_image_path, 'r') as f:
seed = json.load(f)
print("MEEE seed read: " + str(seed))
else:
print(Fore.RED + "MEEE WARNING: Input seed path is None.")
return
if config.walk_rand_seed == None:
rand_seed = random.randint(0, 10000)
else:
rand_seed = config.walk_rand_seed
print(Fore.RED + "MEEE rand seed + 1: " + str(rand_seed + 1))
random.seed(rand_seed)
# rand_state_json_path = './samples/Walk_{}_randState.json'.format(time_stamp)
# with open(rand_state_json_path, 'w') as outfile:
# json.dump(walk_rand_state, outfile)
# print(Fore.CYAN + "MEEE saved rand state json: " + rand_state_json_path)
walked = 0
# max_vector_length = 0.003 #0.005 # PARAM
vector = np.random.uniform(-max_vector_length, max_vector_length, size=(1, dcgan.z_dim))[0]
# Zero out half the vector if mode 11 and move 50/100 vectors
if mode == 11:
vector[50:] = np.zeros(50)
temp_max_val = 0.0
while walked < walk_num:
z_sample_list = []
for i in range(config.batch_size):
z_sample_list.append(seed)
# seed = walk_seed(seed)
if mode == 6:
seed, vector, temp_max_val = vector_walk_seed(seed, vector, 6, 0.0003, None)
elif mode == 7:
seed, vector = vector_walk_seed(seed, vector, 7, 0.0003, None)
elif mode == 8:
seed = walk_seed(seed)
elif mode == 9:
seed, vector = vector_walk_seed(seed, vector, 9, 0.0003, None)
elif mode == 11:
seed, vector = vector_walk_seed(seed, vector, 11, 0.0003, None)
elif mode == 16:
seed, vector, temp_max_val = vector_walk_seed(seed, vector, 16, config.max_jump_step, config.min_jump_step, temp_max_val)
# seed = walk_seed(seed, config.max_jump_step)
# Generate batch images
z_sample = np.asarray(z_sample_list, dtype=np.float32)
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: z_sample})
for i in range(config.batch_size):
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{}_{}_{:05d}'.format(json_file_name, config.dataset, rand_seed, time_stamp , count)
count += 1
img_path = config.sample_dir + "/" + save_name + '.png'
scipy.misc.imsave(img_path, samples[i, :, :, :])
print(Fore.CYAN + "MEEE walk image generated: " + img_path)
walked += 1
if walked >= walk_num:
print("Final temp_max_val: " + str(temp_max_val))
return count
return count
# Walk with a vector
def vector_walk_seed(seed, vector, walk_mode, max_step, min_step, temp_max_val=None):
# maxVectorWalkStep = max_step#0.0003 #0.0005 # PARAM
result_vector = []
result_seed = []
max_vel = max_step * 27.3 # PARAM
max_vel_perc = 0.2 # PARAM
for idx in range(len(seed)):
if min_step == None:
vectorWalkStep = random.uniform(-max_step, max_step)
else:
diff = max_step - min_step
rand_step = random.uniform(-diff, diff)
vectorWalkStep = rand_step + (rand_step / abs(rand_step)) * min_step
# vectorWalkStep = random.uniform(-1.0, 1.0)
if walk_mode == 11:
if idx > 49:
vectorWalkStep = 0.0
vector_cell = vector[idx] + vectorWalkStep
if walk_mode == 16: # Cap velocity and push towards the center if around max/min
vel_bound = max_vel * (1.0-max_vel_perc)
if vector_cell > vel_bound: # If near upper bound, needs to go lower dir
vector_cell = vector[idx] - abs(vectorWalkStep)
elif vector_cell < -vel_bound: # If near lower bound, needs to go higher dir
vector_cell = vector[idx] + abs(vectorWalkStep)
# Debug
if abs(vector_cell) > temp_max_val and temp_max_val != None:
temp_max_val = abs(vector_cell)
print("Vector curr val: " + str(vector_cell))
cell = seed[idx] + vector_cell
if walk_mode == 6: # clamp mode
cell = max(min(cell, 1.0), -1.0)
elif walk_mode == 7 or walk_mode == 11 or walk_mode == 16:
if cell > 1: # Wrap mode
print("MEEE vector walk cell before: " + str(cell))
frac, whole = math.modf(cell)
cell = -1 + frac
print("MEEE vector walk cell after: " + str(cell))
elif cell < -1:
print("MEEE vector walk cell before: " + str(cell))
frac, whole = math.modf(cell)
cell = 1 + frac
print("MEEE vector walk cell after: " + str(cell))
elif walk_mode == 9:
if cell > 1 or cell < -1:
vector_cell = -vector_cell
result_vector.append(vector_cell)
result_seed.append(cell)
return result_seed, result_vector, temp_max_val
# Walk a single step for all 100 numbers in a seed
def walk_seed(seed, max_step=0.035):
# maxWalkStep = 0.035 # PARAM
maxWalkStep = max_step # PARAM
result_seed = []
for idx in range(len(seed)):
random_cell = random.uniform(-maxWalkStep, maxWalkStep)
print("MEEE random cell: " + str(random_cell))
cell = seed[idx] + random_cell
# print("MEEE updated cell: " + str(cell))
# cell = np.clip(-1.0, 1.0, cell)
cell = max(min(cell, 1.0), -1.0)
# print("MEEE after clip: " + str(cell))
result_seed.append(cell)
np_result_seed = np.asarray(result_seed, dtype=np.float32)
np_seed = np.asarray(seed, dtype=np.float32)
# print("MEEE walk seed diff: " + str(np_result_seed - np_seed))
return result_seed
def generate_continuous_random_interps(sess, dcgan, config, base_dir, time_stamp, cut, count):
total_frame_num = cut["total_frame_num"]
mode = cut["mode_num"]
if mode == 2:
is_cut, is_rand_steps_per_interp = True, True
elif mode == 3:
is_cut, is_rand_steps_per_interp = True, False
elif mode == 4:
is_cut, is_rand_steps_per_interp = False, True
elif mode == 5:
is_cut, is_rand_steps_per_interp = False, False
else:
is_cut, is_rand_steps_per_interp = None, None
# steps_per_interp = 32 # 16 # PARAM
steps_per_interp = cut["steps_per_interp"] # 16 # PARAM
stored_images = 0
num_queued_images = 0
# time_stamp = strftime("%Y%m%d-%H%M%S", gmtime())
rand_batch_z = np.random.uniform(-1, 1, size=(2 , dcgan.z_dim))
z1 = np.asarray(rand_batch_z[0, :])
z2 = np.asarray(rand_batch_z[1, :])
while stored_images < total_frame_num:
batch_idx = 0
batch_seeds = np.zeros(shape=(config.batch_size, 100))
while batch_idx < config.batch_size:
interp_idx = num_queued_images % steps_per_interp
print("interp_idx: " + str(interp_idx))
# for i, ratio in enumerate(np.linspace(0, 1, steps_per_interp)):
ratio = np.linspace(0, 1, steps_per_interp)[interp_idx]
# print("i: " + str(i) + " ratio: " + str(ratio))
print(" ratio: " + str(ratio))
slerped_z = slerp(ratio, z1, z2)
# print("MEEE ratio: " + str(ratio) + " z1: " + str(z1.shape) + " z2: " + str(z2.shape))
# batch_seeds = np.append(batch_seeds, [slerped_z], axis=0)
print("MEEE batch_idx: " + str(batch_idx))
batch_seeds[batch_idx] = slerped_z
# print("MEEE batch_seeds: " + str(batch_seeds.shape) + " , slerped_z: " + str(slerped_z.shape))
batch_idx += 1
num_queued_images += 1
# if batch_idx >= config.batch_size:
# break
if num_queued_images % steps_per_interp == 0:
interp_frame_nums = [8, 16, 32, 8, 25, 36, 85, 7, 16, 10, 40, 10, 30, 20, 30, 34, 50, 25, 50, 100, 120, 250, 300, 512]
if is_rand_steps_per_interp:
steps_per_interp = interp_frame_nums[random.randint(0, len(interp_frame_nums)-1)]
num_queued_images = 0
rand_batch_z = np.random.uniform(-1, 1, size=(config.batch_size , dcgan.z_dim))
if is_cut:
z1 = np.asarray(rand_batch_z[1, :]) #PARAM A - B - C or A - B | C - D
else:
z1 = z2
z2 = np.asarray(rand_batch_z[0, :])
print("MEEE newly assigned z1: " + str(z1))
print("MEEE newly gen uniform z2: " + str(z2))
samples = sess.run(dcgan.sampler, feed_dict={dcgan.z: batch_seeds})
# Naming
for i in range(config.batch_size):
json_file = config.gen_json.split("/")[-1]
json_file_name = json_file.split(".")[0]
save_name = '{}_{}_{}_{:05d}'.format(json_file_name, config.dataset, time_stamp , count)
count += 1
img_path = config.sample_dir + "/" + save_name + '.png'
scipy.misc.imsave(img_path, samples[i, :, :, :])
print(Fore.CYAN + "MEEE Continuous random interp image generated: " + img_path)
stored_images += 1
if stored_images >= total_frame_num:
return count
return count
def generate_continuous_interps_from_json(sess, dcgan, rand_state, config, base_dir, time_stamp, cut, count):
mode_num = cut["mode_num"]
interp_data = cut["interp_data"]
steps_per_interp = interp_data[0][2] # 16 # PARAM
stored_images = 0
num_queued_images = 0
base_json_path = base_dir
seedA = []
seedB = []
with open(base_json_path + '/' + interp_data[0][0] + ".json", 'r') as f:
seedA = json.load(f)
with open(base_json_path + '/' + interp_data[0][1] + ".json", 'r') as f:
seedB = json.load(f)
total_frame_num = 0
for i in range(len(interp_data)):
total_frame_num += interp_data[i][2]
curr_cut_idx = 0
rand_batch_z = rand_state.uniform(-1, 1, size=(2 , dcgan.z_dim))
z1 = np.asarray(seedA, dtype=np.float32)
z2 = np.asarray(seedB, dtype=np.float32)
while stored_images < total_frame_num:
batch_idx = 0
batch_seeds = np.zeros(shape=(config.batch_size, 100), dtype=np.float32)
while batch_idx < config.batch_size:
interp_idx = num_queued_images % steps_per_interp
steps_per_interp_mode = steps_per_interp
if mode_num == 2 or mode_num == 6: # And not last frame
steps_per_interp_mode = steps_per_interp + 1
ratio = np.linspace(0, 1, steps_per_interp_mode)[interp_idx]
ratio = np.float32(ratio)
print(" ratio: " + str(ratio))
if mode_num == 1 or mode_num == 2:
result_z = slerp(ratio, z1, z2)
elif mode_num == 6: # Mode 6
result_z = lerp(ratio, z1, z2)
elif mode_num == 7: # Mode 6
result_z = wrap_lerp(ratio, z1, z2, cut)
elif mode_num == 9:
result_z = exp_ease(ratio, z1, z2, cut)
elif mode_num == 10:
result_z = sinusoid_ease(ratio, z1, z2, cut)
elif mode_num == 11:
result_z = flicker_lerp(ratio, z1, z2, rand_state, cut)
elif mode_num == 12:
result_z = exp_ease_inout(ratio, z1, z2, cut)
elif mode_num == 13:
result_z = exp_ease_inout(ratio, z1, z2, cut)
result_z = step_flicker(result_z, rand_state, cut)