File size: 16,822 Bytes
dc2106c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# Copyright (c) ONNX Project Contributors
#
# SPDX-License-Identifier: Apache-2.0

import numpy as np

import onnx
from onnx.backend.test.case.base import Base
from onnx.backend.test.case.node import expect


class ConvTranspose(Base):
    @staticmethod
    def export() -> None:
        x = np.array(
            [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]]  # (1, 1, 3, 3)
        ).astype(np.float32)

        W = np.array(
            [
                [
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],  # (1, 2, 3, 3)
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                ]
            ]
        ).astype(np.float32)

        node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"])

        y = np.array(
            [
                [
                    [
                        [0.0, 1.0, 3.0, 3.0, 2.0],  # (1, 2, 5, 5)
                        [3.0, 8.0, 15.0, 12.0, 7.0],
                        [9.0, 21.0, 36.0, 27.0, 15.0],
                        [9.0, 20.0, 33.0, 24.0, 13.0],
                        [6.0, 13.0, 21.0, 15.0, 8.0],
                    ],
                    [
                        [0.0, 1.0, 3.0, 3.0, 2.0],
                        [3.0, 8.0, 15.0, 12.0, 7.0],
                        [9.0, 21.0, 36.0, 27.0, 15.0],
                        [9.0, 20.0, 33.0, 24.0, 13.0],
                        [6.0, 13.0, 21.0, 15.0, 8.0],
                    ],
                ]
            ]
        ).astype(np.float32)

        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose")

    @staticmethod
    def export_convtranspose_1d() -> None:
        x = np.array([[[0.0, 1.0, 2.0]]]).astype(np.float32)  # (1, 1, 3)

        W = np.array([[[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]]).astype(  # (1, 2, 3)
            np.float32
        )

        node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"])

        y = np.array(
            [[[0.0, 1.0, 3.0, 3.0, 2.0], [0.0, 1.0, 3.0, 3.0, 2.0]]]  # (1, 2, 5)
        ).astype(np.float32)

        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_1d")

    @staticmethod
    def export_convtranspose_3d() -> None:
        x = np.array(
            [
                [
                    [
                        [
                            [0.0, 1.0, 2.0, 3.0, 4.0],  # (1, 1, 3, 4, 5)
                            [5.0, 6.0, 7.0, 8.0, 9.0],
                            [10.0, 11.0, 12.0, 13.0, 14.0],
                            [15.0, 16.0, 17.0, 18.0, 19.0],
                        ],
                        [
                            [20.0, 21.0, 22.0, 23.0, 24.0],
                            [25.0, 26.0, 27.0, 28.0, 29.0],
                            [30.0, 31.0, 32.0, 33.0, 34.0],
                            [35.0, 36.0, 37.0, 38.0, 39.0],
                        ],
                        [
                            [40.0, 41.0, 42.0, 43.0, 44.0],
                            [45.0, 46.0, 47.0, 48.0, 49.0],
                            [50.0, 51.0, 52.0, 53.0, 54.0],
                            [55.0, 56.0, 57.0, 58.0, 59.0],
                        ],
                    ]
                ]
            ]
        ).astype(np.float32)

        W = np.array(
            [
                [
                    [
                        [
                            [1.0, 1.0, 1.0],  # (1, 2, 3, 3, 3)
                            [1.0, 1.0, 1.0],
                            [1.0, 1.0, 1.0],
                        ],
                        [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                        [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                    ],
                    [
                        [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                        [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                        [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                    ],
                ]
            ]
        ).astype(np.float32)

        node = onnx.helper.make_node("ConvTranspose", ["X", "W"], ["Y"])

        y = np.array(
            [
                [
                    [
                        [
                            [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0],  # (1, 2, 5, 6, 7)
                            [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],
                            [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],
                            [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],
                            [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],
                            [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],
                        ],
                        [
                            [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],
                            [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],
                            [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],
                            [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],
                            [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],
                            [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],
                        ],
                        [
                            [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],
                            [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],
                            [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],
                            [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],
                            [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],
                            [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],
                        ],
                        [
                            [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],
                            [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],
                            [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],
                            [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],
                            [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],
                            [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],
                        ],
                        [
                            [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],
                            [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],
                            [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],
                            [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],
                            [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],
                            [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],
                        ],
                    ],
                    [
                        [
                            [0.0, 1.0, 3.0, 6.0, 9.0, 7.0, 4.0],
                            [5.0, 12.0, 21.0, 27.0, 33.0, 24.0, 13.0],
                            [15.0, 33.0, 54.0, 63.0, 72.0, 51.0, 27.0],
                            [30.0, 63.0, 99.0, 108.0, 117.0, 81.0, 42.0],
                            [25.0, 52.0, 81.0, 87.0, 93.0, 64.0, 33.0],
                            [15.0, 31.0, 48.0, 51.0, 54.0, 37.0, 19.0],
                        ],
                        [
                            [20.0, 42.0, 66.0, 72.0, 78.0, 54.0, 28.0],
                            [50.0, 104.0, 162.0, 174.0, 186.0, 128.0, 66.0],
                            [90.0, 186.0, 288.0, 306.0, 324.0, 222.0, 114.0],
                            [120.0, 246.0, 378.0, 396.0, 414.0, 282.0, 144.0],
                            [90.0, 184.0, 282.0, 294.0, 306.0, 208.0, 106.0],
                            [50.0, 102.0, 156.0, 162.0, 168.0, 114.0, 58.0],
                        ],
                        [
                            [60.0, 123.0, 189.0, 198.0, 207.0, 141.0, 72.0],
                            [135.0, 276.0, 423.0, 441.0, 459.0, 312.0, 159.0],
                            [225.0, 459.0, 702.0, 729.0, 756.0, 513.0, 261.0],
                            [270.0, 549.0, 837.0, 864.0, 891.0, 603.0, 306.0],
                            [195.0, 396.0, 603.0, 621.0, 639.0, 432.0, 219.0],
                            [105.0, 213.0, 324.0, 333.0, 342.0, 231.0, 117.0],
                        ],
                        [
                            [60.0, 122.0, 186.0, 192.0, 198.0, 134.0, 68.0],
                            [130.0, 264.0, 402.0, 414.0, 426.0, 288.0, 146.0],
                            [210.0, 426.0, 648.0, 666.0, 684.0, 462.0, 234.0],
                            [240.0, 486.0, 738.0, 756.0, 774.0, 522.0, 264.0],
                            [170.0, 344.0, 522.0, 534.0, 546.0, 368.0, 186.0],
                            [90.0, 182.0, 276.0, 282.0, 288.0, 194.0, 98.0],
                        ],
                        [
                            [40.0, 81.0, 123.0, 126.0, 129.0, 87.0, 44.0],
                            [85.0, 172.0, 261.0, 267.0, 273.0, 184.0, 93.0],
                            [135.0, 273.0, 414.0, 423.0, 432.0, 291.0, 147.0],
                            [150.0, 303.0, 459.0, 468.0, 477.0, 321.0, 162.0],
                            [105.0, 212.0, 321.0, 327.0, 333.0, 224.0, 113.0],
                            [55.0, 111.0, 168.0, 171.0, 174.0, 117.0, 59.0],
                        ],
                    ],
                ]
            ]
        ).astype(np.float32)

        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_3d")

    @staticmethod
    def export_convtranspose_attributes() -> None:
        x = np.array(
            [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]]  # (1, 1, 3, 3)
        ).astype(np.float32)

        W = np.array(
            [
                [
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],  # (1, 2, 3, 3)
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                ]
            ]
        ).astype(np.float32)

        y = np.array(
            [
                [
                    [
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],  # (1, 2, 10, 8)
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],
                        [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
                    ],
                    [
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0, 2.0, 0.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0, 5.0, 0.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0, 8.0, 0.0],
                        [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
                    ],
                ]
            ]
        ).astype(np.float32)

        node = onnx.helper.make_node(
            "ConvTranspose", ["X", "W"], ["Y"], strides=[3, 2], output_shape=[10, 8]
        )
        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_output_shape")

        node = onnx.helper.make_node(
            "ConvTranspose", ["X", "W"], ["Y"], strides=[3, 2], output_padding=[1, 1]
        )
        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_pad")

        node = onnx.helper.make_node(
            "ConvTranspose",
            ["X", "W"],
            ["Y"],
            name="test",
            strides=[3, 2],
            output_shape=[10, 8],
            kernel_shape=[3, 3],
            output_padding=[1, 1],
        )
        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_kernel_shape")

    @staticmethod
    def export_convtranspose_pads() -> None:
        x = np.array(
            [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]]  # (1, 1, 3, 3)
        ).astype(np.float32)

        W = np.array(
            [
                [
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],  # (1, 2, 3, 3)
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                ]
            ]
        ).astype(np.float32)

        node = onnx.helper.make_node(
            "ConvTranspose", ["X", "W"], ["Y"], strides=[3, 2], pads=[1, 2, 1, 2]
        )

        y = np.array(
            [
                [
                    [
                        [1.0, 1.0, 3.0],  # (1, 2, 7, 3)
                        [1.0, 1.0, 3.0],
                        [7.0, 4.0, 9.0],
                        [7.0, 4.0, 9.0],
                        [7.0, 4.0, 9.0],
                        [13.0, 7.0, 15.0],
                        [13.0, 7.0, 15.0],
                    ],
                    [
                        [1.0, 1.0, 3.0],
                        [1.0, 1.0, 3.0],
                        [7.0, 4.0, 9.0],
                        [7.0, 4.0, 9.0],
                        [7.0, 4.0, 9.0],
                        [13.0, 7.0, 15.0],
                        [13.0, 7.0, 15.0],
                    ],
                ]
            ]
        ).astype(np.float32)

        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_pads")

    @staticmethod
    def export_convtranspose_dilations() -> None:
        x = np.array(
            [[[[3.0, 8.0, 1.0], [9.0, 5.0, 7.0], [3.0, 2.0, 6.0]]]]  # (1, 1, 3, 3)
        ).astype(np.float32)
        W = np.array([[[[7.0, 2.0], [1.0, 9.0]]]]).astype(np.float32)  # (1, 1, 2, 2)

        node = onnx.helper.make_node(
            "ConvTranspose", ["X", "W"], ["Y"], dilations=[2, 2]
        )

        y = np.array(
            [
                [
                    [
                        [21.0, 56.0, 13.0, 16.0, 2.0],  # [1, 1, 5, 5]
                        [63.0, 35.0, 67.0, 10.0, 14.0],
                        [24.0, 22.0, 76.0, 76.0, 21.0],
                        [9.0, 5.0, 88.0, 45.0, 63.0],
                        [3.0, 2.0, 33.0, 18.0, 54.0],
                    ]
                ]
            ]
        ).astype(np.float32)

        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_dilations")

    @staticmethod
    def export_convtranspose_autopad_same() -> None:
        x = np.array(
            [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]]  # (1, 1, 3, 3)
        ).astype(np.float32)

        W = np.array(
            [
                [
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],  # (1, 2, 3, 3)
                    [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0], [1.0, 1.0, 1.0]],
                ]
            ]
        ).astype(np.float32)

        node = onnx.helper.make_node(
            "ConvTranspose", ["X", "W"], ["Y"], auto_pad="SAME_UPPER", strides=[2, 2]
        )

        y = np.array(
            [
                [
                    [
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],
                        [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],
                        [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],
                    ],
                    [
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],
                        [0.0, 0.0, 1.0, 1.0, 3.0, 2.0],
                        [3.0, 3.0, 8.0, 5.0, 12.0, 7.0],
                        [3.0, 3.0, 7.0, 4.0, 9.0, 5.0],
                        [9.0, 9.0, 20.0, 11.0, 24.0, 13.0],
                        [6.0, 6.0, 13.0, 7.0, 15.0, 8.0],
                    ],
                ]
            ]
        ).astype(np.float32)

        expect(node, inputs=[x, W], outputs=[y], name="test_convtranspose_autopad_same")