Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
31b01b28
提交
31b01b28
authored
6月 20, 2013
作者:
Frederic
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
pep8
上级
3bbc5357
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
53 行增加
和
31 行删除
+53
-31
downsample.py
theano/tensor/signal/downsample.py
+53
-31
没有找到文件。
theano/tensor/signal/downsample.py
浏览文件 @
31b01b28
...
@@ -5,16 +5,20 @@ DownsampleFactorMax, DownsampleAvg, DownsampleSoftmax.
...
@@ -5,16 +5,20 @@ DownsampleFactorMax, DownsampleAvg, DownsampleSoftmax.
"""
"""
#This file should move along with conv.py
#This file should move along with conv.py
import
__builtin__
import
numpy
import
theano
from
theano
import
gof
,
Op
,
tensor
,
Variable
,
Apply
from
theano
import
gof
,
Op
,
tensor
,
Variable
,
Apply
import
numpy
,
theano
import
__builtin__
def
max_pool2D
(
*
args
,
**
kwargs
):
def
max_pool2D
(
*
args
,
**
kwargs
):
import
sys
import
sys
print
>>
sys
.
stderr
,
"DEPRECATION: max_pool2D renamed to max_pool_2d"
print
>>
sys
.
stderr
,
"DEPRECATION: max_pool2D renamed to max_pool_2d"
return
max_pool_2d
(
*
args
,
**
kwargs
)
return
max_pool_2d
(
*
args
,
**
kwargs
)
def
max_pool_2d
(
input
,
ds
,
ignore_border
=
False
):
def
max_pool_2d
(
input
,
ds
,
ignore_border
=
False
):
"""
"""
Takes as input a N-D tensor, where N >= 2. It downscales the input image by
Takes as input a N-D tensor, where N >= 2. It downscales the input image by
...
@@ -36,11 +40,12 @@ def max_pool_2d(input, ds, ignore_border=False):
...
@@ -36,11 +40,12 @@ def max_pool_2d(input, ds, ignore_border=False):
# count the number of "leading" dimensions, store as dmatrix
# count the number of "leading" dimensions, store as dmatrix
batch_size
=
tensor
.
prod
(
input
.
shape
[:
-
2
])
batch_size
=
tensor
.
prod
(
input
.
shape
[:
-
2
])
batch_size
=
tensor
.
shape_padright
(
batch_size
,
1
)
batch_size
=
tensor
.
shape_padright
(
batch_size
,
1
)
# store as 4D tensor with shape: (batch_size,1,height,width)
# store as 4D tensor with shape: (batch_size,1,height,width)
new_shape
=
tensor
.
cast
(
tensor
.
join
(
0
,
batch_size
,
new_shape
=
tensor
.
cast
(
tensor
.
join
(
0
,
batch_size
,
tensor
.
as_tensor
([
1
,]),
img_shape
),
'int64'
)
tensor
.
as_tensor
([
1
,]),
img_shape
),
'int64'
)
input_4D
=
tensor
.
reshape
(
input
,
new_shape
,
ndim
=
4
)
input_4D
=
tensor
.
reshape
(
input
,
new_shape
,
ndim
=
4
)
# downsample mini-batch of images
# downsample mini-batch of images
...
@@ -53,10 +58,11 @@ def max_pool_2d(input, ds, ignore_border=False):
...
@@ -53,10 +58,11 @@ def max_pool_2d(input, ds, ignore_border=False):
class
DownsampleFactorMax
(
Op
):
class
DownsampleFactorMax
(
Op
):
"""
"""For N-dimensional tensors, consider that the last two
For N-dimensional tensors, consider that the last two dimensions span images.
dimensions span images. This Op downsamples these images by a
This Op downsamples these images by a factor ds, by taking the max over non-
factor ds, by taking the max over non- overlapping rectangular
overlapping rectangular regions.
regions.
"""
"""
@staticmethod
@staticmethod
...
@@ -83,7 +89,7 @@ class DownsampleFactorMax(Op):
...
@@ -83,7 +89,7 @@ class DownsampleFactorMax(Op):
if
len
(
imgshape
)
<
2
:
if
len
(
imgshape
)
<
2
:
raise
TypeError
(
'imgshape must have at least two elements (rows, cols)'
)
raise
TypeError
(
'imgshape must have at least two elements (rows, cols)'
)
r
,
c
=
imgshape
[
-
2
:]
r
,
c
=
imgshape
[
-
2
:]
rval
=
list
(
imgshape
[:
-
2
])
+
[
r
//
ds
[
0
],
c
//
ds
[
1
]
]
rval
=
list
(
imgshape
[:
-
2
])
+
[
r
//
ds
[
0
],
c
//
ds
[
1
]
]
if
not
ignore_border
:
if
not
ignore_border
:
if
isinstance
(
r
,
theano
.
Variable
):
if
isinstance
(
r
,
theano
.
Variable
):
...
@@ -111,13 +117,16 @@ class DownsampleFactorMax(Op):
...
@@ -111,13 +117,16 @@ class DownsampleFactorMax(Op):
self
.
ignore_border
=
ignore_border
self
.
ignore_border
=
ignore_border
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
return
(
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
def
__str__
(
self
):
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
def
make_node
(
self
,
x
):
def
make_node
(
self
,
x
):
if
x
.
type
.
ndim
!=
4
:
if
x
.
type
.
ndim
!=
4
:
...
@@ -130,30 +139,35 @@ class DownsampleFactorMax(Op):
...
@@ -130,30 +139,35 @@ class DownsampleFactorMax(Op):
"""
"""
x
,
=
inp
x
,
=
inp
z
,
=
out
z
,
=
out
if
len
(
x
.
shape
)
!=
4
:
if
len
(
x
.
shape
)
!=
4
:
raise
NotImplementedError
(
'DownsampleFactorMax requires 4D input for now'
)
raise
NotImplementedError
(
'DownsampleFactorMax requires 4D input for now'
)
z_shape
=
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
)
z_shape
=
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
)
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
if
(
z
[
0
]
is
None
)
or
(
z
[
0
]
.
shape
!=
z_shape
):
z
[
0
]
=
numpy
.
zeros
(
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
))
z
[
0
]
=
numpy
.
zeros
(
self
.
out_shape
(
x
.
shape
,
self
.
ds
,
self
.
ignore_border
))
z
[
0
]
=
theano
.
_asarray
(
z
[
0
],
dtype
=
x
.
dtype
)
z
[
0
]
=
theano
.
_asarray
(
z
[
0
],
dtype
=
x
.
dtype
)
zz
=
z
[
0
]
zz
=
z
[
0
]
## zz needs to be initialized with -inf for the following to work
## zz needs to be initialized with -inf for the following to work
zz
-=
numpy
.
inf
zz
-=
numpy
.
inf
ds0
,
ds1
=
self
.
ds
ds0
,
ds1
=
self
.
ds
if
self
.
ignore_border
:
if
self
.
ignore_border
:
x_usable2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
x_usable2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
else
:
x_usable2
=
x
.
shape
[
2
]
else
:
x_usable2
=
x
.
shape
[
2
]
if
self
.
ignore_border
:
if
self
.
ignore_border
:
x_usable3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
x_usable3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
else
:
x_usable3
=
x
.
shape
[
3
]
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
i
in
xrange
(
x_usable2
):
for
i
in
xrange
(
x_usable2
):
zi
=
i
/
ds0
zi
=
i
/
ds0
for
j
in
xrange
(
x_usable3
):
for
j
in
xrange
(
x_usable3
):
zj
=
j
/
ds1
zj
=
j
/
ds1
zz
[
n
,
k
,
zi
,
zj
]
=
__builtin__
.
max
(
zz
[
n
,
k
,
zi
,
zj
],
x
[
n
,
k
,
i
,
j
])
zz
[
n
,
k
,
zi
,
zj
]
=
__builtin__
.
max
(
zz
[
n
,
k
,
zi
,
zj
],
x
[
n
,
k
,
i
,
j
])
def
infer_shape
(
self
,
node
,
in_shapes
):
def
infer_shape
(
self
,
node
,
in_shapes
):
shp
=
self
.
out_shape
(
in_shapes
[
0
],
self
.
ds
,
self
.
ignore_border
)
shp
=
self
.
out_shape
(
in_shapes
[
0
],
self
.
ds
,
self
.
ignore_border
)
...
@@ -163,12 +177,14 @@ class DownsampleFactorMax(Op):
...
@@ -163,12 +177,14 @@ class DownsampleFactorMax(Op):
x
,
=
inp
x
,
=
inp
gz
,
=
grads
gz
,
=
grads
maxout
=
self
(
x
)
maxout
=
self
(
x
)
return
[
DownsampleFactorMaxGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
)(
x
,
maxout
,
gz
)]
return
[
DownsampleFactorMaxGrad
(
self
.
ds
,
ignore_border
=
self
.
ignore_border
)(
x
,
maxout
,
gz
)]
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
def
c_code
(
self
,
node
,
name
,
inp
,
out
,
sub
):
x
,
=
inp
x
,
=
inp
z
,
=
out
z
,
=
out
fail
=
sub
[
'fail'
]
fail
=
sub
[
'fail'
]
ignore_border
=
int
(
self
.
ignore_border
)
ignore_border
=
int
(
self
.
ignore_border
)
ds0
,
ds1
=
self
.
ds
ds0
,
ds1
=
self
.
ds
return
"""
return
"""
...
@@ -209,7 +225,8 @@ class DownsampleFactorMax(Op):
...
@@ -209,7 +225,8 @@ class DownsampleFactorMax(Op):
dims[1]=PyArray_DIMS(
%(x)
s)[1];
dims[1]=PyArray_DIMS(
%(x)
s)[1];
dims[2]=z_shp0;
dims[2]=z_shp0;
dims[3]=z_shp1;
dims[3]=z_shp1;
%(z)
s = (PyArrayObject*) PyArray_ZEROS(4, dims, typenum,0); //TODO: zeros not necessary
//TODO: zeros not necessary
%(z)
s = (PyArrayObject*) PyArray_ZEROS(4, dims, typenum,0);
}
}
if (z_shp0 && z_shp1)
if (z_shp0 && z_shp1)
...
@@ -237,7 +254,7 @@ class DownsampleFactorMax(Op):
...
@@ -237,7 +254,7 @@ class DownsampleFactorMax(Op):
"""
%
locals
()
"""
%
locals
()
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
0
,
1
)
return
(
0
,
1
)
class
DownsampleFactorMaxGrad
(
Op
):
class
DownsampleFactorMaxGrad
(
Op
):
...
@@ -247,20 +264,23 @@ class DownsampleFactorMaxGrad(Op):
...
@@ -247,20 +264,23 @@ class DownsampleFactorMaxGrad(Op):
self
.
ignore_border
=
ignore_border
self
.
ignore_border
=
ignore_border
def
__eq__
(
self
,
other
):
def
__eq__
(
self
,
other
):
return
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
return
(
type
(
self
)
==
type
(
other
)
and
self
.
ds
==
other
.
ds
and
self
.
ignore_border
==
other
.
ignore_border
)
def
__hash__
(
self
):
def
__hash__
(
self
):
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
return
hash
(
type
(
self
))
^
hash
(
self
.
ds
)
^
hash
(
self
.
ignore_border
)
def
__str__
(
self
):
def
__str__
(
self
):
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
return
'
%
s{
%
s,
%
s}'
%
(
self
.
__class__
.
__name__
,
self
.
ds
,
self
.
ignore_border
)
def
make_node
(
self
,
x
,
maxout
,
gz
):
def
make_node
(
self
,
x
,
maxout
,
gz
):
# make_node should only be called by the grad function of
DownsampleFactorMax,
# make_node should only be called by the grad function of
# so these asserts should not fail.
#
DownsampleFactorMax,
so these asserts should not fail.
assert
isinstance
(
x
,
Variable
)
and
x
.
ndim
==
4
assert
isinstance
(
x
,
Variable
)
and
x
.
ndim
==
4
assert
isinstance
(
maxout
,
Variable
)
and
maxout
.
ndim
==
4
assert
isinstance
(
maxout
,
Variable
)
and
maxout
.
ndim
==
4
assert
isinstance
(
gz
,
Variable
)
and
gz
.
ndim
==
4
assert
isinstance
(
gz
,
Variable
)
and
gz
.
ndim
==
4
return
Apply
(
self
,
[
x
,
maxout
,
gz
],
[
x
.
type
()])
return
Apply
(
self
,
[
x
,
maxout
,
gz
],
[
x
.
type
()])
...
@@ -271,9 +291,11 @@ class DownsampleFactorMaxGrad(Op):
...
@@ -271,9 +291,11 @@ class DownsampleFactorMaxGrad(Op):
ds0
,
ds1
=
self
.
ds
ds0
,
ds1
=
self
.
ds
shape2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
shape2
=
(
x
.
shape
[
2
]
//
ds0
*
ds0
)
if
not
self
.
ignore_border
:
shape2
=
x
.
shape
[
2
]
if
not
self
.
ignore_border
:
shape2
=
x
.
shape
[
2
]
shape3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
shape3
=
(
x
.
shape
[
3
]
//
ds1
*
ds1
)
if
not
self
.
ignore_border
:
shape3
=
x
.
shape
[
3
]
if
not
self
.
ignore_border
:
shape3
=
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
i
in
xrange
(
shape2
):
for
i
in
xrange
(
shape2
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论