generate_makefile 5.24 KB
Newer Older
Loïc Barrault's avatar
Loïc Barrault committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
from six import iteritems
from six.moves import range

arithmetic_files = ('bin/produce_figure templates/arithmetic_figure.txt '
                    'templates/unit.txt')
numerical_files = 'bin/produce_figure templates/numerical_figure.txt'

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

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


def make_header():
    return ('.PHONY : all_animations\nall_animations : {}\n\n'.format(
                ' '.join(['gif/{}.gif'.format(name)
                          for name in animations.keys()])) +
            '.SECONDARY : \n')


def make_report_section():

    return ('conv_arithmetic.pdf : export BSTINPUTS=$BSTINPUTS:./natbib\n'
			'conv_arithmetic.pdf : conv_arithmetic.tex\n'
            '\tpdflatex conv_arithmetic\n'
            '\tpdflatex conv_arithmetic\n'
            '\tbibtex conv_arithmetic\n'
            '\tpdflatex conv_arithmetic\n'
            '\tpdflatex conv_arithmetic\n\n'
            '.PHONY : clean\n'
            'clean : \n'
            '\trm -f conv_arithmetic.{aux,bbl,blg,log}\n')


def make_gif_section():
    rules = []
    for name, config in iteritems(animations):
        if config['transposed']:
            steps = config['input-size'] ** 2
        else:
            steps = config['output-size'] ** 2
        rules.append(
            'gif/{}.gif : '.format(name) +
            ' '.join(['png/{}_{:02d}.png'.format(name, i)
                      for i in range(steps)]) + '\n' +
            '\tconvert -delay 100 -loop 0 -layers Optimize +map -dispose previous $^ $@\n' +
            '\tgifsicle --batch -O3 $@\n')
    return '\n'.join(rules)


def make_png_section():
    return ('png/%.png : pdf/%.pdf\n'
            '\tconvert -density 600 $< -flatten -resize 25% $@\n')


def make_pdf_section():
    rules = []
    for name, config in iteritems(animations):
        if config['transposed']:
            steps = config['input-size'] ** 2
        else:
            steps = config['output-size'] ** 2
        if config['type'] == 'arithmetic':
            dependencies = arithmetic_files
        else:
            dependencies = numerical_files
        subrules = []
        for i in range(steps):
            subrules.append(
                'pdf/{}_{:02d}.pdf : {}\n'.format(name, i, dependencies) +
                '\t./bin/produce_figure ' +
                '{} {} {} '.format(config['type'], name, i) +
                '--input-size={} '.format(config['input-size']) +
                '--output-size={} '.format(config['output-size']) +
                '--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'))
        rules.append('\n'.join(subrules))
    return '\n'.join(rules)


def main():
    with open('Makefile', 'w') as makefile:
        makefile.write('\n'.join([make_report_section(), make_header(),
                                  make_gif_section(), make_png_section(),
                                  make_pdf_section()]))


if __name__ == "__main__":
    main()