Skip to content

Commit

Permalink
Add dilation support
Browse files Browse the repository at this point in the history
  • Loading branch information
John Tapsell committed Oct 7, 2016
1 parent 06d90ec commit c595e98
Show file tree
Hide file tree
Showing 91 changed files with 64 additions and 43 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ A technical report on convolution arithmetic in the context of deep learning.
<td>Padding, strides, transposed (odd)</td>
<td></td>
</tr>
<tr>
<td><img src="gif/dilation.gif"></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td>No padding, no stride, dilation</td>
<td></td>
<td></td>
<td></td>
</tr>
</table>

## Generating the Makefile
Expand Down
42 changes: 23 additions & 19 deletions bin/generate_makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,45 +8,47 @@ numerical_files = 'bin/produce_figure templates/numerical_figure.txt'

animations = (
('no_padding_no_strides',
('arithmetic', 4, 2, 0, 3, 1, 'convolution', False)),
('arithmetic', 4, 2, 0, 3, 1, 1, 'convolution', False)),
('no_padding_no_strides_transposed',
('arithmetic', 4, 2, 0, 3, 1, 'convolution', True)),
('arithmetic', 4, 2, 0, 3, 1, 1, 'convolution', True)),
('arbitrary_padding_no_strides',
('arithmetic', 5, 6, 2, 4, 1, 'convolution', False)),
('arithmetic', 5, 6, 2, 4, 1, 1, 'convolution', False)),
('arbitrary_padding_no_strides_transposed',
('arithmetic', 5, 6, 2, 4, 1, 'convolution', True)),
('arithmetic', 5, 6, 2, 4, 1, 1, 'convolution', True)),
('same_padding_no_strides',
('arithmetic', 5, 5, 1, 3, 1, 'convolution', False)),
('arithmetic', 5, 5, 1, 3, 1, 1, 'convolution', False)),
('same_padding_no_strides_transposed',
('arithmetic', 5, 5, 1, 3, 1, 'convolution', True)),
('arithmetic', 5, 5, 1, 3, 1, 1, 'convolution', True)),
('full_padding_no_strides',
('arithmetic', 5, 7, 2, 3, 1, 'convolution', False)),
('arithmetic', 5, 7, 2, 3, 1, 1, 'convolution', False)),
('full_padding_no_strides_transposed',
('arithmetic', 5, 7, 2, 3, 1, 'convolution', True)),
('arithmetic', 5, 7, 2, 3, 1, 1, 'convolution', True)),
('no_padding_strides',
('arithmetic', 5, 2, 0, 3, 2, 'convolution', False)),
('arithmetic', 5, 2, 0, 3, 2, 1, 'convolution', False)),
('no_padding_strides_transposed',
('arithmetic', 5, 2, 0, 3, 2, 'convolution', True)),
('arithmetic', 5, 2, 0, 3, 2, 1, 'convolution', True)),
('padding_strides',
('arithmetic', 5, 3, 1, 3, 2, 'convolution', False)),
('arithmetic', 5, 3, 1, 3, 2, 1, 'convolution', False)),
('padding_strides_transposed',
('arithmetic', 5, 3, 1, 3, 2, 'convolution', True)),
('arithmetic', 5, 3, 1, 3, 2, 1, 'convolution', True)),
('padding_strides_odd',
('arithmetic', 6, 3, 1, 3, 2, 'convolution', False)),
('arithmetic', 6, 3, 1, 3, 2, 1, 'convolution', False)),
('padding_strides_odd_transposed',
('arithmetic', 6, 3, 1, 3, 2, 'convolution', True)),
('arithmetic', 6, 3, 1, 3, 2, 1, 'convolution', True)),
('dilation',
('arithmetic', 7, 3, 0, 3, 1, 2, 'convolution', False)),
('numerical_no_padding_no_strides',
('numerical', 5, 3, 0, 3, 1, 'convolution', False)),
('numerical', 5, 3, 0, 3, 1, 1, 'convolution', False)),
('numerical_padding_strides',
('numerical', 5, 3, 1, 3, 2, 'convolution', False)),
('numerical', 5, 3, 1, 3, 2, 1, 'convolution', False)),
('numerical_average_pooling',
('numerical', 5, 3, 0, 3, 1, 'average', False)),
('numerical', 5, 3, 0, 3, 1, 1, 'average', False)),
('numerical_max_pooling',
('numerical', 5, 3, 0, 3, 1, 'max', False)),
('numerical', 5, 3, 0, 3, 1, 1, 'max', False)),
)

fields = ('type', 'input-size', 'output-size', 'padding', 'kernel-size',
'stride', 'mode', 'transposed')
'stride', 'dilation', 'mode', 'transposed')
animations = dict([(name, dict(zip(fields, config)))
for name, config in animations])

Expand Down Expand Up @@ -115,6 +117,8 @@ def make_pdf_section():
'--padding={} '.format(config['padding']) +
'--kernel-size={} '.format(config['kernel-size']) +
'--stride={} '.format(config['stride']) +
('--dilation={} '.format(config['dilation'])
if config['dilation'] != 1 else '' ) +
('--mode={} '.format(config['mode'])
if config['type'] == 'numerical' else '') +
('--transposed\n' if config['transposed'] else '\n'))
Expand Down
32 changes: 16 additions & 16 deletions bin/produce_figure
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ numpy.random.seed(1234)


def make_numerical_tex_string(step, input_size, output_size, padding,
kernel_size, stride, mode):
kernel_size, stride, dilation, mode):
"""Creates a LaTeX string for a numerical convolution animation.
Parameters
Expand Down Expand Up @@ -42,6 +42,8 @@ def make_numerical_tex_string(step, input_size, output_size, padding,
if mode not in ('convolution', 'average', 'max'):
raise ValueError("wrong convolution mode, choices are 'convolution', "
"'average' or 'max'")
if dilation != 1:
raise ValueError("Only a dilation of 1 is currently supported for numerical output")
max_steps = output_size ** 2
if step >= max_steps:
raise ValueError('step {} out of bounds (there are '.format(step) +
Expand Down Expand Up @@ -116,7 +118,7 @@ def make_numerical_tex_string(step, input_size, output_size, padding,


def make_arithmetic_tex_string(step, input_size, output_size, padding,
kernel_size, stride, transposed):
kernel_size, stride, dilation, transposed):
"""Creates a LaTeX string for a convolution arithmetic animation.
Parameters
Expand All @@ -133,6 +135,8 @@ def make_arithmetic_tex_string(step, input_size, output_size, padding,
Convolution kernel size.
stride : int
Convolution stride.
dilation: int
Input Dilation
transposed : bool
If ``True``, generate strings for the transposed convolution
animation.
Expand All @@ -144,6 +148,7 @@ def make_arithmetic_tex_string(step, input_size, output_size, padding,
animation.
"""
kernel_size = (kernel_size - 1)*dilation + 1
if transposed:
input_size, output_size, padding, spacing, stride = (
output_size, input_size, kernel_size - 1 - padding, stride, 1)
Expand Down Expand Up @@ -177,22 +182,15 @@ def make_arithmetic_tex_string(step, input_size, output_size, padding,
padding + spacing * j + 1)
for i, j in itertools.product(range(input_size),
range(input_size))),
'INPUT_GRID_FROM': '{},{}'.format(
stride * offset_x, y_adjustment + stride * offset_y),
'INPUT_GRID_TO': '{},{}'.format(
stride * offset_x + kernel_size,
y_adjustment + stride * offset_y + kernel_size),
'INPUT_BOTTOM_LEFT': '{},{}'.format(
stride * offset_x, y_adjustment + stride * offset_y),
'INPUT_BOTTOM_RIGHT': '{},{}'.format(
stride * offset_x + kernel_size,
'INPUT_GRID_FROM_X': '{}'.format(
stride * offset_x),
'INPUT_GRID_FROM_Y': '{}'.format(
y_adjustment + stride * offset_y),
'INPUT_TOP_LEFT': '{},{}'.format(
stride * offset_x,
y_adjustment + stride * offset_y + kernel_size),
'INPUT_TOP_RIGHT': '{},{}'.format(
stride * offset_x + kernel_size,
'INPUT_GRID_TO_X': '{}'.format(
stride * offset_x + kernel_size),
'INPUT_GRID_TO_Y': '{}'.format(
y_adjustment + stride * offset_y + kernel_size),
'DILATION': '{}'.format(dilation),
'OUTPUT_BOTTOM_LEFT': '{},{}'.format(offset_x, offset_y),
'OUTPUT_BOTTOM_RIGHT': '{},{}'.format(offset_x + 1, offset_y),
'OUTPUT_TOP_LEFT': '{},{}'.format(offset_x, offset_y + 1),
Expand Down Expand Up @@ -239,6 +237,8 @@ if __name__ == "__main__":
help="kernel size")
parent_parser.add_argument("-s", "--stride", type=int, default=1,
help="stride")
parent_parser.add_argument("-d", "--dilation", type=int, default=1,
help="dilation")

subparser = subparsers.add_parser('arithmetic', parents=[parent_parser],
help='convolution arithmetic animation')
Expand Down
Binary file modified gif/arbitrary_padding_no_strides.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/arbitrary_padding_no_strides_transposed.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/full_padding_no_strides_transposed.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/no_padding_no_strides.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/no_padding_no_strides_transposed.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/no_padding_strides.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/padding_strides.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/padding_strides_odd.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/padding_strides_odd_transposed.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/padding_strides_transposed.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gif/same_padding_no_strides.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified pdf/arbitrary_padding_no_strides_transposed_00.pdf
Binary file not shown.
Binary file modified pdf/arbitrary_padding_no_strides_transposed_01.pdf
Binary file not shown.
Binary file modified pdf/arbitrary_padding_no_strides_transposed_02.pdf
Binary file not shown.
Binary file modified pdf/arbitrary_padding_no_strides_transposed_03.pdf
Binary file not shown.
Binary file modified pdf/full_padding_no_strides_transposed_00.pdf
Binary file not shown.
Binary file modified pdf/full_padding_no_strides_transposed_01.pdf
Binary file not shown.
Binary file modified pdf/full_padding_no_strides_transposed_02.pdf
Binary file not shown.
Binary file modified pdf/full_padding_no_strides_transposed_03.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_00.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_01.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_02.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_03.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_transposed_00.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_transposed_01.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_transposed_02.pdf
Binary file not shown.
Binary file modified pdf/no_padding_no_strides_transposed_03.pdf
Binary file not shown.
Binary file modified pdf/no_padding_strides_00.pdf
Binary file not shown.
Binary file modified pdf/no_padding_strides_01.pdf
Binary file not shown.
Binary file modified pdf/no_padding_strides_02.pdf
Binary file not shown.
Binary file modified pdf/no_padding_strides_03.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_00.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_01.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_02.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_03.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_04.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_05.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_06.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_07.pdf
Binary file not shown.
Binary file modified pdf/numerical_average_pooling_08.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_00.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_01.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_02.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_03.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_04.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_05.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_06.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_07.pdf
Binary file not shown.
Binary file modified pdf/numerical_max_pooling_08.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_00.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_01.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_02.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_03.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_04.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_05.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_06.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_07.pdf
Binary file not shown.
Binary file modified pdf/numerical_no_padding_no_strides_08.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_00.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_01.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_02.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_03.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_04.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_05.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_06.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_07.pdf
Binary file not shown.
Binary file modified pdf/numerical_padding_strides_08.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_00.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_01.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_02.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_03.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_00.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_01.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_02.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_03.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_transposed_00.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_transposed_01.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_transposed_02.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_odd_transposed_03.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_transposed_00.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_transposed_01.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_transposed_02.pdf
Binary file not shown.
Binary file modified pdf/padding_strides_transposed_03.pdf
Binary file not shown.
Binary file modified pdf/same_padding_no_strides_00.pdf
Binary file not shown.
Binary file modified pdf/same_padding_no_strides_01.pdf
Binary file not shown.
Binary file modified pdf/same_padding_no_strides_02.pdf
Binary file not shown.
Binary file modified pdf/same_padding_no_strides_03.pdf
Binary file not shown.
21 changes: 13 additions & 8 deletions templates/arithmetic_figure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@
yslant=0.5,xslant=-0.7]
\draw[step=10mm, base03, dashed, thick] (0,0) grid ({PADDING_TO});
{INPUT_UNITS}
\draw[fill=base02, opacity=0.4] ({INPUT_GRID_FROM}) rectangle
({INPUT_GRID_TO});
\draw[step=10mm, base03, thick] ({INPUT_GRID_FROM}) grid
({INPUT_GRID_TO});
\coordinate (BL) at ({INPUT_BOTTOM_LEFT});
\coordinate (BR) at ({INPUT_BOTTOM_RIGHT});
\coordinate (TL) at ({INPUT_TOP_LEFT});
\coordinate (TR) at ({INPUT_TOP_RIGHT});

\foreach \x in {{ {INPUT_GRID_FROM_X},\number\numexpr {INPUT_GRID_FROM_X}+{DILATION},...,\number\numexpr {INPUT_GRID_TO_X}-1 }} {{
\foreach \y in {{ {INPUT_GRID_FROM_Y},\number\numexpr {INPUT_GRID_FROM_Y}+{DILATION},...,\number\numexpr {INPUT_GRID_TO_Y}-1 }} {{
\draw[fill=base02, opacity=0.4] (\x,\y) rectangle
(\x+1,\y+1);
}}
}}
\draw[step=10mm, base03, thick] ({INPUT_GRID_FROM_X}, {INPUT_GRID_FROM_Y}) grid
({INPUT_GRID_TO_X}, {INPUT_GRID_TO_Y});
\coordinate (BL) at ({INPUT_GRID_FROM_X},{INPUT_GRID_FROM_Y});
\coordinate (BR) at ({INPUT_GRID_TO_X},{INPUT_GRID_FROM_Y});
\coordinate (TL) at ({INPUT_GRID_FROM_X},{INPUT_GRID_TO_Y});
\coordinate (TR) at ({INPUT_GRID_TO_X},{INPUT_GRID_TO_Y});
\end{{scope}}
\begin{{scope}}[xshift=-5, yshift={OUTPUT_ELEVATION},
every node/.append style={{yslant=0.5,xslant=-0.7}},
Expand Down

0 comments on commit c595e98

Please sign in to comment.