Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
ad22f517
提交
ad22f517
authored
11月 18, 2014
作者:
Sina Honari
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fixing the pooling with stride plus adding a test.
上级
54d5d47d
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
57 行增加
和
75 行删除
+57
-75
downsample.py
theano/tensor/signal/downsample.py
+19
-49
test_downsample.py
theano/tensor/signal/tests/test_downsample.py
+38
-26
没有找到文件。
theano/tensor/signal/downsample.py
浏览文件 @
ad22f517
...
@@ -78,10 +78,11 @@ class DownsampleFactorMax(Op):
...
@@ -78,10 +78,11 @@ class DownsampleFactorMax(Op):
scalar Theano variable.
scalar Theano variable.
:param ds: downsample factor over rows and columns
:param ds: downsample factor over rows and columns
this parameter indicates the pooling region
this parameter indicates the
size of the
pooling region
:type ds: list or tuple of two ints
:type ds: list or tuple of two ints
:param st: the stride size
:param st: the stride size. This is the distance between the pooling
regions. If it's set to None, in which case it equlas ds.
:type st: list or tuple of two ints
:type st: list or tuple of two ints
:param ignore_border: if ds doesn't divide imgshape, do we include an
:param ignore_border: if ds doesn't divide imgshape, do we include an
...
@@ -97,42 +98,27 @@ class DownsampleFactorMax(Op):
...
@@ -97,42 +98,27 @@ class DownsampleFactorMax(Op):
raise
TypeError
(
'imgshape must have at least two elements '
raise
TypeError
(
'imgshape must have at least two elements '
'(rows, cols)'
)
'(rows, cols)'
)
if
st
==
None
:
if
st
is
None
:
st
=
ds
st
=
ds
r
,
c
=
imgshape
[
-
2
:]
r
,
c
=
imgshape
[
-
2
:]
if
st
[
0
]
>=
ds
[
0
]:
nr
=
r
//
st
[
0
]
else
:
nr
=
(
r
-
ds
[
0
])
//
st
[
0
]
+
1
if
st
[
1
]
>=
ds
[
1
]:
nr
=
(
r
-
ds
[
0
])
//
st
[
0
]
+
1
nc
=
c
//
st
[
1
]
nc
=
(
c
-
ds
[
1
])
//
st
[
1
]
+
1
else
:
nc
=
(
c
-
ds
[
1
])
//
st
[
1
]
+
1
rval
=
list
(
imgshape
[:
-
2
])
+
[
nr
,
nc
]
rval
=
list
(
imgshape
[:
-
2
])
+
[
nr
,
nc
]
if
not
ignore_border
:
if
not
ignore_border
:
if
st
[
0
]
>=
ds
[
0
]:
if
isinstance
(
r
,
theano
.
Variable
):
if
isinstance
(
r
,
theano
.
Variable
):
rr
=
r
%
st
[
0
]
rval
[
-
2
]
=
tensor
.
switch
(
r
%
st
[
0
],
rval
[
-
2
]
+
1
,
rval
[
-
2
])
rval
[
-
2
]
=
tensor
.
switch
(
tensor
.
and_
((
rr
%
ds
[
0
]),
tensor
.
eq
(
rr
//
ds
[
0
],
0
)),
rval
[
-
2
]
+
1
,
rval
[
-
2
])
elif
r
%
ds
[
0
]:
elif
(
r
%
st
[
0
])
%
ds
[
0
]:
rval
[
-
2
]
+=
1
rval
[
-
2
]
+=
1
else
:
if
isinstance
(
c
,
theano
.
Variable
):
if
isinstance
(
r
,
theano
.
Variable
):
cr
=
c
%
st
[
1
]
rval
[
-
2
]
=
tensor
.
switch
((
r
-
ds
[
0
])
%
st
[
0
],
rval
[
-
2
]
+
1
,
rval
[
-
2
])
crn
=
cr
-
ds
[
1
]
elif
(
r
-
ds
[
0
])
%
st
[
0
]:
rval
[
-
1
]
=
tensor
.
switch
(
tensor
.
lt
(
crn
,
0
),
rval
[
-
1
]
+
1
,
rval
[
-
1
])
rval
[
-
2
]
+=
1
elif
(
c
%
st
[
1
])
%
ds
[
1
]:
rval
[
-
1
]
+=
1
if
st
[
1
]
>=
ds
[
1
]:
if
isinstance
(
c
,
theano
.
Variable
):
rval
[
-
1
]
=
tensor
.
switch
(
c
%
st
[
1
],
rval
[
-
1
]
+
1
,
rval
[
-
1
])
elif
c
%
ds
[
1
]:
rval
[
-
1
]
+=
1
else
:
if
isinstance
(
c
,
theano
.
Variable
):
rval
[
-
1
]
=
tensor
.
switch
((
c
-
ds
[
1
])
%
st
[
1
],
rval
[
-
1
]
+
1
,
rval
[
-
1
])
elif
(
c
-
ds
[
1
])
%
st
[
1
]:
rval
[
-
1
]
+=
1
return
rval
return
rval
def
__init__
(
self
,
ds
,
ignore_border
=
False
,
st
=
None
):
def
__init__
(
self
,
ds
,
ignore_border
=
False
,
st
=
None
):
...
@@ -158,7 +144,7 @@ class DownsampleFactorMax(Op):
...
@@ -158,7 +144,7 @@ class DownsampleFactorMax(Op):
raise
ValueError
(
raise
ValueError
(
"DownsampleFactorMax downsample parameters must be ints."
"DownsampleFactorMax downsample parameters must be ints."
" Got
%
s"
%
str
(
ds
))
" Got
%
s"
%
str
(
ds
))
if
st
==
None
:
if
st
is
None
:
st
=
ds
st
=
ds
self
.
st
=
tuple
(
st
)
self
.
st
=
tuple
(
st
)
self
.
ignore_border
=
ignore_border
self
.
ignore_border
=
ignore_border
...
@@ -206,22 +192,6 @@ class DownsampleFactorMax(Op):
...
@@ -206,22 +192,6 @@ class DownsampleFactorMax(Op):
img_rows
=
x
.
shape
[
-
2
]
img_rows
=
x
.
shape
[
-
2
]
img_cols
=
x
.
shape
[
-
1
]
img_cols
=
x
.
shape
[
-
1
]
if
self
.
ignore_border
:
if
st0
>=
ds0
:
x_usable2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
else
:
x_usable2
=
(
x
.
shape
[
2
]
-
ds0
)
//
st0
*
st0
+
ds0
else
:
x_usable2
=
x
.
shape
[
2
]
if
self
.
ignore_border
:
if
st1
>=
ds1
:
x_usable3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
else
:
x_usable3
=
(
x
.
shape
[
3
]
-
ds1
)
//
st1
*
st1
+
ds1
else
:
x_usable3
=
x
.
shape
[
3
]
for
n
in
xrange
(
x
.
shape
[
0
]):
for
n
in
xrange
(
x
.
shape
[
0
]):
for
k
in
xrange
(
x
.
shape
[
1
]):
for
k
in
xrange
(
x
.
shape
[
1
]):
for
r
in
xrange
(
pr
):
for
r
in
xrange
(
pr
):
...
...
theano/tensor/signal/tests/test_downsample.py
浏览文件 @
ad22f517
...
@@ -52,41 +52,29 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -52,41 +52,29 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
st
=
ds
st
=
ds
xi
=
0
xi
=
0
yi
=
0
yi
=
0
img_rows
=
input
.
shape
[
-
2
]
img_cols
=
input
.
shape
[
-
1
]
if
not
ignore_border
:
if
not
ignore_border
:
if
st
[
0
]
>=
ds
[
0
]:
rr
=
(
img_rows
)
%
st
[
0
]
if
input
.
shape
[
-
2
]
%
st
[
0
]:
cr
=
(
img_cols
)
%
st
[
1
]
xi
+=
1
if
rr
>
0
and
rr
<
ds
[
0
]:
else
:
xi
+=
1
if
(
input
.
shape
[
-
2
]
-
ds
[
0
])
%
st
[
0
]:
if
cr
>
0
and
cr
<
ds
[
1
]:
xi
+=
1
yi
+=
1
if
st
[
1
]
>=
ds
[
1
]:
if
input
.
shape
[
-
1
]
%
st
[
1
]:
yi
+=
1
else
:
if
(
input
.
shape
[
-
1
]
%
-
ds
[
1
])
%
st
[
1
]:
yi
+=
1
out_shp
=
list
(
input
.
shape
[:
-
2
])
out_shp
=
list
(
input
.
shape
[:
-
2
])
if
st
[
0
]
>=
ds
[
0
]:
out_shp
.
append
((
img_rows
-
ds
[
0
])
/
st
[
0
]
+
1
+
xi
)
out_shp
.
append
(
input
.
shape
[
-
2
]
/
ds
[
0
]
+
xi
)
out_shp
.
append
((
img_cols
-
ds
[
1
])
/
st
[
1
]
+
1
+
yi
)
else
:
out_shp
.
append
((
input
.
shape
[
-
2
]
-
ds
[
0
])
/
st
[
0
]
+
1
+
xi
)
if
st
[
1
]
>=
ds
[
1
]:
out_shp
.
append
(
input
.
shape
[
-
1
]
/
ds
[
1
]
+
yi
)
else
:
out_shp
.
append
((
input
.
shape
[
-
1
]
-
ds
[
1
])
/
st
[
1
]
+
1
+
yi
)
output_val
=
numpy
.
zeros
(
out_shp
)
output_val
=
numpy
.
zeros
(
out_shp
)
img_rows
=
input
.
shape
[
-
2
]
img_cols
=
input
.
shape
[
-
1
]
for
k
in
numpy
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
k
in
numpy
.
ndindex
(
*
input
.
shape
[:
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii_st
=
i
*
ds
[
0
]
ii_st
=
i
*
st
[
0
]
ii_end
=
__builtin__
.
min
(
ii_st
+
ds
[
0
],
img_rows
)
ii_end
=
__builtin__
.
min
(
ii_st
+
ds
[
0
],
img_rows
)
for
j
in
range
(
output_val
.
shape
[
-
1
]):
for
j
in
range
(
output_val
.
shape
[
-
1
]):
jj_st
=
j
*
ds
[
1
]
jj_st
=
j
*
st
[
1
]
jj_end
=
__builtin__
.
min
(
jj_st
+
ds
[
1
],
img_cols
)
jj_end
=
__builtin__
.
min
(
jj_st
+
ds
[
1
],
img_cols
)
patch
=
input
[
k
][
ii_st
:
ii_end
,
jj_st
:
jj_end
]
patch
=
input
[
k
][
ii_st
:
ii_end
,
jj_st
:
jj_end
]
output_val
[
k
][
i
,
j
]
=
numpy
.
max
(
patch
)
output_val
[
k
][
i
,
j
]
=
numpy
.
max
(
patch
)
...
@@ -119,6 +107,30 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
...
@@ -119,6 +107,30 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
output_val
=
f
(
imval
)
output_val
=
f
(
imval
)
assert
(
numpy
.
abs
(
output_val
-
numpy_output_val
)
<
1e-5
)
.
all
()
assert
(
numpy
.
abs
(
output_val
-
numpy_output_val
)
<
1e-5
)
.
all
()
def
test_DownsampleFactorMaxStride
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
# generate random images
maxpoolshps
=
((
1
,
1
),
(
2
,
2
),
(
3
,
3
),
(
2
,
3
))
stridesizes
=
((
1
,
1
),
(
2
,
2
),
(
3
,
1
),
(
2
,
5
),
(
5
,
7
))
imval
=
rng
.
rand
(
4
,
10
,
64
,
64
)
images
=
tensor
.
dtensor4
()
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
for
stride
in
stridesizes
:
print
'maxpoolshp ='
,
maxpoolshp
print
'ignore_border ='
,
ignore_border
print
'stride ='
,
stride
#DownsampleFactorMax op
numpy_output_val
=
self
.
numpy_max_pool_2d_stride
(
imval
,
maxpoolshp
,
ignore_border
,
stride
)
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
ignore_border
,
st
=
stride
)(
images
)
f
=
function
([
images
],
maxpool_op
)
output_val
=
f
(
imval
)
assert
(
numpy
.
abs
(
output_val
-
numpy_output_val
)
<
1e-5
)
.
all
()
def
test_DownsampleFactorMax_grad
(
self
):
def
test_DownsampleFactorMax_grad
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),
(
3
,
2
),
(
2
,
3
))
maxpoolshps
=
((
1
,
1
),
(
3
,
2
),
(
2
,
3
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论