Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
85501a81
提交
85501a81
authored
6月 10, 2017
作者:
Aleksandar Botev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Added and mode "full".
Added checks in the GPU code for invalid inputs.
上级
b8bee3c6
显示空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
201 行增加
和
24 行删除
+201
-24
neighbours.py
theano/gpuarray/neighbours.py
+55
-6
neighbours.py
theano/tensor/nnet/neighbours.py
+77
-13
test_neighbours.py
theano/tensor/nnet/tests/test_neighbours.py
+69
-5
没有找到文件。
theano/gpuarray/neighbours.py
浏览文件 @
85501a81
...
@@ -23,11 +23,11 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -23,11 +23,11 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
"""
"""
def
__init__
(
self
,
mode
=
'valid'
):
def
__init__
(
self
,
mode
=
'valid'
):
if
mode
not
in
[
'valid'
,
'
ignore_borders'
,
'wrap_centered'
,
'half'
]:
if
mode
not
in
[
'valid'
,
'
half'
,
'full'
,
raise
NotImplementedError
(
"Only the mode valid, ignore_borders"
'ignore_borders'
,
'wrap_centered'
]:
", wrap_centered and half
"
raise
NotImplementedError
(
"Only the mode valid, half, full,
"
"
have been implemented for the op
"
"
ignore_borders and wrap_centered have
"
" GpuImages2Neibs"
)
"
been implemented for
GpuImages2Neibs"
)
self
.
mode
=
mode
self
.
mode
=
mode
def
make_node
(
self
,
ten4
,
neib_shape
,
neib_step
=
None
):
def
make_node
(
self
,
ten4
,
neib_shape
,
neib_step
=
None
):
...
@@ -121,6 +121,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -121,6 +121,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
ten4_2 -= height;
ten4_2 -= height;
} else if ("
%(mode)
s"=="half"){
} else if ("
%(mode)
s"=="half"){
ten4_2 -= wrap_centered_half_idx_shift_x;
ten4_2 -= wrap_centered_half_idx_shift_x;
} else if ("
%(mode)
s"=="full"){
ten4_2 -= c - 1;
}
}
ga_int j = LID_0; // loop over d
ga_int j = LID_0; // loop over d
{
{
...
@@ -133,6 +135,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -133,6 +135,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
ten4_3 -= width;
ten4_3 -= width;
} else if ("
%(mode)
s"=="half"){
} else if ("
%(mode)
s"=="half"){
ten4_3 -= wrap_centered_half_idx_shift_y;
ten4_3 -= wrap_centered_half_idx_shift_y;
} else if ("
%(mode)
s"=="full"){
ten4_3 -= d - 1;
}
}
ga_int z_col = j + d * i;
ga_int z_col = j + d * i;
...
@@ -216,6 +220,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -216,6 +220,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
ten4_2 -= height;
ten4_2 -= height;
} else if ("
%(mode)
s"=="half"){
} else if ("
%(mode)
s"=="half"){
ten4_2 -= wrap_centered_half_idx_shift_x;
ten4_2 -= wrap_centered_half_idx_shift_x;
} else if ("
%(mode)
s"=="full"){
ten4_2 -= c - 1;
}
}
// 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)
...
@@ -229,6 +235,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -229,6 +235,8 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
ten4_3 -= width;
ten4_3 -= width;
} else if ("
%(mode)
s"=="half"){
} else if ("
%(mode)
s"=="half"){
ten4_3 -= wrap_centered_half_idx_shift_y;
ten4_3 -= wrap_centered_half_idx_shift_y;
} else if ("
%(mode)
s"=="full"){
ten4_3 -= d - 1;
}
}
ga_int z_col = j + d * i;
ga_int z_col = j + d * i;
...
@@ -330,6 +338,22 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -330,6 +338,22 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
const npy_intp step_y = (npy_intp) *(npy_
%(dtype_neib_step)
s*)
const npy_intp step_y = (npy_intp) *(npy_
%(dtype_neib_step)
s*)
PyArray_GETPTR1(
%(neib_step)
s, 1);
PyArray_GETPTR1(
%(neib_step)
s, 1);
if (step_x <=0 || step_y <=0)
{
PyErr_Format(PyExc_ValueError,
"neib_step wrong step ; values <= 0. Got
%%
lld
%%
lld.",
(long long) step_x, (long long) step_y);
%(fail)
s;
}
if (c <=0 || d <=0)
{
PyErr_Format(PyExc_ValueError,
"neib_shape values <= 0. Got
%%
lld
%%
lld.",
(long long)c, (long long)d);
%(fail)
s;
}
if ( "
%(mode)
s" == "wrap_centered") {
if ( "
%(mode)
s" == "wrap_centered") {
if (c
%%2
!=1 || d
%%2
!=1){
if (c
%%2
!=1 || d
%%2
!=1){
PyErr_Format(PyExc_TypeError,
PyErr_Format(PyExc_TypeError,
...
@@ -409,6 +433,31 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -409,6 +433,31 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
grid_c = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[2]-(c
%%2
))/step_x);
grid_c = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[2]-(c
%%2
))/step_x);
//number of patch in width
//number of patch in width
grid_d = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[3]-(d
%%2
))/step_y);
grid_d = 1+(((PyGpuArray_DIMS(
%(ten4)
s))[3]-(d
%%2
))/step_y);
}else if ( "
%(mode)
s" == "full") {
if ( ((PyGpuArray_DIMS(
%(ten4)
s))[2] < c) ||
( (((PyGpuArray_DIMS(
%(ten4)
s))[2]+c-2)
%%
step_x)!=0))
{
PyErr_Format(PyExc_TypeError,
"neib_shape[0]=
%%
ld, neib_step[0]=
%%
ld and"
" ten4.shape[2]=
%%
ld not consistent",
(long int)c, (long int)step_x,
(long int)(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,
"neib_shape[1]=
%%
ld, neib_step[1]=
%%
ld and"
" ten4.shape[3]=
%%
ld not consistent",
(long int)d, (long int)step_y,
(long int)(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'");
...
@@ -527,5 +576,5 @@ class GpuImages2Neibs(GpuKernelBase, Images2Neibs, Op):
...
@@ -527,5 +576,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'
,
'half
'
]:
if
op
.
mode
in
[
'valid'
,
'
half'
,
'full'
,
'ignore_borders'
,
'wrap_centered
'
]:
return
GpuImages2Neibs
(
op
.
mode
)
return
GpuImages2Neibs
(
op
.
mode
)
theano/tensor/nnet/neighbours.py
浏览文件 @
85501a81
...
@@ -24,24 +24,28 @@ class Images2Neibs(Op):
...
@@ -24,24 +24,28 @@ class Images2Neibs(Op):
- 'valid' :
- 'valid' :
Requires an input that is a multiple of the pooling factor
Requires an input that is a multiple of the pooling factor
(in each direction).
(in each direction).
- 'half' :
Equivalent to 'valid' if we pre-pad with zeros the input on
each side by (neib_shape[0]//2, neib_shape[1]//2)
- 'full' :
Equivalent to 'valid' if we pre-pad with zeros the input on
each side by (neib_shape[0] - 1, neib_shape[1] - 1)
- 'ignore_borders' :
- 'ignore_borders' :
Same as valid, but will ignore the borders if the shape(s)
Same as valid, but will ignore the borders if the shape(s)
of the input is not a multiple of the pooling factor(s).
of the input is not a multiple of the pooling factor(s).
- 'wrap_centered' :
- 'wrap_centered' :
?? TODO comment
?? TODO comment
- 'half' :
Equivalent to 'valid' if we pre-pad with zeros the input on
each side by (neib_shape[0]//2, neib_shape[1]//2)
"""
"""
__props__
=
(
"mode"
,)
__props__
=
(
"mode"
,)
def
__init__
(
self
,
mode
=
'valid'
):
def
__init__
(
self
,
mode
=
'valid'
):
if
mode
not
in
[
'valid'
,
'wrap_centered'
,
'ignore_borders'
,
'half'
]:
if
mode
not
in
[
'valid'
,
'half'
,
'full'
,
raise
NotImplementedError
(
"Only the mode valid, ignore_borders"
'wrap_centered'
,
'ignore_borders'
]:
",wrap_centered and half have been"
raise
NotImplementedError
(
"Only the mode valid, half, full, "
" implemented for the op Images2Neibs"
)
"ignore_borders and wrap_centered have "
"been implemented for Images2Neibs"
)
self
.
mode
=
mode
self
.
mode
=
mode
def
__str__
(
self
):
def
__str__
(
self
):
...
@@ -155,7 +159,7 @@ class Images2Neibs(Op):
...
@@ -155,7 +159,7 @@ class Images2Neibs(Op):
grad_undefined
(
self
,
2
,
neib_step
)]
grad_undefined
(
self
,
2
,
neib_step
)]
def
c_code_cache_version
(
self
):
def
c_code_cache_version
(
self
):
return
(
7
,)
return
(
8
,)
def
perform
(
self
,
node
,
inp
,
out_
):
def
perform
(
self
,
node
,
inp
,
out_
):
ten4
,
neib_shape
,
neib_step
=
inp
ten4
,
neib_shape
,
neib_step
=
inp
...
@@ -241,9 +245,28 @@ class Images2Neibs(Op):
...
@@ -241,9 +245,28 @@ class Images2Neibs(Op):
grid_c
=
1
+
((
ten4
.
shape
[
2
]
-
(
c
%
2
))
//
step_x
)
grid_c
=
1
+
((
ten4
.
shape
[
2
]
-
(
c
%
2
))
//
step_x
)
# number of patch in width
# number of patch in width
grid_d
=
1
+
((
ten4
.
shape
[
3
]
-
(
d
%
2
))
//
step_y
)
grid_d
=
1
+
((
ten4
.
shape
[
3
]
-
(
d
%
2
))
//
step_y
)
elif
mode
==
"full"
:
# This is equivalent to 'valid' with padding (c - 1, d - 1) on both sides
# Thus the expanded image will have size (h + 2 * (c - 1), w + 2 * (d - 1))
# Plugging these in the equation for 'valid' we get
# h + 2 * (c - 1) - c = h + c - 2
# w + 2 * (d - 1) - c = w + d - 2
if
(
ten4
.
shape
[
2
]
<
c
)
or
(((
ten4
.
shape
[
2
]
+
c
-
2
)
%
step_x
)
!=
0
):
raise
TypeError
(
"neib_shape[0]=
%
d, neib_step[0]=
%
d and"
" ten4.shape[2]=
%
d not consistent"
%
(
c
,
step_x
,
ten4
.
shape
[
2
]))
if
(
ten4
.
shape
[
3
]
<
d
)
or
(((
ten4
.
shape
[
3
]
+
d
-
2
)
%
step_y
)
!=
0
):
raise
TypeError
(
"neib_shape[1]=
%
d, neib_step[1]=
%
d and"
" ten4.shape[3]=
%
d not consistent"
%
(
d
,
step_y
,
ten4
.
shape
[
3
]))
# number of patch in height
grid_c
=
1
+
((
ten4
.
shape
[
2
]
+
c
-
2
)
//
step_x
)
# number of patch in width
grid_d
=
1
+
((
ten4
.
shape
[
3
]
+
d
-
2
)
//
step_y
)
else
:
else
:
raise
TypeError
(
"Images2Neibs: unknow mode '
%
s'"
%
mode
)
raise
TypeError
(
"Images2Neibs: unknow mode '
%
s'"
%
mode
)
z_dim0
=
grid_c
*
grid_d
*
ten4
.
shape
[
1
]
*
ten4
.
shape
[
0
]
z_dim0
=
grid_c
*
grid_d
*
ten4
.
shape
[
1
]
*
ten4
.
shape
[
0
]
z_dim1
=
c
*
d
z_dim1
=
c
*
d
z
[
0
]
=
np
.
empty
((
z_dim0
,
z_dim1
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
z
[
0
]
=
np
.
empty
((
z_dim0
,
z_dim1
),
dtype
=
node
.
outputs
[
0
]
.
dtype
)
...
@@ -272,6 +295,8 @@ class Images2Neibs(Op):
...
@@ -272,6 +295,8 @@ class Images2Neibs(Op):
ten4_2
-=
height
ten4_2
-=
height
elif
mode
==
"half"
:
elif
mode
==
"half"
:
ten4_2
-=
wrap_centered_half_idx_shift_x
ten4_2
-=
wrap_centered_half_idx_shift_x
elif
mode
==
"full"
:
ten4_2
-=
c
-
1
if
ten4_2
<
0
or
ten4_2
>=
height
:
if
ten4_2
<
0
or
ten4_2
>=
height
:
z
[
0
][
z_row
,
d
*
i
:
d
*
i
+
d
]
=
0
z
[
0
][
z_row
,
d
*
i
:
d
*
i
+
d
]
=
0
else
:
else
:
...
@@ -285,6 +310,8 @@ class Images2Neibs(Op):
...
@@ -285,6 +310,8 @@ class Images2Neibs(Op):
ten4_3
-=
width
ten4_3
-=
width
elif
mode
==
"half"
:
elif
mode
==
"half"
:
ten4_3
-=
wrap_centered_half_idx_shift_y
ten4_3
-=
wrap_centered_half_idx_shift_y
elif
mode
==
"full"
:
ten4_3
-=
d
-
1
z_col
=
j
+
d
*
i
z_col
=
j
+
d
*
i
if
ten4_3
<
0
or
ten4_3
>=
width
:
if
ten4_3
<
0
or
ten4_3
>=
width
:
z
[
0
][
z_row
,
z_col
]
=
0
z
[
0
][
z_row
,
z_col
]
=
0
...
@@ -307,6 +334,11 @@ class Images2Neibs(Op):
...
@@ -307,6 +334,11 @@ class Images2Neibs(Op):
elif
self
.
mode
==
'half'
:
elif
self
.
mode
==
'half'
:
grid_c
=
1
+
((
in_shape
[
2
]
-
(
c
%
2
))
//
step_x
)
grid_c
=
1
+
((
in_shape
[
2
]
-
(
c
%
2
))
//
step_x
)
grid_d
=
1
+
((
in_shape
[
3
]
-
(
d
%
2
))
//
step_y
)
grid_d
=
1
+
((
in_shape
[
3
]
-
(
d
%
2
))
//
step_y
)
elif
self
.
mode
==
'full'
:
grid_c
=
1
+
((
in_shape
[
2
]
+
c
-
2
)
//
step_x
)
grid_d
=
1
+
((
in_shape
[
3
]
+
d
-
2
)
//
step_y
)
else
:
raise
TypeError
(
"Images2Neibs: unknow mode '
%
s'"
%
self
.
mode
)
z_dim0
=
grid_c
*
grid_d
*
in_shape
[
1
]
*
in_shape
[
0
]
z_dim0
=
grid_c
*
grid_d
*
in_shape
[
1
]
*
in_shape
[
0
]
z_dim1
=
c
*
d
z_dim1
=
c
*
d
return
[(
z_dim0
,
z_dim1
)]
return
[(
z_dim0
,
z_dim1
)]
...
@@ -453,7 +485,32 @@ class Images2Neibs(Op):
...
@@ -453,7 +485,32 @@ class Images2Neibs(Op):
grid_c = 1+(((PyArray_DIMS(
%(ten4)
s))[2]-(c
%%2
))/step_x);
grid_c = 1+(((PyArray_DIMS(
%(ten4)
s))[2]-(c
%%2
))/step_x);
//number of patch in width
//number of patch in width
grid_d = 1+(((PyArray_DIMS(
%(ten4)
s))[3]-(d
%%2
))/step_y);
grid_d = 1+(((PyArray_DIMS(
%(ten4)
s))[3]-(d
%%2
))/step_y);
}else{
}else if ( "
%(mode)
s" == "full") {
if ( ((PyArray_DIMS(
%(ten4)
s))[2] < c) ||
( (((PyArray_DIMS(
%(ten4)
s))[2]+c-2)
%%
step_x)!=0))
{
PyErr_Format(PyExc_TypeError,
"neib_shape[0]=
%%
ld, neib_step[0]=
%%
ld and"
" ten4.shape[2]=
%%
ld not consistent",
(long int)c, (long int)step_x,
(long int)(PyArray_DIMS(
%(ten4)
s)[2]));
%(fail)
s;
}
if ( ((PyArray_DIMS(
%(ten4)
s))[3] < d) ||
( (((PyArray_DIMS(
%(ten4)
s))[3]+d-2)
%%
step_y)!=0))
{
PyErr_Format(PyExc_TypeError,
"neib_shape[1]=
%%
ld, neib_step[1]=
%%
ld and"
" ten4.shape[3]=
%%
ld not consistent",
(long int)d, (long int)step_y,
(long int)(PyArray_DIMS(
%(ten4)
s)[3]));
%(fail)
s;
}
//number of patch in height
grid_c = 1+(((PyArray_DIMS(
%(ten4)
s))[2]+c-2)/step_x);
//number of patch in width
grid_d = 1+(((PyArray_DIMS(
%(ten4)
s))[3]+d-2)/step_y);
}else {
PyErr_Format(PyExc_TypeError,
PyErr_Format(PyExc_TypeError,
"Images2Neibs: unknow mode '
%(mode)
s'");
"Images2Neibs: unknow mode '
%(mode)
s'");
%(fail)
s;
%(fail)
s;
...
@@ -521,6 +578,8 @@ class Images2Neibs(Op):
...
@@ -521,6 +578,8 @@ class Images2Neibs(Op):
else if (ten4_2 >= height) ten4_2 -= height;
else if (ten4_2 >= height) ten4_2 -= height;
} else if ( "
%(mode)
s" == "half" ){
} else if ( "
%(mode)
s" == "half" ){
ten4_2 -= wrap_centered_half_idx_shift_x;
ten4_2 -= wrap_centered_half_idx_shift_x;
} else if ( "
%(mode)
s" == "full" ){
ten4_2 -= c - 1;
}
}
if (ten4_2 < 0 | ten4_2 >= height) {
if (ten4_2 < 0 | ten4_2 >= height) {
dtype_
%(z)
s* curr_z = (dtype_
%(z)
s*) PyArray_GETPTR2(
%(z)
s, z_row, d * i);
dtype_
%(z)
s* curr_z = (dtype_
%(z)
s*) PyArray_GETPTR2(
%(z)
s, z_row, d * i);
...
@@ -535,6 +594,8 @@ class Images2Neibs(Op):
...
@@ -535,6 +594,8 @@ class Images2Neibs(Op):
else if (ten4_3 >= width) ten4_3 -= width;
else if (ten4_3 >= width) ten4_3 -= width;
} else if ( "
%(mode)
s" == "half" ){
} else if ( "
%(mode)
s" == "half" ){
ten4_3 -= wrap_centered_half_idx_shift_y;
ten4_3 -= wrap_centered_half_idx_shift_y;
} else if ( "
%(mode)
s" == "full" ){
ten4_3 -= d - 1;
}
}
int z_col = j + d * i;
int z_col = j + d * i;
dtype_
%(z)
s* curr_z = (dtype_
%(z)
s*) PyArray_GETPTR2(
%(z)
s, z_row, z_col);
dtype_
%(z)
s* curr_z = (dtype_
%(z)
s*) PyArray_GETPTR2(
%(z)
s, z_row, z_col);
...
@@ -583,14 +644,17 @@ def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'):
...
@@ -583,14 +644,17 @@ def images2neibs(ten4, neib_shape, neib_step=None, mode='valid'):
``valid``
``valid``
Requires an input that is a multiple of the
Requires an input that is a multiple of the
pooling factor (in each direction).
pooling factor (in each direction).
``half``
Equivalent to 'valid' if we pre-pad with zeros the input on
each side by (neib_shape[0]//2, neib_shape[1]//2)
``full``
Equivalent to 'valid' if we pre-pad with zeros the input on
each side by (neib_shape[0] - 1, neib_shape[1] - 1)
``ignore_borders``
``ignore_borders``
Same as valid, but will ignore the borders if the shape(s) of
Same as valid, but will ignore the borders if the shape(s) of
the input is not a multiple of the pooling factor(s).
the input is not a multiple of the pooling factor(s).
``wrap_centered``
``wrap_centered``
?? TODO comment
?? TODO comment
``half``
Equivalent to 'valid' if we pre-pad with zeros the input on
each side by (neib_shape[0]//2, neib_shape[1]//2)
Returns
Returns
-------
-------
...
...
theano/tensor/nnet/tests/test_neighbours.py
浏览文件 @
85501a81
...
@@ -261,6 +261,31 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -261,6 +261,31 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
f
=
theano
.
function
([],
close
,
mode
=
self
.
mode
)
f
=
theano
.
function
([],
close
,
mode
=
self
.
mode
)
assert
f
()
assert
f
()
def
test_neibs_full_step_by_valid
(
self
):
for
shp_idx
,
(
shape
,
neib_step
)
in
enumerate
([
[(
7
,
8
,
11
,
11
),
(
1
,
1
)],
[(
7
,
8
,
11
,
11
),
(
2
,
2
)],
[(
7
,
8
,
11
,
11
),
(
3
,
3
)],
[(
7
,
8
,
11
,
11
),
(
1
,
3
)],
[(
7
,
8
,
11
,
11
),
(
3
,
1
)],
[(
80
,
90
,
11
,
11
),
(
1
,
2
)],
[(
1025
,
9
,
11
,
11
),
(
2
,
1
)],
[(
1
,
1
,
11
,
1037
),
(
2
,
3
)],
[(
1
,
1
,
1043
,
11
),
(
3
,
2
)]]
):
for
neib_shape
in
[(
3
,
3
),
(
3
,
9
),
(
9
,
3
)]:
for
dtype
in
self
.
dtypes
:
x
=
theano
.
shared
(
np
.
random
.
randn
(
*
shape
)
.
astype
(
dtype
))
extra
=
(
neib_shape
[
0
]
-
1
,
neib_shape
[
1
]
-
1
)
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_full
=
images2neibs
(
x
,
neib_shape
,
neib_step
,
mode
=
"full"
)
close
=
T
.
allclose
(
x_using_valid
,
x_using_full
)
f
=
theano
.
function
([],
close
,
mode
=
self
.
mode
)
assert
f
()
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
)
...
@@ -317,6 +342,17 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -317,6 +342,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_full
(
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
=
'full'
)
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'
)
...
@@ -382,6 +418,17 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -382,6 +418,17 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
mode
=
self
.
mode
)
mode
=
self
.
mode
)
self
.
assertRaises
(
TypeError
,
f
,
images_val
)
self
.
assertRaises
(
TypeError
,
f
,
images_val
)
def
test_neibs_full_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
=
'full'
)),
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
...
@@ -408,11 +455,6 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -408,11 +455,6 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
(
1
,
1
,
3
,
320
,
320
))
(
1
,
1
,
3
,
320
,
320
))
# End up with a step of 0
# End up with a step of 0
# This can lead to division by zero in DebugMode
# This can lead to division by zero in DebugMode
# This can not be ran on the GPU since from the C code we get
# no ZeroDivisionError, but rather the whole processes crashes
# with floating point exception.
if
"gpu"
not
in
self
.
mode
.
provided_optimizer
.
include
and
\
"gpuarray"
not
in
self
.
mode
.
provided_optimizer
.
include
:
self
.
assertRaises
((
ValueError
,
ZeroDivisionError
),
f
,
neibs
,
self
.
assertRaises
((
ValueError
,
ZeroDivisionError
),
f
,
neibs
,
(
3
,
320
,
320
,
1
))
(
3
,
320
,
320
,
1
))
...
@@ -454,6 +496,19 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -454,6 +496,19 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for
i
in
range
(
1000
):
for
i
in
range
(
1000
):
f
()
f
()
def
speed_neibs_full
(
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
=
"full"
),
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'
)
...
@@ -502,6 +557,15 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
...
@@ -502,6 +557,15 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
self
.
_compile_and_check
(
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'half'
)],
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'half'
)],
[
images
],
Images2Neibs
)
[
images
],
Images2Neibs
)
shape
=
(
100
,
40
,
6
,
5
)
images
=
np
.
ones
(
shape
)
.
astype
(
'float32'
)
x
=
T
.
ftensor4
()
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
1
),
mode
=
'full'
)],
[
images
],
Images2Neibs
)
self
.
_compile_and_check
(
[
x
],
[
images2neibs
(
x
,
neib_shape
=
(
2
,
3
),
mode
=
'full'
)],
[
images
],
Images2Neibs
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论