|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "Original paper: https://arxiv.org/abs/1806.00035" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "# 0. Read real and generated images#" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "code", |
| 19 | + "execution_count": null, |
| 20 | + "metadata": {}, |
| 21 | + "outputs": [], |
| 22 | + "source": [ |
| 23 | + "import math\n", |
| 24 | + "import numpy as np\n", |
| 25 | + "import matplotlib.pyplot as plt\n", |
| 26 | + "import matplotlib.lines as mlines\n", |
| 27 | + "import matplotlib" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "metadata": {}, |
| 34 | + "outputs": [], |
| 35 | + "source": [ |
| 36 | + "CHANNEL_NUM = 3\n", |
| 37 | + "PICTURE_SIZE = 36\n" |
| 38 | + ] |
| 39 | + }, |
| 40 | + { |
| 41 | + "cell_type": "code", |
| 42 | + "execution_count": null, |
| 43 | + "metadata": {}, |
| 44 | + "outputs": [], |
| 45 | + "source": [ |
| 46 | + "class ParticleDataset():\n", |
| 47 | + " def __init__(self, file):\n", |
| 48 | + " self.data = np.load(file)\n", |
| 49 | + " self.image = self.data['Pictures'].reshape(-1, CHANNEL_NUM*PICTURE_SIZE*PICTURE_SIZE)\n", |
| 50 | + "\n", |
| 51 | + " def __len__(self):\n", |
| 52 | + " return len(self.image)\n", |
| 53 | + "\n", |
| 54 | + " def __getitem__(self, i):\n", |
| 55 | + " return {\n", |
| 56 | + " \"Pictures\": self.image[i],\n", |
| 57 | + " }" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": null, |
| 63 | + "metadata": {}, |
| 64 | + "outputs": [], |
| 65 | + "source": [ |
| 66 | + "real_data = ParticleDataset('real.npz')\n", |
| 67 | + "vae_data = ParticleDataset('vae.npz')\n", |
| 68 | + "gan_data = ParticleDataset11('gan.npz')" |
| 69 | + ] |
| 70 | + }, |
| 71 | + { |
| 72 | + "cell_type": "markdown", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "Make sure that the values of real and generated data are of the same order - it is important for cooperative binarizing" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "print (np.min(real.image), np.max(real.image))\n", |
| 85 | + "print (np.min(gan_data.image), np.max(gan_data.image))\n", |
| 86 | + "print (np.min(vae.image), np.max(vae.image))" |
| 87 | + ] |
| 88 | + }, |
| 89 | + { |
| 90 | + "cell_type": "markdown", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "# 1. Binarize# " |
| 94 | + ] |
| 95 | + }, |
| 96 | + { |
| 97 | + "cell_type": "markdown", |
| 98 | + "metadata": {}, |
| 99 | + "source": [ |
| 100 | + "To understand how real and generated objects are close to each other, we need to choose a space of features in which we look these objects at" |
| 101 | + ] |
| 102 | + }, |
| 103 | + { |
| 104 | + "cell_type": "markdown", |
| 105 | + "metadata": {}, |
| 106 | + "source": [ |
| 107 | + "We go the easiest way and take pixels' values as features." |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "code", |
| 112 | + "execution_count": null, |
| 113 | + "metadata": {}, |
| 114 | + "outputs": [], |
| 115 | + "source": [ |
| 116 | + "from sklearn.cluster import KMeans, MiniBatchKMeans\n", |
| 117 | + "import math\n", |
| 118 | + "## function which map object to probability distribution ##\n", |
| 119 | + "\n", |
| 120 | + "def bin_counts (real_data, generated_data, number_of_bins=25):\n", |
| 121 | + " # binirize real and generated data, plot histogram and found density function\n", |
| 122 | + " return real_density, gen_density" |
| 123 | + ] |
| 124 | + }, |
| 125 | + { |
| 126 | + "cell_type": "markdown", |
| 127 | + "metadata": {}, |
| 128 | + "source": [ |
| 129 | + "Create $\\alpha-$ and $\\beta-$ vectors as in\n", |
| 130 | + "\n", |
| 131 | + "$\\hat{PRD}(Q,P) = \\{(\\alpha(\\lambda), \\beta(\\lambda))| \\lambda \\in \\Lambda \\}$, where $\\Lambda = \\{\\tan (\\frac{i}{m+1} \\frac{\\pi}{2}) | i = 1, 2 ... m\\}$" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": null, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "def count_alpha_beta (real_density, gen_density, num_angles = 1000):\n", |
| 141 | + " assert real_density.shape == gen_density.shape\n", |
| 142 | + " alpha_vec = []\n", |
| 143 | + " beta_vec = []\n", |
| 144 | + " angles = np.linspace(1e-6, np.pi/2 - 1e-6, num=num_angles)\n", |
| 145 | + " # you code\n", |
| 146 | + " return alpha_vec, beta_vec" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "For stability, take the average of several repetitions" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": null, |
| 159 | + "metadata": {}, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "def count_prd(reals, gens, repeat_number = 10):\n", |
| 163 | + " vectors = [count_alpha_beta(reals, gens) for i in range(repeat_number)]\n", |
| 164 | + " vectors = np.array(vectors).mean(axis=0)\n", |
| 165 | + " print (vectors.shape)\n", |
| 166 | + " return vectors" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "markdown", |
| 171 | + "metadata": {}, |
| 172 | + "source": [ |
| 173 | + "## 2. Apply it##" |
| 174 | + ] |
| 175 | + }, |
| 176 | + { |
| 177 | + "cell_type": "code", |
| 178 | + "execution_count": null, |
| 179 | + "metadata": {}, |
| 180 | + "outputs": [], |
| 181 | + "source": [ |
| 182 | + "a, b = bin_counts(real_data.image, fake_data1.image)\n", |
| 183 | + "c, d = bin_counts(real_gan_data.image, gan_data.image)" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "markdown", |
| 188 | + "metadata": {}, |
| 189 | + "source": [ |
| 190 | + "## 3. Make vectors for plot and plot ##" |
| 191 | + ] |
| 192 | + }, |
| 193 | + { |
| 194 | + "cell_type": "code", |
| 195 | + "execution_count": null, |
| 196 | + "metadata": {}, |
| 197 | + "outputs": [], |
| 198 | + "source": [ |
| 199 | + "data_for_plots = count_prd(a, b)\n", |
| 200 | + "data_for_plots2 = count_prd(c, d)" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": null, |
| 206 | + "metadata": {}, |
| 207 | + "outputs": [], |
| 208 | + "source": [ |
| 209 | + "fig = plt.figure(figsize=(2.5, 2.5), dpi=200)\n", |
| 210 | + "fig.add_subplot(111).tick_params(axis='both', which='major', labelsize=8)\n", |
| 211 | + "plt.xlim([0, 1])\n", |
| 212 | + "plt.ylim([0, 1])\n", |
| 213 | + "plt.xlabel('Recall', fontsize=12)\n", |
| 214 | + "plt.ylabel('Precision', fontsize=12)\n", |
| 215 | + "plt.plot(data_for_plots[0], data_for_plots[1], label = \"VAE\")\n", |
| 216 | + "plt.plot(data_for_plots2[0], data_for_plots2[1], label = \"GAN\")\n", |
| 217 | + "plt.legend()\n", |
| 218 | + "plt.show()" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "markdown", |
| 223 | + "metadata": {}, |
| 224 | + "source": [ |
| 225 | + "**What curves were obtained for the first(VAE) and the second(GAN) models? What can we say about the advantages and disadvantages of each model?**" |
| 226 | + ] |
| 227 | + }, |
| 228 | + { |
| 229 | + "cell_type": "code", |
| 230 | + "execution_count": null, |
| 231 | + "metadata": {}, |
| 232 | + "outputs": [], |
| 233 | + "source": [ |
| 234 | + "#type answer here" |
| 235 | + ] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "markdown", |
| 239 | + "metadata": { |
| 240 | + "collapsed": true |
| 241 | + }, |
| 242 | + "source": [ |
| 243 | + "## Bonus: about features' space##\n", |
| 244 | + "\n", |
| 245 | + "It is possible to transfer the picture-> embedding, for example, using the 1st part of the Inception network as a feature extraxtor. This embedding can be used for bin counts also" |
| 246 | + ] |
| 247 | + }, |
| 248 | + { |
| 249 | + "cell_type": "code", |
| 250 | + "execution_count": null, |
| 251 | + "metadata": {}, |
| 252 | + "outputs": [], |
| 253 | + "source": [ |
| 254 | + "# if you came here and still alive, the implementation of idea above will give you extra points =)\n" |
| 255 | + ] |
| 256 | + } |
| 257 | + ], |
| 258 | + "metadata": { |
| 259 | + "kernelspec": { |
| 260 | + "display_name": "Python 3", |
| 261 | + "language": "python", |
| 262 | + "name": "python3" |
| 263 | + }, |
| 264 | + "language_info": { |
| 265 | + "codemirror_mode": { |
| 266 | + "name": "ipython", |
| 267 | + "version": 3 |
| 268 | + }, |
| 269 | + "file_extension": ".py", |
| 270 | + "mimetype": "text/x-python", |
| 271 | + "name": "python", |
| 272 | + "nbconvert_exporter": "python", |
| 273 | + "pygments_lexer": "ipython3", |
| 274 | + "version": "3.6.6" |
| 275 | + } |
| 276 | + }, |
| 277 | + "nbformat": 4, |
| 278 | + "nbformat_minor": 2 |
| 279 | +} |
0 commit comments