-
Notifications
You must be signed in to change notification settings - Fork 1
/
phyre_utils.py
1255 lines (1106 loc) · 47.5 KB
/
phyre_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
import numpy as np
import matplotlib.pyplot as plt
import torch as T
import torch.nn.functional as F
from phyre_rolllout_collector import load_phyre_rollouts, collect_solving_observations, collect_solving_dataset
from matplotlib import cm
import matplotlib.colors as colors
import cv2
import phyre
import os
import pickle
import random
import json
import gzip
from PIL import ImageDraw, Image, ImageFont
import io
import logging as L
def make_dual_dataset(path, size=(32,32), save=True):
if os.path.exists(path+".pickle"):
with open(path+'.pickle', 'rb') as fhandle:
X, Y = pickle.load(fhandle)
else:
X = load_phyre_rollouts(path)
X, Y = prepare_data(X, size)
X = T.tensor(X).float()
Y = T.tensor(Y).float()
if save:
with open(path+'.pickle', 'wb') as fhandle:
pickle.dump((X,Y), fhandle)
dataloader = T.utils.data.DataLoader(T.utils.data.TensorDataset(X,Y), 32, shuffle=True)
return dataloader
def make_mono_dataset_old(path, size=(32,32), save=True, tasks=[], shuffle=True):
if os.path.exists(path+".pickle") and os.path.exists(path+"_index.pickle"):
X = T.load(path+'.pickle')
index = T.load(path+'_index.pickle')
print(f"Loaded dataset from {path} with shape:", X.shape)
else:
if tasks:
collect_solving_observations(path, tasks, n_per_task=1, stride=5, size=size)
data_generator = load_phyre_rollout_data(path)
data, index = format_raw_rollout_data(data_generator, size=size)
X = T.tensor(data).float()
if save:
T.save(X, path+'.pickle')
T.save(index, path+'_index.pickle')
dataloader = T.utils.data.DataLoader(T.utils.data.TensorDataset(X), 32, shuffle=shuffle)
return dataloader, index
def make_mono_dataset(path, size=(32,32), tasks=[], batch_size = 32, solving=True, n_per_task=1, shuffle=True, proposal_dict=None, dijkstra=False, pertempl=False):
if os.path.exists(path+"/data.pickle") and os.path.exists(path+"/index.pickle"):
try:
with gzip.open(path+'/data.pickle', 'rb') as fp:
X, Y = pickle.load(fp)
X = T.tensor(X).float()
Y = T.tensor(Y).float()
except OSError as e:
print("WARNING still unzipped data file at", path)
with open(path+'/data.pickle', 'rb') as fp:
X, Y = pickle.load(fp)
X = T.tensor(X).float()
Y = T.tensor(Y).float()
with open(path+'/index.pickle', 'rb') as fp:
index = pickle.load(fp)
# TRAIN TEST SPLIT
print(f"Loaded dataset from {path} with shape:", X.shape)
else:
if proposal_dict is None:
train_ids, dev_ids, test_ids = phyre.get_fold("ball_within_template", 0)
all_tasks = train_ids + dev_ids + test_ids
else:
all_tasks = tasks
collect_solving_dataset(path, all_tasks, n_per_task=n_per_task, stride=5, size=size, solving=solving, proposal_dict=proposal_dict, dijkstra=dijkstra, pertempl=pertempl)
with gzip.open(path+'/data.pickle', 'rb') as fp:
X, Y = pickle.load(fp)
with open(path+'/index.pickle', 'rb') as fp:
index = pickle.load(fp)
X = T.tensor(X).float()
Y = T.tensor(Y).float()
print(f"Loaded dataset from {path} with shape:", X.shape)
# MAKE CORRECT SELECTION
selection = [i for (i,task) in enumerate(index) if task in tasks]
#print(len(index), len(tasks), len(selection))
X = X[selection]
Y = Y[selection]
index = [index[s] for s in selection]
L.info(f"Loaded dataset from {path} with shape: {X.shape}")
assert len(X)==len(Y)==len(index), "All should be of equal length"
X = X/255 # correct for uint8 encoding
I = T.arange(len(X), dtype=int)
dataloader = T.utils.data.DataLoader(T.utils.data.TensorDataset(X, Y, I), batch_size, shuffle=shuffle)
return dataloader, index
def shrink_data(path):
for folder in os.listdir(path):
if folder.__contains__("64xy"):
print("loading:", folder)
try:
with open(path+'/'+folder+'/data.pickle', 'rb') as fp:
data = pickle.load(fp)
data = (np.array(data)*255).astype(np.uint8)
with gzip.GzipFile(path+'/'+folder+'/data.pickle', 'wb') as fp:
pickle.dump(data, fp)
except Exception as e:
print(f"error loading {folder}:\n{e}")
finally:
print(folder, "finished")
def compare_viz(paths, column):
tlist = []
beginning = "isy2020/phyre/result/flownet/inspect/VS-bottleneck/default/ball_within_template_fold_0/poch_0_0_diff.png"
for path in paths:
if type(path==tuple):
name = "isy2020/phyre/result/flownet/inspect/"+path[0]+path[1]+"/ball_within_template_fold_0/eval-viz-tensor.pt"
else:
name = "isy2020/phyre/result/flownet/inspect/"+path+"/default/ball_within_template_fold_0/eval-viz-tensor.pt"
tensor = T.load(name)
tlist.append(tensor[:,column])
T.stack(tlist, dim=1)
rows = [str(i/4) for i in range(100)]
vis_batch(tlist, "./", "viz_compare.png", rows = rows)
def vis_batch(batch, path, pic_id, text = [], rows=[], descr=[], save=True, font_size=11):
#print(batch.shape)
if len(batch.shape) == 4:
padded = F.pad(batch, (1,1,1,1), value=0.5)
elif len(batch.shape) == 5:
padded = F.pad(batch, (0,0,1,1,1,1), value=0.5)
else:
print("Unknown shape:", batch.shape)
#print(padded.shape)
reshaped = T.cat([T.cat([channels for channels in sample], dim=1) for sample in padded], dim=0)
#print(reshaped.shape)
if np.max(reshaped.numpy())>1.0:
reshaped = reshaped/256
os.makedirs(path, exist_ok=True)
if text or rows or descr:
if rows:
row_width = int(1.5*font_size//2*max([len(item) for item in rows]))
else:
row_width = 0
if descr:
descr_wid = int(1.5*font_size//2*max([len(item) for item in descr]))
else:
descr_wid = 0
if text:
text_height= int(1.5*font_size*max([len(item.split('\n')) for item in text]))
else:
text_height=0
if len(reshaped.shape) == 2:
reshaped = F.pad(reshaped, (row_width,descr_wid,text_height,0), value=1)
img = Image.fromarray(np.uint8(reshaped.numpy()*255), mode="L")
elif len(reshaped.shape) == 3:
reshaped = F.pad(reshaped, (0,0,row_width,descr_wid,text_height,0), value=1)
img = Image.fromarray(np.uint8(reshaped.numpy()*255))
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf", font_size)
draw = ImageDraw.Draw(img)
for i, words in enumerate(text):
x, y = row_width+i*(reshaped.shape[1]-row_width-descr_wid)//len(text), 0
draw.text((x, y), words, fill=(0) if len(reshaped.shape)==2 else (0,0,0), font=font)
for j, words in enumerate(rows):
x,y = 3, 10+text_height+j*(reshaped.shape[0]-text_height)//len(rows)
draw.text((x, y), words, fill=(0) if len(reshaped.shape)==2 else (0,0,0), font=font)
for j, words in enumerate(descr):
x,y = 5+reshaped.shape[1]-descr_wid, text_height+j*(reshaped.shape[0]-text_height)//len(descr)
#print(x,y)
draw.text((x, y), words, fill=(0) if len(reshaped.shape)==2 else (0,0,0), font=font)
if save:
img.save(f'{path}/'+pic_id+'.png')
else:
return img
else:
if save:
plt.imsave(f'{path}/'+pic_id+'.png', reshaped.numpy(), dpi=1000)
else:
return reshaped
def gifify(batch, path, pic_id, text = [], constant=None):
#print(batch.shape)
if np.max(batch.numpy())>1.0:
batch = batch/256
if len(batch.shape) == 4:
padded = F.pad(batch, (1,1,1,1), value=0.5)
elif len(batch.shape) == 5:
padded = F.pad(batch, (0,0,1,1,1,1), value=0.5)
else:
print("Unknown shape:", batch.shape)
os.makedirs(path, exist_ok=True)
frames = []
for f_id in range(padded.shape[1]):
frame = padded[:,f_id]
frame = T.cat([sample for sample in frame], dim=1)
if text:
text_height= 30
if len(frame.shape) == 2:
#frame = F.pad(frame, (0,0,text_height,0), value=0.0)
img = Image.fromarray(np.uint8(frame.numpy()*255), mode="L")
elif len(frame.shape) == 3:
#frame = F.pad(frame, (0,0,0,0,text_height,0), value=0.0)
img = Image.fromarray(np.uint8(frame.numpy()*255))
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf", 9)
draw = ImageDraw.Draw(img)
for i, words in enumerate(text):
x, y = i*frame.shape[1]//len(text), 0
draw.text((x, y), words, fill=(0) if len(frame.shape)==2 else (0,0,0), font=font)
else:
if len(frame.shape) == 2:
img = Image.fromarray(np.uint8(frame.numpy()*255), mode="L")
elif len(frame.shape) == 3:
img = Image.fromarray(np.uint8(frame.numpy()*255))
if constant is not None:
dst = Image.new('RGB', (img.width, img.height + constant.height), (255, 255, 255))
dst.paste(constant, (0, 0))
dst.paste(img, (0, constant.height))
img = dst
frames.append(img)
frames[0].save(f'{path}/'+pic_id+'.gif', save_all=True, append_images=frames[1:], optimize=True, duration=300, loop=0)
def make_visuals():
sim = phyre.initialize_simulator(["00018:013", "00020:007", "00018:035"], 'ball')
res = sim.simulate_action(0, sim.sample(0), stride=40)
while not res.status.is_solved():
res = sim.simulate_action(0, sim.sample(0), stride=40)
#init.save("result/visuals/init1.png")
obs = phyre.observations_to_uint8_rgb(sim.initial_scenes[0])
obs = np.pad(obs, ((5,5),(5,5),(0,0)))
init1 = Image.fromarray(obs)
init = init1.copy()
for frame in res.images:
obs = phyre.observations_to_uint8_rgb(frame)
obs = np.pad(obs, ((5,5),(5,5),(0,0)))
frame = np.pad(frame, ((5,5),(5,5)))
objects = Image.fromarray(np.flip((frame!=0), axis=0).astype(np.uint8)*100)
pic = Image.fromarray(obs)
#pic.putalpha(0.5)
init.paste(pic, (0,0), objects)
blended1 = init
res = sim.simulate_action(1, sim.sample(1), stride=20)
while not res.status.is_solved():
res = sim.simulate_action(1, sim.sample(1), stride=20)
#init.save("result/visuals/init1.png")
obs = phyre.observations_to_uint8_rgb(sim.initial_scenes[1])
obs = np.pad(obs, ((5,5),(5,5),(0,0)))
init2 = Image.fromarray(obs)
init = init2.copy()
for frame in res.images:
obs = phyre.observations_to_uint8_rgb(frame)
obs = np.pad(obs, ((5,5),(5,5),(0,0)))
frame = np.pad(frame, ((5,5),(5,5)))
objects = Image.fromarray(np.flip((frame!=0), axis=0).astype(np.uint8)*100)
pic = Image.fromarray(obs)
#pic.putalpha(0.5)
init.paste(pic, (0,0), objects)
blended2 = init
base = 256+10
back = Image.new("RGB",(4*base+15, base))
back.paste(init1, (0,0))
back.paste(blended1, (base+5,0))
back.paste(init2, (2*base+10,0))
back.paste(blended2, (3*base+15,0))
os.makedirs("result/visuals", exist_ok=True)
back.save("result/visuals/phyre.png")
"""
obs = phyre.observations_to_uint8_rgb(sim.initial_scenes)
print(obs.shape)
padded = np.flip(np.pad(obs, ((0,0),(5,5),(5,5),(0,0))), axis=1)
print(padded.shape)
init = Image.fromarray(np.concatenate(padded, axis=1))
"""
#init.save("result/visuals/blended1.png")
#objects.save("result/visuals/red.png")"""
def prepare_data(data, size):
targetchannel = 1
X, Y = [], []
print("Preparing dataset...")
#x = np.zeros((X.shape[0], 7, size[0], size[1]))
for variations in data:
with_base = len(variations) > 1
for (j, rollout) in enumerate(variations):
if not isinstance(rollout, np.ndarray):
break
#length = (2*len(rollout))//3
#rollout = rollout[:length]
roll = np.zeros((len(rollout), 7, size[0], size[1]))
for i, scene in enumerate(rollout):
channels = [(scene==j).astype(float) for j in range(1,8)]
roll[i] = np.stack([(cv2.resize(c, size, cv2.INTER_MAX)>0).astype(float) for c in channels])
roll = np.flip(roll, axis=2)
trajectory = (np.sum(roll[:,targetchannel], axis=0)>0).astype(float)
if not(with_base and j == 0):
action = (np.sum(roll[:,0], axis=0)>0).astype(float)
#goal_prior = dist_map(roll[0, 2] + roll[0, 3])
#roll[0, 0] = goal_prior
# TESTING ONLY
#roll[0, 1] = roll[0, 0]
if with_base and j == 0:
base = trajectory
else:
action_ball = roll[0, 0].copy()
roll[0, 0] = np.zeros_like(roll[0,0])
#print(goal_prior)
# Contains the initial scene without action
X.append(roll[0])
# Contains goaltarget, actiontarget, basetrajectory
Y.append(np.stack((trajectory, action, base if with_base else np.zeros_like(roll[0,0]), action_ball)))
#plt.imshow(trajectory)
#plt.show()
print("Finished preparing!")
return X, Y
def extract_channels_and_paths(rollout, path_idxs=[1,0], size=(32,32), gamma=1):
"""
returns init scenes from 'channels' followed by paths specified by 'path_idxs'
"""
paths = np.zeros((len(path_idxs), len(rollout), size[0], size[1]))
alpha = 1
for i, chans in enumerate(rollout):
# extract color codings from channels
#chans = np.array([(scene==ch).astype(float) for ch in channels])
# if first frame extract init scene
if not i:
init_scene = np.array([(cv2.resize(chans[ch], size, cv2.INTER_MAX)>0).astype(float) for ch in range(len(chans))])
# add path_idxs channels to paths
for path_i, idx in enumerate(path_idxs):
paths[path_i, i] = alpha*(cv2.resize(chans[idx], size, cv2.INTER_MAX)>0).astype(float)
alpha *= gamma
# flip y axis and concat init scene with paths
paths = np.flip(np.max(paths, axis=1).astype(float), axis=1)
init_scene = np.flip(init_scene, axis=1)
result = np.concatenate([init_scene, paths])
return result
def format_raw_rollout_data(data, size=(32,32)):
targetchannel = 1
data_bundle = []
lib_dict = dict()
print("Formating data...")
#x = np.zeros((X.shape[0], 7, size[0], size[1]))
for i, (base, trial, info) in enumerate(data):
print(f"at sample {i}; {info}")
#base_path = extract_channels_and_paths(base, channels=[1], path_idxs=[0], size=size)[1]
#trial_channels = extract_channels_and_paths(trial, size=size)
#sample = np.append(trial_channels, base_path[None], axis=0)
try:
task, subtask, number = info
base_path = extract_channels_and_paths(base, path_idxs=[1], size=size)[-1]
trial_channels = extract_channels_and_paths(trial, path_idxs=[1,2,0], size=size)
sample = np.append(trial_channels, base_path[None], axis=0)
#plt.imshow(np.concatenate(tuple(np.concatenate((sub, T.ones(32,1)*0.5), axis=1) for sub in sample), axis=1))
#plt.show()
data_bundle.append(sample)
# Create indexing dict
key = task+':'+subtask
if not key in lib_dict:
lib_dict[key] = [i]
else:
lib_dict[key].append(i)
except Exception as identifier:
print(identifier)
print("Finished preparing!")
return data_bundle, lib_dict
def load_phyre_rollout_data(path, base=True):
s = "/"
fp ="observations.pickle"
for task in os.listdir(path):
for variation in os.listdir(path+s+task):
if base:
with open(path+s+task+s+variation+s+'base'+s+fp, 'rb') as handle:
base_rollout = pickle.load(handle)
for trialfolder in os.listdir(path+s+task+s+variation):
final_path = path+s+task+s+variation+s+trialfolder+s+fp
with open(final_path, 'rb') as handle:
trial_rollout = pickle.load(handle)
if base:
yield(base_rollout, trial_rollout, (task, variation, trialfolder))
else:
yield(trial_rollout)
def draw_ball(w, x, y, r, invert_y=False):
"""inverts y axis """
x = int(w*x)
y = int(w*(1-y)) if invert_y else int(w*y)
r = w*r
X = T.arange(w).repeat((w, 1)).float()
Y = T.arange(w).repeat((w, 1)).transpose(0, 1).float()
X -= x # X Distance
Y -= y # Y Distance
dist = (X.pow(2)+Y.pow(2)).pow(0.5)
return (dist<r).float()
def action_delta_generator(pure_noise=False):
temp = 1
radfac = 0.025
coordfac = 0.1
#for x,y,r in zip([0.05,-0.05,0.1,-0.1],[0,0,0,0],[-0.1,-0.2,-0.3,0]):
#yield x,y,r
if not pure_noise:
for fac in [0.5,1,2]:
for rad in [0,1,-1]:
for xd,yd in [(1,0), (-1,0), (2,0), (-2,0), (-1,2), (1,2), (-1,-2), (-1,-2)]:
#print((fac*np.array((coordfac*xd, coordfac*yd, rad*radfac))))
yield (fac*np.array((coordfac*xd, coordfac*yd, rad*radfac)))
count = 0
while True:
count += 1
action = ((np.random.randn(3))*np.array([0.2,0.1,0.2])*temp)*0.1
#print(count,"th", "ACTION:", action)
if np.linalg.norm(action)<0.05:
continue
yield action
temp = 1.04*temp if temp<5 else temp
def pic_to_action_vector(pic, r_fac=1):
X, Y = 0, 0
for y in range(pic.shape[0]):
for x in range(pic.shape[1]):
if pic[y,x]:
X += pic[y,x]*x
Y += pic[y,x]*y
summed = pic.sum()
X /= pic.shape[0]*summed
Y /= pic.shape[0]*summed
r = np.sqrt(pic.sum()/(3.141592*pic.shape[0]**2))
return [X.item(), 1-Y.item(), r_fac*r.item()]
def grow_action_vector(pic, r_fac=1, show=False, num_seeds=1, mask=None, check_border=False, updates=5):
id = int((T.rand(1)*100))
#os.makedirs("result/flownet/solver/grower", exist_ok=True)
#plt.imsave(f"result/flownet/solver/grower/{id}.png", pic)
pic = pic*(pic>pic.mean())
#plt.imsave(f"result/flownet/solver/grower/{id}_thresh.png", pic)
wid = pic.shape[0]
def get_value(x,y,r):
ball = draw_ball(wid, x, y, r)
potential = T.sum(ball)
actual = T.sum(pic[ball.bool()])
value = (actual**0.5)*actual/potential
if mask is not None:
overlap = mask[ball>0].sum()
if overlap>0:
return -overlap
if check_border and ((x-r)<-0.00 or (y-r)<-0.00 or (x+r)>1.00 or (y+r)>1.00):
return min([x-r, 1-(x+r), y-r, 1-(y+r)])
return value
def move_and_grow(x,y,r,v):
delta = 0.7
positions = [(x+dx,y+dy) for (dx,dy) in [(-(0.3+delta)/30,0), ((0.3+delta)/30,0), (0,-(0.3+delta)/30), (0,(0.3+delta)/30)] if (0<=x+dx<1) and (0<=y+dy<1)]
bestpos = (x,y)
bestrad = r
bestv = v
for pos in positions:
value = get_value(*pos, r)
rad, val = grow(*pos,r,value)
if val>bestv:
bestpos = pos
bestrad = rad
bestv = val
return bestpos[0], bestpos[1], bestrad, bestv
def grow(x,y,r,v):
bestv = v
bestrad = r
for rad in [r+0.005, r+0.01, r+0.03, r-0.01]:
if 0<rad<0.3:
value = get_value(x,y,rad)
if value>bestv:
bestv = value
bestrad = rad
return bestrad, bestv
seeds = []
while len(seeds)<num_seeds:
r = 0.04 +np.random.rand()*0.05
try:
y, x = random.choice(T.nonzero((pic>0.01)))+T.rand(2)*0.05
seeds.append((x.item()/wid,y.item()/wid,r))
except Exception as e:
print("EXCEPTION", e)
y, x = wid//2, wid//2
seeds.append((x/wid,y/wid,r))
final_seeds = []
for (x,y,r) in seeds:
v = get_value(x,y,r)
#plt.imshow(pic+draw_ball(wid,x,y,r))
#plt.show()
for i in range(updates):
x, y, r, v = move_and_grow(x,y,r,v)
#r, v = grow(x,y,r,v)
if show:
print(x,y,r,v)
plt.imshow(pic+draw_ball(wid,x,y,r))
plt.show()
final_seeds.append(((x,y,r),v))
action = np.array(max(final_seeds, key= lambda x: x[1])[0])
action[1] = 1-action[1]
plt.imsave(f"result/flownet/solver/grower/{id}_drawn.png", draw_ball(wid, *action, invert_y = True))
action[2]*=r_fac
compare_action = action.copy()
action[2] = action[2] if action[2]<0.125 else 0.125
action[0] = action[0] if action[0]> 0 else 0
action[0] = action[0] if action[0]<1- 0 else 1- 0
action[1] = action[1] if action[1]> 0 else 0
action[1] = action[1] if action[1]<1- 0 else 1- 0
if np.any(action!=compare_action):
#print("something was out of bounce:", action, compare_action)
pass
return action
def sample_action_vector(pic, actions, uniform=False, radmode="random", show=False, mask=None, check_border=False):
#os.makedirs("result/flownet/solver/grower", exist_ok=True)
#plt.imsave(f"result/flownet/solver/grower/{id}.png", pic)
pic = pic*(pic>pic.mean())
wid = pic.shape[0]
#plt.imsave(f"result/flownet/solver/grower/{id}_thresh.png", pic)
if check_border:
mask[:,0] = 1
mask[:,-1] = 1
mask[0,:] = 1
mask[-1,:] = 1
shape = pic.shape
flat = pic.numpy().reshape(-1)
flat = flat/np.sum(flat)
indexes = np.where(flat)[0]
probs = flat[flat>0]
#print(probs, indexes)
valid = False
while not valid:
# POS SAMPLING
if uniform:
flat_choice = np.random.choice(indexes)
else:
flat_choice = np.random.choice(indexes, p=probs)
flat_mask = flat * 0
flat_mask[flat_choice] = 1
reshaped = flat_mask.reshape(shape)
choice = np.where(reshaped)
#print(choice)
y, x = choice[0].item()/wid, choice[1].item()/wid
# RADIUS Selection
radii = actions[:,2]
radii = radii[radii>0.005]
if radmode=="random":
rad = np.random.choice(radii)
ball = draw_ball(wid, x, y, rad/8)>0
if mask[ball].sum():
continue
else:
valid = True
else:
rads = []
tries = 0
while len(rads)<10:
tries += 1
#print(tries)
valid = True
rad = np.random.choice(radii)
ball = draw_ball(wid, x, y, rad/8)>0
#print(ball)
if tries>50:
print("tried 50 radii!")
valid = False
break
if mask[ball].sum():
continue
if radmode=="mean":
try:
value = pic[ball].mean()
except Exception as e:
print("CONTINUING", e)
continue
elif radmode=="median":
try:
value = pic[ball].median()
except Exception as e:
print("CONTINUING", e)
continue
rads.append((rad, value))
if valid:
rads.sort(key=lambda x: x[1])
rad = rads[-1][0]
#print(type(rad), rad)
return np.array([x,1-y,rad/8])
def collect_actions():
train, dev, test = phyre.get_fold('ball_within_template', 0)
tasks = list(train + dev + test)
#print(len(tasks))
#bad = tasks.index("00024:440")
tasks.remove("00024:440")
sim = phyre.initialize_simulator(tasks, 'ball')
actions = []
cache = phyre.get_default_100k_cache('ball')
cached_actions = cache.action_array
for ti, task in enumerate(tasks):
count = 0
solutions = cached_actions[cache.load_simulation_states(task)==1]
idxs = np.arange(len(solutions))
selection = np.random.choice(idxs, 10)
actions.extend(list(solutions[selection]))
#print(max(idxs), selection)
#print(list(solutions[selection]), task, len(solutions))
"""
while True:
try:
action = random.choice(solutions)
except:
print("empty")
action = sim.sample(ti)
#res = sim.simulate_action(ti, action)
#print("collected", count, "actions for", task, end='\r')
#if res.status.is_solved():
actions.append(action)
count +=1
if count >=10:
break
"""
actions = np.array(actions)
#print(actions.shape)
plt.hist(actions[:,2], bins=100)
plt.savefig("action-hist.png")
np.save("data/sample-actions.npy", actions)
def pic_hist_to_action(pic, r_fac=3):
# thresholding
pic = pic*(pic>0.2)
# columns part of ball
cols = [idx for (idx,val) in enumerate(np.sum(pic, axis=0)) if val>2]
start, end = min(cols), max(cols)
x = (start+end)/2
x /= pic.shape[1]
# rows part of ball
rows = [idx for (idx,val) in enumerate(np.sum(pic, axis=1)) if val>2]
start, end = min(rows), max(rows)
y = (start+end)/2
y /= pic.shape[0]
# radius
r = np.sqrt(pic.sum()/(3.141592*pic.shape[0]**2))
r = 0.1
return x, y, r
def scenes_to_channels(X, size=(32,32)):
x = np.zeros((X.shape[0], 7, size[0], size[1]))
for i, scene in enumerate(X):
channels = [(scene==j).astype(float) for j in range(1,8)]
x[i] = np.flip(np.stack([(cv2.resize(c, size, cv2.INTER_MAX)>0).astype(float) for c in channels]), axis=1)
return x
def rollouts_to_specific_paths(batch, channel, size=(32,32), gamma=1):
trajectory = np.zeros((len(batch), size[0], size[1]))
for j, r in enumerate(batch):
path = np.zeros((len(r), size[0], size[1]))
alpha = 1
for i, scene in enumerate(r):
chan = (scene==channel).astype(float)
path[i] = alpha*(cv2.resize(chan, size, cv2.INTER_MAX)>0).astype(float)
alpha *= gamma
path = np.flip(path, axis=1)
base = np.max(path, axis=0).astype(float)
trajectory[j] = base
return trajectory
def extract_individual_auccess(path):
with open(path+"/auccess-dict.json") as fp:
dic = json.load(fp)
w_res = dict((i,[]) for i in range(25))
c_res = dict((i,[]) for i in range(25))
keys = dic.keys()
w_keys = [k for k in keys if k.__contains__("within")]
c_keys = [k for k in keys if k.__contains__("cross")]
print(c_keys)
for k in w_keys:
templ = int(k.split('_')[4][-2:])
w_res[templ].append(dic[k])
print(w_res)
for k in c_keys:
templ = int(k.split('_')[4][-2:])
c_res[templ].append(dic[k])
print(c_res)
with open(path+"/average-auccess-horizontal.txt", "w") as fp:
fp.write("within cross\n")
within = [sum(templ)/len(templ) for templ in [w_res[i] for i in range(25)]]
cross = [sum(templ)/len(templ) for templ in [(c_res[i] or [0]) for i in range(25)]]
#fp.writelines([f"{('0000'+str(i))[-5:]} {w} {c}\n" for i,(w,c) in enumerate(zip(within, cross))])
fp.writelines([str(round(item, 2))[-3:]+' & ' for item in within]+['\n'])
fp.writelines([str(round(item, 2))[-3:]+' & ' for item in cross]+['\n'])
fp.write(f"average {sum(within)/len(within)} {sum(cross)/(len(cross)-1)}")
def collect_traj_lookup(tasks, save_path, number_per_task, show=False, stride=10):
end_char = '\n'
tries = 0
max_tries = 100
base_path = save_path
cache = phyre.get_default_100k_cache('ball')
actions = cache.action_array
print("Amount per task", number_per_task)
keys = []
values = []
sim = phyre.initialize_simulator(tasks, 'ball')
for idx, task in enumerate(tasks):
# COLLECT SOLVES
n_collected = 0
while n_collected < number_per_task:
tries += 1
# getting action
action = actions[cache.load_simulation_states(task)==1]
print(f"collecting {n_collected+1} interactions from {task} with {tries} tries", end = end_char)
if len(action)==0:
print("no solution action in cache at task", task)
action = [np.random.rand(3)]
action = random.choice(action)
# simulating action
res = sim.simulate_action(idx, action,
need_featurized_objects=True, stride=1)
while res.status.is_invalid():
action = np.random.rand(3)
res = sim.simulate_action(idx, action,
need_featurized_objects=True, stride=1)
# checking result for contact
def check_contact(res: phyre.Simulation):
#print(res.images.shape)
#print(len(res.bitmap_seq))
#print(res.status.is_solved())
idx1 = res.body_list.index('RedObject')
idx2 = res.body_list.index('GreenObject')
#print(idx1, idx2)
#print(res.body_list)
green_idx = res.featurized_objects.colors.index('GREEN')
red_idx = res.featurized_objects.colors.index('RED')
target_dist = sum(res.featurized_objects.diameters[[green_idx,red_idx]])/2
for i,m in enumerate(res.bitmap_seq):
if m[idx1][idx2]:
pos = res.featurized_objects.features[i,[green_idx,red_idx],:2]
dist = np.linalg.norm(pos[1]-pos[0])
#print(dist, target_dist)
if not dist<target_dist+0.005:
continue
red_radius = res.featurized_objects.diameters[red_idx]*4
action_at_interaction = np.append(pos[1], red_radius)
return (True, i, pos[0], action_at_interaction, target_dist)
return (False, 0, (0,0), 0, 0)
contact, i_step, green_pos, red_pos, summed_radii = check_contact(res)
if contact:
tries = 0
step_n = 10
# check whether contact happend too early
if i_step-step_n < 0:
continue
try:
green_idx = res.featurized_objects.colors.index('GREEN')
red_idx = res.featurized_objects.colors.index('RED')
green_minus, _ = res.featurized_objects.features[i_step-stride,[green_idx,red_idx],:2]
green_zero, _ = res.featurized_objects.features[i_step,[green_idx,red_idx],:2]
green_plus, _ = res.featurized_objects.features[i_step+stride,[green_idx,red_idx],:2]
green_key, _ = green_minus-green_zero, 0
green_value, _ = green_zero-green_plus, 0
keys.append((green_key[0], green_key[1]))
values.append((green_value[0], green_value[1]))
except:
continue
n_collected += 1
if tries>max_tries:
break
keys = np.round(256*np.array(keys))
k_x_max = keys[np.argmax(np.abs(keys[:,0])),0]
k_y_max = keys[np.argmax(np.abs(keys[:,1])),1]
"""keys[:,0] /= k_x_max/5
keys[:,1] /= k_y_max/5
k_x_max = np.max(np.abs(keys[:,0]))
k_y_max = np.max(np.abs(keys[:,1]))"""
values = np.round(256*np.array(values))
v_x_max = values[np.argmax(np.abs(values[:,0])), 0]
v_y_max = values[np.argmax(np.abs(values[:,1])), 1]
"""values[:,0] /= v_x_max/5
values[:,1] /= v_y_max/5
v_x_max = np.max(np.abs(values[:,0]))
v_y_max = np.max(np.abs(values[:,1]))"""
table = dict()
for i in range(len(keys)):
k = tuple(keys[i])
v = tuple(values[i])
if k in table:
table[k][v] = table[k][v] + 1 if v in table[k] else 1
else:
table[k] = {v:1}
# Save data to file
os.makedirs(base_path, exist_ok=True)
with open(f'{base_path}/lookup.pickle', 'wb') as fp:
pickle.dump(table, fp)
print(f"FINISH collecting trajectory lookup!")
return keys, values, k_x_max, k_y_max, v_x_max, v_y_max, table
def visualize_actions_from_cache(amount):
cache = phyre.get_default_100k_cache("ball")
actions = cache.action_array[:amount]
plt.scatter(actions[:,0], actions[:,1], alpha=0.3, s=1000*actions[:,2], c=actions[:,2])
plt.show()
def print_folds():
eval_setup = 'ball_within_template'
for fold_id in range(10):
#print(phyre.get_fold(eval_setup, fold_id)[0][:10])
print(phyre.get_fold(eval_setup, fold_id)[0][:10]
== phyre.get_fold(eval_setup, fold_id)[0][:10])
def get_auccess_for_n_tries(n):
eva = phyre.Evaluator(['00000:000'])
for _ in range(n-1):
eva.maybe_log_attempt(0, phyre.SimulationStatus.NOT_SOLVED)
for _ in range(101-n):
eva.maybe_log_attempt(0, phyre.SimulationStatus.SOLVED)
return eva.compute_all_metrics()
def get_auccess_for_n_tries_first_only(n):
eva = phyre.Evaluator(['00000:000'])
for i in range(1,101):
if n==i:
eva.maybe_log_attempt(0, phyre.SimulationStatus.SOLVED)
else:
eva.maybe_log_attempt(0, phyre.SimulationStatus.NOT_SOLVED)
return eva.get_auccess()
def add_dijkstra_to_data(path):
if os.path.exists(path+"/data.pickle") and os.path.exists(path+"/index.pickle"):
with open(path+'/data.pickle', 'rb') as fp:
data = pickle.load(fp)
X = T.tensor(data).float()
else:
print("Path not found")
for scene in X:
red = T.stack((X[:,0],X[:,0]*0,X[:,0]*0), dim=-1)
green = T.stack((X[:,1],X[:,0]*0,X[:,0]*0), dim=-1)
blues = T.stack((X[:,2],X[:,0]*0,X[:,0]*0), dim=-1)
blued = T.stack((X[:,3],X[:,0]*0,X[:,0]*0), dim=-1)
grey = T.stack((X[:,4],X[:,4],X[:,4]), dim=-1)
black = T.stack((X[:,5],X[:,0]*0,X[:,0]*0), dim=-1)
def create_eval_overview(paths, wid = 64):
inspect = "result/flownet/inspect/"
train = "result/flownet/training/"
fold = 0
tasks_img = None
for setup in ['within', 'cross']:
comb_viz = []
names = []
aucc_names = []
res = []
losses = []
for path in paths:
base = inspect+path+"/default/"
mid = f"ball_{setup}_template_fold_{fold}/"
end = "eval-viz-tensor.pt"
tstart = train+path+"/default/"
auccess = f"ball_{setup}_template_{fold}-auccess.txt"
loss_pic = f"loss_plot.png"
loss_txt = f"loss.txt"
try:
#train:
tmp_res = np.loadtxt(tstart+auccess, usecols=2)
if tmp_res.shape[0] == 4:
res.append(tmp_res)
aucc_names.append(path)
else:
continue
print(res[-1].shape)
with open(tstart+loss_txt, "r") as fp:
data = fp.readlines()
data = [[float(item) for item in (line.replace("tensor([", "").replace("])\n", "")).split(",")] for line in data]
log = np.array(data)
#print(log.shape)
loss_labels = ['combined', 'base', 'target', 'act-path', 'act-ball']
for i in range(5):
plt.plot(np.mean(log[:,i].reshape(-1,10), axis=1), label=loss_labels[i])
plt.legend()
plt.title(aucc_names[-1])
plt.ylim(0,0.25)
plt.grid()
plt.savefig(tstart+loss_pic)
plt.close()
loss_plot = cv2.imread(tstart+loss_pic)
#print(loss_plot.shape)
losses.append(loss_plot)
#inspect:
viz = T.load(base+mid+end)
if tasks_img is None:
pic = "poch_0_0_diff.png"
img = cv2.imread(base+mid+pic)
tasks_img = cv2.imread(base+mid+pic)[:,:40]
comb_viz.append(viz[:,7,None])
#print(base+mid+end, comb_viz[-1].shape)
names.append(path)
except Exception as e:
print(e)
# save auccess file:
np.savetxt(f"{setup}-aucces.csv", np.stack(res, axis=0).T, header=",".join(aucc_names), delimiter=",", fmt='%1.7f')
# save loss plots:
loss_plots = Image.fromarray(np.concatenate(losses, axis=1))
font = ImageFont.truetype("/usr/share/fonts/truetype/ubuntu/Ubuntu-R.ttf", 20)
draw = ImageDraw.Draw(loss_plots)
for i,x in enumerate(range(0,loss_plots.width, int(loss_plots.width/len(names)))):
draw.text((10+x, 5), names[i], fill= (0,0,0), font=font)
loss_plots.save(f"{setup}-losses.png")
if False:
comb_viz = T.cat(comb_viz, dim=1)
viz = np.array(vis_batch(comb_viz, "./", f"{setup}-inspects", text=names, save=False))
height = viz.shape[0]
tasks_img = tasks_img[-height:]
final_viz = np.concatenate((tasks_img, viz), axis=1)
plt.imsave(f"{setup}-inspects.png", final_viz, dpi=1000)