Spaces:
Build error
Build error
File size: 3,640 Bytes
29cdbe6 |
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 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Copyright 2020 Erik Härkönen. All rights reserved.\n",
"# This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n",
"# you may not use this file except in compliance with the License. You may obtain a copy\n",
"# of the License at http://www.apache.org/licenses/LICENSE-2.0\n",
"\n",
"# Unless required by applicable law or agreed to in writing, software distributed under\n",
"# the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n",
"# OF ANY KIND, either express or implied. See the License for the specific language\n",
"# governing permissions and limitations under the License.\n",
"\n",
"%matplotlib inline\n",
"from notebook_init import *\n",
"\n",
"out_root = Path('out/figures/biggan_style')\n",
"makedirs(out_root, exist_ok=True)\n",
"\n",
"model = get_model('BigGAN-512', 'husky', device)\n",
"rand = lambda : np.random.randint(np.iinfo(np.int32).max)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"classes = ['husky', 'church']\n",
"base_seeds = [665823877, 419650361]\n",
"style_seeds = [1922988331, 286873059, 1376693511, 1853453896]\n",
"print(base_seeds, style_seeds)\n",
"\n",
"num_keep = [1, 4, 8] # switch latent after first, fourth, and eighth layer\n",
"layer_names = ['layer1', 'layer4', 'layer8']\n",
"\n",
"model.truncation = 0.9\n",
"\n",
"for class_idx, class_name in enumerate(classes):\n",
" print(class_name, base_seeds[class_idx])\n",
" \n",
" model.set_output_class(class_name)\n",
" \n",
" for n_base in num_keep:\n",
" strip = []\n",
" \n",
" # Base\n",
" z0 = model.sample_latent(1, seed=base_seeds[class_idx])\n",
" out = model.sample_np(z0)\n",
" \n",
" # Resample style\n",
" plt.figure(figsize=(25,25))\n",
" for img_idx, seed in enumerate(style_seeds):\n",
" z1 = model.sample_latent(1, seed=seed)\n",
" \n",
" # Use style latent after 'n_base' layers\n",
" n_style = model.get_max_latents() - n_base\n",
" z = [z0] * n_base + [z1] * n_style\n",
" \n",
" img = model.sample_np(z)\n",
" strip.append(img)\n",
" \n",
" # Save individually\n",
" layer_name = f'layer{n_base}'\n",
" img_name = out_root / f'style_resample_{class_name}_{layer_name}_{img_idx}.png'\n",
" im = Image.fromarray((255*img).astype(np.uint8))\n",
" im.save(img_name)\n",
" \n",
" # Show strip\n",
" plt.imshow(np.hstack(strip))\n",
" plt.axis('off')\n",
" plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|