Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bd5dc5a8
提交
bd5dc5a8
authored
3月 02, 2015
作者:
Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8, and grad finally worked
上级
33551a31
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
55 行增加
和
45 行删除
+55
-45
downsample.py
theano/tensor/signal/downsample.py
+32
-25
test_downsample.py
theano/tensor/signal/tests/test_downsample.py
+23
-20
没有找到文件。
theano/tensor/signal/downsample.py
浏览文件 @
bd5dc5a8
...
...
@@ -12,13 +12,14 @@ import numpy
import
theano
from
theano
import
gof
,
Op
,
tensor
,
Variable
,
Apply
def
max_pool2D
(
*
args
,
**
kwargs
):
import
sys
print
>>
sys
.
stderr
,
"DEPRECATION: max_pool2D renamed to max_pool_2d"
return
max_pool_2d
(
*
args
,
**
kwargs
)
def
max_pool_2d
(
input
,
ds
,
ignore_border
=
False
,
st
=
None
,
padding
=
(
0
,
0
)):
def
max_pool_2d
(
input
,
ds
,
ignore_border
=
False
,
st
=
None
,
padding
=
(
0
,
0
)):
"""
Takes as input a N-D tensor, where N >= 2. It downscales the input image by
the specified factor, by keeping only the maximum value of non-overlapping
...
...
@@ -79,7 +80,7 @@ class DownsampleFactorMax(Op):
__props__
=
(
'ds'
,
'ignore_border'
,
'st'
,
'padding'
)
@staticmethod
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
,
st
=
None
,
padding
=
(
0
,
0
)):
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
,
st
=
None
,
padding
=
(
0
,
0
)):
"""Return the shape of the output from this op, for input of given
shape and flags.
...
...
@@ -100,8 +101,9 @@ class DownsampleFactorMax(Op):
extra row/col of partial downsampling (False) or ignore it (True).
:type ignore_border: bool
:param padding: pad zeros on four borders of the images
:type padding: tuple of two ints
:param padding: (pad_h, pad_w), pad zeros on four borders
of the images, pad_h for padding rows, and pad_w for columns.
:type padding: tuple of two ints
:rtype: list
:returns: the shape of the output from this op, for input of given
...
...
@@ -117,7 +119,7 @@ class DownsampleFactorMax(Op):
r
,
c
=
imgshape
[
-
2
:]
r
+=
padding
[
0
]
*
2
c
+=
padding
[
1
]
*
2
if
ignore_border
:
out_r
=
(
r
-
ds
[
0
])
//
st
[
0
]
+
1
out_c
=
(
c
-
ds
[
1
])
//
st
[
1
]
+
1
...
...
@@ -153,7 +155,7 @@ class DownsampleFactorMax(Op):
rval
=
list
(
imgshape
[:
-
2
])
+
[
nr
,
nc
]
return
rval
def
__init__
(
self
,
ds
,
ignore_border
=
False
,
st
=
None
,
padding
=
(
0
,
0
)):
def
__init__
(
self
,
ds
,
ignore_border
=
False
,
st
=
None
,
padding
=
(
0
,
0
)):
"""
:param ds: downsample factor over rows and column.
ds indicates the pool region size.
...
...
@@ -170,8 +172,9 @@ class DownsampleFactorMax(Op):
(no overlap on pooling regions)
: type st: list or tuple of two ints
:param padding: pad zeros on four borders of the images
:type padding: tuple of two ints
:param padding: (pad_h, pad_w), pad zeros on four borders
of the images, pad_h for padding rows, and pad_w for columns.
:type padding: tuple of two ints
"""
self
.
ds
=
tuple
(
ds
)
...
...
@@ -185,13 +188,16 @@ class DownsampleFactorMax(Op):
self
.
ignore_border
=
ignore_border
self
.
padding
=
tuple
(
padding
)
self
.
padding
=
padding
if
padding
!=
(
0
,
0
)
and
not
ignore_border
:
raise
NotImplementedError
(
'padding works only with ignore_boarder=True'
)
if
padding
!=
(
0
,
0
)
and
not
ignore_border
:
raise
NotImplementedError
(
'padding works only with ignore_boarder=True'
)
if
self
.
padding
[
0
]
>=
self
.
ds
[
0
]
or
self
.
padding
[
1
]
>=
self
.
ds
[
1
]:
raise
NotImplementedError
(
'padding_h and padding_w must be smaller than strides'
)
raise
NotImplementedError
(
'padding_h and padding_w must be smaller than strides'
)
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s,
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
st
,
self
.
ignore_border
,
self
.
padding
)
self
.
ds
,
self
.
st
,
self
.
ignore_border
,
self
.
padding
)
def
make_node
(
self
,
x
):
if
x
.
type
.
ndim
!=
4
:
...
...
@@ -214,7 +220,6 @@ class DownsampleFactorMax(Op):
self
.
ignore_border
,
self
.
st
,
self
.
padding
),
dtype
=
x
.
dtype
)
zz
=
z
[
0
]
#number of pooling output rows
pr
=
zz
.
shape
[
-
2
]
#number of pooling output cols
...
...
@@ -228,7 +233,9 @@ class DownsampleFactorMax(Op):
# pad the image
fill
=
x
.
min
()
-
1.
y
=
numpy
.
zeros
((
x
.
shape
[
0
],
x
.
shape
[
1
],
img_rows
,
img_cols
),
dtype
=
x
.
dtype
)
+
fill
y
=
numpy
.
zeros
(
(
x
.
shape
[
0
],
x
.
shape
[
1
],
img_rows
,
img_cols
),
dtype
=
x
.
dtype
)
+
fill
y
[:,
:,
pad_h
:(
img_rows
-
pad_h
),
pad_w
:(
img_cols
-
pad_w
)]
=
x
# max pooling
for
n
in
xrange
(
x
.
shape
[
0
]):
...
...
@@ -253,7 +260,7 @@ class DownsampleFactorMax(Op):
maxout
=
self
(
x
)
return
[
DownsampleFactorMaxGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
,
st
=
self
.
st
)(
st
=
self
.
st
,
padding
=
self
.
padding
)(
x
,
maxout
,
gz
)]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
...
...
@@ -261,7 +268,7 @@ class DownsampleFactorMax(Op):
# the stride size and the pooling size are different.
# An exception is raised for such a case.
if
self
.
ds
!=
self
.
st
:
raise
theano
.
gof
.
utils
.
MethodNotDefined
()
raise
theano
.
gof
.
utils
.
MethodNotDefined
()
x
,
=
inp
z
,
=
out
fail
=
sub
[
'fail'
]
...
...
@@ -340,7 +347,7 @@ class DownsampleFactorMax(Op):
class
DownsampleFactorMaxGrad
(
Op
):
__props__
=
(
'ds'
,
'ignore_border'
,
'st'
,
'padding'
)
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
,
padding
=
(
0
,
0
)):
def
__init__
(
self
,
ds
,
ignore_border
,
st
=
None
,
padding
=
(
0
,
0
)):
self
.
ds
=
tuple
(
ds
)
self
.
ignore_border
=
ignore_border
if
st
is
None
:
...
...
@@ -350,7 +357,7 @@ class DownsampleFactorMaxGrad(Op):
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s,
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
st
,
self
.
ignore_border
,
self
.
padding
)
self
.
ds
,
self
.
st
,
self
.
ignore_border
,
self
.
padding
)
def
make_node
(
self
,
x
,
maxout
,
gz
):
# make_node should only be called by the grad function of
...
...
@@ -364,7 +371,6 @@ class DownsampleFactorMaxGrad(Op):
def
perform
(
self
,
node
,
inp
,
out
):
x
,
maxout
,
gz
=
inp
gx_stg
,
=
out
#number of pooling output rows
pr
=
maxout
.
shape
[
-
2
]
#number of pooling output cols
...
...
@@ -375,11 +381,10 @@ class DownsampleFactorMaxGrad(Op):
img_cols
=
x
.
shape
[
-
1
]
+
2
*
self
.
padding
[
1
]
pad_h
=
self
.
padding
[
0
]
pad_w
=
self
.
padding
[
1
]
# pad the image
fill
=
x
.
min
()
-
1
y
=
numpy
.
zeros
((
x
.
shape
[
0
],
x
.
shape
[
1
],
img_rows
,
img_cols
),
dtype
=
x
.
dtype
)
+
fill
y
=
numpy
.
zeros
(
(
x
.
shape
[
0
],
x
.
shape
[
1
],
img_rows
,
img_cols
),
dtype
=
x
.
dtype
)
+
fill
y
[:,
:,
pad_h
:(
img_rows
-
pad_h
),
pad_w
:(
img_cols
-
pad_w
)]
=
x
gx
=
numpy
.
zeros_like
(
y
)
for
n
in
xrange
(
x
.
shape
[
0
]):
...
...
@@ -408,15 +413,17 @@ class DownsampleFactorMaxGrad(Op):
return
[
theano
.
tensor
.
zeros_like
(
x
),
theano
.
tensor
.
zeros_like
(
maxout
),
DownsampleFactorMaxGradGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
,
st
=
self
.
st
)(
x
,
maxout
,
ggx
)]
self
.
ds
,
ignore_border
=
self
.
ignore_border
,
st
=
self
.
st
)(
x
,
maxout
,
ggx
)]
else
:
return
[
theano
.
tensor
.
zeros_like
(
x
),
theano
.
tensor
.
zeros_like
(
maxout
),
theano
.
gradients
.
grad_not_implemented
(
self
,
2
,
gz
,
'Hessian not implemented with padding'
)]
self
,
2
,
gz
,
'Hessian not implemented with padding'
)]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
if
self
.
ds
!=
self
.
st
:
raise
theano
.
gof
.
utils
.
MethodNotDefined
()
raise
theano
.
gof
.
utils
.
MethodNotDefined
()
x
,
z
,
gz
=
inp
gx
,
=
out
fail
=
sub
[
'fail'
]
...
...
theano/tensor/signal/tests/test_downsample.py
浏览文件 @
bd5dc5a8
...
...
@@ -37,22 +37,24 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
patch
=
input
[
k
][
ii
:
ii
+
ds
[
0
],
jj
:
jj
+
ds
[
1
]]
output_val
[
k
][
i
,
j
]
=
numpy
.
max
(
patch
)
return
output_val
@staticmethod
def
numpy_max_pool_2d_stride_padding
(
x
,
ds
,
ignore_border
=
True
,
st
=
None
,
padding
=
(
0
,
0
)):
x
,
ds
,
ignore_border
=
True
,
st
=
None
,
padding
=
(
0
,
0
)):
pad_h
=
padding
[
0
]
pad_w
=
padding
[
1
]
h
=
x
.
shape
[
-
2
]
w
=
x
.
shape
[
-
1
]
assert
ds
[
0
]
>
pad_h
assert
ds
[
1
]
>
pad_w
assert
ds
[
1
]
>
pad_w
def
pad_img
(
x
):
fill
=
x
.
min
()
-
1
t
=
numpy
.
ones
((
x
.
shape
[
0
],
x
.
shape
[
1
],
1
,
1
))
t
=
numpy
.
ones
((
x
.
shape
[
0
],
x
.
shape
[
1
],
1
,
1
))
ud_bar
=
(
numpy
.
zeros
((
pad_h
,
w
))
+
fill
)[
numpy
.
newaxis
,
numpy
.
newaxis
,
:,
:]
*
t
numpy
.
newaxis
,
numpy
.
newaxis
,
:,
:]
*
t
lr_bar
=
(
numpy
.
zeros
((
pad_h
*
2
+
h
,
pad_w
))
+
fill
)[
numpy
.
newaxis
,
numpy
.
newaxis
,
:,
:]
*
t
numpy
.
newaxis
,
numpy
.
newaxis
,
:,
:]
*
t
y
=
numpy
.
concatenate
([
ud_bar
,
x
,
ud_bar
],
axis
=
2
)
y
=
numpy
.
concatenate
([
lr_bar
,
y
,
lr_bar
],
axis
=
3
)
return
y
...
...
@@ -64,7 +66,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
out_shp
.
append
(
out_r
)
out_shp
.
append
(
out_c
)
ds0
,
ds1
=
ds
st0
,
st1
=
st
st0
,
st1
=
st
output_val
=
numpy
.
zeros
(
out_shp
)
tt
=
[]
y
=
pad_img
(
x
)
...
...
@@ -236,14 +238,14 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
f
=
function
([
images
],
maxpool_op
)
output_val
=
f
(
imval
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxPaddingStride
(
self
):
ignore_border
=
True
# padding does not support ignore_border=False
ignore_border
=
True
# padding does not support ignore_border=False
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolsizes
=
[(
3
,
3
),
(
4
,
4
),
(
3
,
4
),
(
4
,
3
)]
stridesizes
=
[(
2
,
2
),
(
2
,
2
),
(
1
,
1
),
(
1
,
2
)]
paddingsizes
=
[(
2
,
2
),
(
1
,
2
),
(
2
,
1
),
(
0
,
0
)]
imgsizes
=
[(
5
,
5
),
(
5
,
5
),
(
5
,
6
),
(
6
,
5
)]
maxpoolsizes
=
[(
3
,
3
),
(
4
,
4
),
(
3
,
4
),
(
4
,
3
)]
stridesizes
=
[(
2
,
2
),
(
2
,
2
),
(
1
,
1
),
(
1
,
2
)]
paddingsizes
=
[(
2
,
2
),
(
1
,
2
),
(
2
,
1
),
(
0
,
0
)]
imgsizes
=
[(
5
,
5
),
(
5
,
5
),
(
5
,
6
),
(
6
,
5
)]
m
=
4
# minibatch
c
=
10
# channel size
images
=
tensor
.
dtensor4
()
...
...
@@ -254,21 +256,22 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
maxpoolsize
=
maxpoolsizes
[
indx
]
paddingsize
=
paddingsizes
[
indx
]
numpy_output_val
=
self
.
numpy_max_pool_2d_stride_padding
(
imval
,
maxpoolsize
,
ignore_border
,
stridesize
,
paddingsize
)
maxpool_op
=
DownsampleFactorMax
(
maxpoolsize
,
ignore_border
=
ignore_border
,
st
=
stridesize
,
padding
=
paddingsize
)(
images
)
imval
,
maxpoolsize
,
ignore_border
,
stridesize
,
paddingsize
)
maxpool_op
=
DownsampleFactorMax
(
maxpoolsize
,
ignore_border
=
ignore_border
,
st
=
stridesize
,
padding
=
paddingsize
)(
images
)
f
=
function
([
images
],
maxpool_op
)
output_val
=
f
(
imval
)
utt
.
assert_allclose
(
output_val
,
numpy_output_val
)
def
test_DownsampleFactorMaxPaddingStride_grad
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
imval
=
rng
.
rand
(
1
,
1
,
10
,
10
)
*
10.0
maxpoolsize
=
(
5
,
3
)
stridesize
=
(
3
,
2
)
paddingsize
=
(
2
,
2
)
paddingsize
=
(
2
,
2
)
def
mp
(
input
):
return
DownsampleFactorMax
(
maxpoolsize
,
ignore_border
=
True
,
...
...
@@ -276,7 +279,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
padding
=
paddingsize
,
)(
input
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_DownsampleFactorMax_grad
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),
(
3
,
2
),
(
2
,
3
))
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论