stylegan3/Build_Style_Archive.ipynb
2022-11-25 19:54:23 +01:00

1.1 MiB

In [19]:
from runs import Run, Snapshot, get_runs_in_dir
import tabulate
import os
from IPython.display import Markdown as md

import matplotlib.pyplot as plt
import matplotlib
import shutil
import jinja2
from tqdm.notebook import trange, tqdm

Build an archive of the various runs & snapshots

This notebook can be used to generate a printable html archive, of all runs and their snapshots.

Configuration

In [2]:
outdir = os.path.abspath('out/html')

Output Run Index

In [3]:
palette = ["#c99ae0",
"#6bd154",
"#bb79f1",
"#c2d436",
"#ec68d3",
"#82d086",
"#e87fb7",
"#4dd7c4",
"#c7ba5c",
"#64a6e7"]
In [4]:
#see also https://github.com/matplotlib/matplotlib/blob/f6e0ee49c598f59c6e6cf4eefe473e4dc634a58a/lib/matplotlib/_cm.py#L859
palette = matplotlib.cm.datad['Accent']['listed']
palette = matplotlib.cm.datad['Set3']['listed']
In [5]:
def get_rgb_for_idx(i):
    return f"rgb({palette[i][0]*255}, {palette[i][1]*255},{palette[i][2]*255})"
In [1]:
%run ThisPlaceDoesExist.ipynb
In [2]:

Out[2]:
[<runs.Run at 0x7efd79998ac0>,
 <runs.Run at 0x7efd79998ca0>,
 <runs.Run at 0x7efd79998d90>,
 <runs.Run at 0x7efd7972cdf0>,
 <runs.Run at 0x7efd796ba5e0>,
 <runs.Run at 0x7efd79998a90>,
 <runs.Run at 0x7efd79998e20>,
 <runs.Run at 0x7efd79998cd0>,
 <runs.Run at 0x7efd79998d60>]
In [ ]:

In [8]:
%matplotlib inline

plot = plot_runs(runs, dpi=300, palette=palette)
plot.legend(bbox_to_anchor=(1,0), loc="lower left")
# plot.show()

plt.xlabel('Iterations')
plt.ylabel('Fréchet inception distance (FID)')
plt.savefig(os.path.join(outdir, 'runs.png'), bbox_inches='tight', transparent=True)
In [26]:
plot = plot_stats([
    'Loss/D/loss',
    'Loss/G/loss',
], runs, palette=palette)
# plot.legend()
plt.savefig(os.path.join(outdir, 'run_losses.png'), bbox_inches='tight', transparent=True)
In [ ]:

In [10]:
index_html = tabulate.tabulate([
        {
         #   "idx": i,
         # "conditional": run.dataset_is_conditional(),
         **run.get_summary(),
         "nr": f"<a href='#run{run.as_nr}'>{run.as_nr}</a>",
            
         "page": f"<span class='tocitem' style='color:{get_rgb_for_idx(i)}' data-ref='#run{run.as_nr}'>&#129926;</span>",
            
        } for i, run in enumerate(runs)
    ], tablefmt='unsafehtml', headers="keys", colalign=("left","left")
)
In [11]:
index_html
Out[11]:
nr dataset conditional resolution gammaduration iterations last_fidpage
00001paris3 True 256 8.23 days, 10:34:26 2600 502.277 🮆
00002paris3 True 256 2 5 days, 3:43:08 6560 190.346 🮆
00003paris3 True 256 2 18 days, 13:01:50 25000 42.9661🮆
00004paris3 False 256 2 15 days, 16:13:20 22800 15.6691🮆
00009paris3-1024.zip False 1024 32 0:00:00 0 549.99 🮆
00010paris3-1024.zip False 1024 32 50 days, 3:15:24 15200 33.2466🮆
00011paris3-1024.zip False 1024 10 5 days, 18:48:04 1760 200.356 🮆
00014paris3-cropped-256False 256 8 2 days, 20:08:22 4160 20.1699🮆
00016paris3-cropped-256False 256 8 7 days, 18:18:10 11360 17.0625🮆
In [12]:
jinja_env = jinja2.Environment(
    loader=jinja2.FileSystemLoader("templates"),
    autoescape=jinja2.select_autoescape()
)
In [13]:
template = jinja_env.get_template("runs.j2")
In [21]:
template_vars = {
    "runs_graph": "runs.png",
    "runs_losses_graph": "run_losses.png",
    "runs_table": index_html,
    "runs": runs
}
In [23]:
with open(os.path.join(outdir, 'index.html'), 'w') as fp:
    fp.write(template.render(**template_vars))
In [24]:
[run.id for run in runs]
Out[24]:
['00001-stylegan3-t--gpus1-batch32-gamma8.2',
 '00002-stylegan3-t--gpus1-batch32-gamma2',
 '00003-stylegan3-r--gpus1-batch32-gamma2',
 '00004-stylegan3-r--gpus1-batch32-gamma2',
 '00009-stylegan3-r-paris3-1024-gpus1-batch32-gamma32',
 '00010-stylegan3-r-paris3-1024-gpus1-batch32-gamma32',
 '00011-stylegan3-r-paris3-1024-gpus1-batch32-gamma10',
 '00014-stylegan3-r-paris3-cropped-256-gpus1-batch32-gamma8',
 '00016-stylegan3-r-paris3-cropped-256-gpus1-batch32-gamma8']

Copy necessary auxilary files to the output directory:

In [25]:
files = [
    "templates/style.css", 
    "templates/pagedjs-interface.css",
]
for src in files:
    shutil.copy(src, outdir)
In [20]:
for run in runs:
    nr = 7 if run.resolution > 512 else 8
    for snapshot in tqdm(run.snapshots):
        filename = os.path.join(outdir, 'imgs', snapshot.id + ".jpg")
        if not os.path.exists(filename):
            img = snapshot.get_preview_img(nr,1)
            img.save(filename)
  0%|          | 0/14 [00:00<?, ?it/s]
  0%|          | 0/83 [00:00<?, ?it/s]
  0%|          | 0/314 [00:00<?, ?it/s]
  0%|          | 0/115 [00:00<?, ?it/s]
  0%|          | 0/1 [00:00<?, ?it/s]
  0%|          | 0/96 [00:00<?, ?it/s]
  0%|          | 0/12 [00:00<?, ?it/s]
  0%|          | 0/27 [00:00<?, ?it/s]
  0%|          | 0/72 [00:00<?, ?it/s]
In [ ]:

In [ ]: