-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master'
- Loading branch information
Showing
7 changed files
with
703 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,368 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": { | ||
"toc": true | ||
}, | ||
"source": [ | ||
"<h1>Table of Contents<span class=\"tocSkip\"></span></h1>\n", | ||
"<div class=\"toc\" style=\"margin-top: 1em;\"><ul class=\"toc-item\"><li><span><a href=\"#Instantiate-CPPN\" data-toc-modified-id=\"Instantiate-CPPN-1\"><span class=\"toc-item-num\">1 </span>Instantiate CPPN</a></span></li><li><span><a href=\"#Generate-Image\" data-toc-modified-id=\"Generate-Image-2\"><span class=\"toc-item-num\">2 </span>Generate Image</a></span></li><li><span><a href=\"#Animation\" data-toc-modified-id=\"Animation-3\"><span class=\"toc-item-num\">3 </span>Animation</a></span></li><li><span><a href=\"#Parameters-Grid-Search\" data-toc-modified-id=\"Parameters-Grid-Search-4\"><span class=\"toc-item-num\">4 </span>Parameters Grid Search</a></span></li><li><span><a href=\"#Considerations\" data-toc-modified-id=\"Considerations-5\"><span class=\"toc-item-num\">5 </span>Considerations</a></span></li></ul></div>" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stderr", | ||
"output_type": "stream", | ||
"text": [ | ||
"Using TensorFlow backend.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import numpy as np\n", | ||
"import yaml\n", | ||
"import os\n", | ||
"import cv2\n", | ||
"import pandas as pd\n", | ||
"import seaborn as sns\n", | ||
"import matplotlib.pyplot as plt\n", | ||
"from matplotlib import animation\n", | ||
"from datetime import datetime\n", | ||
"\n", | ||
"from pathlib import Path\n", | ||
"\n", | ||
"plt.rcParams['animation.ffmpeg_path'] = str(Path.home() / \"anaconda3/envs/image-processing/bin/ffmpeg\")\n", | ||
"\n", | ||
"%matplotlib notebook\n", | ||
"\n", | ||
"%load_ext autoreload\n", | ||
"%autoreload 2\n", | ||
"\n", | ||
"from CPPN import CPPN" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Instantiate CPPN" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"res_path = Path.home() / 'Documents' / 'datasets' / 'cppn'" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"extra_funs= {\n", | ||
" 'base': lambda x,y: x*y,\n", | ||
" 'cos_sin': lambda x,y: np.cos(x)*np.sin(y),\n", | ||
" 'cube': lambda x,y: x**3 + 3*y - y**3 -3*x,\n", | ||
" 'rand': lambda x,y: np.sqrt(x*x+y*y) + (x*x) + np.tan(y),\n", | ||
"}" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"{'hidden_dim': 16,\n", | ||
" 'inner_activation': 'tanh',\n", | ||
" 'inner_architecture_key': 'base',\n", | ||
" 'kernel_init_mean': 0.0,\n", | ||
" 'kernel_init_stddev': 1.0,\n", | ||
" 'nb_channels': 3,\n", | ||
" 'nb_hidden_layers': 5,\n", | ||
" 'scale_factor': 1.0,\n", | ||
" 'z_dim': 16}" | ||
] | ||
}, | ||
"execution_count": 4, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"# load config file\n", | ||
"with open('cppn_config.yaml', 'r') as f:\n", | ||
" model_config = yaml.load(f)\n", | ||
"model_config = model_config['rgb_test']\n", | ||
"model_config" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# customize model config\n", | ||
"batch_size = 1\n", | ||
"\n", | ||
"img_width = 256\n", | ||
"img_height = 256\n", | ||
"img_size = (img_width, img_height)\n", | ||
"\n", | ||
"model_config['nb_hidden_layers'] = 8\n", | ||
"#model_config['kernel_init_stddev'] = 1.\n", | ||
"model_config['kernel_init_mean'] = 0.\n", | ||
"model_config['nb_channels'] = 1\n", | ||
"\n", | ||
"#model_config['inner_architecture_key'] = 'residual'\n", | ||
"\n", | ||
"# init model\n", | ||
"cppn = CPPN(batch_size=batch_size, img_width=img_width, img_height=img_height, \n", | ||
" **model_config)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"cppn.model.summary()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Generate Image" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"x, y, r, e = cppn.get_data(extra_fun=extra_funs['cos_sin'])" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"z = cppn.get_z()" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plt.imshow(cppn.generate_imgs(x, y, r, e, z)[0], cmap='gray')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Animation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"save_anim = False\n", | ||
"animate_data = True\n", | ||
"FRAMES = 300\n", | ||
"batch_size = 1\n", | ||
"img_width = 1080\n", | ||
"img_height = 720\n", | ||
"img_size = (img_width, img_height)\n", | ||
"\n", | ||
"\n", | ||
"dpi = 100\n", | ||
"if save_anim:\n", | ||
" fig, ax = plt.subplots(dpi=dpi, figsize=(img_width/dpi, img_height/dpi))\n", | ||
"else:\n", | ||
" fig, ax = plt.subplots(dpi=100, figsize=(5, 5))\n", | ||
"plt.axis('off')\n", | ||
"\n", | ||
"# Init model and data\n", | ||
"with open('cppn_config.yaml', 'r') as f:\n", | ||
" model_config = yaml.load(f)\n", | ||
"data_config = model_config['test_config']\n", | ||
"model_config = model_config['rgb_test']\n", | ||
"cppn = CPPN(batch_size=batch_size, img_width=img_width, img_height=img_height, \n", | ||
" **model_config)\n", | ||
"x, y, r, e = cppn.get_data(scale=data_config['scale'], \n", | ||
" translation=data_config['translation'],\n", | ||
" rotation=data_config['rotation'], \n", | ||
" extra_fun=extra_funs[data_config['extra_fun']])\n", | ||
"z = cppn.get_z()\n", | ||
"\n", | ||
"z_max_val = 1\n", | ||
"#z = np.zeros((1, model_config['z_dim']))-z_max_val\n", | ||
"add_val = (z_max_val*2)/FRAMES\n", | ||
"add_val = 1.0\n", | ||
"def animate(i, ax, cppn, x, y, r, e, z, data_config, extra_funs, animate_data):\n", | ||
" z_idx = int(i/(int(FRAMES/model_config['z_dim'])))\n", | ||
" z[0][z_idx] += add_val\n", | ||
" #z[0] += add_val\n", | ||
" \n", | ||
" if animate_data:\n", | ||
" x, y, r, e = cppn.get_data(scale=data_config['scale']+i*data_config['scale_speed'], \n", | ||
" translation=data_config['translation']+i*data_config['translation_speed'], \n", | ||
" rotation=data_config['rotation']+i*data_config['rotation_speed'], \n", | ||
" extra_fun=extra_funs[data_config['extra_fun']])\n", | ||
" \n", | ||
" ax.imshow(cppn.generate_imgs(x, y, r, e, z)[0], cmap='gray')\n", | ||
"\n", | ||
"ani = animation.FuncAnimation(fig, animate, frames=FRAMES, interval=10,\n", | ||
" fargs=[ax, cppn, x, y, r, e, z, data_config, extra_funs, animate_data])\n", | ||
"if save_anim:\n", | ||
" ani.save(str(res_path / 'tests' / 'anim_{}.mp4'.format(datetime.strftime(datetime.now(), \"%Y-%m-%d_%H-%M\"))), \n", | ||
" animation.FFMpegFileWriter(fps=30))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Parameters Grid Search" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from sklearn.model_selection import ParameterGrid" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"param_grid = {\n", | ||
" 'inner_architecture_key': ['base', 'residual', 'softplus'],\n", | ||
" 'kernel_init_stddev': np.linspace(0.7, 4., num=4),\n", | ||
" 'scale': np.linspace(-2., 2., num=5),\n", | ||
" 'translation': np.linspace(-4., 4., num=3),\n", | ||
" #'rotation': np.linspace(1, 360, num=4),\n", | ||
" 'nb_hidden_layers': np.arange(3, 7, 2),\n", | ||
" 'z_dim': [8, 16, 32, 64],\n", | ||
" 'hidden_dim': [16, 32, 64],\n", | ||
" 'extra_fun': ['base', 'cos_sin', 'cube', 'rand']\n", | ||
" }\n", | ||
"grid = ParameterGrid(param_grid)\n", | ||
"\n", | ||
"# Init model and data\n", | ||
"with open('cppn_config.yaml', 'r') as f:\n", | ||
" model_config = yaml.load(f)\n", | ||
"data_config = model_config['test_config']\n", | ||
"model_config = model_config['rgb_test']\n", | ||
"\n", | ||
"batch_size = 5\n", | ||
"img_width = 1920\n", | ||
"img_height = 1080\n", | ||
"\n", | ||
"for i, params in enumerate(grid):\n", | ||
" print(\"Params {}: {}\".format(i, params))\n", | ||
" out_dir = res_path / 'hq' / f'params_{i}'\n", | ||
" out_dir.mkdir(parents=True, exist_ok=False) \n", | ||
" current_config = model_config.copy()\n", | ||
" current_data_config = data_config.copy()\n", | ||
" current_config.update(params)\n", | ||
" current_data_config.update(params)\n", | ||
" cppn = CPPN(batch_size=batch_size, img_width=img_width, img_height=img_height, \n", | ||
" **current_config)\n", | ||
" x, y, r, e = cppn.get_data(scale=data_config['scale'], \n", | ||
" translation=data_config['translation'], \n", | ||
" rotation=data_config['rotation'],\n", | ||
" extra_fun=extra_funs['cos_sin'])\n", | ||
" z = cppn.get_z()\n", | ||
" imgs = cppn.generate_imgs(x, y, r, e, z)\n", | ||
" for j, img in enumerate(imgs): \n", | ||
" plt.imsave(str(out_dir / f'sample_{j}.png'), img, cmap='gray')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Considerations" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"ReLu more interesting gradients but clear cut producing uninteresting entries for higher stddev." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Should use a bias_initializer? Should it be the same as kernel?\n", | ||
"\n", | ||
"In general low kernel_init_stddev provides blurry results, still around 0.5. One can notice improvements with increased values for hidden_dim and even nb_hidden_layers, but still too much blur to be interesting. With 5.0 stddev we are instead already on the other opposite, with clear cute areas, cell-shading like.\n", | ||
"\n", | ||
"Didn't notice major differenced when increasing the z_dim, sometimes even had impression of more interesting results with less" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python [conda env:image-processing]", | ||
"language": "python", | ||
"name": "conda-env-image-processing-py" | ||
}, | ||
"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.6.7" | ||
}, | ||
"notify_time": "30", | ||
"toc": { | ||
"nav_menu": {}, | ||
"number_sections": true, | ||
"sideBar": true, | ||
"skip_h1_title": false, | ||
"toc_cell": true, | ||
"toc_position": {}, | ||
"toc_section_display": "block", | ||
"toc_window_display": true | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.