Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Hypo
candock
提交
a551ac6c
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 搜索 >>
提交
a551ac6c
编写于
5月 11, 2020
作者:
H
hypox64
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
No video
上级
e7f4d240
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
34 addition
and
41 deletion
+34
-41
train.py
train.py
+29
-32
util/plot.py
util/plot.py
+5
-9
未找到文件。
train.py
浏览文件 @
a551ac6c
...
...
@@ -66,7 +66,6 @@ def evalnet(net,signals,labels,sequences,epoch,plot_result={}):
signal
=
transformer
.
ToInputShape
(
signal
,
opt
,
test_flag
=
True
)
signal
,
label
=
transformer
.
ToTensor
(
signal
,
label
,
no_cuda
=
opt
.
no_cuda
)
with
torch
.
no_grad
():
if
opt
.
model_name
==
'autoencoder'
:
out
,
feature
=
net
(
signal
)
loss
=
criterion_auto
(
out
,
signal
)
...
...
@@ -139,18 +138,14 @@ for fold in range(opt.k_fold):
loss
.
backward
()
optimizer
.
step
()
# iter_cnt += 1
# if iter_cnt%opt.plotfreq==0 and i>(len(train_sequences[fold])//opt.batchsize)/2:
# plot_result['train'].append(epoch_loss/i)
# plot.draw_loss(plot_result,epoch+i/(train_sequences.shape[1]/opt.batchsize),opt)
# if opt.model_name != 'autoencoder':
# plot.draw_heatmap(confusion_mat,opt,name = 'current_train')
# confusion_mat[:]=0
###tmp
plot_result
[
'train'
].
append
(
epoch_loss
/
i
)
plot
.
draw_loss
(
plot_result
,
epoch
+
i
/
(
train_sequences
.
shape
[
1
]
/
opt
.
batchsize
),
opt
)
#plot.draw_scatter(features, opt)
###
iter_cnt
+=
1
if
iter_cnt
%
opt
.
plotfreq
==
0
and
i
>
(
len
(
train_sequences
[
fold
])
//
opt
.
batchsize
)
/
2
:
plot_result
[
'train'
].
append
(
epoch_loss
/
i
)
plot
.
draw_loss
(
plot_result
,
epoch
+
i
/
(
train_sequences
.
shape
[
1
]
/
opt
.
batchsize
),
opt
)
if
opt
.
model_name
!=
'autoencoder'
:
plot
.
draw_heatmap
(
confusion_mat
,
opt
,
name
=
'current_train'
)
confusion_mat
[:]
=
0
plot_result
,
confusion_mat_eval
=
evalnet
(
net
,
signals
,
labels
,
test_sequences
[
fold
],
epoch
+
1
,
plot_result
)
confusion_mats
.
append
(
confusion_mat_eval
)
...
...
@@ -164,23 +159,25 @@ for fold in range(opt.k_fold):
t2
=
time
.
time
()
if
epoch
+
1
==
1
:
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
# final_confusion_mat = confusion_mats[pos]
# if opt.k_fold==1:
# statistics.statistics(final_confusion_mat, opt, 'final', 'final_test')
# np.save(os.path.join(opt.save_dir,'confusion_mat.npy'), final_confusion_mat)
# else:
# fold_final_confusion_mat += final_confusion_mat
# util.writelog('fold -> macro-prec,reca,F1,err,kappa: '+str(statistics.report(final_confusion_mat)),opt,True)
# util.writelog('confusion_mat:\n'+str(final_confusion_mat)+'\n',opt,True)
# plot.draw_heatmap(final_confusion_mat,opt,name = 'fold'+str(fold+1)+'_test')
# if opt.k_fold != 1:
# statistics.statistics(fold_final_confusion_mat, opt, 'final', 'k-fold-final_test')
# np.save(os.path.join(opt.save_dir,'confusion_mat.npy'), fold_final_confusion_mat)
# if opt.mergelabel:
# mat = statistics.mergemat(fold_final_confusion_mat, opt.mergelabel)
# statistics.statistics(mat, opt, 'merge', 'mergelabel_final')
#save result
if
opt
.
model_name
!=
'autoencoder'
:
pos
=
plot_result
[
'test'
].
index
(
min
(
plot_result
[
'test'
]))
-
1
final_confusion_mat
=
confusion_mats
[
pos
]
if
opt
.
k_fold
==
1
:
statistics
.
statistics
(
final_confusion_mat
,
opt
,
'final'
,
'final_test'
)
np
.
save
(
os
.
path
.
join
(
opt
.
save_dir
,
'confusion_mat.npy'
),
final_confusion_mat
)
else
:
fold_final_confusion_mat
+=
final_confusion_mat
util
.
writelog
(
'fold -> macro-prec,reca,F1,err,kappa: '
+
str
(
statistics
.
report
(
final_confusion_mat
)),
opt
,
True
)
util
.
writelog
(
'confusion_mat:
\n
'
+
str
(
final_confusion_mat
)
+
'
\n
'
,
opt
,
True
)
plot
.
draw_heatmap
(
final_confusion_mat
,
opt
,
name
=
'fold'
+
str
(
fold
+
1
)
+
'_test'
)
if
opt
.
model_name
!=
'autoencoder'
:
if
opt
.
k_fold
!=
1
:
statistics
.
statistics
(
fold_final_confusion_mat
,
opt
,
'final'
,
'k-fold-final_test'
)
np
.
save
(
os
.
path
.
join
(
opt
.
save_dir
,
'confusion_mat.npy'
),
fold_final_confusion_mat
)
if
opt
.
mergelabel
:
mat
=
statistics
.
mergemat
(
fold_final_confusion_mat
,
opt
.
mergelabel
)
statistics
.
statistics
(
mat
,
opt
,
'merge'
,
'mergelabel_final'
)
util/plot.py
浏览文件 @
a551ac6c
...
...
@@ -179,11 +179,10 @@ def draw_loss(plot_result,epoch,opt):
plt
.
plot
(
test_x
,
test
,
label
=
'test'
,
linewidth
=
1.5
)
plt
.
legend
(
loc
=
1
)
plt
.
title
(
'Running loss'
,
fontsize
=
'large'
)
plt
.
savefig
(
os
.
path
.
join
(
opt
.
save_dir
,
'running_loss
'
+
'%06d'
%
plotcnt
+
'
.png'
))
plt
.
savefig
(
os
.
path
.
join
(
opt
.
save_dir
,
'running_loss.png'
))
#---------------------------------scatter---------------------------------
plotcnt
=
0
def
label_statistics
(
labels
):
labels
=
(
np
.
array
(
labels
)).
astype
(
np
.
int64
)
label_num
=
np
.
max
(
labels
)
+
1
...
...
@@ -218,12 +217,8 @@ def draw_scatter(data,opt):
ax
.
scatter
(
data
[
cnt
:
cnt
+
label_cnt
[
i
],
0
],
data
[
cnt
:
cnt
+
label_cnt
[
i
],
1
],
data
[
cnt
:
cnt
+
label_cnt
[
i
],
2
],
)
cnt
+=
label_cnt
[
i
]
global
plotcnt
plotcnt
+=
1
plt
.
xlim
(
-
1.5
,
1.5
)
plt
.
ylim
(
-
1.5
,
1.5
)
plt
.
savefig
(
os
.
path
.
join
(
opt
.
save_dir
,
'feature_scatter
'
+
'%06d'
%
plotcnt
+
'
.png'
))
plt
.
savefig
(
os
.
path
.
join
(
opt
.
save_dir
,
'feature_scatter.png'
))
np
.
save
(
os
.
path
.
join
(
opt
.
save_dir
,
'feature_scatter.npy'
),
data
)
plt
.
close
(
'all'
)
...
...
@@ -234,7 +229,7 @@ def draw_autoencoder_result(true_signal,pred_signal,opt):
plt
.
subplot
(
212
)
plt
.
plot
(
pred_signal
[
0
][
0
])
plt
.
title
(
'Pred'
)
plt
.
savefig
(
os
.
path
.
join
(
opt
.
save_dir
,
'autoencoder_result
'
+
'%06d'
%
plotcnt
+
'
.png'
))
plt
.
savefig
(
os
.
path
.
join
(
opt
.
save_dir
,
'autoencoder_result.png'
))
plt
.
close
(
'all'
)
def
showscatter3d
(
data
):
...
...
@@ -245,8 +240,9 @@ def showscatter3d(data):
cnt
=
0
for
i
in
range
(
label_num
):
#c = colors[i%10],marker = markers[i//10]
ax
.
scatter
(
data
[
cnt
:
cnt
+
label_cnt
[
i
],
0
],
data
[
cnt
:
cnt
+
label_cnt
[
i
],
1
],
data
[
cnt
:
cnt
+
label_cnt
[
i
],
2
],
c
=
colors
[
i
%
10
],
marker
=
markers
[
i
//
10
]
)
)
cnt
+=
label_cnt
[
i
]
plt
.
show
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录