-
Notifications
You must be signed in to change notification settings - Fork 7
/
train_funcs.py
889 lines (653 loc) · 33.3 KB
/
train_funcs.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
from tqdm import tqdm
import time
import copy
from dataload import get_cifar,get_test_loader_cifar
from general_utils import test_data_evaluation
from KD_Loss import kd_loss
import numpy as np
import torch
from torch import nn
from DML_Loss import dml_loss_function
import pdb
criterion = nn.CrossEntropyLoss()
def train_regular_ce(model,
optimizer,
path_to_save,
dataset="cifar10",
epochs = 200,
train_on="cuda:0",
multiple_gpu=None,
scheduler= None,
seed=3,
batch_size = 64):
device = torch.device(train_on)
if ("cuda" in train_on) and (multiple_gpu is not None):
model = nn.DataParallel(model,device_ids=multiple_gpu)
# benchmark time
since = time.time()
# to gpu
model.to(device)
# dataload.py
data_loader_dict,dataset_sizes = get_cifar(batch_size=batch_size, # 64
cifar10_100=dataset) # cifar10/cifar100
# {'train': 50000, 'val': 10000}
# copy the state to best_model_wts
best_model_wts = copy.deepcopy(model.state_dict())
previous_loss = 0.0
best_val_acc = 0.0
best_train_acc = 0.0
train_acc_dict = {}
train_loss_dict = {}
val_acc_dict = {}
val_loss_dict = {}
# tqdm is for progress bar
for epoch in tqdm(range(epochs)):
print('Epoch {}/{}'.format(epoch+1, epochs ))
print('-' * 10)
for phase in ["train","val"]: #phase_list : #['train', 'val']:
if phase == 'train':
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# len(data_loader_dict['train'] = 782
# 782 * 64 = 50048
# len(data_loader_dict['val'] = 157
# 157 * 64 = 10048
for inputs, labels in data_loader_dict[phase]: # phase = train or val
inputs = inputs.to(device) # torch.Size([64, 3, 32, 32])
labels = labels.to(device) # torch.Size([64])
with torch.set_grad_enabled(phase == 'train'):
optimizer.zero_grad()
model_outputs = model(inputs)
# model_outputs[0].shape
# torch.Size([64, 100])
if isinstance(model_outputs, tuple):
# len(preds) = 64
_, preds = torch.max(model_outputs[0], 1) # preds = tensor([68, 58....
loss = criterion(model_outputs[0], labels)
else:
_, preds = torch.max(model_outputs, 1)
loss = criterion(model_outputs,labels)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
# end for
if phase == 'train' and scheduler != None:
scheduler.step()
epoch_loss = running_loss / dataset_sizes[phase]
if previous_loss == 0.0 and phase == "val":
previous_loss = epoch_loss
epoch_acc = running_corrects * 1.0 / dataset_sizes[phase]
if best_val_acc == 0.0 and phase == "val":
best_val_acc = epoch_acc
if best_train_acc == 0.0 and phase == "train":
best_train_acc = epoch_acc
print('{} Loss: {:.4f} ACC: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
if phase == "train":
train_acc_dict[(epochs + 1 )] = epoch_acc
train_loss_dict[(epoch + 1 )] = epoch_loss
elif phase == "val":
val_acc_dict[(epoch + 1 )] = epoch_acc
val_loss_dict[(epoch + 1 )] = epoch_loss
if phase == "val" and epoch_acc > best_val_acc:
best_val_acc = epoch_acc
print('Best VAL Acc: {:4f}'.format(best_val_acc))
# deep copy the model
if phase == 'val' and previous_loss >= epoch_loss:
previous_loss = epoch_loss
torch.save(model.state_dict(), path_to_save)
best_model_wts = copy.deepcopy(model.state_dict())
elif phase == "val" and previous_loss < epoch_loss:
print("Previous Validation Loss is smaller!")
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
print('Best VAL Acc: {:4f}'.format(best_val_acc))
# load best model weights
model.load_state_dict(best_model_wts)
model.eval()
return model
#from defined_losses import *
#from tqdm import tqdm
#import copy
#import time
def train_regular_middle_logits(model,
optimizer,
path_to_save,
middle_logits_model_dict,
dataset="cifar10",
epochs =200,
train_on="cuda",
multiple_gpu=None,
scheduler= None,
batch_size = 64):
device = torch.device(train_on)
paralledled_middle_logits_model_dict = {}
if ("cuda" in train_on) and torch.cuda.is_available():
model = nn.DataParallel(model,device_ids=multiple_gpu)
for key,middle_model in middle_logits_model_dict.items():
if multiple_gpu is not None:
paralledled_middle_logits_model_dict[key] = nn.DataParallel(middle_model,device_ids=multiple_gpu)
else:
paralledled_middle_logits_model_dict[key] = middle_logits_model_dict[key]
paralledled_middle_logits_model_dict[key].to(device)
since = time.time()
model.to(device)
data_loader_dict, dataset_sizes = get_cifar(cifar10_100=dataset,
batch_size=batch_size)
best_acc_branch_dict = {}
best_model_save_path_dict = {}
previous_loss_branch_dict = {}
best_val_acc_branch_dict = {}
best_train_acc_branch_dict = {}
for (branch_key,branch) in paralledled_middle_logits_model_dict.items():
best_acc_branch_dict[branch_key] = 0.0
previous_loss_branch_dict[branch_key] = 0.0
best_val_acc_branch_dict[branch_key] = 0.0
best_train_acc_branch_dict[branch_key] = 0.0
model.eval()
for epoch in tqdm(range(epochs)):
print('Epoch {}/{}'.format(epoch+1, epochs ))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ["train","val"]:
if phase == 'train':
for branch in paralledled_middle_logits_model_dict.values():
branch.train()
else:
for branch in paralledled_middle_logits_model_dict.values():
branch.eval()
running_loss_middle_dict={}
running_corrects_middle_dict = {}
for branch_key,branch in paralledled_middle_logits_model_dict.items():
running_loss_middle_dict[branch_key] = 0.0
running_corrects_middle_dict[branch_key] = 0
# Iterate over data.
for inputs, labels in data_loader_dict[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
optimizer.zero_grad()
model_outputs = model(inputs)
preds_dict ={}
if isinstance(model_outputs,tuple):
_, preds = torch.max(model_outputs[0], 1)
#loss = criterion(model_outputs[0], labels)
else:
_, preds = torch.max(model_outputs, 1)
#loss = criterion(model_outputs,labels)
branch_loss_dict ={}
total_loss = 0.0
for branch_key,branch in paralledled_middle_logits_model_dict.items():
#for VGG Models which returns the intermediate outputs in a list
if not isinstance(model_outputs[1],list):
branch_probs_out = branch(model_outputs[branch_key].detach())
else:
branch_probs_out = branch(model_outputs[1][branch_key-1].detach())
_, preds_dict[branch_key] = torch.max(branch_probs_out, 1)
branch_loss_dict[branch_key] = criterion(branch_probs_out,labels)
total_loss += branch_loss_dict[branch_key]
# backward + optimize only if in training phase
if phase == 'train':
total_loss.backward()
optimizer.step()
for (key_branch,branch_predict) in preds_dict.items():
running_corrects_middle_dict[key_branch] += torch.sum(branch_predict == labels.data)
if phase == 'train' and scheduler != None:
scheduler.step()
epoch_loss_dict = {}
for (key_branch, branch_loss_value) in branch_loss_dict.items():
epoch_loss_dict[key_branch] = branch_loss_value / dataset_sizes[phase]
epoch_acc_dict = {}
for key, value in running_corrects_middle_dict.items():
epoch_acc_dict[key] = value * 1.0 / dataset_sizes[phase]
if phase == "val":
for (branch_key,branch_previous_loss) in previous_loss_branch_dict.items():
if branch_previous_loss == 0.0:
previous_loss_branch_dict[branch_key] =epoch_loss_dict[branch_key]
for (branch_key,branch_best_val_acc) in best_val_acc_branch_dict.items():
if branch_best_val_acc == 0.0:
best_val_acc_branch_dict[branch_key] =epoch_acc_dict[branch_key]
if phase == "train":
for (branch_key, branch_best_train_acc) in best_train_acc_branch_dict.items():
if branch_best_train_acc == 0.0:
best_train_acc_branch_dict[branch_key] = epoch_acc_dict[branch_key]
for key in epoch_acc_dict.keys():
print(phase,"\t",key,"\tACC: ",epoch_acc_dict[key].data,"\tLoss: ",epoch_loss_dict[key].data)
if phase == "val":
for (branch_key, branch_epoch_acc) in epoch_acc_dict.items():
if branch_epoch_acc > best_val_acc_branch_dict[branch_key]:
best_val_acc_branch_dict[branch_key] = epoch_acc_dict[branch_key]
# deep copy the model
if phase == 'val':
for (branch_key,branch_previous_loss) in previous_loss_branch_dict.items():
if branch_previous_loss >= epoch_loss_dict[branch_key]:
previous_loss_branch_dict[branch_key] = epoch_loss_dict[branch_key]
best_val_acc_branch_dict[branch_key] = epoch_acc_dict[branch_key]
torch.save(paralledled_middle_logits_model_dict[branch_key].state_dict(),path_to_save+"_"+str(branch_key))
# _= checkpoint_saver(server=server,
# experiment_name=experiment_name,
# model_name=specific_file_name+str(branch_key),
# epoch=epoch,
# model_state_dict=paralledled_middle_logits_model_dict[branch_key].state_dict(),
# optimizer_state_dict=optimizer.state_dict(),
# loss=loss,
# is_best=True)
# best_model_save_path_dict[branch_key] = experiment_result_saver(model=paralledled_middle_logits_model_dict[branch_key],
# experiment_name=experiment_name,
# server=server,
# specific_file_name=specific_file_name + str(branch_key))
else:
print("Previous Validation Loss is smaller! Branch ==> ",branch_key)
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
def train_kd_or_fitnets_2(student,
trained_teacher,
optimizer,
path_to_save,
dataset="cifar10",
epochs =200,
train_on="cuda",
multiple_gpu=[0,1,2,3],
scheduler= None,
input_data_size=(64,32,32),
kd_alpha = 0.1,
kd_temperature = 5,
seed = 3
):
"""
:param student: either full student for KD or partial model up to guided layer. It can be Regressor added as well.
:param trained_teacher: either full teacher or teacher up to hint layer.
:param optimizer
:param path_to_save: saving path for the trained model.
:param dataset: Default : CIFAR10
:param epochs: Default : 200
:param train_on: the main environment for training(CPU/GPU). Default : Cuda:0 .
:param multiple_gpu: List of GPUs for data-parallelization
:param scheduler:
:param data_output_width: 32(CIFAR10/100)
:param data_output_height: 32(CIFAR10/100)
:param batch_size: 64
:param kd_alpha: Default :0.1 NOTE ==> For FitNets stages, set it to None.
:param kd_temperature: Default :5 NOTE ==> For FitNets stages, set it to None.
"""
np.random.seed(seed)
torch.manual_seed(seed)
if "cuda" in train_on:
torch.cuda.manual_seed_all(seed=seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# print("REPRODUCIBLE")
device = torch.device(train_on)
student.to(device)
trained_teacher.to(device)
if "cuda" in train_on:
student = nn.DataParallel(student, device_ids=multiple_gpu)
trained_teacher = nn.DataParallel(trained_teacher, device_ids=multiple_gpu)
trained_teacher.eval()
since = time.time()
student.to(device)
trained_teacher.to(device)
previous_saved_loss = 0.0
if dataset == "cifar10" or dataset=="cifar100":
data_loader_dict, dataset_sizes = get_cifar(batch_size=input_data_size[0],cifar10_100= dataset)
test_loader = get_test_loader_cifar(dataset=dataset,
batch_size=input_data_size[0],
output_height=input_data_size[1],
output_width=input_data_size[2])
print("Train size ====> ",dataset_sizes["train"],"Val Size ===> ",dataset_sizes["val"])
best_model_wts = copy.deepcopy(student.state_dict())
train_acc_dict = {}
train_loss_dict = {}
val_acc_dict = {}
val_loss_dict = {}
for epoch in tqdm(range(epochs)):
print('Epoch {}/{}'.format(epoch+1, epochs ))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'val']:
if phase == 'train':
student.train() # Set model to training mode
else:
student.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in data_loader_dict[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
optimizer.zero_grad()
student_outputs = student(inputs)
teacher_logits = trained_teacher(inputs)
if isinstance(student_outputs,tuple):
out_s = student_outputs[0]
#_, preds = torch.max(student_outputs[0], 1)
else:
out_s = student_outputs
_, preds = torch.max(out_s, 1)
if isinstance(teacher_logits, tuple):
out_t = teacher_logits[0]
else:
out_t = teacher_logits
if kd_temperature != None and kd_alpha !=None:
#Knowledge Distillation.
# Criterion ==> L_total = (1-alph) * L_CE + (alpha * temperature **2) * D_KL( soft_student_output,soft teacher_output)
loss = kd_loss(out_s= out_s,
out_t= out_t,
target= labels,
alpha=kd_alpha,
temperature=kd_temperature)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
if phase == 'train' and scheduler != None:
scheduler.step()
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects * 1.0 / dataset_sizes[phase]
if phase == "train":
train_acc_dict[epoch + 1] = epoch_acc
train_loss_dict[epoch + 1] = epoch_loss
elif phase == "val":
val_acc_dict[epoch + 1] = epoch_acc
val_loss_dict[epoch + 1] = epoch_loss
if epoch == 0:
previous_saved_loss = epoch_loss
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
# deep copy the model
if phase == 'val' and epoch_loss <= previous_saved_loss:
previous_saved_loss = epoch_loss
print("Improvement in Val loss, saved!")
torch.save(student.state_dict(), path_to_save)
best_model_wts = copy.deepcopy(student.state_dict())
elif phase == "val" and epoch_loss > previous_saved_loss:
print("Previous saved loss is smaller! NOT Saving.")
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
# load best model weights
student.load_state_dict(best_model_wts)
test_data_evaluation(student,
test_loader,
device=train_on,
state_dict=best_model_wts)
from tqdm import tqdm
import time
import copy
hint_loss_criterion = torch.nn.MSELoss()
def stage_1_fitnet_train(partial_student,
frozen_student_modules,
partail_teacher,
optimizer,
path_to_save,
guided_layer= None,
dataset="cifar10",
epochs =200,
train_on="cuda",
multiple_gpu=[0,1,2,3],
scheduler= None,
input_data_size=(64,32,32)):
"""
:param partial_student: the student up to the guided layer/regressor.
:param frozen_student_modules: the list of modules in the student that should not be trained, i.e., the layers after the guided/regressor layer.
:param partail_teacher:
:param optimizer : the trained and loaded teacher upto Hint layer.
:param path_to_save
:param guided_layer: if not None, it means that a regressor was needed due to dimension mismatch between the hint layer and th
target intermediate layer in the student for the first stage of FitNets.
:param dataset
:param epochs: The total number of epcohs (default 40)
:param train_on: The device to train on ("cuda"/"cpu")
:param multiple_gpu: None
:param scheduler: scheduler for adjusting the learning rate.
:param input_data_size:
:return: the trained parametrs upto guided layer in the student (state_dict)
"""
device = torch.device(train_on)
partial_student.to(device)
partail_teacher.to(device)
if "cuda" in train_on and multiple_gpu is not None:
partial_student = nn.DataParallel(partial_student, device_ids=multiple_gpu)
partail_teacher = nn.DataParallel(partail_teacher, device_ids=multiple_gpu)
partail_teacher.eval()
since = time.time()
partial_student.to(device)
partail_teacher.to(device)
previous_saved_loss = 0.0
data_loader_dict, dataset_sizes = get_cifar(batch_size=input_data_size[0],
cifar10_100=dataset)
print("Train size ====> ",dataset_sizes["train"],"Val Size ===> ",dataset_sizes["val"])
best_model_wts = copy.deepcopy(partial_student.state_dict())
train_loss_dict = {}
val_loss_dict = {}
for epoch in tqdm(range(epochs)):
print('Epoch {}/{}'.format(epoch+1, epochs ))
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'val']:
if phase == 'train':
partial_student.train() # Set model to training mode
if guided_layer is not None: # if due to dimension mismatch a regressor has been added, train the regressor too.
guided_layer.train()
for m in frozen_student_modules: # the layers after the guided layer or the added regressor should be frozen.
m.eval()
else:
partial_student.eval() # Set model to evaluate mode
if guided_layer is not None:
guided_layer.eval()
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in data_loader_dict[phase]:
inputs = inputs.to(device)
#labels = labels.to(device)
# zero the parameter gradients
# forward
# track history if only in train
with torch.set_grad_enabled(phase == 'train'):
optimizer.zero_grad()
student_outputs = partial_student(inputs)
teacher_logits = partail_teacher(inputs)
if isinstance(student_outputs,tuple):
out_s = student_outputs[1]
else:
out_s = student_outputs
if guided_layer is not None:
out_s = guided_layer(out_s)
if isinstance(teacher_logits, tuple):
out_t = teacher_logits[1]
loss = hint_loss_criterion(out_s,out_t)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
if phase == 'train' and scheduler != None:
scheduler.step()
epoch_loss = running_loss / dataset_sizes[phase]
if phase == "train":
train_loss_dict[epoch + 1] = epoch_loss
elif phase == "val":
val_loss_dict[epoch + 1] = epoch_loss
if epoch == 0:
previous_saved_loss = epoch_loss
print('{} Loss: {:.4f}'.format(
phase, epoch_loss ))
# deep copy the model
if phase == 'val' and epoch_loss <= previous_saved_loss:
previous_saved_loss = epoch_loss
print("Improvement in Val loss, saved!")
torch.save(partial_student.state_dict(), path_to_save)
best_model_wts = copy.deepcopy(partial_student.state_dict())
elif phase == "val" and epoch_loss > previous_saved_loss:
print("Previous saved loss is smaller! NOT Saving.")
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))
return partial_student.state_dict()
def dml_train_regular(peers,
optimizers,
path_directory_to_save,
epochs=200,
train_on="cuda",
multiple_gpu=None,
dataset="cifar10",
scheduler=None,
alpha_dict={1:0.1,2:0.1},
temperature_dict=None,
data_input_size=(64, 32, 32),
seed=3):
"""
:param peers: dictionary of peers {1:peer1,2:peer2,....}
:param optimizers dictionary of optimizers {1:optimizer_peer1,2:optimizer_peer2,....}
:param path_directory_to_save (it should be directory to save each peer by using its key value)
:param epochs: deafult = 200
:param train_on: cuda:cpu
:param multiple_gpu: None
:param dataset
:param scheduler: dictionary of schedulers {1:scheduler_peer1,2:scheduler_peer2,....}
:param alpha_dict: alpha for each peer. deafult = 0.1
:param temperature_dict: tempearture for each peer ,default is 1
:param data_input_size: default (62,32,32)
:param seed: for reproducability default =3
"""
np.random.seed(seed)
torch.manual_seed(seed)
if "cuda" in train_on:
torch.cuda.manual_seed_all(seed=seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
peers_encyclopedia = {}
paralleld_dict = {}
device = torch.device(train_on)
for (peer_key, peer) in peers.items():
peer.to(device)
if multiple_gpu is not None:
peer = nn.DataParallel(peer, multiple_gpu)
paralleld_dict[peer_key] = peer
since = time.time()
data_loader_dict, dataset_sizes = get_cifar(batch_size=data_input_size[0],cifar10_100=dataset)
for (key, value) in paralleld_dict.items():
peers_encyclopedia["best_model_wts_" + key] = copy.deepcopy(peers[key].state_dict())
peers_encyclopedia["best_train_acc_" + key] = 0.0
peers_encyclopedia["best_train_loss_" + key] = 0.0
peers_encyclopedia["best_val_acc_" + key] = 0.0
peers_encyclopedia["best_val_loss_" + key] = 0.0
peers_encyclopedia["best_acc_dict_" + key] = {}
peers_encyclopedia["running_loss_dict_" + key] = {}
peers_encyclopedia["running_corrects_dict_" + key] = {}
peers_encyclopedia["train_loss_dict_" + key] = {}
peers_encyclopedia["train_acc_dict_" + key] = {}
peers_encyclopedia["val_acc_dict_" + key] = {}
peers_encyclopedia["val_loss_dict_" + key] = {}
peers_encyclopedia["previous_loss_" + key] = 0.0
peers_encyclopedia["periodic_saved_checkpoint_val_loss_" + key] = {}
peers_encyclopedia["best_saved_checkpoint_val_loss_" + key] = 0.0
for epoch in tqdm(range(epochs)):
print('Epoch {}/{}'.format(epoch + 1, epochs))
print('-' * 10)
# Each epoch has a training and validation phase
for (key, peer) in paralleld_dict.items():
print("\t\t \t \t KEY :::::::::::::::::::::> ", key)
final_alpha = alpha_dict[key]
for phase in ['train', 'val']:
optimizer = optimizers[key]
if phase == 'train':
peer.train() # Set model to training mode
else:
peer.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
for (inputs, labels) in data_loader_dict[phase]:
inputs, labels = inputs.to(device), labels.to(device)
with torch.set_grad_enabled(phase == 'train'):
optimizer.zero_grad()
student_input = inputs
peer_final_output = peer(student_input)
if isinstance(peer_final_output, tuple):
peer_final_output = peer_final_output[0]
teachers_final_ouput_dict = {}
for key_others, other_peer in paralleld_dict.items():
if key_others != key:
other_peer_final_output = other_peer(inputs)
if isinstance(other_peer_final_output, tuple):
teachers_final_ouput_dict[key_others] = other_peer_final_output[0]
else:
teachers_final_ouput_dict[key_others] = other_peer_final_output
# Check whether a regressor has been added in the forward method and the output
# is a tuple or the model(forward's method has not been modified, so the output of the model is a tensor)
if isinstance(peer_final_output, tuple):
_, preds = torch.max(peer_final_output[0], 1)
else:
_, preds = torch.max(peer_final_output, 1)
if temperature_dict != None:
if temperature_dict[key] != None:
final_temperature = temperature_dict[key]
else:
final_temperature = 1
else:
final_temperature = 1
loss = dml_loss_function(student_peer_output=peer_final_output,
teacher_peers_outputs=teachers_final_ouput_dict,
target_labels=labels,
alpha=final_alpha,
temperature=final_temperature)
# backward + optimize only if in training phase
if phase == 'train':
# loss.backward()
torch.autograd.backward(loss)
optimizer.step()
input_batch_size = inputs.size(0)
running_loss += loss * input_batch_size
running_corrects += torch.sum(preds == labels.data)
if phase == 'train' and scheduler != None:
if scheduler[key]:
scheduler[key].step()
epoch_loss = running_loss / dataset_sizes[phase]
epoch_acc = running_corrects.double() / dataset_sizes[phase]
if phase == "val" and epoch == 0:
peers_encyclopedia["previous_loss_" + key] = epoch_loss
peers_encyclopedia["best_val_acc_" + key] = epoch_acc
if phase == "train" and epoch == 0:
peers_encyclopedia["best_train_acc_" + key] = epoch_acc
print('Student => {} ===> {} Loss: {:.4f} Acc: {:.4f}'.format(key,
phase, epoch_loss, epoch_acc))
if phase == "train":
peers_encyclopedia["train_acc_dict_" + key][epoch + 1] = epoch_acc
peers_encyclopedia["train_loss_dict_" + key][epoch + 1] = epoch_loss
elif phase == "val":
peers_encyclopedia["val_acc_dict_" + key][epoch + 1] = epoch_acc
peers_encyclopedia["val_loss_dict_" + key][epoch + 1] = epoch_loss
if epoch_acc > peers_encyclopedia["best_val_acc_" + key]:
peers_encyclopedia["best_val_acc_" + key] = epoch_acc
peers_encyclopedia["best_acc_dict_" + key] = epoch_acc
if phase == "val" and peers_encyclopedia["previous_loss_" + key] >= epoch_loss:
peers_encyclopedia["previous_loss_" + key] = epoch_loss
torch.save(peer.state_dict,path_directory_to_save+"/"+key+".pth")
peers_encyclopedia["best_model_wts_" + key] = copy.deepcopy(peer.state_dict())
peers_encyclopedia["best_saved_checkpoint_val_loss_" + key] = epoch_loss
print("Improvement in VAL Loss ==> SAVED! EPOCH ====>", str(epoch + 1))
if phase == "val" and peers_encyclopedia["previous_loss_" + key] < epoch_loss:
print("Previous Validation Loss is smaller!")
time_elapsed = time.time() - since
print('Training complete in {:.0f}m {:.0f}s'.format(
time_elapsed // 60, time_elapsed % 60))