-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost_main.py
365 lines (297 loc) · 13.9 KB
/
post_main.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
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--explore_type", type=str)
args = parser.parse_args()
import os
import shutil
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import cv2
import matplotlib.pyplot as plt
import numpy as np
from itertools import chain
import torch
# When I import this, it tries using provided parameters for utils' args.
from utils import arena_dict, plots
import datetime
start_time = datetime.datetime.now()
def reset_start_time():
global start_time
start_time = datetime.datetime.now()
def duration():
global start_time
change_time = datetime.datetime.now() - start_time
change_time = change_time - datetime.timedelta(microseconds=change_time.microseconds)
return(change_time)
from colorsys import hsv_to_rgb
def plot_positions(positions_lists, arena_name, folder, load_name):
arena_map = plt.imread("arenas/" + arena_name + ".png")
arena_map = np.flip(arena_map, 0)
h, w, _ = arena_map.shape
fig, ax = plt.subplots(figsize=(10, 10))
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
extent = [-.5, w-.5, -h+.5, .5]
ax.imshow(arena_map, extent=extent, zorder = 1, origin='lower')
exits = arena_dict[arena_name + ".png"].exits
rewards = exits["Reward"].values.tolist()
rewards = list(set(rewards))
rewards_ints = [r if type(r) in [int, float] else sum([w*r_ for (w, r_) in r]) for r in rewards]
rewards_ints.sort()
reward_color = {rewards_ints[0] : (1, 0, 0), rewards_ints[-1] : (0, 1, 0)}
for n, k, v in zip(exits["Name"].values.tolist(), exits["Position"].values.tolist(), exits["Reward"].values.tolist()):
r = v if type(v) in [int, float] else sum([w*v_ for (w, v_) in v])
ax.text(k[1], -k[0], n, size = 10, bbox={'facecolor': reward_color[r], 'alpha': 0.5, 'pad': 5})
colors = []
for i in range(len(positions_lists)):
hue = i/len(positions_lists)
r, g, b = hsv_to_rgb(hue, 1, 1)
colors.append((r, g, b))
for i, positions_list in enumerate(positions_lists):
for j, positions in enumerate(positions_list):
x = [p[1] for p in positions]
y = [-p[0] for p in positions]
ax.plot(x, y, zorder = 2, color = colors[i], alpha = .5)
ax.scatter(x[-1], y[-1], s = 100, color = "black", alpha = .5, marker = "*", zorder = 3)
ax.scatter(x[-1], y[-1], s = 75, color = colors[i], alpha = .5, marker = "*", zorder = 4)
plt.title("{}: {} epochs, arena {}".format("_".join(folder.split("_")[:-1]), load_name, arena_name))
files = os.listdir("saves")
if(folder in files): pass
else: os.mkdir("saves/"+folder)
plt.savefig("saves/"+folder+"/arena_{}_tracks_{}".format(arena_name, load_name)+".png", bbox_inches='tight')
plt.close()
def plot_all_positions(training_name):
pos_dicts = []
f = os.listdir("saves")
for folder in f:
breaks = folder.split("_")
if(breaks[-1] in ["predictions", "positions", "done", "shared"] or folder[-4:] == ".png"): pass
elif("_".join(breaks[:-1]) == training_name):
_, pos_dict = torch.load("saves/" + folder + "/pos_dict.pt")
pos_dicts.append(pos_dict)
pos_dict = pos_dicts[0]
for k in pos_dict.keys():
for pd in pos_dicts[1:]:
pos_dict[k] += pd[k]
for (load_name, arena_name), positions_lists in pos_dict.items():
if(positions_lists == []): pass
else:
plot_positions(positions_lists = positions_lists,
arena_name = arena_name,
folder = training_name + "_shared",
load_name = load_name)
def make_vid(training_name, fps = 1):
files = []
folder = "saves/{}_shared".format(training_name)
for file in os.listdir(folder):
if(file[-4:] == ".png" and file[:5] != "plots"):
files.append(file)
files.sort()
frame = cv2.imread(folder + "/" + files[0]); height, width, layers = frame.shape
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
video = cv2.VideoWriter("saves/{}_video.avi".format(training_name), fourcc, fps, (width, height))
for file in files:
video.write(cv2.imread(folder + "/" + file))
cv2.destroyAllWindows()
video.release()
def make_mega_vid(order, fps = 1):
types = {}
for row in order:
for t in row:
types[t] = []
for k in types.keys():
if(k != "empty_space"):
folder = "saves/{}_shared".format(k)
for file in os.listdir(folder):
if(file[-4:] == ".png" and file[:5] != "plots"):
types[k].append(file)
types[k].sort()
folders = []
for folder in os.listdir("saves"):
folders.append(folder)
if("all_positions" in folders): shutil.rmtree("saves/all_positions")
try: os.mkdir("saves/all_shared")
except: pass
length = len(types[list(types.keys())[0]])
rows = len(order)
columns = max([len(order[i]) for i in range(rows)])
positions = []
for i, row in enumerate(order):
for j, column in enumerate(row):
positions.append((i,j))
xs = [] ; ys = []
for i in range(length):
images = []
for kind in list(types.keys()):
if(kind != "empty_space"):
images.append(Image.open("saves/{}_shared/{}".format(kind, types[kind][i])))
for image in images:
xs.append(image.size[0]) ; ys.append(image.size[1])
x = max(xs) ; y = max(ys)
for i in range(length):
images = []
for kind in list(chain(*order)):
if(kind != "empty_space"):
images.append(Image.open("saves/{}_shared/{}".format(kind, types[kind][i])))
else:
images.append(None)
#print(images[-1].shape)
new_image = Image.new("RGB", (columns*x, rows*y), color="white")
for j, image in enumerate(images):
row, column = positions[j]
if(image != None): new_image.paste(image, (column*x,row*y))
#print("Together:", new_image.shape)
#print()
new_image.save("saves/all_shared/{}.png".format(str(i).zfill(5)), format="PNG")
make_vid("all", fps)
def tuple_min_max(min_max_list):
mins = [min_max[0] for min_max in min_max_list]
maxs = [min_max[1] for min_max in min_max_list]
return((min(mins), max(maxs)))
def get_min_max(this, plot_dict_dict, cumulative = False):
plot_dict_list = []
for key in plot_dict_dict.keys(): plot_dict_list += plot_dict_dict[key]
these = [[i for i in plot_dict[this] if i != None] for plot_dict in plot_dict_list]
these = [t for t in these if t != []]
these = np.array(these)
if(cumulative):
these = np.cumsum(these, -1)
if(these.shape == (0,)): return((0,0))
return(np.amin(these), np.amax(these))
def make_end_pics(order):
real_order = []
for o in order: real_order += [o_ for o_ in o if o_ != "empty_space"]
order = real_order
all_folders = []
for folder in os.listdir("saves"):
breaks = folder.split("_")
if("_".join(breaks[:-1]) in order and not breaks[-1] in ["shared", "predictions"] and folder[-4:] != ".png"):
all_folders.append(folder)
all_folders.sort()
plot_dict_dict = {training_name : [] for training_name in order}
for folder in all_folders:
training_name = "_".join(folder.split("_")[:-1])
plot_dict_dict[training_name].append(torch.load("saves/" + folder + "/plot_dict.pt"))
rew_min_max = get_min_max("rew", plot_dict_dict, True)
pun_min_max = get_min_max("pun", plot_dict_dict, True)
ext_min_max = get_min_max("ext", plot_dict_dict)
cur_min_max = get_min_max("cur", plot_dict_dict)
ent_min_max = get_min_max("ent", plot_dict_dict)
mse_min_max = get_min_max("mse", plot_dict_dict)
dkl_min_max = get_min_max("dkl", plot_dict_dict)
alpha_min_max = get_min_max("alpha", plot_dict_dict)
actor_min_max = get_min_max("actor", plot_dict_dict)
critic1_min_max = get_min_max("crit1", plot_dict_dict)
critic2_min_max = get_min_max("crit2", plot_dict_dict)
weight_mean_min_max = get_min_max("weight_mean", plot_dict_dict)
weight_std_min_max = get_min_max("weight_std", plot_dict_dict)
bias_mean_min_max = get_min_max("bias_mean", plot_dict_dict)
bias_std_min_max = get_min_max("bias_std", plot_dict_dict)
dkl_change_min_max = get_min_max("dkl_change", plot_dict_dict)
critic_min_max = tuple_min_max([critic1_min_max, critic2_min_max])
rew_min_max = tuple_min_max([rew_min_max, pun_min_max])
rew_min_max = (rew_min_max[0]*1.2, rew_min_max[1]*1.2)
ext_min_max = tuple_min_max([ext_min_max, cur_min_max, ent_min_max])
mins_maxs = [rew_min_max, ext_min_max, mse_min_max, dkl_min_max, actor_min_max, critic_min_max, alpha_min_max,
weight_mean_min_max, bias_mean_min_max, weight_std_min_max, bias_std_min_max, dkl_change_min_max]
print("\n\nStarting plots.\n{}\n\n".format(duration()))
for training_name, plot_dict_list in plot_dict_dict.items():
for i, plot_dict in enumerate(plot_dict_list):
plots(plot_dict, mins_maxs, folder = plot_dict["folder"] + "/plots")
print("\n\nPlot done.\n{}\n\n".format(duration()))
plots(plot_dict_list, mins_maxs, folder = "saves/" + training_name + "_shared")
print("\n\nMany plots done.\n{}\n\n".format(duration()))
for training_name in order:
folders = []
for folder in os.listdir("saves"):
breaks = folder.split("_")
if("_".join(breaks[:-1]) == training_name):
if(breaks[-1] in ["predictions", "positions", "done"] or folder[-4:] == ".png"): pass
else: folders.append("saves/" + folder)
folders.sort()
images = []
for folder in folders:
if(folder.split("_")[-1] == "shared"):
images.append(Image.open(folder + "/plots.png"))
else:
images.append(Image.open(folder + "/plots/plots.png"))
new_image = Image.new("RGB", (len(folders)*images[0].size[0], images[0].size[1]))
for i, image in enumerate(images):
new_image.paste(image, (i*image.size[0], 0))
new_image.save("saves/all_{}_plots.png".format(training_name))
# Predictions
for training_name in order:
try: os.mkdir("saves/{}_predictions".format(training_name))
except: pass
folders = []
for folder in os.listdir("saves"):
breaks = folder.split("_")
if("_".join(breaks[:-1]) == training_name and not breaks[-1] in ["shared", "predictions"] and folder[-4:] != ".png"):
folders.append(folder)
for folder in folders:
images = []
files = os.listdir("saves/{}/predictions".format(folder)) ; files.sort()
for f in files:
images.append(Image.open("saves/{}/predictions/{}".format(folder, f)))
new_image = Image.new("RGB", (images[0].size[0], len(files)*images[0].size[1]))
for i, image in enumerate(images):
new_image.paste(image, (0, i*image.size[1]))
new_image.save("saves/{}_predictions/predictions_{}.png".format(training_name, folder.split("_")[-1]))
#shutil.rmtree("saves/{}/predictions".format(folder))
images = []
files = os.listdir("saves/{}_predictions".format(training_name)) ; files.sort()
for f in files:
images.append(Image.open("saves/{}_predictions/{}".format(training_name, f)))
new_image = Image.new("RGB", (len(images)*(10+images[0].size[0]), images[0].size[1]))
for i, image in enumerate(images):
new_image.paste(image, (i*(10+image.size[0]), 0))
new_image.save("saves/{}_predictions.png".format(training_name))
#shutil.rmtree("saves/{}_predictions".format(training_name))
def make_together_pic(order):
real_order = []
for o in order: real_order += [o_ for o_ in o if o_ != "empty_space"]
order = real_order
images = [] ; names = []
for f in os.listdir("saves"):
if f[-4:] == ".png" and f[:4] == "all_":
name = f[4:-10]
if name in order:
images.append(Image.open("saves/{}".format(f)))
names.append(name)
indices = [names.index(name) for name in order]
names = [names[index] for index in indices]
images = [images[index] for index in indices]
width = 0
for f in os.listdir("saves/{}_001/plots".format(order[0])):
image = Image.open("saves/{}_001/plots/{}".format(order[0], f))
width = max(width, image.size[0])
w, h = images[0].size
images = [image.crop((w-width, 0, w, h)) for image in images]
font = ImageFont.truetype('arial.ttf', 50)
new_image = Image.new("RGB", ((len(order))*width, images[0].size[1]+150), color="white")
for i, image in enumerate(images):
new_image.paste(image, (i*image.size[0], 30))
I1 = ImageDraw.Draw(new_image)
I1.text((i*image.size[0]+50,5), names[i], font = font, fill = (0,0,0))
new_image.save("saves/together_plots.png")
#%%
if(args.explore_type[0] != "("):
plot_all_positions(args.explore_type)
print("\n\nDone with {}!".format(args.explore_type))
else:
order = args.explore_type[1:-1]
order = order.split("+")
if(order[-1] != "break"): order.append("break")
row = [] ; rows = []
for i, job in enumerate(order):
if(job != "break"):
row.append(job)
else:
rows.append(row) ; row = []
order = rows
make_end_pics(order)
make_together_pic(order)
make_mega_vid(order)
print("\n\nFinished!")
print(duration())