-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
209 lines (177 loc) · 8.43 KB
/
runner.py
File metadata and controls
209 lines (177 loc) · 8.43 KB
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
import torch
from PIL import Image
from tqdm import tqdm
import json
import os
import cv2
from models.losses import Distribution_loss
from models import get_models
from utils import set_grad, prob2entropy, Plotter
from metric import compute_mIoU, dice_score, iou
class BaseRunner(object):
def __init__(self, teacher, student, loss, optimizer, args):
self.teacher = teacher
self.student = student
self.loss = loss
self.optimizer = optimizer
self._args = args
def update_param(self):
pass
def set_args(self, args:dict):
self._args = args
def inference(self):
pass
def evaluate(self):
pass
def get_cfg(self, filename:str = 'train_cfg.json'):
print(self._args)
with open(filename, "w") as outfile:
json.dump(self._args, outfile)
################################################################################################################
class TSRunner(BaseRunner):
def __init__(self, args):
self._args = args
self.teacher = get_models(args.model, is_cls=True,args=args)
self.student = get_models(args.model, is_cls=True,args=args)
self.lossfn = Distribution_loss()
self.device = torch.device(args.device)
self.optimizer = None
self.loss_val = 0
# initialize
self._toDevice()
self.set_args()
def inference(self, imgpath:str, modelName:str = 'teacher', entropymap:bool=False):
'''
Predict segmentation mask for image
modelName: teacher | student
'''
model = modelName.lower()
self._toDevice()
self._eval()
# Process data
img = torch.from_numpy(cv2.imread(imgpath,cv2.IMREAD_GRAYSCALE)).unsqueeze(2).permute(2,0,1)
img = img.unsqueeze(0) #加入批次軸
img = img.to(dtype=torch.float32, device=self._args.device)
if model == 'teacher':
mask_pred = self._predictMask(model = self.teacher, data = img, entropymap=entropymap)
self._saveImg(img=mask_pred, path=f"./predict_{os.path.basename(imgpath)}")
elif model == 'student':
mask_pred = self._predictMask(model = self.student, data = img, entropymap=entropymap)
self._saveImg(img=mask_pred, path=f"./predict_{os.path.basename(imgpath)}")
else:
raise NameError(f"modelName must be 'teacher' or 'student' .")
def evaluate(self, testdataset, modelName:str = 'teacher'):
model = modelName.lower()
self._toDevice()
self._eval()
evaluation_dict = {}
if not evaluation_dict.get("miou"):
evaluation_dict["miou"] = []
if not evaluation_dict.get("dice"):
evaluation_dict["dice"] = []
if not evaluation_dict.get("iou"):
evaluation_dict["iou"] = []
net = self.student if model == 'student' else self.teacher
for i,(img, truth) in enumerate(tqdm(testdataset)):
img = img.unsqueeze(0)#加入批次軸
img = img.to(device=self._args.device,dtype=torch.float32)
truth = truth.unsqueeze(0).to(device=self._args.device,dtype=torch.int64)#加入批次軸
with torch.no_grad():
mask_pred = self._predictMask(model=net, data=img)
#compute the mIOU and dice score
miou = compute_mIoU(mask_pred.numpy(), truth.numpy())
f1 = dice_score(mask_pred.detach().squeeze(1), truth.detach().squeeze(1))
chromosome_iou = iou(mask_pred.squeeze(1), truth.squeeze(1))
# print(miou)
evaluation_dict["miou"].append(miou)
evaluation_dict["dice"].append(f1)
evaluation_dict["iou"].append(chromosome_iou)
for k in evaluation_dict:
evaluation_dict[k] = sum(evaluation_dict[k]) / len(evaluation_dict[k])
print(evaluation_dict)
return evaluation_dict
def update_param(self, dataForTeacher, dataForStudent, needWeight:bool=False):
logits, labels, weights = self._forward(dataForTeacher, dataForStudent, needWeight)
loss = self.lossfn(labels, logits, weights)
self.loss_val = loss.item()
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step() # update student
self._update_teacher_param(self._args.momentum) # update teacher
def set_args(self, args= None):
try:
self._args = args if args is not None else self._args
self.loss_val = 0
self.optimizer = torch.optim.Adam(self.student.parameters(), lr = self._args.init_lr,betas=(0.9,0.999))
self.lossfn.set_metric(self._args.loss)
self.pretrain_model(self._args.loadpath)
set_grad(model=self.teacher, is_requires_grad=False)
self.device = torch.device(self._args.device)
except:
pass
def pretrain_model(self, weightPath = None):
'''Load the pretrained weight into the teacher and student model'''
if (weightPath is None):
return
pretrained_model_param_dict = torch.load(weightPath)
student_param_dict = self.student.state_dict()
teacher_param_dict = self.teacher.state_dict()
# 1. filter out unnecessary keys
pretrained_dict_s = {k: v for k, v in pretrained_model_param_dict.items() if k in student_param_dict}
pretrained_dict_t = {k: v for k, v in pretrained_model_param_dict.items() if k in teacher_param_dict}
# 2. overwrite entries in the existing state dict
student_param_dict.update(pretrained_dict_s)
teacher_param_dict.update(pretrained_dict_t)
# 3. load the new state dict
self.student.load_state_dict(student_param_dict)
self.teacher.load_state_dict(teacher_param_dict)
def save_weight(self, saveFolder, suffix):
torch.save(self.student.state_dict(), os.path.join(saveFolder,f'student_{suffix}.pth'))
torch.save(self.teacher.state_dict(), os.path.join(saveFolder,f'teacher_{suffix}.pth'))
def _eval(self):
self.teacher.eval()
self.student.eval()
def _predictMask(self, model, data, entropymap:bool=False):
logit = model(data)
prob = torch.softmax(logit, dim=1)
if entropymap:
plotter = Plotter()
plotter.plot_entropy(prob,saved=True,is_heat=True)
mask_pred = torch.argmax(prob,dim=1,keepdim=True).to(torch.int64)
return mask_pred # (1, 1, h, w)
def _saveImg(self, img, path):
'''img: torch.Tensor'''
img = img.squeeze().to(torch.uint8).numpy()
img = img*255
im = Image.fromarray(img)
im.save(path)
def _forward(self, dataForTeacher, dataForStudent, needWeight:bool=False):
weight = None
self.teacher.train()
self.student.train()
self._toDevice()
dataForTeacher = dataForTeacher.to(device=self._args.device,dtype=torch.float32)
dataForStudent = dataForStudent.to(device=self._args.device,dtype=torch.float32)
# Generate pseudoLabel
logit_t = self.teacher(dataForTeacher)
logit_t = logit_t.detach()
hard_label = torch.zeros_like(logit_t)
index = torch.argmax(torch.softmax(logit_t.detach(),dim=1),dim=1,keepdim=True)
hard_label.scatter_(1, index, 1)
if needWeight:
entmap = prob2entropy(torch.softmax(logit_t.detach(),dim=1)) #上下限0~1
entmap = torch.where(torch.isnan(entmap),torch.full_like(entmap,0),entmap) # NaN 補 0 # entropy高 權重越高
# print(entmap.min(),entmap.max())
weight = torch.ones_like(entmap) - entmap # entropy低 權重越高
logit_s = self.student(dataForStudent)
return logit_s, hard_label, weight
def _update_teacher_param(self, momentum:float):
# update teacher model and it also apply the situation which both architectures of student model and teacher model are different
with torch.no_grad():
student_name_parameters = { param[0]:param[1].data.detach() for param in self.student.named_parameters()}
for param_t in self.teacher.named_parameters():
if param_t[0] in student_name_parameters.keys():
param_t[1].data = param_t[1].data.mul_(momentum).add_((1-momentum)*student_name_parameters[param_t[0]])
def _toDevice(self):
self.teacher.to(self._args.device)
self.student.to(self._args.device)