-
Notifications
You must be signed in to change notification settings - Fork 0
/
step2_teacher_student_dtfdmil_distillation.py
166 lines (135 loc) · 7.87 KB
/
step2_teacher_student_dtfdmil_distillation.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
from model.network import Classifier_1fc, DimReduction
from model.feature_extraction import resnet50_baseline
import argparse
import torch
from dataset.RandomPatchDistillationDataset import RandomPatchDistillationDataset
import numpy as np
import torch.nn.functional as F
parser = argparse.ArgumentParser(description='abc')
parser.add_argument('--name', default='abc', type=str)
parser.add_argument('--EPOCH', default=200, type=int)
parser.add_argument('--epoch_step', default='[100]', type=str)
parser.add_argument('--device', default='cuda', type=str)
# parser.add_argument('--isPar', default=False, type=bool)
# parser.add_argument('--log_dir', default='./debug_log', type=str) ## log file path
# parser.add_argument('--train_show_freq', default=40, type=int)
parser.add_argument('--droprate', default='0', type=float)
parser.add_argument('--droprate_2', default='0', type=float)
parser.add_argument('--lr', default=1e-5, type=float)
parser.add_argument('--weight_decay', default=1e-4, type=float)
parser.add_argument('--lr_decay_ratio', default=0.2, type=float)
# parser.add_argument('--batch_size', default=60, type=int)
# parser.add_argument('--batch_size_v', default=1, type=int)
parser.add_argument('--num_workers', default=4, type=int)
parser.add_argument('--num_cls', default=2, type=int)
# parser.add_argument('--mDATA0_dir_train0', default='', type=str) ## Train Set
# parser.add_argument('--mDATA0_dir_val0', default='', type=str) ## Validation Set
# parser.add_argument('--mDATA_dir_test0', default='', type=str) ## Test Set
# parser.add_argument('--numGroup', default=5, type=int)
# parser.add_argument('--total_instance', default=4, type=int)
# parser.add_argument('--numGroup_test', default=4, type=int)
# parser.add_argument('--total_instance_test', default=4, type=int)
parser.add_argument('--mDim', default=512, type=int)
parser.add_argument('--grad_clipping', default=5, type=float)
# parser.add_argument('--isSaveModel', action='store_false')
# parser.add_argument('--debug_DATA_dir', default='', type=str)
parser.add_argument('--numLayer_Res', default=0, type=int)
# parser.add_argument('--temperature', default=1, type=float)
# parser.add_argument('--num_MeanInference', default=1, type=int)
# parser.add_argument('--distill_type', default='AFS', type=str) ## MaxMinS, MaxS, AFS
params = parser.parse_args()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_teacher=resnet50_baseline(True).to(device)
for param in model_teacher.parameters():
param.requires_grad = False
model_teacher.eval()
model_student=resnet50_baseline(True).to(device) # remember to load the new weights for the second round of iteration
classifier_teacher = Classifier_1fc(params.mDim, params.num_cls, params.droprate).to(params.device)
dimReduction_teacher = DimReduction(1024, params.mDim, numLayer_Res=params.numLayer_Res).to(params.device)
checkpoint=torch.load('/home/why/Workspace-Python/DTFD-MIL.PyTorch/model_best.pth',map_location='cpu')
classifier_teacher.load_state_dict(
{"fc.weight":checkpoint['att_classifier']['classifier.fc.weight'],"fc.bias":checkpoint['att_classifier']['classifier.fc.bias']})
dimReduction_teacher.load_state_dict(checkpoint['dim_reduction'])
for param in classifier_teacher.parameters():
param.requires_grad = False
for param in dimReduction_teacher.parameters():
param.requires_grad = False
classifier_student = Classifier_1fc(params.mDim, params.num_cls, params.droprate).to(params.device)
dimReduction_student = DimReduction(1024, params.mDim, numLayer_Res=params.numLayer_Res).to(params.device)
classifier_student.load_state_dict(
{"fc.weight":checkpoint['att_classifier']['classifier.fc.weight'],"fc.bias":checkpoint['att_classifier']['classifier.fc.bias']})
dimReduction_student.load_state_dict(checkpoint['dim_reduction'])
trainset=RandomPatchDistillationDataset('/your/path/to/CAMELYON16/',mode='train',level=1)
valset=RandomPatchDistillationDataset('/your/path/to/CAMELYON16/',mode='val',level=1)
# testset=RandomPatchDistillationDataset('/your/path/to/CAMELYON16/',mode='test',level=1)
trainloader=torch.utils.data.DataLoader(trainset, batch_size=100, shuffle=True, drop_last=False)
valloader=torch.utils.data.DataLoader(valset, batch_size=100, shuffle=True, drop_last=False)
# testloader=torch.utils.data.DataLoader(testset, batch_size=100, shuffle=False, drop_last=False)
optimizer0 = torch.optim.Adam(list(model_student.parameters())+list(dimReduction_student.parameters())+list(classifier_student.parameters()), lr=params.lr, weight_decay=params.weight_decay)
best_epoch = -1
test_auc = 0
ce_cri = torch.nn.KLDivLoss(reduction='none').to(params.device)
def TestModel(test_loader):
model_student.eval()
dimReduction_student.eval()
classifier_student.eval()
y_score=[]
y_true=[]
for i, data in enumerate(test_loader):
inputs, inputs1, inputs2=data
labels=classifier_teacher(dimReduction_teacher(model_teacher(inputs.type(torch.FloatTensor).to(params.device))))
labels=torch.softmax(labels,-1).cpu().data.numpy().tolist()
with torch.no_grad():
inputs_tensor=model_student(inputs1.type(torch.FloatTensor).to(params.device))
tmidFeat = dimReduction_student(inputs_tensor).squeeze(0)
tPredict = classifier_student(tmidFeat)
gSlidePred = torch.softmax(tPredict, dim=1)
pred=(gSlidePred.cpu().data.numpy()).tolist()
y_score.extend(pred)
y_true.extend(labels)
acc = np.sum(np.argmax(y_true,axis=1)==np.argmax(y_score,axis=1))/len(y_true)
# auc = roc_auc_score(y_true,[x[-1] for x in y_score])
mae = np.mean(np.abs(np.array(y_score)-np.array(y_true)))
print('test result: mae:{},acc:{}'.format(mae,acc))
return mae,acc
best_mae=1
# TestModel(testloader)
for ii in range(params.EPOCH):
model_student.train()
for param_group in optimizer0.param_groups:
curLR = param_group['lr']
print('current learning rate {}'.format(curLR))
for i, data in enumerate(trainloader):
inputs, inputs1, inputs2=data
inputs_tensor_teacher=model_teacher(inputs.type(torch.FloatTensor).to(params.device))
tmidFeat_teacher = dimReduction_teacher(inputs_tensor_teacher)
tPredict_teacher = classifier_teacher(tmidFeat_teacher)
inputs_tensor_student=model_student(inputs1.type(torch.FloatTensor).to(params.device))
tmidFeat_student = dimReduction_student(inputs_tensor_student)
tPredict_student = classifier_student(tmidFeat_student)
consistency_tmidFeat=dimReduction_student(inputs_tensor_teacher)
consistency_tPredict=classifier_student(consistency_tmidFeat)
loss0 = ce_cri(F.log_softmax(tPredict_student,dim=-1), F.softmax(tPredict_teacher.detach(),dim=-1)).mean()
loss1 = ce_cri(F.log_softmax(consistency_tPredict,dim=-1), F.softmax(tPredict_teacher,dim=-1)).mean()
loss2 = ce_cri(F.log_softmax(consistency_tmidFeat,dim=-1), F.softmax(tmidFeat_teacher,dim=-1)).mean()
loss=loss0+0.5*loss1+0.5*loss2
optimizer0.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(dimReduction_student.parameters(), params.grad_clipping)
torch.nn.utils.clip_grad_norm_(classifier_student.parameters(), params.grad_clipping)
optimizer0.step()
if i%400==0:
print('[EPOCH{}:ITER{}] loss0:{}; '.format(ii,i,loss0.item()))
if i>100 and i%5000==0:
# print('Testing:')
# mae,acc=TestModel(valloader)
# if mae<best_mae:
# best_mae=mae
torch.save(model_student.state_dict(), 'ResNet50_DTFD_teacher_student_best1024.pth')
print('new best auc, weights saved. ')
print('End of epoch',ii)
mae,acc=TestModel(valloader)
if mae<best_mae:
best_mae=mae
torch.save(model_student.state_dict(), 'ResNet50_DTFD_teacher_student_best1024.pth')
print('new best auc, weights saved. ')