Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
02c959a7
提交
02c959a7
authored
1月 29, 2010
作者:
Pascal Lamblin
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Rewrite of test_downsample to test the different settings of max_pool2D.
上级
834e31d0
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
134 行增加
和
107 行删除
+134
-107
downsample.py
theano/sandbox/downsample.py
+2
-2
test_downsample.py
theano/sandbox/test_downsample.py
+132
-105
没有找到文件。
theano/sandbox/downsample.py
浏览文件 @
02c959a7
...
@@ -147,7 +147,7 @@ class DownsampleFactorMaxGrad(Op):
...
@@ -147,7 +147,7 @@ class DownsampleFactorMaxGrad(Op):
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
()
return
()
def
max_pool2D
(
input
,
ds
,
ignore_border
=
False
):
def
max_pool2D
(
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
...
@@ -166,7 +166,7 @@ def max_pool2D(input, ds, ignore_border=False):
...
@@ -166,7 +166,7 @@ def max_pool2D(input, ds, ignore_border=False):
# extract image dimensions
# extract image dimensions
img_shape
=
input
.
shape
[
-
2
:]
img_shape
=
input
.
shape
[
-
2
:]
# 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
)
...
...
theano/sandbox/test_downsample.py
浏览文件 @
02c959a7
import
unittest
,
sys
,
time
import
unittest
,
sys
,
time
import
numpy
as
N
import
numpy
import
theano.tensor
as
T
import
theano.tensor
as
tensor
from
theano.tests
import
unittest_tools
as
utt
from
theano.tests
import
unittest_tools
as
utt
from
theano.sandbox.downsample
import
DownsampleFactorMax
from
theano.sandbox.downsample
import
DownsampleFactorMax
,
max_pool2D
from
theano
import
function
,
Mode
from
theano
import
function
,
Mode
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.
class
TestDownsampleFactorMax
(
unittest
.
TestCase
):
def
setUp
(
self
):
utt
.
seed_rng
()
Takes as input a 2D tensor of shape batch_size x img_size and performs max pooling.
@staticmethod
Max pooling downsamples by taking the max value in a given area, here defined by
def
numpy_max_pool2D
(
input
,
ds
,
ignore_border
=
False
):
maxpoolshp. Outputs a 2D tensor of shape batch_size x output_size.
'''Helper function, implementing max_pool2D in pure numpy'''
if
len
(
input
.
shape
)
<
2
:
raise
NotImplementedError
(
'input should have at least 2 dim, shape is
%
s'
\
%
str
(
input
.
shape
))
Parameters are keyword arguments in order to use func_to_mod.
xi
=
0
yi
=
0
if
not
ignore_border
:
if
input
.
shape
[
-
2
]
%
ds
[
0
]:
xi
+=
1
if
input
.
shape
[
-
1
]
%
ds
[
1
]:
yi
+=
1
@param images: 2D tensor containing images on which to apply convolution.
out_shp
=
list
(
input
.
shape
[:
-
2
])
Assumed to be of shape batch_size x img_size
out_shp
.
append
(
input
.
shape
[
-
2
]
/
ds
[
0
]
+
xi
)
@param imgshp: tuple containing image dimensions
out_shp
.
append
(
input
.
shape
[
-
1
]
/
ds
[
1
]
+
yi
)
@param maxpoolshp: tuple containing shape of area to max pool over
@output out1: symbolic result (2D tensor)
@output out2: logical shape of the output
"""
output_val
=
numpy
.
zeros
(
out_shp
)
if
len
(
imshp
)
==
2
:
imshp
=
(
1
,)
+
imshp
elif
len
(
imshp
)
!=
3
:
raise
NotImplementedError
(
"!"
)
# all these reshapes should happen in place
imrshp
=
T
.
stack
(
images
.
shape
[
0
],
*
[
T
.
as_tensor
(
x
)
for
x
in
imshp
])
imtensor
=
T
.
reshape
(
images
,
imrshp
)
maxpop
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
)
for
k
in
numpy
.
ndindex
(
input
.
shape
[:
-
2
]):
rval
=
maxpop
(
imtensor
)
for
i
in
range
(
output_val
.
shape
[
-
2
]):
ii
=
i
*
ds
[
0
]
for
j
in
range
(
output_val
.
shape
[
-
1
]):
jj
=
j
*
ds
[
1
]
patch
=
input
[
k
][
ii
:
ii
+
ds
[
0
],
jj
:
jj
+
ds
[
1
]]
output_val
[
k
][
i
,
j
]
=
numpy
.
max
(
patch
)
return
output_val
return
T
.
flatten
(
rval
,
2
),
maxpop
.
out_shape
(
imshp
,
maxpoolshp
,
ignore_border
)
def
test_DownsampleFactorMax
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
class
TestDownsampleFactorMax
(
unittest
.
TestCase
):
# generate random images
def
test_maxpool
(
self
):
# generate flatted images
maxpoolshps
=
((
1
,
1
),(
2
,
2
),(
3
,
3
),(
2
,
3
))
maxpoolshps
=
((
1
,
1
),(
2
,
2
),(
3
,
3
),(
2
,
3
))
imval
=
N
.
random
.
rand
(
4
,
10
,
64
,
64
)
imval
=
rng
.
rand
(
4
,
10
,
64
,
64
)
images
=
T
.
dmatrix
()
images
=
tensor
.
dtensor4
()
dmatrix4
=
T
.
TensorType
(
'float64'
,
(
False
,
False
,
False
,
False
))
images4
=
dmatrix4
()
tctot
,
tpytot
,
ntot
=
[],[],[]
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
for
border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
print
'maxpoolshp'
,
maxpoolshp
,
'border'
,
border
print
'maxpoolshp ='
,
maxpoolshp
print
'ignore_border ='
,
ignore_border
# numeric verification
xi
=
0
## Pure Numpy computation
yi
=
0
numpy_output_val
=
self
.
numpy_max_pool2D
(
imval
,
maxpoolshp
,
ignore_border
)
if
not
border
:
if
imval
.
shape
[
-
2
]
%
maxpoolshp
[
0
]:
output
=
max_pool2D
(
images
,
maxpoolshp
,
ignore_border
)
xi
+=
1
if
imval
.
shape
[
-
1
]
%
maxpoolshp
[
1
]:
yi
+=
1
my_output_val
=
N
.
zeros
((
imval
.
shape
[
0
],
imval
.
shape
[
1
],
imval
.
shape
[
2
]
/
maxpoolshp
[
0
]
+
xi
,
imval
.
shape
[
3
]
/
maxpoolshp
[
1
]
+
yi
))
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
#### wrapper to DownsampleFactorMax op ####
output
,
outshp
=
max_pool
(
images
,
imval
.
shape
[
1
:],
maxpoolshp
,
border
)
assert
N
.
prod
(
my_output_val
.
shape
[
1
:])
==
N
.
prod
(
outshp
)
assert
N
.
prod
(
my_output_val
.
shape
[
1
:])
==
N
.
prod
(
outshp
)
f
=
function
([
images
,],[
output
,])
f
=
function
([
images
,],[
output
,])
imval2
=
imval
.
reshape
(
imval
.
shape
[
0
],
-
1
)
output_val
=
f
(
imval
)
output_val
=
f
(
imval2
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
assert
N
.
all
(
output_val
==
my_output_val
)
#DownsampleFactorMax op
#DownsampleFactorMax op
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
border
)(
images4
)
maxpool_op
=
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
ignore_border
)(
images
)
f
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"py"
))
f
=
function
([
images
],
maxpool_op
)
f2
=
function
([
images4
],
maxpool_op
,
mode
=
Mode
(
linker
=
"c"
))
f3
=
function
([
images4
],
maxpool_op
)
#for when we want to use the debug mode
time1
=
time
.
time
()
output_val
=
f
(
imval
)
output_val
=
f
(
imval
)
tctot
+=
[
time
.
time
()
-
time1
]
assert
(
numpy
.
abs
(
output_val
-
numpy_output_val
)
<
1e-5
)
.
all
()
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val
.
flatten
())
<
1e-5
)
.
all
()
time1
=
time
.
time
()
output_val
=
f2
(
imval
)
tpytot
+=
[
time
.
time
()
-
time1
]
assert
(
N
.
abs
(
my_output_val
.
flatten
()
-
output_val
.
flatten
())
<
1e-5
)
.
all
()
output_val
=
f3
(
imval
)
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
def
test_DownsampleFactorMax_grad
(
self
):
def
test_DownsampleFactorMax_grad
(
self
):
# generate flatted images
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),(
3
,
2
),(
2
,
3
))
maxpoolshps
=
((
1
,
1
),(
3
,
2
),(
2
,
3
))
imval
=
N
.
random
.
rand
(
2
,
3
,
3
,
4
)
*
10.0
#more variance means numeric gradient will be more accurate
imval
=
rng
.
rand
(
2
,
3
,
3
,
4
)
*
10.0
#more variance means numeric gradient will be more accurate
do_theano
=
True
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
print
'maxpoolshp ='
,
maxpoolshp
print
'ignore_border ='
,
ignore_border
def
mp
(
input
):
return
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
ignore_border
)(
input
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool2D_2D
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
((
1
,
1
),(
3
,
2
))
imval
=
rng
.
rand
(
4
,
7
)
images
=
tensor
.
dmatrix
()
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
print
'maxpoolshp ='
,
maxpoolshp
print
'ignore_border ='
,
ignore_border
numpy_output_val
=
self
.
numpy_max_pool2D
(
imval
,
maxpoolshp
,
ignore_border
)
output
=
max_pool2D
(
images
,
maxpoolshp
,
ignore_border
)
output_val
=
function
([
images
],
output
)(
imval
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
def
mp
(
input
):
return
max_pool2D
(
input
,
maxpoolshp
,
ignore_border
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool2D_3D
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
[(
1
,
2
)]
imval
=
rng
.
rand
(
2
,
3
,
4
)
images
=
tensor
.
dtensor3
()
for
maxpoolshp
in
maxpoolshps
:
for
maxpoolshp
in
maxpoolshps
:
for
border
in
[
True
,
False
]:
for
ignore_border
in
[
True
,
False
]:
print
'maxpoolshp'
,
maxpoolshp
,
'border'
,
border
print
'maxpoolshp ='
,
maxpoolshp
print
'ignore_border ='
,
ignore_border
numpy_output_val
=
self
.
numpy_max_pool2D
(
imval
,
maxpoolshp
,
ignore_border
)
output
=
max_pool2D
(
images
,
maxpoolshp
,
ignore_border
)
output_val
=
function
([
images
],
output
)(
imval
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
c
=
tensor
.
sum
(
output
)
c_val
=
function
([
images
],
c
)(
imval
)
g
=
tensor
.
grad
(
c
,
images
)
g_val
=
function
([
images
],
[
g
.
shape
,
tensor
.
min
(
tensor
.
min
(
tensor
.
min
(
g
))),
tensor
.
max
(
tensor
.
max
(
tensor
.
max
(
g
)))])(
imval
)
def
mp
(
input
):
def
mp
(
input
):
return
DownsampleFactorMax
(
maxpoolshp
,
ignore_border
=
border
)(
input
)
return
max_pool2D
(
input
,
maxpoolshp
,
ignore_border
)
utt
.
verify_grad
(
mp
,
[
imval
])
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
def
test_max_pool2D_6D
(
self
):
rng
=
numpy
.
random
.
RandomState
(
utt
.
fetch_seed
())
maxpoolshps
=
[(
3
,
2
)]
imval
=
rng
.
rand
(
2
,
1
,
1
,
1
,
3
,
4
)
images
=
tensor
.
TensorType
(
'float64'
,
[
False
]
*
6
)()
for
maxpoolshp
in
maxpoolshps
:
for
ignore_border
in
[
True
,
False
]:
print
'maxpoolshp ='
,
maxpoolshp
print
'ignore_border ='
,
ignore_border
numpy_output_val
=
self
.
numpy_max_pool2D
(
imval
,
maxpoolshp
,
ignore_border
)
output
=
max_pool2D
(
images
,
maxpoolshp
,
ignore_border
)
output_val
=
function
([
images
],
output
)(
imval
)
assert
numpy
.
all
(
output_val
==
numpy_output_val
)
def
mp
(
input
):
return
max_pool2D
(
input
,
maxpoolshp
,
ignore_border
)
utt
.
verify_grad
(
mp
,
[
imval
],
rng
=
rng
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
t
=
TestDownsampleFactorMax
(
"test_maxpool"
)
.
run
()
unittest
.
main
()
#t.test_maxpool()
from
theano.tests
import
main
# main("test_sp")
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论