Andrei Cozma commited on
Commit
50efa30
·
1 Parent(s): eb7667c
Files changed (2) hide show
  1. creategif.py +70 -0
  2. qtable_policy.gif +0 -0
creategif.py ADDED
@@ -0,0 +1,70 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import wandb
3
+
4
+ from PIL import Image
5
+ from PIL.Image import Transpose, Resampling
6
+
7
+
8
+ api = wandb.Api()
9
+ run = api.run("acozma/cs581/5ttfkav8")
10
+
11
+ print(run.summary)
12
+ print("Downloading images...")
13
+
14
+ for file in run.files():
15
+ if file.name.endswith(".png"):
16
+ file.download(exist_ok=True)
17
+
18
+ print("Finished downloading images")
19
+
20
+
21
+ def process_images(image_fnames, upscale=20):
22
+ image_fnames.sort(key=lambda x: int(x.split("_")[-2]))
23
+ frames = [Image.open(image) for image in image_fnames]
24
+ frames = [frame.transpose(Transpose.ROTATE_90) for frame in frames]
25
+ frames = [
26
+ frame.resize(
27
+ (frame.size[0] * upscale, frame.size[1] * upscale),
28
+ resample=Resampling.NEAREST,
29
+ )
30
+ for frame in frames
31
+ ]
32
+ return frames
33
+
34
+
35
+ def images_to_gif(frames, fname, duration=500):
36
+ print(f"Creating gif: {fname}")
37
+
38
+ frame_one = frames[0]
39
+ frame_one.save(
40
+ f"{fname}.gif",
41
+ format="GIF",
42
+ append_images=frames,
43
+ save_all=True,
44
+ duration=duration,
45
+ loop=0,
46
+ )
47
+
48
+
49
+ folder_path = "./media/images"
50
+ all_fnames = [os.path.join(folder_path, f) for f in os.listdir(folder_path)]
51
+
52
+
53
+ fnames_policy = [f for f in all_fnames if os.path.basename(f).startswith("Policy")]
54
+ policy_frames = process_images(fnames_policy)
55
+
56
+ fnames_qtable = [f for f in all_fnames if os.path.basename(f).startswith("Q-table")]
57
+ qtable_frames = process_images(fnames_qtable)
58
+
59
+ spacing_factor = 1 / 2
60
+
61
+ final_frames = []
62
+ for i, (qtable, policy) in enumerate(zip(qtable_frames, policy_frames)):
63
+ width, height = qtable.size
64
+ final_height = int(height * 2 + height * spacing_factor)
65
+ new_frame = Image.new("RGB", (width, final_height), color="white")
66
+ new_frame.paste(qtable, (0, 0))
67
+ new_frame.paste(policy, (0, height + int(height * spacing_factor)))
68
+ final_frames.append(new_frame)
69
+
70
+ images_to_gif(final_frames, "qtable_policy")
qtable_policy.gif ADDED