提交 fbec580d 编写于 作者: H hypox64

Modified evaluation indicators and optimized log files

上级 7fe15a28
......@@ -13,6 +13,10 @@ def CreatNet(opt):
net = resnet_1d.resnet18()
net.conv1 = nn.Conv1d(opt.input_nc, 64, 7, 2, 3, bias=False)
net.fc = nn.Linear(512, label_num)
elif name == 'resnet34_1d':
net = resnet_1d.resnet34()
net.conv1 = nn.Conv1d(opt.input_nc, 64, 7, 2, 3, bias=False)
net.fc = nn.Linear(512, label_num)
elif name == 'multi_scale_resnet_1d':
net = multi_scale_resnet_1d.Multi_Scale_ResNet(inchannel=opt.input_nc, num_classes=label_num)
elif name == 'micro_multi_scale_resnet_1d':
......
import argparse
import os
import numpy as np
import torch
import time
import util
# python3 train.py --dataset_dir '/media/hypo/Hypo/physionet_org_train' --dataset_name cc2018 --signal_name 'C4-M1' --sample_num 20 --model_name lstm --batchsize 64 --epochs 20 --lr 0.0005 --no_cudnn
# python3 train.py --dataset_dir './datasets/sleep-edfx/' --dataset_name sleep-edfx --signal_name 'EEG Fpz-Cz' --sample_num 50 --model_name lstm --batchsize 64 --network_save_freq 5 --epochs 25 --lr 0.0005 --BID 5_95_th --select_sleep_time --no_cudnn --select_sleep_time
......@@ -14,6 +14,7 @@ class Options():
def initialize(self):
#base
self.parser.add_argument('--no_cuda', action='store_true', help='if input, do not use gpu')
self.parser.add_argument('--gpu_id', type=int, default=0,help='choose which gpu want to use, 0 | 1 | 2 ...')
self.parser.add_argument('--no_cudnn', action='store_true', help='if input, do not use cudnn')
self.parser.add_argument('--label', type=int, default=5,help='number of labels')
self.parser.add_argument('--input_nc', type=int, default=3, help='# of input channels')
......@@ -52,6 +53,12 @@ class Options():
if self.opt.dataset_name == 'sleep-edf':
self.opt.sample_num = 8
if self.opt.dataset_name not in ['sleep-edf','sleep-edfx','cc2018']:
self.opt.BID = 'not-supported'
self.opt.select_sleep_time = 'not-supported'
self.opt.signal_name = 'not-supported'
self.opt.sample_num = 'not-supported'
if self.opt.no_cuda:
self.opt.no_cudnn = True
......@@ -72,4 +79,24 @@ class Options():
names = names.split(",")
self.opt.label_name = names
return self.opt
\ No newline at end of file
"""Print and save options
It will print both current options and default values(if different).
It will save options into a text file / [checkpoints_dir] / opt.txt
"""
message = ''
message += '----------------- Options ---------------\n'
for k, v in sorted(vars(self.opt).items()):
comment = ''
default = self.parser.get_default(k)
if v != default:
comment = '\t[default: %s]' % str(default)
message += '{:>20}: {:<30}{}\n'.format(str(k), str(v), comment)
message += '----------------- End -------------------'
localtime = time.asctime(time.localtime(time.time()))
util.makedirs(self.opt.save_dir)
util.writelog(str(localtime)+'\n'+message, self.opt,True)
return self.opt
\ No newline at end of file
......@@ -16,6 +16,15 @@ def label_statistics(labels):
label_cnt_per = label_cnt/len(labels)
return label_cnt,label_cnt_per,label_num
def mat2predtrue(mat):
y_pred = [];y_true = []
for i in range(mat.shape[0]):
for j in range(mat.shape[1]):
for x in range(mat[i][j]):
y_true.append(i)
y_pred.append(j)
return y_true,y_pred
def Kappa(mat):
mat=mat/10000 # avoid overflow
mat_length=np.sum(mat)
......@@ -32,7 +41,7 @@ def Kappa(mat):
def result(mat,print_sub=False):
wide=mat.shape[0]
sub_recall = np.zeros(wide)
sub_precise = np.zeros(wide)
sub_precision = np.zeros(wide)
sub_F1 = np.zeros(wide)
sub_acc = np.zeros(wide)
_err = 0
......@@ -45,16 +54,16 @@ def result(mat,print_sub=False):
_err += mat[i,i]
sub_acc[i]=(TP+TN)/(TP+FN+TN+FP)
sub_precise[i] = TP/np.clip((TP+FP), 1e-5, 1e10)
sub_precision[i] = TP/np.clip((TP+FP), 1e-5, 1e10)
sub_recall[i]=(TP)/np.clip((TP+FN), 1e-5, 1e10)
#F1 score = 2 * P * R / (P + R)
sub_F1[i] = 2*sub_precise[i]*sub_recall[i] / np.clip((sub_precise[i]+sub_recall[i]),1e-5,1e10)
sub_F1[i] = 2*sub_precision[i]*sub_recall[i] / np.clip((sub_precision[i]+sub_recall[i]),1e-5,1e10)
if print_sub == True:
print('sub_recall:',sub_recall,'\nsub_acc:',sub_acc,'\nsub_sp:',sub_sp)
err = 1-_err/np.sum(mat)
Macro_precision = np.mean(sub_precise)
Macro_precision = np.mean(sub_precision)
Macro_recall = np.mean(sub_recall)
Macro_F1 = np.mean(sub_F1)
Macro_acc = np.mean(sub_acc)
......
......@@ -15,6 +15,7 @@ import heatmap
from creatnet import CreatNet
from options import Options
'''
change your own data to train
but the data needs meet the following conditions:
......@@ -26,14 +27,11 @@ but the data needs meet the following conditions:
'''
opt = Options().getparse()
util.makedirs(opt.save_dir)
localtime = time.asctime(time.localtime(time.time()))
util.writelog('\n\n'+str(localtime)+'\n'+str(opt),opt)
torch.cuda.set_device(opt.gpu_id)
t1 = time.time()
signals,labels = dataloader.loaddataset(opt)
label_cnt,label_cnt_per,_ = statistics.label_statistics(labels)
util.writelog('label statistics: '+str(label_cnt),opt,True)
signals,labels = transformer.batch_generator(signals,labels,opt.batchsize,shuffle = False)
train_sequences,test_sequences = transformer.k_fold_generator(len(labels),opt.k_fold)
show_freq = int(len(train_sequences[0])/5)
......@@ -43,6 +41,7 @@ t2 = time.time()
print('load data cost time: %.2f'% (t2-t1),'s')
net=CreatNet(opt)
util.writelog('network:\n'+str(net),opt,True)
util.show_paramsnumber(net,opt)
weight = np.ones(opt.label)
......@@ -50,7 +49,8 @@ if opt.weight_mod == 'auto':
weight = np.log(1/label_cnt_per)
weight = weight/np.median(weight)
weight = np.clip(weight, 0.8, 2)
print('Loss_weight:',weight)
util.writelog('label statistics: '+str(label_cnt),opt,True)
util.writelog('Loss_weight:'+str(weight),opt,True)
weight = torch.from_numpy(weight).float()
# print(net)
......@@ -144,7 +144,7 @@ for fold in range(opt.k_fold):
t2=time.time()
if epoch+1==1:
print('>>> per epoch cost time: %.2f' % (t2-t1),'s')
util.writelog('>>> per epoch cost time:'+str(round((t2-t1),2))+'s',opt,True)
#save result
pos = plot_result['test'].index(min(plot_result['test']))-1
......@@ -152,16 +152,16 @@ for fold in range(opt.k_fold):
if opt.k_fold==1:
util.writelog('------------------------------ final result ------------------------------',opt,True)
util.writelog('final -> macro-prec,reca,F1,err,kappa: '+str(statistics.result(final_confusion_mat)),opt,True)
util.writelog('confusion_mat:\n'+str(final_confusion_mat),opt,True)
util.writelog('confusion_mat:\n'+str(final_confusion_mat)+'\n',opt,True)
heatmap.draw(final_confusion_mat,opt,name = 'final_test')
else:
fold_final_confusion_mat += final_confusion_mat
util.writelog('fold -> macro-prec,reca,F1,err,kappa: '+str(statistics.result(final_confusion_mat)),opt,True)
util.writelog('confusion_mat:\n'+str(final_confusion_mat),opt,True)
util.writelog('confusion_mat:\n'+str(final_confusion_mat)+'\n',opt,True)
heatmap.draw(final_confusion_mat,opt,name = 'fold'+str(fold+1)+'_test')
if opt.k_fold != 1:
util.writelog('------------------------------ final result ------------------------------',opt,True)
util.writelog('final -> macro-prec,reca,F1,err,kappa: '+str(statistics.result(fold_final_confusion_mat)),opt,True)
util.writelog('confusion_mat:\n'+str(fold_final_confusion_mat),opt,True)
util.writelog('confusion_mat:\n'+str(fold_final_confusion_mat)+'\n',opt,True)
heatmap.draw(fold_final_confusion_mat,opt,name = 'k-fold-final_test')
\ No newline at end of file
......@@ -111,8 +111,8 @@ def random_transform_1d(data,finesize,test_flag):
#random amp
result = result*random.uniform(0.9,1.1)
#add noise
noise = np.random.rand(ch,finesize)
result = result + (noise-0.5)*0.01
# noise = np.random.rand(ch,finesize)
# result = result + (noise-0.5)*0.01
return result
def random_transform_2d(img,finesize = (224,122),test_flag = True):
......@@ -150,7 +150,7 @@ def ToInputShape(data,opt,test_flag = False):
result.append(dsp.getfeature(randomdata))
result = np.array(result).reshape(batchsize,_finesize*5)
elif opt.model_name in['cnn_1d','resnet18_1d','multi_scale_resnet_1d','micro_multi_scale_resnet_1d']:
elif opt.model_name in['cnn_1d','resnet18_1d','resnet34_1d','multi_scale_resnet_1d','micro_multi_scale_resnet_1d']:
result =[]
for i in range(0,batchsize):
randomdata=random_transform_1d(data[i],finesize = _finesize,test_flag=test_flag)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册