Skip to content

Commit 2d1e96d

Browse files
author
Victor Yurchenko
committed
hw3 part2&3
1 parent 0717a01 commit 2d1e96d

8 files changed

+3115
-0
lines changed

homework03/homework03_part2_autoencoders_basic.ipynb

Lines changed: 1031 additions & 0 deletions
Large diffs are not rendered by default.

homework03/homework03_part2_vae_advanced.ipynb

Lines changed: 563 additions & 0 deletions
Large diffs are not rendered by default.

homework03/homework03_part3_gan_basic.ipynb

Lines changed: 475 additions & 0 deletions
Large diffs are not rendered by default.

homework03/homework03_part3a_gan_advanced.ipynb

Lines changed: 507 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
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+
}

homework03/lfw_dataset.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import numpy as np
2+
import os
3+
from scipy.misc import imread,imresize
4+
import pandas as pd
5+
6+
def fetch_lfw_dataset(attrs_name = "lfw_attributes.txt",
7+
images_name = "lfw-deepfunneled",
8+
raw_images_name = "lfw",
9+
use_raw=False,
10+
dx=80,dy=80,
11+
dimx=45,dimy=45
12+
): # sad smile
13+
14+
#download if not exists
15+
if (not use_raw) and not os.path.exists(images_name):
16+
print("images not found, donwloading...")
17+
os.system("wget http://vis-www.cs.umass.edu/lfw/lfw-deepfunneled.tgz -O tmp.tgz")
18+
print("extracting...")
19+
os.system("tar xvzf tmp.tgz && rm tmp.tgz")
20+
print("done")
21+
assert os.path.exists(images_name)
22+
23+
if use_raw and not os.path.exists(raw_images_name):
24+
print("images not found, donwloading...")
25+
os.system("wget http://vis-www.cs.umass.edu/lfw/lfw.tgz -O tmp.tgz")
26+
print("extracting...")
27+
os.system("tar xvzf tmp.tgz && rm tmp.tgz")
28+
print("done")
29+
assert os.path.exists(raw_images_name)
30+
31+
if not os.path.exists(attrs_name):
32+
print("attributes not found, downloading...")
33+
os.system("wget http://www.cs.columbia.edu/CAVE/databases/pubfig/download/%s" % attrs_name)
34+
print("done")
35+
36+
#read attrs
37+
df_attrs = pd.read_csv("lfw_attributes.txt",sep='\t',skiprows=1,)
38+
df_attrs = pd.DataFrame(df_attrs.iloc[:,:-1].values, columns = df_attrs.columns[1:])
39+
40+
41+
#read photos
42+
dirname = raw_images_name if use_raw else images_name
43+
photo_ids = []
44+
for dirpath, dirnames, filenames in os.walk(dirname):
45+
for fname in filenames:
46+
if fname.endswith(".jpg"):
47+
fpath = os.path.join(dirpath,fname)
48+
photo_id = fname[:-4].replace('_',' ').split()
49+
person_id = ' '.join(photo_id[:-1])
50+
photo_number = int(photo_id[-1])
51+
photo_ids.append({'person':person_id,'imagenum':photo_number,'photo_path':fpath})
52+
53+
photo_ids = pd.DataFrame(photo_ids)
54+
55+
#mass-merge
56+
#(photos now have same order as attributes)
57+
df_attrs['imagenum'] = df_attrs['imagenum'].astype(np.int64)
58+
df = pd.merge(df_attrs, photo_ids, on=('person','imagenum'))
59+
60+
assert len(df)==len(df_attrs),"lost some data when merging dataframes"
61+
62+
#image preprocessing
63+
all_photos = df['photo_path'].apply(imread)\
64+
.apply(lambda img:img[dy:-dy,dx:-dx])\
65+
.apply(lambda img: imresize(img,[dimx,dimy]))
66+
67+
all_photos = np.stack(all_photos.values).astype('uint8')
68+
all_attrs = df.drop(["photo_path","person","imagenum"],axis=1)
69+
70+
return all_photos,all_attrs
71+

homework03/linear.png

59.9 KB
Loading

0 commit comments

Comments
 (0)