-
Notifications
You must be signed in to change notification settings - Fork 0
/
FRC_train.py
252 lines (217 loc) · 11.1 KB
/
FRC_train.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
import os
import cv2, torch, random, itertools, time,datetime
import numpy as np
from torch.utils.data import Dataset, DataLoader
from torch.utils.data.sampler import Sampler
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.nn.modules.loss import CrossEntropyLoss
from torchvision import transforms
from torch.nn import BCEWithLogitsLoss, MSELoss
#from FRC_model import *
from new_model import *
from dataset import *
from utils import *
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
seed = 2023
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.deterministic = True
def train():
base_lr = 0.0005
ema_decay = 0.99
num_classes = 5
batch_size = 4
base_dir = './data/idrid/' # polyp skin idrid
dataset = 'idrid'
image_size = (960,960)
consistency = 0.1
max_epoch = 62
def create_model(image_size, num_classes,ema):
model = Adapter_FRC_SegFormer_B4(image_size,num_classes)
if ema:
for param in model.parameters():
param.detach_()
return model
model = create_model(image_size[0], num_classes,False)
model.cuda()
for name,p in model.named_parameters():
if 'linear_pred' in name or 'DTC' in name:
p.requires_grad = True
elif 'prompt' in name or 'ssf' in name or 'adaptmlp' in name:
p.requires_grad = True
else:
p.requires_grad = False
model_name = 'SegB4_our_polyp'
ema_model = create_model(image_size[0], num_classes,True)
ema_model.cuda()
print('Total model parameters: ', sum(p.numel() for p in model.parameters())/1e6,'M' )
print('Trainable model parameters: ', sum(p.numel() for p in model.parameters() if p.requires_grad == True)/1e6,'M' )
db_train = semi_BaseDataSets(base_dir+'train/', 'train.txt',image_size,dataset)
total_samples = len(db_train)
labeled_samples = int(total_samples*0.1)
labeled_bs = int(batch_size/2)
print("=> Total samples is: {}, labeled samples is: {}".format(total_samples, labeled_samples))
labeled_idxs = list(range(0, labeled_samples)) *9
unlabeled_idxs = list(range(labeled_samples, total_samples))
batch_sampler = TwoStreamBatchSampler(labeled_idxs, unlabeled_idxs, batch_size, batch_size-labeled_bs)
train_loader = DataLoader(db_train, batch_sampler=batch_sampler,num_workers=4, pin_memory=True)
print('=> train len:', len(train_loader))
#optimizer = optim.Adam(model.parameters(), betas=(0.9,0.99), lr=base_lr, weight_decay=0.0001)
optimizer = optim.AdamW(model.parameters(),lr=base_lr,weight_decay=0.0001)
ce_loss = CrossEntropyLoss()
criterion_cos = CriterionCosineSimilarity()
dice_loss = DiceLoss(num_classes)
ssim_loss = SSIM()
scaler = torch.cuda.amp.GradScaler()
iter_num = 0
max_indicator = 0
best_IoU =0
best_Dice =0
best_MAE = 0
best_Acc = 0
tbest_IoU =0
tbest_Dice =0
tbest_MAE = 0
tbest_Acc = 0
max_iterations = max_epoch * len(train_loader)
for epoch_num in range(max_epoch):
train_acc = 0
train_loss = 0
start_time = time.time()
model.train()
for i_batch, sampled_batch in enumerate(train_loader):
images, labels = sampled_batch['image'].cuda(), sampled_batch['label'].cuda()
ema_inputs = images#[labeled_bs:]
with torch.cuda.amp.autocast():
#if True:
outputs,[q1,q3,q5], feat_DCT = model(images)
outputs_soft = torch.softmax(outputs, dim=1)
with torch.no_grad():
ema_output,[q2,q4,q6],ema_DCT = ema_model(ema_inputs)
ema_output_soft = torch.softmax(ema_output, dim=1)
# frequency cnsisency
feq_cons = 0.5*(F.mse_loss(feat_DCT, ema_DCT) + F.mse_loss(ema_DCT, feat_DCT))
# region-level content-aware loss
self_sim_matrix_1 = criterion_cos(q1, q1)
self_sim_matrix_2 = criterion_cos(q2, q2)
self_sim_matrix_3 = criterion_cos(q3, q3)
self_sim_matrix_4 = criterion_cos(q4, q4)
self_sim_matrix_5 = criterion_cos(q5, q5)
self_sim_matrix_6 = criterion_cos(q6, q6)
region_loss1 = F.mse_loss(self_sim_matrix_1, self_sim_matrix_2)
region_loss2 = F.mse_loss(self_sim_matrix_3, self_sim_matrix_4)
region_loss3 = F.mse_loss(self_sim_matrix_5, self_sim_matrix_6)
region_loss = region_loss1+region_loss2+region_loss3
#ssimloss = ssim_loss(outputs_soft[:labeled_bs,1,:,:],labels[:labeled_bs].float())
ce_losses = ce_loss(outputs[:labeled_bs],labels[:labeled_bs].long())
dice_losses = dice_loss(outputs_soft[:labeled_bs],labels[:labeled_bs].long())
sup_loss = 0.5*(ce_losses+dice_losses) #+ ssimloss
consistency_loss = F.mse_loss(outputs_soft, ema_output_soft)+region_loss+feq_cons
consistency_weight = get_current_consistency_weight(consistency,iter_num,max_iterations)
loss = sup_loss + consistency_weight*consistency_loss
prediction = torch.max(outputs[:labeled_bs],1)[1]
train_correct = (prediction == labels[:labeled_bs]).float().mean().cpu().numpy()
train_acc = train_acc + train_correct
train_loss = train_loss + loss.detach().cpu().numpy()
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.unscale_(optimizer)
scaler.step(optimizer)
scaler.update()
update_ema_variables(model, ema_model, ema_decay, iter_num)
lr = base_lr * (1.0 - iter_num / max_iterations) ** 0.9
for param_group in optimizer.param_groups:
param_group['lr'] = lr
iter_num = iter_num + 1
total_time = time.time() - start_time
total_time_str = str(datetime.timedelta(seconds=int(total_time)))
model.eval()
print('Epoch: {} / {} '.format(epoch_num, max_epoch), 'Training time {}'.format(total_time_str),'Initial LR {:4f}'.format(lr))
print('train_loss: ',train_loss/len(train_loader),' train_acc: ',train_acc/(len(train_loader)),'LR {:4f}'.format(lr))
save_dir='./results/'
db_val = testBaseDataSets(base_dir+'test/', 'test.txt',image_size,dataset)
valloader = DataLoader(db_val, batch_size=1, shuffle=False,num_workers=0)
model.eval()
j = 0
if dataset == 'idrid':
evaluator_EX= Evaluator()
evaluator_HE= Evaluator()
evaluator_MA= Evaluator()
evaluator_SE= Evaluator()
with torch.no_grad():
for sampled_batch in valloader:
images, labels = sampled_batch['image'], sampled_batch['label']
images, labels = images.cuda(),labels.cuda()
pred,_,_ = model(images)
probs = pred.cpu().numpy()
save_results(probs,save_dir,image_size[0],image_size[1],j)
j = j+1
predictions = torch.argmax(pred, dim=1)
pred = F.one_hot(predictions.long(), num_classes=num_classes)
new_labels = F.one_hot(labels.long(), num_classes=num_classes)
evaluator_EX.update(pred[0,:,:,1], new_labels[0,:,:,1].float())
evaluator_HE.update(pred[0,:,:,2], new_labels[0,:,:,2].float())
evaluator_MA.update(pred[0,:,:,3], new_labels[0,:,:,3].float())
evaluator_SE.update(pred[0,:,:,4], new_labels[0,:,:,4].float())
MAE_ex, Recall_ex, Pre_ex, Acc_ex, Dice_ex, IoU_ex = evaluator_EX.show(False)
MAE_he, Recall_he, Pre_he, Acc_he, Dice_he, IoU_he = evaluator_HE.show(False)
MAE_ma, Recall_ma, Pre_ma, Acc_ma, Dice_ma, IoU_ma = evaluator_MA.show(False)
MAE_se, Recall_se, Pre_se, Acc_se, Dice_se, IoU_se = evaluator_SE.show(False)
MAE = (MAE_ex + MAE_he + MAE_ma +MAE_se )/4
Acc = (Acc_ex + Acc_he + Acc_ma +Acc_se )/4
Dice = (Dice_ex + Dice_he + Dice_ma +Dice_se)/4
IoU = (IoU_ex + IoU_he + IoU_ma +IoU_se)/4
indicator = Dice+IoU
if indicator > max_indicator:
best_Dice =Dice
best_IoU =IoU
best_MAE = MAE
best_Acc = Acc
max_indicator = indicator
torch.save(model.state_dict(), './new/'+model_name+'.pth')
print("MAE: ", "%.2f" % MAE," Acc: ", "%.2f" % Acc," Dice: ", "%.2f" % Dice," IoU: " , "%.2f" % IoU)
else:
evaluator = Evaluator()
evaluator2 = Evaluator()
with torch.no_grad():
for sampled_batch in valloader:
images, labels = sampled_batch['image'], sampled_batch['label']
images, labels = images.cuda(),labels.cuda()
predictions,_,_ = model(images)
pred = predictions[0,1,:,:]
predictions2,_,_ = ema_model(images)
pred2 = predictions2[0,1,:,:]
evaluator.update(pred, labels[0,:,:].float())
evaluator2.update(pred2, labels[0,:,:].float())
for i in range(1):
#images = images[i].cpu().numpy()
#labels = labels.cpu().numpy()
#label = (labels[i]*255)
pred = pred.cpu().numpy()
#cv2.imwrite(save_dir+'image'+str(j)+'.jpg',images.transpose(1, 2, 0)[:,:,::-1])
#cv2.imwrite(save_dir+'GT'+str(j)+'.jpg',label*255)
cv2.imwrite(save_dir+'Pre'+str(j)+'.jpg',pred*255)
j=j+1
MAE, Recall, Pre, Acc, Dice, IoU = evaluator.show(False)
ema_MAE, ema_Rec, ema_Pre, ema_Acc, ema_Dice, ema_IoU = evaluator2.show(False)
indicator = Dice+IoU
if indicator > max_indicator:
best_Dice =Dice
best_IoU =IoU
best_MAE = MAE
best_Acc = Acc
tbest_Dice =ema_Dice
tbest_IoU =ema_IoU
tbest_MAE = ema_MAE
tbest_Acc = ema_Acc
max_indicator = indicator
torch.save(model.state_dict(), './new/'+model_name+'.pth')
print('Stu: ',"MAE: ", "%.2f" % MAE," Acc: ", "%.2f" % Acc," Dice: ", "%.2f" % Dice," IoU: " , "%.2f" % IoU)
print('Tea: ',"MAE: ", "%.2f" % ema_MAE," Acc: ", "%.2f" % ema_Acc," Dice: ", "%.2f" % ema_Dice," IoU: " , "%.2f" % ema_IoU)
print('best student: MAE %.2f Acc %.2f Dice %.2f IoU %.2f' %(best_MAE,best_Acc,best_Dice, best_IoU))
print('best teacher: MAE %.2f Acc %.2f Dice %.2f IoU %.2f' %(tbest_MAE,tbest_Acc,tbest_Dice, tbest_IoU))
train()