Newer
Older
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Celeb Faces Dataset (CelebA)\n",
"=================================================\n",
"---\n",
"Introduction au Deep Learning (IDLE) - S. Arias, E. Maldonado, JL. Parouty - CNRS/SARI/DEVLOG - 2020 \n",
"\n",
"We'll do the same thing again but with a more interesting dataset: CelebFaces \n",
"About this dataset : http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html\n",
"\n",
"## Episode 1 : Preparation of data - Batch mode\n",
"\n",
" - Save enhanced datasets in h5 file format\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1 - Import and init\n",
"### 1.2 - Import"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import pandas as pd\n",
"from skimage import io, transform\n",
"\n",
"import os,time,sys,json,glob\n",
"import csv\n",
"import math, random\n",
"\n",
"from importlib import reload\n",
"\n",
"sys.path.append('..')\n",
"import fidle.pwk as ooo\n",
"\n",
"ooo.init()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.2 - Directories and files :"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"place, dataset_dir = ooo.good_place( { 'GRICAD' : f'{os.getenv(\"SCRATCH_DIR\",\"\")}/PROJECTS/pr-fidle/datasets/celeba',\n",
" 'IDRIS' : f'{os.getenv(\"WORK\",\"\")}/datasets/celeba' } )\n",
"\n",
"dataset_csv = f'{dataset_dir}/list_attr_celeba.csv'\n",
"dataset_img = f'{dataset_dir}/img_align_celeba'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2 - Read filenames catalog"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset_desc = pd.read_csv(dataset_csv, header=0)\n",
"dataset_desc = dataset_desc.reindex(np.random.permutation(dataset_desc.index))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3 - Save as clusters of n images"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.2 - Cooking function"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def read_and_save( dataset_img, dataset_desc, \n",
" cluster_size=1000, cluster_dir='./dataset_cluster', cluster_name='images',\n",
" image_size=(128,128)):\n",
" \n",
" def save_cluster(imgs,desc,cols,id):\n",
" file_img = f'{cluster_dir}/{cluster_name}-{id:03d}.npy'\n",
" file_desc = f'{cluster_dir}/{cluster_name}-{id:03d}.csv'\n",
" np.save(file_img, np.array(imgs))\n",
" df=pd.DataFrame(data=desc,columns=cols)\n",
" df.to_csv(file_desc, index=False)\n",
" return [],[],id+1\n",
" \n",
" start_time = time.time()\n",
" cols = list(dataset_desc.columns)\n",
"\n",
" # ---- Check if cluster files exist\n",
" #\n",
" if os.path.isfile(f'{cluster_dir}/images-000.npy'):\n",
" print('\\n*** Oops. There are already clusters in the target folder!\\n')\n",
" return 0,0\n",
" \n",
" # ---- Create cluster_dir\n",
" #\n",
" os.makedirs(cluster_dir, mode=0o750, exist_ok=True)\n",
" \n",
" # ---- Read and save clusters\n",
" #\n",
" imgs, desc, cluster_id = [],[],0\n",
" #\n",
" for i,row in dataset_desc.iterrows():\n",
" #\n",
" filename = f'{dataset_img}/{row.image_id}'\n",
" #\n",
" # ---- Read image, resize (and normalize)\n",
" #\n",
" img = io.imread(filename)\n",
" img = transform.resize(img, image_size)\n",
" #\n",
" # ---- Add image and description\n",
" #\n",
" imgs.append( img )\n",
" desc.append( row.values )\n",
" #\n",
" # ---- Progress bar\n",
" #\n",
" ooo.update_progress(f'Cluster {cluster_id:03d} :',len(imgs),cluster_size)\n",
" #\n",
" # ---- Save cluster if full\n",
" #\n",
" if len(imgs)==cluster_size:\n",
" imgs,desc,cluster_id=save_cluster(imgs,desc,cols, cluster_id)\n",
"\n",
" # ---- Save uncomplete cluster\n",
" if len(imgs)>0 : imgs,desc,cluster_id=save_cluster(imgs,desc,cols,cluster_id)\n",
"\n",
" duration=time.time()-start_time\n",
" return cluster_id,duration\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 4.3 - Cluster building"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ---- Cluster size\n",
"\n",
"cluster_size_train = 10000\n",
"cluster_size_test = 10000\n",
"image_size = (192,160)\n",
"\n",
"# ---- Clusters location\n",
"\n",
"train_dir = f'{dataset_dir}/clusters-M.train'\n",
"test_dir = f'{dataset_dir}/clusters-M.test'\n",
"\n",
"# ---- x_train, x_test\n",
"#\n",
"n1,d1 = read_and_save(dataset_img, dataset_desc[:200000],\n",
" cluster_size = cluster_size_train, \n",
" cluster_dir = train_dir,\n",
" image_size = image_size )\n",
"\n",
"n2,d2 = read_and_save(dataset_img, dataset_desc[200000:],\n",
" cluster_size = cluster_size_test, \n",
" cluster_dir = test_dir,\n",
" image_size = image_size )\n",
" \n",
"print(f'\\n\\nDuration : {d1+d2:.2f} s or {ooo.hdelay(d1+d2)}')\n",
"print(f'Train clusters : {train_dir}')\n",
"print(f'Test clusters : {test_dir}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"----\n",
"That's all folks !"
]
},
{
"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": 4
}