-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
executable file
·80 lines (52 loc) · 2.37 KB
/
utils.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
import argparse
import torch
import copy
import torch.nn.functional as F
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def get_classwise_performance_report(model, classwise_dict, device):
performance_dict = {}
for c in classwise_dict:
curr_dataset = torch.stack(classwise_dict[c]).type(
torch.float32).to(device=device)
curr_labels = (torch.ones(size=(len(curr_dataset), ))
* c).to(device=device)
with torch.no_grad():
model.eval()
val_student_logits = model(curr_dataset)
val_softmax_student = F.softmax(val_student_logits, dim=1)
matching = torch.eq(torch.argmax(
val_softmax_student, dim=1), curr_labels)
performance_dict[c] = torch.sum(
matching, dim=0).item()/len(matching)
return performance_dict
def create_parser_train_student():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--save", action="store_true", default=False)
parser.add_argument(
"teacher_weights", help="filepath to the weights of the teacher model", type=str)
parser.add_argument(
"save_dir", help="directory to save the model", type=str)
parser.add_argument("lr", help="learning rate", type=float)
parser.add_argument("T", help="softmax temperature", type=float)
parser.add_argument("classes", default=9,
help="classes to train the network on", type=int)
parser.add_argument(
"weight", help="weight given to soft target loss term in the distillation loss", type=float)
parser.add_argument("epochs", help="epochs", type=int)
return parser
def create_parser_train_teacher():
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--save", action="store_true", default=False)
parser.add_argument(
"save_dir", help="directory to save the model", type=str)
parser.add_argument("lr", help="learning rate", type=float)
parser.add_argument("epochs", help="epochs", type=int)
return parser
def create_parser_grid_search():
parser = argparse.ArgumentParser()
parser.add_argument(
"config_path", help="path to grid search file", type=str)
parser.add_argument(
"teacher_weights", help="weights for teacher network", type=str)
return parser