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
|
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# LFW Names\n",
"\n",
"- add gender and format names"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"%reload_ext autoreload\n",
"%autoreload 2\n",
"\n",
"import os\n",
"from os.path import join\n",
"import math\n",
"from glob import glob\n",
"from random import randint\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"\n",
"import sys\n",
"sys.path.append('/work/megapixels_dev/megapixels/')\n",
"from app.utils import file_utils"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"DATA_STORE = '/data_store_nas/'\n",
"dir_dataset = join(DATA_STORE, 'datasets/people/lfw')\n",
"fp_names = join(dir_dataset, 'lfw_names.csv')\n",
"fp_male = join(dir_dataset, 'male_names.txt')\n",
"fp_female = join(dir_dataset, 'female_names.txt')"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Alfred Ford', 'Craig Fitzgibbon']\n",
"['Claudia Coslovich', 'Allison Searing']\n"
]
}
],
"source": [
"# load names\n",
"df_names = pd.read_csv(fp_names)\n",
"names = df_names.to_dict('index')\n",
"# load gender\n",
"names_male = file_utils.load_text(fp_male)\n",
"names_female = file_utils.load_text(fp_female)\n",
"# convert filenames to csv names\n",
"names_male = [t.replace('_',' ')[:-9] for t in names_male]\n",
"names_female = [t.replace('_',' ')[:-9] for t in names_female]\n",
"# check names\n",
"print(names_male[:2])\n",
"print(names_female[:2])"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'AJ Cook', 'images': 1}\n"
]
}
],
"source": [
"for idx, n in names.items():\n",
" print(n)\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"# add gender to name item dict\n",
"for idx, item in names.items():\n",
" name = item['name']\n",
" if name in names_male:\n",
" g = 'm'\n",
" elif name in names_female:\n",
" g = 'f'\n",
" elif name == 'Tara Kirk':\n",
" g = 'f' # unlabeled item\n",
" else:\n",
" g = 'x'\n",
" names[idx]['gender'] = g"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'AJ Cook', 'images': 1, 'gender': 'f'}\n"
]
}
],
"source": [
"names_list = list(names.values())\n",
"for n in names_list:\n",
" print(n)\n",
" break"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"# save to csv\n",
"fp_gendered = join(dir_dataset, 'lfw_names_gendered.csv')\n",
"df_names_gendered = pd.DataFrame.from_dict(list(names.values())) # ignore the indices\n",
"df_names_gendered.to_csv(fp_gendered, index=False)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"#%cat $fp_names_gendered | head -n2"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n",
"4277 1472 5749 None\n"
]
}
],
"source": [
"f = [x for k, x in names.items() if x['gender'] == 'f']\n",
"m = [x for k, x in names.items() if x['gender'] == 'm']\n",
"x = [x for k, x in names.items() if x['gender'] not in ['f','m']]\n",
"print(len(m), len(f), len(f) + len(m), print(x))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5749\n"
]
}
],
"source": [
"print(len(names))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:megapixels]",
"language": "python",
"name": "conda-env-megapixels-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"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|