Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
恰到好处a
e3d_handpose_x
提交
6ea17475
e3d_handpose_x
项目概览
恰到好处a
/
e3d_handpose_x
与 Fork 源项目一致
Fork自
Eric.Lee2021 / e3d_handpose_x
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
e3d_handpose_x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
6ea17475
编写于
6月 22, 2021
作者:
Eric.Lee2021
🚴🏻
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add pso script
上级
82126f1c
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
173 addition
and
0 deletion
+173
-0
op_pso.py
op_pso.py
+173
-0
未找到文件。
op_pso.py
0 → 100644
浏览文件 @
6ea17475
import
random
import
numpy
as
np
import
torch
from
manopth.manolayer
import
ManoLayer
from
tqdm
import
tqdm
from
optimize_shape
import
align_bone_len
from
utils
import
bone
import
matplotlib.pyplot
as
plt
from
utils.LM_new
import
LM_Solver
class
PSO
:
def
__init__
(
self
,
parameters
,
target
,
side
):
"""
particle swarm optimization
parameter: a list type, like [NGEN, pop_size, var_num_min, var_num_max]
"""
if
side
==
'right'
:
self
.
mano_layer
=
ManoLayer
(
side
=
"right"
,
mano_root
=
'./mano/models'
,
use_pca
=
False
,
flat_hand_mean
=
True
)
else
:
self
.
mano_layer
=
ManoLayer
(
side
=
"left"
,
mano_root
=
'./mano/models'
,
use_pca
=
False
,
flat_hand_mean
=
True
)
# 初始化
self
.
NGEN
=
parameters
[
0
]
self
.
pop_size
=
parameters
[
1
]
self
.
var_num
=
parameters
[
2
].
shape
[
1
]
self
.
bound
=
[]
self
.
bound
.
append
(
parameters
[
2
])
self
.
bound
.
append
(
parameters
[
3
])
self
.
set_target
(
target
)
def
set_target
(
self
,
target
):
self
.
target
=
target
.
copy
()
self
.
pop_x
=
np
.
random
.
randn
(
self
.
pop_size
,
self
.
var_num
)
self
.
pop_v
=
np
.
random
.
random
((
self
.
pop_size
,
self
.
var_num
))
self
.
p_best
=
self
.
pop_x
.
copy
()
self
.
p_best_fit
=
self
.
batch_new_get_loss
(
self
.
pop_x
)
g_best_index
=
np
.
argmin
(
self
.
p_best_fit
,
axis
=
0
)
if
g_best_index
.
shape
[
0
]
>
1
:
g_best_index
=
g_best_index
[[
0
]]
self
.
g_best
=
self
.
p_best
[
g_best_index
].
copy
()
def
new_cal_ref_bone
(
self
,
_shape
):
parent_index
=
[
0
,
0
,
1
,
2
,
0
,
4
,
5
,
0
,
7
,
8
,
0
,
10
,
11
,
0
,
13
,
14
]
index
=
[
0
,
1
,
2
,
3
,
# index
4
,
5
,
6
,
# middle
7
,
8
,
9
,
# pinky
10
,
11
,
12
,
# ring
13
,
14
,
15
]
# thumb
reoder_index
=
[
13
,
14
,
15
,
1
,
2
,
3
,
4
,
5
,
6
,
10
,
11
,
12
,
7
,
8
,
9
]
shape
=
torch
.
Tensor
(
_shape
.
reshape
((
-
1
,
10
)))
th_v_shaped
=
torch
.
matmul
(
self
.
mano_layer
.
th_shapedirs
,
shape
.
transpose
(
1
,
0
)).
permute
(
2
,
0
,
1
)
\
+
self
.
mano_layer
.
th_v_template
th_j
=
torch
.
matmul
(
self
.
mano_layer
.
th_J_regressor
,
th_v_shaped
)
temp1
=
th_j
.
clone
().
detach
()
temp2
=
th_j
.
clone
().
detach
()[:,
parent_index
,
:]
result
=
temp1
-
temp2
result
=
torch
.
norm
(
result
,
dim
=-
1
,
keepdim
=
True
)
ref_len
=
result
[:,
[
4
]]
result
=
result
/
ref_len
return
torch
.
squeeze
(
result
,
dim
=-
1
)[:,
reoder_index
].
cpu
().
numpy
()
def
batch_new_get_loss
(
self
,
beta_
):
weight
=
1e-3
beta
=
beta_
.
copy
()
temp
=
self
.
new_cal_ref_bone
(
beta
)
loss
=
np
.
linalg
.
norm
(
temp
-
self
.
target
,
axis
=-
1
,
keepdims
=
True
)
**
2
+
\
weight
*
np
.
linalg
.
norm
(
beta
,
axis
=-
1
,
keepdims
=
True
)
return
loss
def
update_operator
(
self
,
pop_size
):
c1
=
2
c2
=
2
w
=
0.4
self
.
pop_v
=
w
*
self
.
pop_v
\
+
c1
*
np
.
multiply
(
np
.
random
.
rand
(
pop_size
,
1
),
(
self
.
p_best
-
self
.
pop_x
))
\
+
c2
*
np
.
multiply
(
np
.
random
.
rand
(
pop_size
,
1
),
(
self
.
g_best
-
self
.
pop_x
))
self
.
pop_x
=
self
.
pop_x
+
self
.
pop_v
low_flag
=
self
.
pop_x
<
self
.
bound
[
0
]
up_flag
=
self
.
pop_x
>
self
.
bound
[
1
]
self
.
pop_x
[
low_flag
]
=
-
3.0
self
.
pop_x
[
up_flag
]
=
3.0
temp
=
self
.
batch_new_get_loss
(
self
.
pop_x
)
p_best_flag
=
temp
<
self
.
p_best_fit
p_best_flag
=
p_best_flag
.
reshape
((
pop_size
,))
self
.
p_best
[
p_best_flag
]
=
self
.
pop_x
[
p_best_flag
]
self
.
p_best_fit
[
p_best_flag
]
=
temp
[
p_best_flag
]
g_best_index
=
np
.
argmin
(
self
.
p_best_fit
,
axis
=
0
)
if
g_best_index
.
shape
[
0
]
>
1
:
g_best_index
=
g_best_index
[[
0
]]
self
.
g_best
=
self
.
pop_x
[
g_best_index
]
self
.
g_best_fit
=
self
.
p_best_fit
[
g_best_index
][
0
][
0
]
def
main
(
self
,
solver
):
best_fit
=
[]
self
.
ng_best
=
np
.
zeros
((
1
,
self
.
var_num
))
self
.
ng_best_fit
=
self
.
batch_new_get_loss
(
self
.
ng_best
)[
0
][
0
]
for
gen
in
range
(
self
.
NGEN
):
self
.
update_operator
(
self
.
pop_size
)
# print('############ Generation {} ############'.format(str(gen + 1)))
dis
=
self
.
g_best_fit
-
self
.
ng_best_fit
if
self
.
g_best_fit
<
self
.
ng_best_fit
:
self
.
ng_best
=
self
.
g_best
.
copy
()
self
.
ng_best_fit
=
self
.
g_best_fit
if
abs
(
dis
)
<
1e-6
:
break
# print(':{}'.format(self.ng_best))
# print(':{}'.format(self.ng_best_fit))
# best_fit.append(self.ng_best_fit)
# print("---- End of (successful) Searching ----")
#
# plt.figure()
# plt.title("Figure1")
# plt.xlabel("iterators", size=14)
# plt.ylabel("fitness", size=14)
# t = [t for t in range(self.NGEN)]
# plt.plot(t, best_fit, color='b', linewidth=2)
# plt.show()
err
=
solver
.
new_cal_ref_bone
(
self
.
ng_best
)
err
=
align_bone_len
(
err
,
self
.
target
)
return
err
if
__name__
==
'__main__'
:
import
time
data_set
=
[
'rhd'
,
'stb'
,
'do'
,
'eo'
]
for
data
in
data_set
:
solver
=
LM_Solver
(
num_Iter
=
300
,
th_beta
=
torch
.
zeros
((
1
,
10
)),
th_pose
=
torch
.
zeros
((
1
,
48
)),
lb_target
=
np
.
zeros
((
15
,
1
)),
weight
=
1e-5
)
NGEN
=
60
popsize
=
100
low
=
np
.
zeros
((
1
,
10
))
-
3.0
up
=
np
.
zeros
((
1
,
10
))
+
3.0
parameters
=
[
NGEN
,
popsize
,
low
,
up
]
err
=
np
.
zeros
((
1
,
15
))
path
=
'out_testset/'
+
data
+
'_pre_joints.npy'
print
(
'load:{}'
.
format
(
path
))
target
=
np
.
load
(
path
)
pso_shape
=
np
.
zeros
((
target
.
shape
[
0
],
10
))
for
i
in
tqdm
(
range
(
target
.
shape
[
0
])):
_target
=
target
[[
0
]]
_target
=
bone
.
caculate_length
(
_target
,
label
=
'useful'
)
_target
=
_target
.
reshape
((
1
,
15
))
pso
=
PSO
(
parameters
,
_target
)
err
+=
pso
.
main
()
pso_shape
[[
i
]]
=
pso
.
ng_best
print
(
err
.
sum
()
/
target
.
shape
[
0
])
save_path
=
'out_testset/'
+
data
+
'_pso.npy'
print
(
'save:{}'
.
format
(
save_path
))
np
.
save
(
save_path
,
pso_shape
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录