Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
388dba8b
提交
388dba8b
authored
10月 27, 2009
作者:
Frederic Bastien
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added some preleminary test for DownsampleFactorMax op and added a compatibility fct max_pool()
上级
093f823c
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
145 行增加
和
6 行删除
+145
-6
downsample.py
theano/sandbox/downsample.py
+41
-6
test_downsample.py
theano/sandbox/test_downsample.py
+104
-0
没有找到文件。
theano/sandbox/downsample.py
浏览文件 @
388dba8b
...
@@ -152,13 +152,13 @@ class DownsampleFactorMax(Op):
...
@@ -152,13 +152,13 @@ class DownsampleFactorMax(Op):
@staticmethod
@staticmethod
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
):
def
out_shape
(
imgshape
,
ds
,
ignore_border
=
False
):
#old code not tested (not evenread)
#old code not tested (not evenread)
a
,
b
,
c
,
d
=
imgshape
a
,
b
=
imgshape
[
-
2
:]
rval
=
[
a
,
b
,
c
/
ds
[
0
],
d
/
ds
[
1
]]
rval
=
list
(
imgshape
[:
-
2
])
+
[
a
/
ds
[
0
],
b
/
ds
[
1
]]
if
not
ignore_border
:
if
not
ignore_border
:
if
c
%
ds
[
0
]:
if
a
%
ds
[
0
]:
rval
[
2
]
+=
1
rval
[
-
2
]
+=
1
if
d
%
ds
[
1
]:
if
b
%
ds
[
1
]:
rval
[
3
]
+=
1
rval
[
-
1
]
+=
1
return
rval
return
rval
def
__init__
(
self
,
ds
,
ignore_border
=
False
):
def
__init__
(
self
,
ds
,
ignore_border
=
False
):
...
@@ -276,3 +276,38 @@ class DownsampleFactorMax(Op):
...
@@ -276,3 +276,38 @@ class DownsampleFactorMax(Op):
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
()
return
()
def
max_pool
(
images
=
None
,
imshp
=
None
,
maxpoolshp
=
None
,
ignore_border
=
True
):
"""Implements a max pooling layer
Uses the same API as sp.max_pool but uses the Downsample op instead.
Takes as input a 2D tensor of shape batch_size x img_size and performs max pooling.
Max pooling downsamples by taking the max value in a given area, here defined by
maxpoolshp. Outputs a 2D tensor of shape batch_size x output_size.
Parameters are keyword arguments in order to use func_to_mod.
@param images: 2D tensor containing images on which to apply convolution.
Assumed to be of shape batch_size x img_size
@param imgshp: tuple containing image dimensions
@param maxpoolshp: tuple containing shape of area to max pool over
@output out1: symbolic result (2D tensor)
@output out2: logical shape of the output
"""
if
len
(
imshp
)
==
2
:
imshp
=
(
1
,)
+
imshp
elif
len
(
imshp
)
!=
3
:
raise
NotImplementedError
(
"!"
)
# all these reshapes should happen in place
imrshp
=
tensor
.
stack
(
images
.
shape
[
0
],
*
[
tensor
.
as_tensor
(
x
)
for
x
in
imshp
])
imtensor
=
tensor
.
reshape
(
images
,
imrshp
)
maxpop
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
)
rval
=
maxpop
(
imtensor
)
return
tensor
.
flatten
(
rval
,
2
),
maxpop
.
out_shape
(
imshp
,
maxpoolshp
,
ignore_border
)
theano/sandbox/test_downsample.py
0 → 100644
浏览文件 @
388dba8b
import
unittest
,
sys
,
time
import
numpy
as
N
import
theano.tensor
as
T
from
theano.tests
import
unittest_tools
as
utt
from
theano.sandbox.downsample
import
DownsampleFactorMax
,
max_pool
from
theano
import
function
,
Mode
class
TestDownSample
(
unittest
.
TestCase
):
def
test_maxpool
(
self
):
# generate flatted images
maxpoolshps
=
((
1
,
1
),(
2
,
2
),(
3
,
3
),(
2
,
3
))
imval
=
N
.
random
.
rand
(
4
,
10
,
64
,
64
)
do_theano
=
True
images
=
T
.
dmatrix
()
dmatrix4
=
T
.
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
))
images4
=
dmatrix4
()
tctot
,
tpytot
,
ntot
,
gtot
=
[],[],[],[]
for
maxpoolshp
in
maxpoolshps
:
print
'maxpoolshp'
,
maxpoolshp
# numeric verification
my_output_val
=
N
.
zeros
((
imval
.
shape
[
0
],
imval
.
shape
[
1
],
imval
.
shape
[
2
]
/
maxpoolshp
[
0
],
imval
.
shape
[
3
]
/
maxpoolshp
[
1
]))
time1
=
time
.
time
()
for
n
in
range
(
imval
.
shape
[
0
]):
for
k
in
range
(
imval
.
shape
[
1
]):
for
i
in
range
(
my_output_val
.
shape
[
2
]):
ii
=
i
*
maxpoolshp
[
0
]
for
j
in
range
(
my_output_val
.
shape
[
3
]):
jj
=
j
*
maxpoolshp
[
1
]
patch
=
imval
[
n
,
k
,
ii
:
ii
+
maxpoolshp
[
0
],
jj
:
jj
+
maxpoolshp
[
1
]]
my_output_val
[
n
,
k
,
i
,
j
]
=
N
.
max
(
patch
)
my_output_val
=
my_output_val
.
reshape
(
imval
.
shape
[
0
],
-
1
)
ntot
+=
[
time
.
time
()
-
time1
]
# symbolic stuff
if
do_theano
:
#### wrapper to DownsampleFactorMax op ####
output
,
outshp
=
max_pool
(
images
,
imval
.
shape
[
1
:],
maxpoolshp
)
assert
N
.
prod
(
my_output_val
.
shape
[
1
:])
==
N
.
prod
(
outshp
)
print
outshp
print
my_output_val
.
shape
assert
N
.
prod
(
my_output_val
.
shape
[
1
:])
==
N
.
prod
(
outshp
)
f
=
function
([
images
,],[
output
,])
imval2
=
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
)
output_val
=
f
(
imval2
)
assert
N
.
all
(
output_val
==
my_output_val
)
else
:
tctot
=-
1
output_val
=
my_output_val
.
copy
()
#DownsampleFactorMax op
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
True
)(
images4
)
f
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"py"
))
f2
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"c"
))
time1
=
time
.
time
()
output_val2
=
f
(
imval
)
tpytot
+=
[
time
.
time
()
-
time1
]
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val2
.
flatten
())
<
1e-5
)
.
all
()
time1
=
time
.
time
()
output_val2
=
f2
(
imval
)
tctot
+=
[
time
.
time
()
-
time1
]
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val2
.
flatten
())
<
1e-5
)
.
all
()
def
mp
(
input
):
output
,
outshp
=
max_pool
(
input
,
imval
.
shape
[
1
:],
maxpoolshp
)
return
output
print
'Numpy processing time:
%.3
fs'
%
sum
(
ntot
),
ntot
print
'c Theano(DownsampleFactorMax) processing time:
%.3
fs'
%
sum
(
tctot
),
tctot
print
'py Theano(DownsampleFactorMax) processing time:
%.3
fs'
%
sum
(
tpytot
),
tpytot
d
=
N
.
asarray
(
ntot
)
/
tctot
print
'speed up c theano(DownsampleFactorMax) vs manual:
%.3
f'
%
d
.
mean
(),
d
d
=
N
.
asarray
(
ntot
)
/
tpytot
print
'speed up py theano(DownsampleFactorMax) vs manual:
%.3
f'
%
d
.
mean
(),
d
print
'verify_grad time
%.3
f'
%
sum
(
gtot
),
def
test_maxpool_grad
(
self
):
# generate flatted images
maxpoolshps
=
((
1
,
1
),(
2
,
2
),(
3
,
3
),(
2
,
3
))
imval
=
N
.
random
.
rand
(
3
,
7
,
10
,
10
)
*
10.0
#more variance means numeric gradient will be more accurate
do_theano
=
True
images
=
T
.
dmatrix
()
dmatrix4
=
T
.
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
))
images4
=
dmatrix4
()
for
maxpoolshp
in
maxpoolshps
:
print
'maxpoolshp'
,
maxpoolshp
def
mp
(
input
):
output
,
outshp
=
max_pool
(
input
,
imval
.
shape
[
1
:],
maxpoolshp
)
return
output
print
>>
sys
.
stderr
,
'max_pool verify_grad requires unusually large tolerance... is it correct?'
utt
.
verify_grad
(
mp
,
[
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
)],
tol
=
1e-2
)
if
__name__
==
'__main__'
:
t
=
TestSP
(
"test_convolution"
)
t
=
TestSP
(
"test_maxpool"
)
.
run
()
# t.test_convolution()
# t.test_multilayer_conv()
#t.test_maxpool()
from
theano.tests
import
main
# main("test_sp")
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论