-
Notifications
You must be signed in to change notification settings - Fork 1
/
combined_fine_tuning.py
402 lines (337 loc) · 20.8 KB
/
combined_fine_tuning.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import copy
import os
import utils
if utils.is_local():
os.environ['PYTORCH_ENABLE_MPS_FALLBACK'] = '1'
import numpy as np
import torch
import torch.utils.data
import typing
from tqdm import tqdm
import data_preprocessing
import models
import context_handlers
import neural_evaluation
import neural_metrics
import backbone_pipeline
import neural_fine_tuning
def evaluate_on_test(data_str: str,
model_name: str,
preprocessor: data_preprocessing.FineCoarseDataPreprocessor,
lr: typing.Union[str, float],
fine_tuner: models.FineTuner,
device: torch.device,
loaders: typing.Dict[str, torch.utils.data.DataLoader],
loss: str,
num_epochs: int,
save_files: bool = False):
if loss == "error_BCE":
_, _, test_accuracy, test_f1 = neural_evaluation.evaluate_binary_model(fine_tuner=fine_tuner,
loaders=loaders,
loss=loss,
device=device,
split='test',
preprocessor=preprocessor)
# test_harmonic_mean = 2 / (1 / test_accuracy + 1 / test_f1)
print(utils.blue_text(f'test f1: {test_f1}, test accuracy: {test_accuracy}'))
# print(utils.blue_text(f'harmonic mean of test: {test_harmonic_mean}'))
else:
neural_evaluation.run_combined_evaluating_pipeline(data_str=data_str,
model_name=model_name,
split='test',
lr=lr,
loss=loss,
pretrained_fine_tuner=fine_tuner,
num_epochs=num_epochs,
print_results=True,
save_files=save_files)
print('#' * 100)
def fine_tune_combined_model(data_str: str,
model_name: str,
preprocessor: data_preprocessing.FineCoarseDataPreprocessor,
lr: typing.Union[str, float],
fine_tuner: models.FineTuner,
device: torch.device,
loaders: typing.Dict[str, torch.utils.data.DataLoader],
loss: str,
num_epochs: int,
beta: float = 0.1,
save_files: bool = True,
evaluate_on_test_between_epochs: bool = True,
early_stopping: bool = False,
additional_info: str = None,
save_ground_truth: bool = False):
fine_tuner.to(device)
fine_tuner.train()
train_loader = loaders['train']
num_batches = len(train_loader)
total_train_fine_predictions = None
total_train_coarse_predictions = None
optimizer = torch.optim.Adam(params=fine_tuner.parameters(),
lr=lr)
# scheduler = torch.optim.lr_scheduler.StepLR(optimizer=optimizer,
# step_size=scheduler_step_size,
# gamma=scheduler_gamma)
alpha = preprocessor.num_fine_grain_classes / (preprocessor.num_fine_grain_classes +
preprocessor.num_coarse_grain_classes)
test_fine_ground_truths = []
test_coarse_ground_truths = []
train_eval_losses = []
best_fine_tuner = copy.deepcopy(fine_tuner)
neural_fine_tuning.print_fine_tuning_initialization(fine_tuner=fine_tuner,
num_epochs=num_epochs,
lr=lr,
device=device,
early_stopping=early_stopping)
print('#' * 100 + '\n')
consecutive_epochs_with_no_train_eval_loss_decrease_from_the_minimum = 0
for epoch in range(num_epochs):
# print(f"Current lr={optimizer.param_groups[0]['lr']}")
with (context_handlers.TimeWrapper()):
total_running_loss = torch.Tensor([0.0]).to(device)
running_fine_loss = torch.Tensor([0.0]).to(device)
running_coarse_loss = torch.Tensor([0.0]).to(device)
total_train_fine_predictions = []
total_train_coarse_predictions = []
total_train_fine_ground_truths = []
total_train_coarse_ground_truths = []
error_predictions = []
error_ground_truths = []
batches = tqdm(enumerate(train_loader, 0),
total=num_batches)
for batch_num, batch in batches:
with context_handlers.ClearCache(device=device):
if loss == "error_BCE":
X, Y_pred_fine, Y_pred_coarse, E_true = [b.to(device) for b in batch]
Y_pred_fine_one_hot = torch.nn.functional.one_hot(Y_pred_fine, num_classes=len(
preprocessor.fine_grain_classes_str))
Y_pred_coarse_one_hot = torch.nn.functional.one_hot(Y_pred_coarse, num_classes=len(
preprocessor.coarse_grain_classes_str))
Y_pred = torch.cat(tensors=[Y_pred_fine_one_hot, Y_pred_coarse_one_hot], dim=1).float()
E_pred = fine_tuner(X, Y_pred)
criterion = torch.nn.BCEWithLogitsLoss()
batch_total_loss = criterion(E_pred, E_true.float())
error_predictions += torch.where(E_pred > 0.5, 1, 0).tolist()
error_ground_truths += E_true.tolist()
del X, Y_pred_fine, Y_pred_coarse, E_true
else:
X, Y_true_fine, Y_true_coarse = [batch[i].to(device) for i in [0, 1, 3]]
Y_true_fine_one_hot = torch.nn.functional.one_hot(Y_true_fine, num_classes=len(
preprocessor.fine_grain_classes_str))
Y_true_coarse_one_hot = torch.nn.functional.one_hot(Y_true_coarse, num_classes=len(
preprocessor.coarse_grain_classes_str))
Y_true = torch.cat(tensors=[Y_true_fine_one_hot, Y_true_coarse_one_hot], dim=1).float()
optimizer.zero_grad()
Y_pred = fine_tuner(X)
Y_pred_fine = Y_pred[:, :preprocessor.num_fine_grain_classes]
Y_pred_coarse = Y_pred[:, preprocessor.num_fine_grain_classes:]
if loss == "weighted":
criterion = torch.nn.CrossEntropyLoss()
batch_fine_grain_loss = criterion(Y_pred_fine, Y_true_fine)
batch_coarse_grain_loss = criterion(Y_pred_coarse, Y_true_coarse)
running_fine_loss += batch_fine_grain_loss
running_coarse_loss += batch_coarse_grain_loss
batch_total_loss = alpha * batch_fine_grain_loss + (1 - alpha) * batch_coarse_grain_loss
elif loss == "BCE":
criterion = torch.nn.BCEWithLogitsLoss()
batch_total_loss = criterion(Y_pred, Y_true)
elif loss == "CE":
criterion = torch.nn.CrossEntropyLoss()
batch_total_loss = criterion(Y_pred, Y_true)
elif loss == "soft_marginal":
criterion = torch.nn.MultiLabelSoftMarginLoss()
batch_total_loss = criterion(Y_pred, Y_true)
current_train_fine_predictions = torch.max(Y_pred_fine, 1)[1]
current_train_coarse_predictions = torch.max(Y_pred_coarse, 1)[1]
total_train_fine_predictions += current_train_fine_predictions.tolist()
total_train_coarse_predictions += current_train_coarse_predictions.tolist()
total_train_fine_ground_truths += Y_true_fine.tolist()
total_train_coarse_ground_truths += Y_true_coarse.tolist()
del X, Y_true_fine, Y_true_coarse, Y_pred, Y_pred_fine, Y_pred_coarse
total_running_loss += batch_total_loss.item() / len(batches)
print(f'Current total loss: {total_running_loss.item()}')
if batch_num > 4 and batch_num % 5 == 0:
if loss == "error_BCE":
neural_metrics.print_post_batch_binary_metrics(batch_num=batch_num,
num_batches=num_batches,
train_predictions=error_predictions,
train_ground_truths=error_ground_truths,
batch_total_loss=batch_total_loss.item(), )
else:
neural_metrics.get_and_print_post_metrics(preprocessor=preprocessor,
curr_batch_num=batch_num,
total_batch_num=len(batches),
train_fine_ground_truth=np.array(
total_train_fine_ground_truths),
train_fine_prediction=np.array(
total_train_fine_predictions),
train_coarse_ground_truth=np.array(
total_train_coarse_ground_truths),
train_coarse_prediction=np.array(
total_train_coarse_predictions))
batch_total_loss.backward()
optimizer.step()
if epoch == 0:
print(utils.blue_text(
f'coarse grain label use and count: '
f'{np.unique(np.array(total_train_coarse_ground_truths), return_counts=True)}'))
print(utils.blue_text(
f'fine grain label use and count: '
f'{np.unique(np.array(total_train_fine_ground_truths), return_counts=True)}'))
if loss == "error_BCE":
neural_metrics.get_and_print_post_epoch_binary_metrics(
epoch=epoch,
num_epochs=num_epochs,
train_predictions=error_predictions,
train_ground_truths=error_ground_truths,
total_running_loss=total_running_loss.item()
)
else:
neural_metrics.get_and_print_post_metrics(preprocessor=preprocessor,
curr_epoch=epoch,
total_num_epochs=num_epochs,
train_fine_ground_truth=np.array(
total_train_fine_ground_truths),
train_fine_prediction=np.array(
total_train_fine_predictions),
train_coarse_ground_truth=np.array(
total_train_coarse_ground_truths),
train_coarse_prediction=np.array(
total_train_coarse_predictions))
if evaluate_on_test_between_epochs:
evaluate_on_test(data_str=data_str,
model_name=model_name,
preprocessor=preprocessor,
lr=lr,
fine_tuner=best_fine_tuner,
device=device,
loaders=loaders,
loss=loss,
num_epochs=num_epochs,
save_files=save_files)
if early_stopping:
if loss == "error_BCE":
_, _, train_eval_accuracy, train_eval_f1 = neural_evaluation.evaluate_binary_model(
fine_tuner=fine_tuner,
loaders=loaders,
loss=loss,
device=device,
split='train_eval',
preprocessor=preprocessor)
train_eval_harmonic_mean = 2 / (1 / train_eval_accuracy + 1 / train_eval_f1)
print(utils.blue_text(f'harmonic mean of train eval: {train_eval_harmonic_mean}'))
# current_stopping_criterion_value = train_eval_harmonic_mean
else:
curr_train_eval_loss = neural_evaluation.evaluate_combined_model(preprocessor=preprocessor,
fine_tuner=fine_tuner,
loaders=loaders,
loss=loss,
device=device,
split='train_eval')[-1]
print(f'The current train eval loss is {utils.red_text(curr_train_eval_loss)}')
if not len(train_eval_losses) or \
(len(train_eval_losses) and curr_train_eval_loss < min(train_eval_losses)):
print(utils.green_text(f'The last loss is lower than previous ones. Updating the best fine tuner'))
best_fine_tuner = copy.deepcopy(fine_tuner)
if len(train_eval_losses) and curr_train_eval_loss >= min(train_eval_losses):
consecutive_epochs_with_no_train_eval_loss_decrease_from_the_minimum += 1
else:
consecutive_epochs_with_no_train_eval_loss_decrease_from_the_minimum = 0
if consecutive_epochs_with_no_train_eval_loss_decrease_from_the_minimum == 6:
print(utils.red_text(f'finish training, stop criteria met!!!'))
break
train_eval_losses += [curr_train_eval_loss]
if not evaluate_on_test_between_epochs:
evaluate_on_test(data_str=data_str,
model_name=model_name,
preprocessor=preprocessor,
lr=lr,
fine_tuner=best_fine_tuner,
device=device,
loaders=loaders,
loss=loss,
num_epochs=num_epochs,
save_files=save_files)
if loss == "error_BCE":
torch.save(best_fine_tuner.state_dict(),
f"models/binary_models/binary_error_{best_fine_tuner}_"
f"lr{lr}_loss_{loss}_e{num_epochs}_{additional_info}.pth")
else:
if not os.path.isdir(f'models'):
os.mkdir(f'models')
torch.save(best_fine_tuner.state_dict(),
f"models/{data_str}_{best_fine_tuner}_lr{lr}_{loss}_e{num_epochs}_{additional_info}.pth")
# save prediction file for EDCR and error model
neural_evaluation.run_combined_evaluating_pipeline(data_str=data_str,
model_name=model_name,
split='train',
lr=lr,
loss=loss,
pretrained_fine_tuner=best_fine_tuner,
num_epochs=num_epochs,
print_results=True,
save_files=save_files,
additional_info=additional_info)
neural_evaluation.run_combined_evaluating_pipeline(data_str=data_str,
model_name=model_name,
split='test',
lr=lr,
loss=loss,
pretrained_fine_tuner=best_fine_tuner,
num_epochs=num_epochs,
print_results=True,
save_files=save_files,
additional_info=additional_info)
print('#' * 100)
if save_ground_truth:
if not os.path.exists(f"{backbone_pipeline.combined_results_path}test_fine_true.npy"):
np.save(f"{backbone_pipeline.combined_results_path}test_fine_true.npy", test_fine_ground_truths)
if not os.path.exists(f"{backbone_pipeline.combined_results_path}test_coarse_true.npy"):
np.save(f"{backbone_pipeline.combined_results_path}test_coarse_true.npy", test_coarse_ground_truths)
return total_train_fine_predictions, total_train_coarse_predictions
def run_combined_fine_tuning_pipeline(data_str: str,
model_name: str,
lr: typing.Union[str, float],
num_epochs: int,
loss: str = 'BCE',
pretrained_path: str = None,
save_files: bool = True,
debug: bool = utils.is_debug_mode(),
additional_info: str = None,
evaluate_on_test_between_epochs: bool = True,
evaluate_train_eval: bool = True):
preprocessor, fine_tuners, loaders, devices = (
backbone_pipeline.initiate(data_str=data_str,
model_name=model_name,
lr=lr,
combined=True,
pretrained_path=pretrained_path,
train_eval_split=0.8 if evaluate_train_eval else None,
debug=debug)
)
for fine_tuner in fine_tuners:
fine_tune_combined_model(
data_str=data_str,
model_name=model_name,
preprocessor=preprocessor,
lr=lr,
fine_tuner=fine_tuner,
device=devices[0],
loaders=loaders,
loss=loss,
num_epochs=num_epochs,
save_files=save_files,
additional_info=additional_info,
early_stopping=evaluate_train_eval,
evaluate_on_test_between_epochs=evaluate_on_test_between_epochs
)
print('#' * 100)
if __name__ == '__main__':
run_combined_fine_tuning_pipeline(data_str='openimage',
model_name='vit_b_16',
lr=0.0001,
num_epochs=50,
loss='BCE',
additional_info='additional',
evaluate_on_test_between_epochs=False,
evaluate_train_eval=False)