forked from ZhaoZhibin/Explainability-driven_SOH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_CNN.py
245 lines (219 loc) · 10.7 KB
/
main_CNN.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
import torch
import torch.nn as nn
import os
import numpy as np
from utils.util import AverageMeter,save_to_txt,mkdir,create_logger,eval_metrix
from utils.LRP import get_LRP,get_LRP_weigth
import matplotlib.pyplot as plt
import time
from utils.trainer_function import get_optimizer,get_scheduler,test,load_data,set_random_seed
from Test import final_test
import math
def train(train_loader, valid_loader, test_loader,model,optimizer,lr_scheduler,args):
if args.is_save_logging:
mkdir(args.save_root)
log_name = args.save_root + '/train info.log'
log, consoleHander, fileHander= create_logger(filename=log_name)
log.critical(args)
else:
log, consoleHander = create_logger()
try:
stop = 0
min_test_loss = 10
last_best_model = None
criterion = nn.MSELoss()
for e in range(1,args.n_epoch+1):
model.train()
pred_loss = AverageMeter()
lrp_loss = AverageMeter()
for data,label in train_loader:
model.train()
data = data.to(args.device)
label = label.to(args.device)
pred = model(data)
pred_l = criterion(pred, label)
#################################################
if args.lrp_guided:
model.eval()
(_, R2), _ = get_LRP(model,data)
weight = get_LRP_weigth(R2[0],type=args.weight_type,scaler=args.weight_scaler)
model.train()
lrp_pred = model(data,weight)
lrp_l = criterion(lrp_pred, label)
lrp_loss.update(lrp_l.item())
#################################################
loss = args.alpha * pred_l \
+ args.beta * lrp_l
else:
loss = pred_l
optimizer.zero_grad()
loss.backward()
optimizer.step()
pred_loss.update(loss.item())
if lr_scheduler:
lr_scheduler.step()
train_info = f'training......Epoch:[{e}/{args.n_epoch}], ' \
f'LRP guided={args.lrp_guided}, ' \
f'pred_loss:{100*pred_loss.avg:.4f}, ' \
f'lrp loss:{100*lrp_loss.avg:.4f}'
log.info(train_info)
##################### test #######################
if valid_loader is None:
valid_loader = test_loader
stop += 1
#### valid
true_label,pred_label,lrp_pred_label = final_test(model=model, test_loader=valid_loader, args=args)
raw_matrix = eval_metrix(true_label=true_label,pred_label=pred_label)
lrp_matrix = eval_metrix(true_label=true_label,pred_label=lrp_pred_label)
valid_info = f"validing......valid_MAE:[{raw_matrix[0]:.4f}, {lrp_matrix[0]:.4f}] lr:{optimizer.state_dict()['param_groups'][0]['lr']}"
log.warning(valid_info)
min_loss = min(raw_matrix[0],lrp_matrix[0])
if min_test_loss > min_loss:
min_test_loss = min_loss
true_label,pred_label,lrp_pred_label = final_test(model=model, test_loader=test_loader, args=args)
metrix = eval_metrix(true_label=true_label,pred_label=pred_label)
lrp_matrix = eval_metrix(true_label=true_label,pred_label=lrp_pred_label)
test_loss = metrix[2]*100
test_info = f"testing......Epoch:[{e}/{args.n_epoch}], test_loss(100x):{test_loss:.4f}," \
f"matrix:: MAE={metrix[0]:.6f},MAPE={metrix[1]:.6f},RMSE={metrix[3]:.6f}. " \
f"lrp_matrix:: MAE={lrp_matrix[0]:.6f},MAPE={lrp_matrix[1]:.6f},RMSE={lrp_matrix[3]:.6f}."
log.error(test_info)
stop = 0
#######plot test results#########
if args.is_plot_test_results:
plt.plot(true_label, label='true')
plt.plot(pred_label, label='pred')
plt.plot(lrp_pred_label,label='lrp pred')
plt.title(f"Epoch:{e}, MAE:{metrix[0]:.4f}, {lrp_matrix[0]:.4f}")
plt.legend()
plt.show()
####### save model ########
if args.is_save_best_model:
if last_best_model is not None and e<50:
os.remove(last_best_model) # delete last best model
save_folder = args.save_root + '/pth'
mkdir(save_folder)
best_model = os.path.join(save_folder, f'Epoch{e}.pth')
torch.save(model.state_dict(), best_model)
last_best_model = best_model
#########save test results (test info) to txt #####
if args.is_save_to_txt:
txt_path = args.save_root + '/test_info.txt'
time_now = time.strftime("%Y-%m-%d", time.localtime())
if e == 1:
save_to_txt(txt_path, ' ')
#save_to_txt(txt_path,f'########## experiment {args.experiment_time} ##########')
for k,v in vars(args).items():
save_to_txt(txt_path,f'{k}:{v}')
info = time_now + f' epoch = {e}, test_loss(100x):{test_loss:.6f}, ' \
f'matrix:: MAE={metrix[0]:.6f},MAPE={metrix[1]:.6f},RMSE={metrix[3]:.6f}. \n' \
f'matrix_lrp:: MAE={lrp_matrix[0]:.6f},MAPE={lrp_matrix[1]:.6f},RMSE={lrp_matrix[3]:.6f}'
save_to_txt(txt_path,info)
#########save test results (predict value) to np ######
if args.is_save_test_results:
np.save(args.save_root+'/pred_label',pred_label)
np.save(args.save_root+'/true_label',true_label)
np.save(args.save_root+'/lrp_pred_label',lrp_pred_label)
if args.early_stop > 0 and stop > args.early_stop:
print(' Early Stop !')
if args.is_save_logging:
log.removeHandler(consoleHander)
log.removeHandler(fileHander)
else:
log.removeHandler(consoleHander)
break
except:
txt_path = args.save_root + '/test_info.txt'
save_to_txt(txt_path, 'Error !')
if args.is_save_logging:
log.removeHandler(consoleHander)
log.removeHandler(fileHander)
else:
log.removeHandler(consoleHander)
def main(args):
from utils.model import model
set_random_seed(args.seed)
_, train_loader, valid_loader, test_loader = load_data(args)
m = model().to(args.device)
optimizer = get_optimizer(m, args)
if args.lr_scheduler:
scheduler = get_scheduler(optimizer, args)
else:
scheduler = None
train(train_loader, valid_loader, test_loader, m,optimizer, scheduler, args)
torch.cuda.empty_cache()
del train_loader
del valid_loader
del test_loader
def run_on_MIT_for_weight_select():
from utils.config import get_args
args = get_args()
for i in range(5): # 5次实验
for test_id in [1, 2, 3, 4, 5]:
for data_set in ['MIT2_1', 'MIT2_2']:
for w in [0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1]:
if w == 0:
setattr(args, 'lrp_guided', False)
setattr(args, 'save_root',
f'experiments2/weight select/MIT2/LRP guide False/{data_set}/test battery {test_id}/experiment {i}')
else:
setattr(args, 'lrp_guided', True)
setattr(args, 'save_root',
f'experiments2/weight select/MIT2/LRP guide True_{w}/{data_set}/test battery {test_id}/experiment {i}')
try:
setattr(args, 'source_dir', f'data/MIT/{data_set}')
setattr(args, 'is_save_best_model', False)
setattr(args, 'weight_scaler', w)
setattr(args, 'test_id', test_id)
setattr(args, 'is_DA',False)
main(args)
except:
continue
def run_on_all_MIT_data():
from utils.config import get_args
args = get_args()
for i in range(5):
for test_id in [1, 2, 3, 4, 5]:
for lrp in [True,False]:
for data in ['MIT2_1', 'MIT2_2', 'MIT2_3', 'MIT2_4', 'MIT2_5']:
setattr(args, 'source_dir', f'data/MIT/{data}')
setattr(args, 'lrp_guided', lrp)
setattr(args, 'test_id', test_id)
setattr(args, 'save_root',
f'experiments2/MIT2/LRP guide {lrp}/{data}/test battery {test_id}/experiment {i}')
main(args)
def run_on_all_BIT_data():
from utils.config import get_args
args = get_args()
for i in range(5): # 5 experiments
for test_id in [1, 2, 3, 4, 5, 6, 7, 8]:
for lrp in [True,False]:
for data in ['BIT-1', 'BIT-2']:
try:
setattr(args, 'source_dir', f'data/BIT/{data}')
setattr(args, 'lrp_guided', lrp)
setattr(args, 'test_id', test_id)
setattr(args, 'save_root',
f'experiments2/BIT/LRP guide {lrp}/{data}/test battery {test_id}/experiment {i}')
main(args)
except:
continue
def run_on_all_CALCE_data():
from utils.config import get_args
args = get_args()
for i in range(5): # 5 experiments
for test_id in [1, 2, 3, 4, 5, 6]:
for lrp in [True, False]:
for data in ['CS2', 'CX2']:
try:
setattr(args, 'source_dir', f'data/CALCE/{data}')
setattr(args, 'lrp_guided', lrp)
setattr(args, 'test_id', test_id)
setattr(args, 'save_root',
f'experiments2/CALCE/LRP guide {lrp}/{data}/test battery {test_id}/experiment {i}')
main(args)
except:
continue
if __name__ == '__main__':
os.environ['CUDA_VISIBLE_DEVICES'] = '2'
run_on_all_MIT_data()