-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateGifs.py
More file actions
58 lines (46 loc) · 2.3 KB
/
createGifs.py
File metadata and controls
58 lines (46 loc) · 2.3 KB
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
# Gif-generating code from: https://stackoverflow.com/questions/753190/programmatically-generate-video-or-animated-gif-in-python
import imageio
import os
import sys
import re
# Type this in the terminal to run: python createGif.py
# Will create a gif for each folder of .pngs in the folder /generatedImages
##############
# Source: https://stackoverflow.com/a/5967539
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split('(\d+)', text) ]
##############
def gifItUp():
dirs = os.listdir("runs")
dirs.remove(".ipynb_checkpoints")
for dir_ in dirs:
path = os.path.abspath(os.path.join(os.path.curdir, "runs", dir_))
if (os.path.isdir(path)):
# Loop through each directory (which is a separate run) and create a gif of all
# .pngs from that directory (generatedImages & losses)
dir_with_imgs_losses_weights = os.path.join("runs", dir_)
pictures = os.listdir(os.path.join(dir_with_imgs_losses_weights, "generatedImages"))
losses = os.listdir(os.path.join(dir_with_imgs_losses_weights, "losses"))
if (len(pictures) != 0):
# Pre-processing step. Sometimes a ".ipyn_checkpoint appears and we also want the files sorted
is_not_png = lambda x: ".png" in x
pictures = list(filter(is_not_png, pictures))
pictures.sort(key=natural_keys)
images = []
for pic in pictures:
images.append(imageio.imread(os.path.join(dir_with_imgs_losses_weights, "generatedImages", pic)))
imageio.mimsave(os.path.join("runs", dir_, "generatedImages"+".gif"), images)
if (len(losses) != 0):
losses = list(filter(is_not_png, losses))
losses.sort(key=natural_keys)
images = []
for loss in losses:
images.append(imageio.imread(os.path.join(dir_with_imgs_losses_weights, "losses", loss)))
imageio.mimsave(os.path.join("runs", dir_, "loss"+".gif"), images)