Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
bae4eb91
提交
bae4eb91
authored
5月 12, 2017
作者:
Aleksandar Botev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added mode 'half' to Images2Neibs. Tests pass. #5938
上级
22eaec56
全部展开
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
125 行增加
和
23 行删除
+125
-23
neighbours.py
theano/gpuarray/neighbours.py
+58
-19
neighbours.py
theano/tensor/nnet/neighbours.py
+0
-0
test_neighbours.py
theano/tensor/nnet/tests/test_neighbours.py
+67
-4
没有找到文件。
theano/gpuarray/neighbours.py
浏览文件 @
bae4eb91
...
@@ -23,9 +23,9 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -23,9 +23,9 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
"""
"""
def
__init__
(
self
,
mode
=
'valid'
):
def
__init__
(
self
,
mode
=
'valid'
):
if
mode
not
in
[
'valid'
,
'ignore_borders'
,
'wrap_centered'
]:
if
mode
not
in
[
'valid'
,
'ignore_borders'
,
'wrap_centered'
,
'half'
]:
raise
NotImplementedError
(
"Only the mode valid, ignore_borders"
raise
NotImplementedError
(
"Only the mode valid, ignore_borders"
"
and wrap_centered
"
"
, wrap_centered and half
"
" have been implemented for the op"
" have been implemented for the op"
" GpuImages2Neibs"
)
" GpuImages2Neibs"
)
self
.
mode
=
mode
self
.
mode
=
mode
...
@@ -85,8 +85,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -85,8 +85,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
GLOBAL_MEM
%(type_z)
s * global_out, const ga_size offset_out
GLOBAL_MEM
%(type_z)
s * global_out, const ga_size offset_out
)
)
{
{
const ga_int wrap_centered_idx_shift_x = c/2;
const ga_int wrap_centered_
half_
idx_shift_x = c/2;
const ga_int wrap_centered_idx_shift_y = d/2;
const ga_int wrap_centered_
half_
idx_shift_y = d/2;
global_ten4 = (GLOBAL_MEM const
%(type_ten4)
s *)(((GLOBAL_MEM char *)global_ten4)+offset_ten4);
global_ten4 = (GLOBAL_MEM const
%(type_ten4)
s *)(((GLOBAL_MEM char *)global_ten4)+offset_ten4);
global_out = (GLOBAL_MEM
%(type_z)
s *)(((GLOBAL_MEM char *)global_out)+offset_out);
global_out = (GLOBAL_MEM
%(type_z)
s *)(((GLOBAL_MEM char *)global_out)+offset_out);
...
@@ -111,34 +111,41 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -111,34 +111,41 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
{
{
ga_int ten4_2 = i + a * step_x;
ga_int ten4_2 = i + a * step_x;
if("
%(mode)
s"=="wrap_centered"){
if("
%(mode)
s"=="wrap_centered"){
ten4_2 -= wrap_centered_idx_shift_x;
ten4_2 -= wrap_centered_
half_
idx_shift_x;
if ( ten4_2 < 0 )
if ( ten4_2 < 0 )
ten4_2 += height;
ten4_2 += height;
else if (ten4_2 >= height)
else if (ten4_2 >= height)
ten4_2 -= height;
ten4_2 -= height;
} else if ("
%(mode)
s"=="half"){
ten4_2 -= wrap_centered_half_idx_shift_x;
}
}
ga_int j = LID_0; // loop over d
ga_int j = LID_0; // loop over d
{
{
ga_int ten4_3 = j + b * step_y;
ga_int ten4_3 = j + b * step_y;
if("
%(mode)
s"=="wrap_centered"){
if("
%(mode)
s"=="wrap_centered"){
ten4_3 -= wrap_centered_idx_shift_y;
ten4_3 -= wrap_centered_
half_
idx_shift_y;
if ( ten4_3 < 0 )
if ( ten4_3 < 0 )
ten4_3 += width;
ten4_3 += width;
else if (ten4_3 >= width)
else if (ten4_3 >= width)
ten4_3 -= width;
ten4_3 -= width;
} else if ("
%(mode)
s"=="half"){
ten4_3 -= wrap_centered_half_idx_shift_y;
}
}
ga_int ten4_idx = stride3*ten4_3 +
stride2*ten4_2 +
stride1*s + stride0*n;
ga_int z_col = j + d * i;
ga_int z_col = j + d * i;
ga_int z_idx = z_col * out_s1 +
ga_int z_idx = z_col * out_s1 +
z_row * out_s0;
z_row * out_s0;
if(ten4_2 < 0 || ten4_2 >= height || ten4_3 < 0 || ten4_3 >= width){
global_ot[z_idx] = 0;
} else {
ga_int ten4_idx = stride3*ten4_3 +
stride2*ten4_2 +
stride1*s + stride0*n;
global_out[z_idx] = global_ten4[ten4_idx];
global_out[z_idx] = global_ten4[ten4_idx];
}
}
}
}
}
}
}
}"""
%
locals
()
}"""
%
locals
()
params
=
[
params
=
[
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
...
@@ -172,8 +179,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -172,8 +179,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
GLOBAL_MEM
%(type_z)
s * global_out, const ga_size offset_out
GLOBAL_MEM
%(type_z)
s * global_out, const ga_size offset_out
)
)
{
{
const ga_int wrap_centered_idx_shift_x = c/2;
const ga_int wrap_centered_
half_
idx_shift_x = c/2;
const ga_int wrap_centered_idx_shift_y = d/2;
const ga_int wrap_centered_
half_
idx_shift_y = d/2;
global_ten4 = (GLOBAL_MEM const
%(type_ten4)
s *)(((GLOBAL_MEM char *)global_ten4)+offset_ten4);
global_ten4 = (GLOBAL_MEM const
%(type_ten4)
s *)(((GLOBAL_MEM char *)global_ten4)+offset_ten4);
global_out = (GLOBAL_MEM
%(type_z)
s *)(((GLOBAL_MEM char *)global_out)+offset_out);
global_out = (GLOBAL_MEM
%(type_z)
s *)(((GLOBAL_MEM char *)global_out)+offset_out);
...
@@ -199,36 +206,43 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -199,36 +206,43 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
{
{
ga_int ten4_2 = i + a * step_x;
ga_int ten4_2 = i + a * step_x;
if("
%(mode)
s"=="wrap_centered"){
if("
%(mode)
s"=="wrap_centered"){
ten4_2 -= wrap_centered_idx_shift_x;
ten4_2 -= wrap_centered_
half_
idx_shift_x;
if ( ten4_2 < 0 )
if ( ten4_2 < 0 )
ten4_2 += height;
ten4_2 += height;
else if (ten4_2 >= height)
else if (ten4_2 >= height)
ten4_2 -= height;
ten4_2 -= height;
} else if ("
%(mode)
s"=="half"){
ten4_2 -= wrap_centered_half_idx_shift_x;
}
}
// loop over d
// loop over d
for (ga_int j = LID_0; j < d; j+=LDIM_0)
for (ga_int j = LID_0; j < d; j+=LDIM_0)
{
{
ga_int ten4_3 = j + b * step_y;
ga_int ten4_3 = j + b * step_y;
if("
%(mode)
s"=="wrap_centered"){
if("
%(mode)
s"=="wrap_centered"){
ten4_3 -= wrap_centered_idx_shift_y;
ten4_3 -= wrap_centered_
half_
idx_shift_y;
if ( ten4_3 < 0 )
if ( ten4_3 < 0 )
ten4_3 += width;
ten4_3 += width;
else if (ten4_3 >= width)
else if (ten4_3 >= width)
ten4_3 -= width;
ten4_3 -= width;
} else if ("
%(mode)
s"=="half"){
ten4_3 -= wrap_centered_half_idx_shift_y;
}
}
ga_int ten4_idx = stride3*ten4_3 +
stride2*ten4_2 +
stride1*s + stride0*n;
ga_int z_col = j + d * i;
ga_int z_col = j + d * i;
ga_int z_idx = z_col * out_s1 +
ga_int z_idx = z_col * out_s1 +
z_row * out_s0;
z_row * out_s0;
if(ten4_2 < 0 || ten4_2 >= height || ten4_3 < 0 || ten4_3 >= width){
global_ot[z_idx] = 0;
} else {
ga_int ten4_idx = stride3*ten4_3 +
stride2*ten4_2 +
stride1*s + stride0*n;
global_out[z_idx] = global_ten4[ten4_idx];
global_out[z_idx] = global_ten4[ten4_idx];
}
}
}
}
}
}
}
}
}
"""
%
locals
()
"""
%
locals
()
params
=
[
params
=
[
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
'intc'
,
...
@@ -367,6 +381,31 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -367,6 +381,31 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
grid_c = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[2]-c)/step_x);
grid_c = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[2]-c)/step_x);
//number of patch in width
//number of patch in width
grid_d = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[3]-d)/step_y);
grid_d = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[3]-d)/step_y);
}else if ( "
%(mode)
s" == "half") {
if ( ((PyGpuArray_DIMS(
%(ten4)
s))[2] < c) ||
((((PyGpuArray_DIMS(
%(ten4)
s))[2]-(c
%%2
))
%%
step_x)!=0))
{
PyErr_Format(PyExc_TypeError, "GpuImages2Neibs:"
" neib_shape[0]=
%%
d, neib_step[0]=
%%
d and"
" ten4.shape[2]=
%%
d not consistent",
c, step_x,
PyGpuArray_DIMS(
%(ten4)
s)[2]);
%(fail)
s;
}
if ( ((PyGpuArray_DIMS(
%(ten4)
s))[3] < d) ||
((((PyGpuArray_DIMS(
%(ten4)
s))[3]-(d
%%2
))
%%
step_y)!=0))
{
PyErr_Format(PyExc_TypeError, "GpuImages2Neibs:"
" neib_shape[1]=
%%
d, neib_step[1]=
%%
d and"
" ten4.shape[3]=
%%
d not consistent",
d, step_y,
PyGpuArray_DIMS(
%(ten4)
s)[3]);
%(fail)
s;
}
//number of patch in height
grid_c = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[2]-(c
%%2
))/step_x);
//number of patch in width
grid_d = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[3]-(d
%%2
))/step_y);
}else{
}else{
PyErr_Format(PyExc_TypeError,
PyErr_Format(PyExc_TypeError,
"GpuImages2Neibs:: unknown mode '
%(mode)
s'");
"GpuImages2Neibs:: unknown mode '
%(mode)
s'");
...
@@ -485,5 +524,5 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -485,5 +524,5 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
@op_lifter
([
Images2Neibs
])
@op_lifter
([
Images2Neibs
])
@register_opt2
([
Images2Neibs
],
'fast_compile'
)
@register_opt2
([
Images2Neibs
],
'fast_compile'
)
def
local_gpua_images2neibs
(
op
,
context_name
,
inputs
,
outputs
):
def
local_gpua_images2neibs
(
op
,
context_name
,
inputs
,
outputs
):
if
op
.
mode
in
[
'valid'
,
'ignore_borders'
,
'wrap_centered'
]:
if
op
.
mode
in
[
'valid'
,
'ignore_borders'
,
'wrap_centered'
,
'half'
]:
return
GpuImages2Neibs
(
op
.
mode
)
return
GpuImages2Neibs
(
op
.
mode
)
theano/tensor/nnet/neighbours.py
浏览文件 @
bae4eb91
差异被折叠。
点击展开。
theano/tensor/nnet/tests/test_neighbours.py
浏览文件 @
bae4eb91
...
@@ -236,6 +236,29 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -236,6 +236,29 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
# TODO: why this is commented?
# TODO: why this is commented?
# assert numpy.allclose(images.get_value(borrow=True), g())
# assert numpy.allclose(images.get_value(borrow=True), g())
def
test_neibs_half_step_by_valid
(
self
):
for
shp_idx
,
(
shape
,
neib_shape
,
neib_step
)
in
enumerate
([
[(
7
,
8
,
5
,
5
),
(
3
,
3
),
(
1
,
1
)],
[(
7
,
8
,
5
,
5
),
(
3
,
3
),
(
2
,
2
)],
[(
7
,
8
,
5
,
5
),
(
3
,
3
),
(
4
,
4
)],
[(
7
,
8
,
5
,
5
),
(
3
,
3
),
(
1
,
4
)],
[(
7
,
8
,
5
,
5
),
(
3
,
3
),
(
4
,
1
)],
[(
80
,
90
,
5
,
5
),
(
3
,
3
),
(
1
,
2
)],
[(
1025
,
9
,
5
,
5
),
(
3
,
3
),
(
2
,
1
)],
[(
1
,
1
,
5
,
1037
),
(
3
,
3
),
(
2
,
4
)],
[(
1
,
1
,
1045
,
5
),
(
3
,
3
),
(
4
,
2
)]]
):
for
dtype
in
self
.
dtypes
:
x
=
theano
.
shared
(
np
.
random
.
randn
(
*
shape
)
.
astype
(
dtype
))
extra
=
(
neib_shape
[
0
]
//
2
,
neib_shape
[
1
]
//
2
)
padded_shape
=
(
x
.
shape
[
0
],
x
.
shape
[
1
],
x
.
shape
[
2
]
+
2
*
extra
[
0
],
x
.
shape
[
3
]
+
2
*
extra
[
1
])
padded_x
=
T
.
zeros
(
padded_shape
)
padded_x
=
T
.
set_subtensor
(
padded_x
[:,
:,
extra
[
0
]:
-
extra
[
0
],
extra
[
1
]:
-
extra
[
1
]],
x
)
x_using_valid
=
images2neibs
(
padded_x
,
neib_shape
,
neib_step
,
mode
=
"valid"
)
x_using_half
=
images2neibs
(
x
,
neib_shape
,
neib_step
,
mode
=
"half"
)
close
=
T
.
allclose
(
x_using_valid
,
x_using_half
)
assert
close
.
eval
()
def
test_neibs_bad_shape_wrap_centered
(
self
):
def
test_neibs_bad_shape_wrap_centered
(
self
):
shape
=
(
2
,
3
,
10
,
10
)
shape
=
(
2
,
3
,
10
,
10
)
...
@@ -281,6 +304,17 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -281,6 +304,17 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
self
.
assertRaises
(
TypeError
,
unittest_tools
.
verify_grad
,
self
.
assertRaises
(
TypeError
,
unittest_tools
.
verify_grad
,
fn
,
[
images_val
],
mode
=
self
.
mode
)
fn
,
[
images_val
],
mode
=
self
.
mode
)
def
test_grad_half
(
self
):
# It is not implemented for now. So test that we raise an error.
shape
=
(
2
,
3
,
6
,
6
)
images_val
=
np
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
def
fn
(
images
):
return
images2neibs
(
images
,
(
3
,
3
),
mode
=
'half'
)
self
.
assertRaises
(
TypeError
,
unittest_tools
.
verify_grad
,
fn
,
[
images_val
],
mode
=
self
.
mode
)
def
test_grad_valid
(
self
):
def
test_grad_valid
(
self
):
shape
=
(
2
,
3
,
6
,
6
)
shape
=
(
2
,
3
,
6
,
6
)
images_val
=
np
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
images_val
=
np
.
random
.
rand
(
*
shape
)
.
astype
(
'float32'
)
...
@@ -330,15 +364,22 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -330,15 +364,22 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
images_val
=
np
.
arange
(
np
.
prod
(
shape
),
images_val
=
np
.
arange
(
np
.
prod
(
shape
),
dtype
=
'float32'
)
.
reshape
(
shape
)
dtype
=
'float32'
)
.
reshape
(
shape
)
def
fn
(
images
):
return
T
.
sum
(
T
.
sqr
(
images2neibs
(
images
,
(
2
,
2
),
mode
=
'valid'
)),
axis
=
[
0
,
1
])
f
=
theano
.
function
([
images
],
f
=
theano
.
function
([
images
],
T
.
sqr
(
images2neibs
(
images
,
(
2
,
2
),
mode
=
'valid'
)),
T
.
sqr
(
images2neibs
(
images
,
(
2
,
2
),
mode
=
'valid'
)),
mode
=
self
.
mode
)
mode
=
self
.
mode
)
self
.
assertRaises
(
TypeError
,
f
,
images_val
)
self
.
assertRaises
(
TypeError
,
f
,
images_val
)
def
test_neibs_half_with_inconsistent_borders
(
self
):
shape
=
(
2
,
3
,
5
,
5
)
images
=
T
.
dtensor4
()
images_val
=
np
.
arange
(
np
.
prod
(
shape
),
dtype
=
'float32'
)
.
reshape
(
shape
)
f
=
theano
.
function
([
images
],
T
.
sqr
(
images2neibs
(
images
,
(
2
,
2
),
mode
=
'half'
)),
mode
=
self
.
mode
)
self
.
assertRaises
(
TypeError
,
f
,
images_val
)
def
test_can_not_infer_nb_dim
(
self
):
def
test_can_not_infer_nb_dim
(
self
):
# Was reported in gh-5613. Test that we do not crash
# Was reported in gh-5613. Test that we do not crash
# or that we crash in a few other case found while
# or that we crash in a few other case found while
...
@@ -392,6 +433,19 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -392,6 +433,19 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for
i
in
range
(
1000
):
for
i
in
range
(
1000
):
f
()
f
()
def
speed_neibs_half
(
self
):
shape
=
(
100
,
40
,
18
,
18
)
images
=
shared
(
np
.
arange
(
np
.
prod
(
shape
),
dtype
=
'float32'
)
.
reshape
(
shape
))
neib_shape
=
T
.
as_tensor_variable
((
3
,
3
))
f
=
function
([],
images2neibs
(
images
,
neib_shape
,
mode
=
"half"
),
mode
=
self
.
mode
)
for
i
in
range
(
1000
):
f
()
def
test_infer_shape
(
self
):
def
test_infer_shape
(
self
):
shape
=
(
100
,
40
,
6
,
3
)
shape
=
(
100
,
40
,
6
,
3
)
images
=
np
.
ones
(
shape
)
.
astype
(
'float32'
)
images
=
np
.
ones
(
shape
)
.
astype
(
'float32'
)
...
@@ -431,6 +485,15 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -431,6 +485,15 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
[
x
],
[
images2neibs
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
3
,
3
),
mode
=
'wrap_centered'
)],
x
,
neib_shape
=
(
3
,
3
),
mode
=
'wrap_centered'
)],
[
images
],
Images2Neibs
)
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
6
,
4
)
images
=
np
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'half'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'half'
)],
[
images
],
Images2Neibs
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论