-
Notifications
You must be signed in to change notification settings - Fork 4
/
train_student.py
213 lines (143 loc) · 7.05 KB
/
train_student.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
import os
import torch
from torchvision import datasets, transforms
import torchvision
from torch.utils.tensorboard import SummaryWriter
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
import torch.nn as nn
import timm
from tqdm import tqdm
from torchvision import transforms as tt
import torchvision.models as models
import torchmetrics
import argparse
writer = SummaryWriter()
MEAN = [0.485, 0.456, 0.406]
STD = [0.229, 0.224, 0.225]
class Trainer:
def __init__(self, opt):
super().__init__()
self.train_transform = tt.Compose([tt.Resize((opt.image_size,opt.image_size)), tt.RandomCrop(opt.image_size, padding=4,padding_mode='reflect'),
tt.RandomHorizontalFlip(),
tt.ToTensor(),
tt.Normalize(MEAN,STD,inplace=True)])
self.val_transform = tt.Compose([tt.Resize((opt.image_size,opt.image_size)), tt.ToTensor(), tt.Normalize(MEAN,STD)])
if opt.dataset == "pets37":
dataset = torchvision.datasets.OxfordIIITPet
train_dataset = dataset("./", split="trainval", transform=self.train_transform, download=True)
val_dataset = dataset("./", split="test", transform=self.val_transform, download=True)
num_classes = 37
else:
## Change here
train_dataset = None
val_dataset = None
num_classes = 2
self.student = timm.create_model(opt.student, pretrained=True, num_classes=num_classes).to(opt.device)
self.teacher = torch.load(opt.teacher).to(opt.device)
self.model_optimizer = optim.Adam(self.student.parameters(), lr=opt.lr, weight_decay=opt.weight_decay)
self.model_lr_scheduler = optim.lr_scheduler.StepLR(self.model_optimizer, 15, 0.1)
self.ce = nn.CrossEntropyLoss()
self.kl = nn.KLDivLoss(reduction="batchmean")
self.train_loader = DataLoader(train_dataset, batch_size=opt.batch_size, shuffle=True, num_workers=opt.workers, drop_last=False)
self.val_loader = DataLoader(val_dataset, batch_size=opt.batch_size, shuffle=False, num_workers=opt.workers, drop_last=False)
self.criterion = self.kd_loss
self.epochs = opt.epochs
self.device = opt.device
if opt.model_name is None:
self.model_name = opt.student
else:
self.model_name = opt.model_name
def kd_loss(self, pred_s, pred_t, label, alpha=0.9, T=3):
target = F.softmax(pred_t / T, dim=-1)
pred = F.log_softmax(pred_s / T, dim=-1)
loss_kl = self.kl(pred, target) * (T**2)
loss_ce = self.ce(pred_s, label)
loss = alpha*loss_kl + (1-alpha)*loss_ce
return loss
def accuracy(self, outputs, labels):
_, preds = torch.max(outputs, dim=1)
return torch.tensor(torch.sum(preds == labels).item() / len(preds))
def process_kd_batch(self, batch):
inp, label = batch
inp = inp.to(self.device)
label = label.to(self.device)
pred_s = self.student(inp)
with torch.no_grad():
pred_t = self.teacher(inp)
loss = self.criterion(pred_s, pred_t, label)
return loss, pred_s
def train(self, epoch):
self.student.train()
self.teacher.eval()
train_loss = 0
with tqdm(self.train_loader, unit="batch") as tepoch:
tepoch.set_description(f"Epoch {epoch}")
for idx, batch in enumerate(tepoch):
self.model_optimizer.zero_grad()
loss, pred = self.process_kd_batch(batch)
loss.backward()
self.model_optimizer.step()
train_loss += loss.item()
tepoch.set_postfix(loss=train_loss/(idx+1))
writer.add_scalar("RunningLoss/train", loss.item(), len(self.train_loader)*epoch + idx)
writer.add_scalar("Loss/train", train_loss/(idx+1), epoch)
def val(self, epoch):
self.student.eval()
preds = []
labels = []
val_loss = 0
with tqdm(self.val_loader, unit="batch") as tepoch:
tepoch.set_description(f"Val epoch {epoch}")
for idx, batch in enumerate(tepoch):
_, label = batch
with torch.no_grad():
loss, pred = self.process_kd_batch(batch)
preds.append(pred.detach().cpu().argmax(-1))
labels.append(label)
val_loss += loss.item()
tepoch.set_postfix(val_loss=val_loss/(idx+1))
preds = torch.ravel(torch.cat(preds, dim=0))
labels = torch.ravel(torch.cat(labels, dim=0))
acc = torch.tensor(torch.sum(preds == labels).item() / len(preds))
print(f"Accuracy is: {acc}")
writer.add_scalar("Loss/val", val_loss/(idx+1), epoch)
writer.add_scalar("Metric/Acc", round(acc.item(),3), epoch)
return acc
def train_eval(self):
best_acc = 0
saved_model = None
for epoch in range(self.epochs):
print(f'Epoch {epoch}/{self.epochs - 1}')
self.train(epoch)
self.model_lr_scheduler.step()
acc = self.val(epoch)
if acc.mean()>best_acc:
print("found best model")
if saved_model is not None:
os.remove(saved_model)
saved_model = f"kd_{self.model_name}_{acc.item()}.pt"
best_acc = acc.item()
torch.save(self.student, f"kd_{self.model_name}_{acc.item()}.pt")
def parse_args():
parser = argparse.ArgumentParser()
list_of_devices = [-1, 0, 1, 2, 3]
list_of_models = timm.list_models()
list_of_datasets = ["pets37", "other"]
parser.add_argument('--device', type=int, help='cuda device, i.e. 0 or 0,1,2,3 or cpu (-1)', default=0, choices=list_of_devices)
parser.add_argument('--dataset', type=str, default='pets37', help='Your Training Dataset', choices=list_of_datasets)
parser.add_argument('--epochs', type=int, default=100)
parser.add_argument('--batch-size', type=int, default=128, help='total batch size for all GPUs')
parser.add_argument("--image-size", type=int, help="Training height", default=224)
parser.add_argument("--workers", type=int, help="Dataloader Workers", default=8)
parser.add_argument("--lr", type=int, help="Learning Rate", default=1e-3)
parser.add_argument('--student', type=str, default='resnet18', help='Your student Checkpoint Model', required=True, choices=list_of_models)
parser.add_argument('--teacher', type=str, default='', help='Your Teacher Checkpoint Model', required=True)
parser.add_argument('--model-name', type=str, default=None, help='Your Model Name')
parser.add_argument("--weight-decay", type=float, help="Weight Decay", default=0.001)
return parser.parse_args()
if __name__ == '__main__':
opt = parse_args()
trainer = Trainer(opt)
trainer.train_eval()