Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
47c947ab
提交
47c947ab
authored
5月 27, 2014
作者:
Guillaume Alain
提交者:
Nicolas Ballas
8月 12, 2014
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Implemented the FFT 3d convolution with almost the code shared with the FFT 2d convolution.
上级
1e51644a
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
144 行增加
和
0 行删除
+144
-0
fftconv.py
theano/sandbox/cuda/fftconv.py
+144
-0
没有找到文件。
theano/sandbox/cuda/fftconv.py
浏览文件 @
47c947ab
...
...
@@ -509,3 +509,147 @@ def conv2d_fft(input, filters, image_shape=None, filter_shape=None,
# output should now be the result of a batched valid convolution
# of the input with the filters.
return
basic_ops
.
as_cuda_ndarray_variable
(
output
)
def
conv3d_fft
(
input
,
filters
,
image_shape
=
None
,
filter_shape
=
None
,
border_mode
=
'valid'
,
pad_last_dim
=
False
):
"""
Perform a convolution through fft.
Only support input which will be even on the last dimension
(width). All other dimensions can be anything and the filters can
have an even or odd width.
If you must use input which has an odd width, you can either pad
it or use the `pad_last_dim` argument which will do it for you and
take care to strip the padding before returning. Don't use this
argument if you are not sure the input is odd since the padding is
unconditional and will make even input odd, thus leading to
problems.
On valid mode the filters must be smaller than the input.
input: (b, ic, i0, i1, i2)
filters: (oc, ic, f0, f1, i2)
border_mode: 'valid' of 'full'
pad_last_dim: Unconditionally pad the last dimension of the input
to to turn it from odd to even. Will strip the
padding before returning the result.
"""
# use symbolic shapes to compute shape info at runtime if not specified
if
image_shape
is
None
:
image_shape
=
input
.
shape
if
filter_shape
is
None
:
filter_shape
=
filters
.
shape
# batch size, input channels, input dim 0, input dim 1
b
,
ic
,
i0
,
i1
,
i2
=
image_shape
# output channels, input channels, filter dim 0, filter dim 1
oc
,
ic_
,
f0
,
f1
,
f2
=
filter_shape
# pad filters/image to output shape
if
border_mode
==
'valid'
:
o0
=
i0
o1
=
i1
if
pad_last_dim
:
o2
=
i2
+
1
input_padded
=
T
.
zeros
((
b
,
ic
,
o0
,
o1
,
o2
),
dtype
=
'float32'
)
input_padded
=
T
.
set_subtensor
(
input_padded
[:,
:,
:
i0
,
:
i1
,
:
i2
],
input
)
else
:
o2
=
i2
input_padded
=
input
filters_padded
=
T
.
zeros
((
oc
,
ic
,
o0
,
o1
,
o2
),
dtype
=
'float32'
)
filters_padded
=
T
.
set_subtensor
(
filters_padded
[:,
:,
:
f0
,
:
f1
,
:
f2
],
filters
)
elif
border_mode
==
'full'
:
# In this particular case, the values of (o0, o1) represent
# the dimensions of the work buffer more than the actual dimensions
# of the desired output.
o0
=
i0
+
2
*
(
f0
-
1
)
o1
=
i1
+
2
*
(
f1
-
1
)
o2
=
i2
+
2
*
(
f2
-
1
)
if
pad_last_dim
:
o2
=
o2
+
1
# We line up the filters and the images in a way
# such that the filters are tightly placed against the
# top-left of the array, and the images intersect with
# them on one pixel. The top-left pixel of the images
# is the bottom-right pixel of the filters when we
# do the layout here.
filters_padded
=
T
.
zeros
((
oc
,
ic
,
o0
,
o1
,
o2
),
dtype
=
'float32'
)
filters_padded
=
T
.
set_subtensor
(
filters_padded
[:,
:,
:
f0
,
:
f1
,
:
f2
],
filters
)
input_padded
=
T
.
zeros
((
b
,
ic
,
o0
,
o1
,
o2
),
dtype
=
'float32'
)
input_padded
=
T
.
set_subtensor
(
input_padded
[:,
:,
(
f0
-
1
):(
f0
-
1
+
i0
),
(
f1
-
1
):(
f1
-
1
+
i1
),
(
f2
-
1
):(
f2
-
1
+
i2
)],
input
)
else
:
raise
ValueError
(
'invalid mode'
)
# reshape for FFT
input_flat
=
input_padded
.
reshape
((
b
*
ic
,
o0
,
o1
,
o2
))
filters_flat
=
filters_padded
.
reshape
((
oc
*
ic
,
o0
,
o1
,
o2
))
# perform FFT
input_fft_flat
=
cufft
(
input_flat
)
# (b * ic, o0, o1, o2//2 + 1, 2)
filters_fft_flat
=
cufft
(
filters_flat
)
# (oc * ic, o0, o1, o2//2 + 1, 2)
# Unfold ic dimension.
# We have to collapse two dimensions together
# in order to reuse the same `mult_and_reduce`.
# This explains the o0 * 01 instead of just keeping
# the two dimensions intact.
input_fft_v_shape
=
(
b
,
ic
,
o0
*
o1
,
o2
//
2
+
1
,
2
)
filters_fft_v_shape
=
(
oc
,
ic
,
o0
*
o1
,
o2
//
2
+
1
,
2
)
input_fft_v
=
input_fft_flat
.
reshape
(
input_fft_v_shape
)
filters_fft_v
=
filters_fft_flat
.
reshape
(
filters_fft_v_shape
)
# (b, oc, o0 * o1, o2//2 + 1, 2)
output_fft_s
=
mult_and_reduce
(
input_fft_v
,
filters_fft_v
,
input_shape
=
input_fft_v_shape
,
filter_shape
=
filters_fft_v_shape
)
# reshape for IFFT
output_fft_flat
=
output_fft_s
.
reshape
((
b
*
oc
,
o0
,
o1
,
o2
//
2
+
1
,
2
))
# perform IFFT
output_flat
=
cuifft
(
output_fft_flat
)
# (b * oc, o0, o1, o2)
# reshape
output_circ
=
output_flat
.
reshape
((
b
,
oc
,
o0
,
o1
,
o2
))
# circular!
# Now we extract the region of interest.
# We just cut it out from the output_circ
# array that was used for the computation.
# We do not need to handle pad_last_dim in a
# special way because we specify explicitly here
# how much values are expected.
if
border_mode
==
'valid'
:
output
=
output_circ
[:,
:,
(
f0
-
1
):(
f0
-
1
+
i0
-
f0
+
1
),
(
f1
-
1
):(
f1
-
1
+
i1
-
f1
+
1
),
(
f2
-
1
):(
f2
-
1
+
i2
-
f2
+
1
)]
elif
border_mode
==
'full'
:
output
=
output_circ
[:,
:,
(
f0
-
1
):(
f0
-
1
+
i0
+
f0
-
1
),
(
f1
-
1
):(
f1
-
1
+
i1
+
f1
-
1
),
(
f2
-
1
):(
f2
-
1
+
i2
+
f2
-
1
)]
else
:
raise
ValueError
(
'invalid mode'
)
# Rescale manually. This is just a factor that comes in during the
# trip through FFT and inverse FFT.
output
=
(
1.0
/
T
.
cast
(
o0
*
o1
*
o2
,
'float32'
))
*
output
# output should now be the result of a batched valid convolution
# of the input with the filters.
return
basic_ops
.
as_cuda_ndarray_variable
(
output
)
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论