Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Hypo
candock
提交
fbec580d
C
candock
项目概览
Hypo
/
candock
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
candock
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
fbec580d
编写于
3月 21, 2020
作者:
H
hypox64
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Modified evaluation indicators and optimized log files
上级
7fe15a28
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
59 addition
and
19 deletion
+59
-19
creatnet.py
creatnet.py
+4
-0
options.py
options.py
+30
-3
statistics.py
statistics.py
+13
-4
train.py
train.py
+9
-9
transformer.py
transformer.py
+3
-3
未找到文件。
creatnet.py
浏览文件 @
fbec580d
...
...
@@ -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'
:
...
...
options.py
浏览文件 @
fbec580d
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
statistics.py
浏览文件 @
fbec580d
...
...
@@ -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_precis
e
=
np
.
zeros
(
wide
)
sub_precis
ion
=
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_precis
e
[
i
]
=
TP
/
np
.
clip
((
TP
+
FP
),
1e-5
,
1e10
)
sub_precis
ion
[
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_precis
e
[
i
]
*
sub_recall
[
i
]
/
np
.
clip
((
sub_precise
[
i
]
+
sub_recall
[
i
]),
1e-5
,
1e10
)
sub_F1
[
i
]
=
2
*
sub_precis
ion
[
i
]
*
sub_recall
[
i
]
/
np
.
clip
((
sub_precision
[
i
]
+
sub_recall
[
i
]),
1e-5
,
1e10
)
if
print_sub
==
True
:
print
(
'sub_recall:'
,
sub_recall
,
'
\n
sub_acc:'
,
sub_acc
,
'
\n
sub_sp:'
,
sub_sp
)
err
=
1
-
_err
/
np
.
sum
(
mat
)
Macro_precision
=
np
.
mean
(
sub_precis
e
)
Macro_precision
=
np
.
mean
(
sub_precis
ion
)
Macro_recall
=
np
.
mean
(
sub_recall
)
Macro_F1
=
np
.
mean
(
sub_F1
)
Macro_acc
=
np
.
mean
(
sub_acc
)
...
...
train.py
浏览文件 @
fbec580d
...
...
@@ -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
transformer.py
浏览文件 @
fbec580d
...
...
@@ -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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录