-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_subject_classifier.py
283 lines (244 loc) · 10.4 KB
/
train_subject_classifier.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
277
278
279
280
281
282
283
import os
import numpy as np
import pandas as pd
import torch
from tqdm import tqdm
from joblib import Parallel, delayed
from torch.utils.data import DataLoader, Dataset, WeightedRandomSampler
from sklearn.utils.class_weight import compute_class_weight
import torch.nn.functional as F
from torch import nn
import schedulefree
from tqdm import tqdm as std_tqdm
from accelerate import InitProcessGroupKwargs
from accelerate import Accelerator
from datetime import timedelta
from functools import partial
# Define a custom dataset class
class ClickDataset(Dataset):
def __init__(self, df, max_x, max_y, click_div=4):
self.df = df
self.max_x = max_x
self.max_y = max_y
self.click_div = click_div # Resample factor
self.max_clicks = 100
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
row = self.df.iloc[idx]
label, clicks = row["label"], row["clicks"]
# Turn clicks into a one-hot encoding of x and y
clicks = np.asarray(clicks) // self.click_div
x_enc = np.zeros((len(clicks), self.max_x))
y_enc = np.zeros((len(clicks), self.max_y))
x_enc[:, clicks[:, 0]] = 1
y_enc[:, clicks[:, 1]] = 1
click_enc = np.concatenate((x_enc, y_enc), 1)
if len(click_enc) > self.max_clicks:
click_enc = click_enc[:self.max_clicks]
elif len(click_enc) < self.max_clicks:
click_enc = np.pad(click_enc, ((0, self.max_clicks - len(click_enc)), (0, 0)))
click_enc = torch.from_numpy(click_enc).float()
return label, click_enc
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, output_size, num_layers=2):
super(RNN, self).__init__()
self.hidden_size = hidden_size
self.xy_proj = nn.Linear(input_size, hidden_size)
self.rnn = nn.GRU(
hidden_size * 2,
hidden_size,
num_layers=num_layers,
batch_first=True,
bidirectional=True
)
self.readout = nn.Linear(hidden_size * 2, output_size)
def forward(self, input):
x_proj = self.xy_proj(input[:, ..., :input.shape[-1]//2])
y_proj = self.xy_proj(input[:, ..., input.shape[-1]//2:])
proj = torch.concat((x_proj, y_proj), dim=-1)
output, _ = self.rnn(proj) # Default hidden at t0 to 0 init
output = self.readout(output[:, -1])
return output
def compute_clicks(clickmap_x, clickmap_y, n_jobs=-1):
"""
Parallelizes the processing of clickmaps by zipping x and y coordinates.
Args:
clickmap_x (list or array): List of x coordinates.
clickmap_y (list or array): List of y coordinates.
n_jobs (int, optional): Number of parallel jobs. Defaults to -1 (use all cores).
Returns:
list: List of zipped (x, y) tuples for each clickmap.
"""
def zip_clicks(x, y):
return list(zip(x, y))
clicks = Parallel(n_jobs=n_jobs)(
delayed(zip_clicks)(x, y)
for x, y in tqdm(zip(clickmap_x, clickmap_y), desc="Reformatting clicks", total=len(clickmap_x))
)
return clicks
def main():
# Inputs
cheaters = [780,1045,1551,1548,1549,1550,1173]
bad_players = [1164,664,933,219,961,596,1378,501]
good_players = [1131,1176,350,279,758,969,431, 339,1420,331,1346,878,540,607,1221,686,849, 984,355,931,790,575,1425,1099,347, 743,522,293,264, 976, 988, 619, 869, 1417, 1294, 707, 329, 930, 952, 270, 1382, 1441, 1391, 1486, 404, 1430, 317, 855, 703, 945, 708, 1354, 525, 1124, 182, 783, 222, 870, 326, 382, 434, 701, 1339, 367, 611, 1063, 1042, 385, 694, 625, 1006, 370, 463, 1258, 852, 1278,1002, 671,1076, 1016,729,337,420,1061,281, 368,811,1485,566]
catch_thresh = 0.95
data_file = "clickme_datasets/prj_clickmev2_train_imagenet_10_10_2024.npz"
train_batch_size = 32
train_num_workers = 0
# max_x, max_y = 1000, 1000
click_div = 6
lr = 1e-3
ckpts = "checkpoints"
os.makedirs(ckpts, exist_ok=True)
# Prepare indices
cheaters_and_bad_players = np.concatenate([cheaters, bad_players])
good_players = np.asarray(good_players)
n = len(cheaters_and_bad_players)
np.random.seed(42)
good_players = good_players[np.random.permutation(len(good_players))[:n]]
# Get data
data = np.load(data_file, allow_pickle=True)
image_path = data["file_pointer"]
clickmap_x = data["clickmap_x"]
clickmap_y = data["clickmap_y"]
user_id = data["user_id"]
user_catch_trial = data["user_catch_trial"]
# Remove empties
empty_x = [i for i, x in enumerate(clickmap_x) if len(x) > 0]
empty_y = [i for i, y in enumerate(clickmap_y) if len(y) > 0]
not_empty = np.unique(np.concatenate([empty_x, empty_y]))
image_path = image_path[not_empty]
clickmap_x = clickmap_x[not_empty]
clickmap_y = clickmap_y[not_empty]
user_id = user_id[not_empty]
user_catch_trial = user_catch_trial[not_empty]
# Get max x and y
max_x = max([max(x) for x in clickmap_x if len(x)])
max_y = max([max(x) for x in clickmap_y if len(x)])
max_size = max(max_x, max_y) // click_div
max_x, max_y = max_size, max_size
# Filter subjects by catch trials
catch_trials = user_catch_trial >= catch_thresh
image_path = image_path[catch_trials]
clickmap_x = clickmap_x[catch_trials]
clickmap_y = clickmap_y[catch_trials]
user_id = user_id[catch_trials]
print("Catch trial filter from {} to {}".format(len(user_catch_trial), catch_trials.sum()))
# Filter down to good/bad players
all_players = np.concatenate([good_players, cheaters_and_bad_players])
flt = np.in1d(user_id, all_players)
image_path = image_path[flt]
clickmap_x = clickmap_x[flt]
clickmap_y = clickmap_y[flt]
user_id = user_id[flt]
label = np.in1d(user_id, good_players).astype(int)
# Usage
clicks = compute_clicks(clickmap_x, clickmap_y)
# clicks = [list(zip(x, y)) for x, y in tqdm(zip(clickmap_x, clickmap_y), desc="Reformtting clicks", total=len(clickmap_x))]
# Create dataframe
df = pd.DataFrame({"image_path": image_path, "label": label, "clicks": clicks, "user_id": user_id})
# Close npz
del data.f
data.close() # avoid the "too many files are open" error
# Create data loaders
train_label_index = df.label.values
unique_classes, class_sample_count = np.unique(train_label_index, return_counts=True)
class_weights = compute_class_weight("balanced", classes=unique_classes, y=train_label_index)
class_weights = torch.from_numpy(class_weights).float()
samples_weight_train = np.asarray([class_weights[t] for t in train_label_index])
samples_weight_train = torch.from_numpy(samples_weight_train).double()
sampler = WeightedRandomSampler(samples_weight_train, len(samples_weight_train))
print("Building dataloaders")
train_loader = DataLoader(
ClickDataset(df, max_x, max_y, click_div=click_div),
batch_size=train_batch_size,
sampler=sampler,
drop_last=True,
pin_memory=True,
num_workers=train_num_workers
)
# Initialize model
print("Preparing models")
n_hidden = 32
input_dim = max_x # Do a one-hot encoding of x concatenated with one-hot encoding of y
model = RNN(input_dim, n_hidden, len(unique_classes))
# Save meta data needed to run the model
np.savez(
"participant_model_metadata.npz",
max_x=max_x,
max_y=max_y,
click_div=click_div,
n_hidden=n_hidden,
input_dim=input_dim,
n_classes=len(unique_classes)
)
# Prepare everything else
process_group_kwargs = InitProcessGroupKwargs(timeout=timedelta(seconds=5400))
accelerator = Accelerator(kwargs_handlers=[process_group_kwargs])
device = accelerator.device
tqdm = partial(std_tqdm, dynamic_ncols=True)
optimizer = schedulefree.AdamWScheduleFree(model.parameters(), lr=lr)
# Prepare accelerator
model, optimizer, train_loader = accelerator.prepare(model, optimizer, train_loader)
# Train model
epochs = 10
best_loss = np.inf
losses = []
steps_per_epoch = None
for epoch in range(epochs):
model.train()
if steps_per_epoch is None:
steps_per_epoch = len(train_loader)
if accelerator.is_main_process:
train_progress = tqdm(
total=steps_per_epoch,
desc=f"Training Epoch {epoch+1}/{epochs}"
)
for label, click_enc in train_loader:
optimizer.zero_grad(set_to_none=True)
pred = model(click_enc)
loss = F.cross_entropy(pred, label)
if torch.isnan(loss):
print("Skipping nan loss")
else:
accelerator.backward(loss)
optimizer.step()
loss = loss.item()
losses.append(loss)
if accelerator.is_main_process:
train_progress.set_postfix({"Train loss": f"{loss:.4f}"})
train_progress.update()
if accelerator.is_main_process:
if loss < best_loss:
checkpoint_filename = os.path.join(ckpts, f'model_epoch_{epoch+1}.pth')
torch.save(accelerator.unwrap_model(model).state_dict(), checkpoint_filename)
best_loss = loss
"""
Code for validation loop that needs to work to run:
if accelerator.is_main_process:
val_progress = tqdm(
total=steps_per_epoch,
desc=f"Validation Epoch {epoch+1}/{epochs}"
)
val_losses = []
with torch.no_grad():
for label, click_enc in val_loader:
pred = model(click_enc)
loss = F.cross_entropy(pred, label)
loss = loss.item()
val_losses.append(loss)
if accelerator.is_main_process:
val_progress.update()
val_loss = np.mean(val_losses)
val_progress.set_postfix({"Val loss": f"{val_loss:.4f}"})
val_progress.update()
if accelerator.is_main_process:
if val_loss < best_loss: # Note: Remove the saving logic in the training loop.
checkpoint_filename = os.path.join(ckpts, f'model_epoch_{epoch+1}.pth')
torch.save(accelerator.unwrap_model(model).state_dict(), checkpoint_filename)
best_loss = val_loss
"""
accelerator.wait_for_everyone()
if __name__ == "__main__":
main()