-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
280 lines (218 loc) · 12.6 KB
/
main.py
File metadata and controls
280 lines (218 loc) · 12.6 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
import os
import pandas as pd
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
from dataloader.Dataset import CustomDataSet
from model.diffusion import ConditionalModel
from model import diffusion_utils
from sklearn.model_selection import train_test_split
from model.BuildModel import BuildModel
import torch.nn.functional as F
import warnings
warnings.filterwarnings('ignore')
from utils import *
import time
import numpy as np
print(torch.cuda.is_available())
print(torch.cuda.device_count())
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
device = torch.device('cuda')
def main(random_seeds,batch_size,X,y,epochs,lr,weight_decay,start_t,end_T,num_classes,num_timesteps):
acc_arr = []
pre_arr = []
f1_arr = []
rec_arr = []
for seed in random_seeds:
print(f'random seed:{seed}')
print('-'*20)
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=seed)
train = pd.concat([y_train,X_train],axis=1)
test = pd.concat([y_test,X_test],axis=1)
train_data = pd.DataFrame(train.values,columns=train.columns)
test_data = pd.DataFrame(test.values,columns=test.columns)
train_set= CustomDataSet(
csv_data=train_data,
n_radiomics_col_names=n_rad_col, # 使用特征列名
t_radiomics_col_names=t_rad_col,
blood_col_names=blood_col,
all_col_names=all_col,
t_ct_dataset_path= t_image_path,
n_ct_dataset_path= n_image_path,
is_test=False
)
valid_set = CustomDataSet(
csv_data=test_data,
n_radiomics_col_names=n_rad_col, # 使用特征列名
t_radiomics_col_names=t_rad_col,
blood_col_names=blood_col,
all_col_names=all_col,
t_ct_dataset_path=t_image_path,
n_ct_dataset_path=n_image_path,
is_test=True
)
train_loader = DataLoader(dataset=train_set, batch_size=batch_size, shuffle=False)
valid_loader = DataLoader(dataset=valid_set, batch_size=batch_size, shuffle=False)
guide_model = BuildModel(num_classes).to(device)
CARD = ConditionalModel(timesteps=num_timesteps,num_classes=num_classes,guidance=True).to(device)
optimizer = torch.optim.Adam(CARD.parameters(),lr=lr,weight_decay=weight_decay)
aux_optimizer = torch.optim.Adam(guide_model.parameters(),lr=lr,weight_decay=weight_decay)
# scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(optimizer,T_mult=2,T_0=90,eta_min=1e-5) # 8e-6
aux_scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts(aux_optimizer,T_mult=2,T_0=80,eta_min=1e-4)
BCE_criterion = nn.BCELoss()
MSE_criterion = nn.MSELoss()
max_acc = 0
max_pre = 0
max_rec = 0
max_f1 = 0
max_kappa = 0
for epoch in range(epochs):
betas = diffusion_utils.make_beta_schedule(schedule="linear", num_timesteps=num_timesteps,
start=start_t, end=end_T).to(device)
betas_sqrt = torch.sqrt(betas)
alphas = 1.0 - betas
one_minus_betas_sqrt = torch.sqrt(alphas)
alphas_cumprod = alphas.cumprod(dim=0)
alphas_bar_sqrt = torch.sqrt(alphas_cumprod)
one_minus_alphas_bar_sqrt = torch.sqrt(1 - alphas_cumprod)
alphas_cumprod_prev = torch.cat(
[torch.ones(1).to(device), alphas_cumprod[:-1]], dim=0
)
CARD.train()
guide_model.train()
total_loss = 0
guide_pred_train = None
y_true_train = None
# Train
for batch in train_loader:
t_img = batch['t_img']
blood_data = batch['blood_data']
n_radiomics_data = batch['n_radiomics_data']
t_radiomics_data = batch['t_radiomics_data']
n_img_patch0 = batch['n_img_patch0']
n_img_patch1 = batch['n_img_patch1']
n_img_patch2 = batch['n_img_patch2']
target1 = batch['GT_2']
t_img = t_img.to(device, dtype=torch.float32)
n_img0 = n_img_patch0.to(device, dtype=torch.float32)
n_img1 = n_img_patch1.to(device, dtype=torch.float32)
n_img2 = n_img_patch2.to(device, dtype=torch.float32)
blood_data = blood_data.to(device, dtype=torch.float32)
t_radiomics_data = t_radiomics_data.to(device, dtype=torch.float32)
n_radiomics_data = n_radiomics_data.to(device, dtype=torch.float32)
target1 = target1.to(device, dtype=torch.int64)
n = t_img.size(0)
t = torch.randint(
low=0, high=num_timesteps, size=(n // 2 + 1,)
).to(device)
t = torch.cat([t, num_timesteps - 1 - t], dim=0)[:n]
target = F.one_hot(target1,num_classes=num_classes).float()
target = target.to(device)
t_um, t_m, n_um, n_m, feats, yhat = guide_model(t_img=t_img, n_patch0=n_img0, n_patch1=n_img1,
n_patch2=n_img2,
blood=blood_data,
n_radiomics=n_radiomics_data,
t_radiomics=t_radiomics_data, concat_type='train')
yhat = yhat.softmax(dim=-1)
guide_pred_train = torch.cat([guide_pred_train, yhat]) if guide_pred_train is not None else yhat
y_true_train = torch.cat([y_true_train, target1]) if y_true_train is not None else target1
aux_cost = BCE_criterion(yhat, target) + MSE_criterion(t_m, t_um) + MSE_criterion(n_m, n_um)
aux_optimizer.zero_grad()
aux_cost.backward()
aux_optimizer.step()
aux_scheduler.step()
total_loss += aux_cost.item()
if epoch > 50:
t_um, t_m, n_um, n_m, feats, yhat = guide_model(t_img=t_img, n_patch0=n_img0, n_patch1=n_img1,
n_patch2=n_img2,
blood=blood_data,
n_radiomics=n_radiomics_data,
t_radiomics=t_radiomics_data, concat_type='train')
e = torch.randn_like(target).to(device)
out = diffusion_utils.q_sample(target, yhat.detach(),
# target: ground truth yhat: ground truth 的预测 y_t 前向过程最终得到的噪声
alphas_bar_sqrt, one_minus_alphas_bar_sqrt, t, noise=e)
if support == True:
out = diffusion_utils.y_0_reparam(CARD,feats.detach(),out,yhat.detach(),yhat.detach(),t,one_minus_alphas_bar_sqrt)
out = out.softmax(dim=-1)
output = CARD(x=feats.detach(), y=out, t=t, yhat=yhat.detach())
guide_pred_train = torch.cat([guide_pred_train, yhat]) if guide_pred_train is not None else yhat
y_true_train = torch.cat([y_true_train, target1]) if y_true_train is not None else target1
if support == True:
loss = 0.1*MSE_criterion(e, output) + 0.9*BCE_criterion(out,target)
else:
loss = 0.1 * MSE_criterion(e, output)
optimizer.zero_grad()
loss.backward()
optimizer.step()
#scheduler.step()
total_loss += loss.item()
print('Epoch:{} Guide_loss:{}'.format(epoch, total_loss/len(train_loader)))
ACC_, Prec_, Rec_, F1_, kappa_ = compute_metrics(y_true_train, guide_pred_train)
print('Epoch:{} Train: ACC:{:.4f} Prec:{:.4f}, Rec:{:.4f} F1:{:.4f}, Kappa:{:.4f}'.format(epoch, ACC_,
Prec_,
Rec_, F1_,
kappa_))
CARD.eval()
guide_model.eval()
y_true = None
y_pred = None
guide_pred = None
# Evaluation
print('start evaluate... epoch:{}'.format(epoch))
for batch in valid_loader:
t_img = batch['t_img']
blood_data = batch['blood_data']
n_radiomics_data = batch['n_radiomics_data']
t_radiomics_data = batch['t_radiomics_data']
n_img_patch0 = batch['n_img_patch0']
n_img_patch1 = batch['n_img_patch1']
n_img_patch2 = batch['n_img_patch2']
target1 = batch['GT_2']
all_data = batch['all_data']
t_img = t_img.to(device, dtype=torch.float32)
n_img0 = n_img_patch0.to(device, dtype=torch.float32)
n_img1 = n_img_patch1.to(device, dtype=torch.float32)
n_img2 = n_img_patch2.to(device, dtype=torch.float32)
blood_data = blood_data.to(device, dtype=torch.float32)
t_radiomics_data = t_radiomics_data.to(device, dtype=torch.float32)
n_radiomics_data = n_radiomics_data.to(device, dtype=torch.float32)
target1 = target1.to(device, dtype=torch.int64)
all_data = all_data.to(device, dtype=torch.float32)
with torch.no_grad():
_, _, _, _, feats, yhat = guide_model(t_img=t_img, n_patch0=n_img0, n_patch1=n_img1,
n_patch2=n_img2,
blood=blood_data,
n_radiomics=n_radiomics_data,
t_radiomics=t_radiomics_data, concat_type='train')
y_T_mean = yhat.softmax(dim=-1)
output = diffusion_utils.p_sample_loop(CARD, feats, yhat, y_T_mean,
num_timesteps, alphas,
one_minus_alphas_bar_sqrt,
only_last_sample=True)
output = output.softmax(dim=-1)
y_pred = torch.cat([y_pred, output]) if y_pred is not None else output
guide_pred = torch.cat([guide_pred, y_T_mean]) if guide_pred is not None else y_T_mean
y_true = torch.cat([y_true, target1]) if y_true is not None else target1
ACC, Prec, Rec, F1, kappa = compute_metrics(y_true, y_pred)
ACC1, Prec1, Rec1, F11, kappa1 = compute_metrics(y_true, guide_pred)
print('guide-pred: ACC:{:.4f} Prec:{:.4f}, Rec:{:.4f} F1:{:.4f}, Kappa:{:.4f}'.format( ACC1, Prec1, Rec1, F11, kappa1))
print('Diff: ACC: {:.4f} Prec:{:.4f}, Rec:{:.4f} F1:{:.4f}, Kappa:{:.4f}'.format(ACC, Prec,Rec,F1,kappa))
print('========'*10)
if max_acc < ACC:
states = [
CARD.state_dict(),
optimizer.state_dict(),
epoch,
]
torch.save(states, os.path.join("./saved_models/seed{}_diff_ckpt_best_acc{:.4f}.pth".format(seed,ACC)))
max_acc = max(max_acc,ACC)
max_pre = max(max_pre,Prec)
max_rec = max(max_rec,Rec)
max_f1 = max(max_f1, F1)
max_kappa = max(max_kappa, kappa)
save_record(max_acc, max_pre, max_rec, max_f1, max_kappa, seed, 'best_record.txt')
acc_arr.append(max_acc)
pre_arr.append(max_pre)
f1_arr.append(max_f1)
rec_arr.append(max_rec)