-
Notifications
You must be signed in to change notification settings - Fork 0
/
approaches_pass.py
276 lines (232 loc) · 9.82 KB
/
approaches_pass.py
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import numpy as np
import os
import pandas as pd
import pickle
import torch
from torch import nn
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
from approaches_final import baseline
from sklearn.cluster import KMeans
from sklearn.metrics import roc_curve
from sklearn.model_selection import train_test_split
from tqdm import tqdm
from calibration_methods import BinningCalibration
from calibration_methods import SplinesCalibration
from calibration_methods import IsotonicCalibration
from calibration_methods import BetaCalibration
from approaches_final import find_threshold
from approaches_agenda import collect_embeddings_rfw_agenda, collect_embeddings_bfw_agenda, collect_embeddings_ijbc_agenda
from approaches_agenda import collect_pair_embeddings_rfw, collect_pair_embeddings_bfw, collect_pair_embeddings_ijbc
def pass_att(dataset_name, feature, db_fold, nbins, calibration_method):
if dataset_name == 'rfw':
embeddings, subgroup_embeddings, id_embeddings = collect_embeddings_rfw_agenda(feature, db_fold['cal'])
elif 'bfw' in dataset_name:
embeddings, subgroup_embeddings, id_embeddings = collect_embeddings_bfw_agenda(feature, db_fold['cal'])
elif 'ijbc' in dataset_name:
embeddings, subgroup_embeddings, id_embeddings = collect_embeddings_ijbc_agenda(feature, db_fold['cal'])
subgroup_embeddings = pd.Series(subgroup_embeddings, dtype="category").cat.codes.values
embeddings_train, embeddings_test, id_train, id_test, subgroup_train, subgroup_test \
= train_test_split(embeddings,id_embeddings,subgroup_embeddings, test_size=0.2)
id_train = pd.Series(id_train, dtype="category").cat.codes.values
id_test = pd.Series(id_test, dtype="category").cat.codes.values
train_dataloader = DataLoader(
EmbeddingsDataset(embeddings_train, id_train, subgroup_train),
batch_size=400,
shuffle=True,
num_workers=0)
test_dataloader = DataLoader(
EmbeddingsDataset(embeddings_test, id_test, subgroup_test),
batch_size=400,
shuffle=True,
num_workers=0)
n_id = len(np.unique(id_train))
n_subgroup = len(np.unique(subgroup_train))
Nep = 100
Tep = 10
epochs_stage1 = 100
epochs_stage2 = 100
epochs_stage3 = 5
epochs_stage4 = 5
K = 2
loss_fn = nn.CrossEntropyLoss()
# Initialize
modelM = NeuralNetworkM().cuda()
modelC = NeuralNetworkC(n_id).cuda()
## STAGE 1 ##
# Initialize
modelM = NeuralNetworkM().cuda()
modelC = NeuralNetworkC(n_id).cuda()
optimizer_stage1 = optim.Adam(list(modelM.parameters())+list(modelC.parameters()), lr=1e-2)
for epoch in tqdm(range(epochs_stage1)):
if torch.cuda.is_available():
modelM.train()
modelC.train()
loss_list = []
for batch, (X, y_id, y_subgroup) in enumerate(train_dataloader):
if torch.cuda.is_available():
X = X.cuda()
y_id = y_id.cuda()
y_subgroup = y_subgroup.cuda()
# Compute prediction and loss
prob = modelM(X.float())
prob = modelC(prob)
loss = loss_fn(prob,y_id.long())
loss_list.append(loss)
# Backpropagation
optimizer_stage1.zero_grad()
loss.backward()
optimizer_stage1.step()
for i in tqdm(range(Nep)):
## STAGE 2 ##
if i % Tep == 0:
if torch.cuda.is_available():
modelE = {}
for k in range(K):
modelE[k] = NeuralNetworkE(n_subgroup).cuda()
optimizer_stage2_parameters = list(modelE[0].parameters())
for k in range(1,K):
optimizer_stage2_parameters += list(modelE[k].parameters())
optimizer_stage2 = optim.Adam(optimizer_stage2_parameters, lr=1e-3)
for epoch in tqdm(range(epochs_stage2)):
loss_list = []
for batch, (X, y_id, y_subgroup) in enumerate(train_dataloader):
if torch.cuda.is_available():
X = X.cuda()
y_id = y_id.cuda()
y_subgroup = y_subgroup.cuda()
prob = modelM(X.float())
loss = 0.0
for k in range(K):
loss += loss_fn(modelE[k](prob),y_subgroup.long())
loss_list.append(loss)
# Backpropagation
optimizer_stage2.zero_grad()
loss.backward()
optimizer_stage2.step()
## STAGE 3 ##
optimizer_stage3 = optim.Adam(list(modelM.parameters())+list(modelC.parameters()), lr=1e-4)
for epoch in range(epochs_stage3):
loss_list = []
for batch, (X, y_id, y_subgroup) in enumerate(train_dataloader):
if torch.cuda.is_available():
X = X.cuda()
y_id = y_id.cuda()
y_subgroup = y_subgroup.cuda()
f_out = modelM(X.float())
prob_class = modelC(f_out)
loss_class = loss_fn(prob_class,y_id.long())
loss_deb_list = []
for k in range(K):
prob_subgroup = modelE[k](f_out)
loss_deb = -torch.log(prob_subgroup)/prob_subgroup.shape[1]
loss_deb = loss_deb.sum(axis=1).mean()
loss_deb_list.append(loss_deb)
loss = loss_class+10*max(loss_deb_list)
loss_list.append(loss)
# Backpropagation
optimizer_stage3.zero_grad()
loss.backward()
optimizer_stage3.step()
## STAGE 4 ##
k = i % K
optimizer_stage2 = optim.Adam(modelE[k].parameters(), lr=1e-3)
for epoch in range(epochs_stage4):
modelM.eval()
modelE[k].eval()
size = len(test_dataloader.dataset)
test_loss, correct = 0, 0
scores = torch.zeros(0, 2)
ground_truth = torch.zeros(0)
with torch.no_grad():
for X, y_id, y_subgroup in test_dataloader:
if torch.cuda.is_available():
X = X.cuda()
y_id = y_id.cuda()
y_subgroup = y_subgroup.cuda()
prob = modelM(X.float())
prob = modelE[k](prob)
test_loss += loss_fn(prob,y_subgroup.long()).item()
correct += (prob.argmax(1) == y_subgroup).type(torch.float).sum().item()
test_loss /= size
correct /= size
modelM.train()
modelE[k].train()
if correct > 0.95:
break
for batch, (X, y_id, y_subgroup) in enumerate(train_dataloader):
if torch.cuda.is_available():
X = X.cuda()
y_id = y_id.cuda()
y_subgroup = y_subgroup.cuda()
prob = modelM(X.float())
prob = modelE[k](prob)
loss = loss_fn(prob,y_subgroup.long())
# Backpropagation
optimizer_stage2.zero_grad()
loss.backward()
optimizer_stage2.step()
fair_scores = {}
ground_truth = {}
for dataset in ['cal', 'test']:
if 'ijbc' in dataset_name:
fair_scores[dataset], ground_truth[dataset] = collect_pair_embeddings_ijbc(feature, db_fold[dataset], modelM)
else:
if dataset_name == 'rfw':
embeddings, ground_truth[dataset], subgroups_left, subgroups_right = collect_pair_embeddings_rfw(feature, db_fold[dataset])
elif 'bfw' in dataset_name:
embeddings, ground_truth[dataset], subgroups_left, subgroups_right = collect_pair_embeddings_bfw(feature, db_fold[dataset])
cos = nn.CosineSimilarity(dim=1, eps=1e-6)
modelM.eval()
modelM.cpu()
with torch.no_grad():
temp1 = modelM(embeddings['left'])
temp2 = modelM(embeddings['right'])
output = cos(temp1, temp2)
fair_scores[dataset] = output.numpy()
confidences = baseline(fair_scores, ground_truth, nbins, calibration_method, score_min=-1, score_max=1)
return fair_scores, confidences, modelM, modelC, modelE
class EmbeddingsDataset(Dataset):
"""Embeddings dataset."""
def __init__(self, embeddings, id_embeddings, subgroup_embeddings):
"""
Arguments
"""
self.embeddings = embeddings
self.id_embeddings = id_embeddings
self.subgroup_embeddings = subgroup_embeddings
def __len__(self):
return len(self.embeddings)
def __getitem__(self, idx):
return self.embeddings[idx, :], self.id_embeddings[idx], self.subgroup_embeddings[idx]
class NeuralNetworkM(nn.Module):
def __init__(self):
super(NeuralNetworkM, self).__init__()
self.model = nn.Sequential(
nn.Linear(512, 256),
nn.PReLU(),
)
def forward(self, x):
return self.model(x)
class NeuralNetworkC(nn.Module):
def __init__(self,nClasses):
super(NeuralNetworkC, self).__init__()
self.model = nn.Sequential(
nn.Linear(256, nClasses)
)
def forward(self, x):
return self.model(x)
class NeuralNetworkE(nn.Module):
def __init__(self,nClasses):
super(NeuralNetworkE, self).__init__()
self.model = nn.Sequential(
nn.Linear(256, 128),
nn.SELU(),
nn.Linear(128, 64),
nn.SELU(),
nn.Linear(64, nClasses),
nn.Sigmoid(),
nn.Softmax(dim=1)
)
def forward(self, x):
return self.model(x)