This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_onnx2keras.py
550 lines (473 loc) · 23.5 KB
/
test_onnx2keras.py
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import warnings
from io import BytesIO
from tempfile import NamedTemporaryFile
import onnx
import torch.nn
from torch.nn import Module
import torch.nn.functional as F
from torchvision import models
from numpy.testing import assert_almost_equal
import numpy as np
import tensorflow as tf
from onnx2keras import onnx2keras, compatible_data_format, OnnxConstant, OnnxTensor, InterleavedImageBatch, \
ensure_data_format, OptimizationMissingWarning
def make_onnx_model(net, indata, opset_version=None):
fd = BytesIO()
torch.onnx.export(net, indata, fd, opset_version=opset_version)
# with open("/tmp/t.onnx", "wb") as debug: torch.onnx.export(net, indata, debug, opset_version=opset_version)
fd.seek(0)
return onnx.load(fd)
def convert_and_compare_output(net, indata, precition=5, image_out=True, savable=True, missing_optimizations=False, opset_version=None, **kwargs):
try:
return _convert_and_compare_output(net, indata, precition, image_out, savable, missing_optimizations, opset_version, **kwargs)
except AssertionError:
return _convert_and_compare_output(net, indata, precition, image_out, savable, missing_optimizations, opset_version, **kwargs)
def _convert_and_compare_output(net, indata, precition=5, image_out=True, savable=True, missing_optimizations=False, opset_version=None, **kwargs):
torch_indata = torch.tensor(indata)
y1 = net(torch_indata).detach().numpy()
onnx_model = make_onnx_model(net, torch.zeros_like(torch_indata), opset_version)
with warnings.catch_warnings(record=True) as warns:
warnings.simplefilter("always")
kernas_net = onnx2keras(onnx_model, **kwargs)
warns = [w for w in warns if w.category is OptimizationMissingWarning]
if not missing_optimizations:
assert len(warns) == 0
if savable:
with NamedTemporaryFile() as f:
f.close()
kernas_net.save(f.name)
y2 = kernas_net.predict(indata.transpose(0, 2, 3, 1))
if image_out:
y2 = y2.transpose(0, 3, 1, 2)
assert_almost_equal(y1, y2, precition)
return kernas_net
class GlobalAvgPool(Module):
def forward(self, x):
return x.mean([2, 3])
class TestUtils:
def test_compatible_data_format(self):
assert compatible_data_format(OnnxConstant, OnnxConstant)
assert compatible_data_format(OnnxTensor, OnnxTensor)
assert compatible_data_format(OnnxConstant, OnnxTensor)
assert compatible_data_format(OnnxTensor, OnnxConstant)
assert compatible_data_format(InterleavedImageBatch, InterleavedImageBatch)
assert not compatible_data_format(OnnxTensor, InterleavedImageBatch)
assert not compatible_data_format(OnnxConstant, InterleavedImageBatch)
assert not compatible_data_format(InterleavedImageBatch, OnnxTensor)
assert not compatible_data_format(InterleavedImageBatch, OnnxConstant)
class TestOnnx:
def test_padding(self):
x = np.random.rand(1, 3, 112, 112).astype(np.float32)
model = torch.nn.Sequential(torch.nn.Conv2d(3, 64, (3, 3), 1, 1), torch.nn.MaxPool2d(3, 2, 1))
convert_and_compare_output(model, x)
def test_conv(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 16, 7), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_no_bias(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 16, 7, bias=False), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_padding(self):
net = torch.nn.Sequential(torch.nn.Conv2d(1, 16, 3, padding=1), torch.nn.ReLU())
x = np.random.rand(1, 1, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_prelu(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 16, 7), torch.nn.PReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_prelu_per_channel(self):
act = torch.nn.PReLU(num_parameters=16)
with torch.no_grad():
act.weight[:] = torch.tensor(range(16))
net = torch.nn.Sequential(torch.nn.Conv2d(3, 16, 7), act)
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x, 5)
def test_maxpool(self):
net = torch.nn.Sequential(torch.nn.MaxPool2d(2))
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_maxpool_resnet(self):
net = torch.nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
x = np.random.rand(1, 192, 272, 64).astype(np.float32)
convert_and_compare_output(net, x)
def test_concat(self):
for axis in range(1,4):
class Dbl(torch.nn.Module):
def forward(self, x):
return torch.cat((x, x), axis)
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(Dbl(), x)
def test_conv_transpose(self):
net = torch.nn.Sequential(torch.nn.ConvTranspose2d(3, 16, 5, 2), torch.nn.ReLU())
x = np.random.rand(1, 3, 112, 112).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_transpose_padding(self):
net = torch.nn.Sequential(torch.nn.ConvTranspose2d(3, 16, 4, 2, padding=1), torch.nn.ReLU())
x = np.random.rand(1, 3, 112, 112).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_different_padding(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 64, kernel_size=7, stride=1, padding=(3, 4)))
x = np.random.rand(1, 3, 384, 544).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_transpose_no_bias(self):
net = torch.nn.Sequential(torch.nn.ConvTranspose2d(3, 16, 5, 2, bias=False), torch.nn.ReLU())
x = np.random.rand(1, 3, 112, 112).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_transpose_grouped_no_bias(self):
net = torch.nn.Sequential(torch.nn.ConvTranspose2d(16, 16, 5, 2, groups=2, bias=False), torch.nn.ReLU())
x = np.random.rand(1, 16, 112, 112).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_transpose_grouped_bias(self):
net = torch.nn.Sequential(torch.nn.ConvTranspose2d(16, 16, 5, 2, groups=2), torch.nn.ReLU())
x = np.random.rand(1, 16, 112, 112).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_transpose_grouped_fully(self):
net = torch.nn.Sequential(torch.nn.ConvTranspose2d(16, 16, 5, 2, groups=16), torch.nn.ReLU())
x = np.random.rand(1, 16, 112, 112).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_transpose_output_padding(self):
net = torch.nn.Sequential(torch.nn.ConvTranspose2d(16, 16, 3, 2, output_padding=1), torch.nn.ReLU())
x = np.random.rand(1, 16, 112, 112).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_stride2_padding_strange(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3))
x = np.random.rand(1, 3, 384, 544).astype(np.float32)
convert_and_compare_output(net, x)
def test_conv_stride2_padding_simple_odd(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1))
x = np.random.rand(1, 3, 223, 223).astype(np.float32)
kernas_net = convert_and_compare_output(net, x)
assert [l.__class__.__name__ for l in kernas_net.layers] == ['InputLayer', 'Conv2D']
def test_conv_stride2_padding_simple_even(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1))
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
kernas_net = convert_and_compare_output(net, x)
# assert [l.__class__.__name__ for l in kernas_net.layers] == ['InputLayer', 'Conv2D']
def test_batchnorm(self):
bn = torch.nn.BatchNorm2d(3)
bn.running_mean.uniform_()
bn.running_var.uniform_()
net = torch.nn.Sequential(bn, torch.nn.ReLU())
net.eval()
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_batchnorm1d(self):
bn = torch.nn.BatchNorm1d(3)
bn.running_mean.uniform_()
bn.running_var.uniform_()
net = torch.nn.Sequential(GlobalAvgPool(), bn, torch.nn.ReLU())
net.eval()
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_clamp(self):
class Clamp(Module):
def forward(self, x):
return torch.clamp(x, 0.3, 0.7)
net = torch.nn.Sequential(torch.nn.ReLU(), Clamp(), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x, savable=False)
def test_relu6(self):
class Clamp(Module):
def forward(self, x):
return torch.clamp(x, 0, 6)
net = torch.nn.Sequential(torch.nn.ReLU(), Clamp(), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_leaky_relu(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 3, 3), torch.nn.LeakyReLU(), torch.nn.Conv2d(3, 3, 3))
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_depthwise(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 3, 7, groups=3), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_groupwise(self):
net = torch.nn.Conv2d(8, 8, 7, groups=4)
x = np.random.rand(1, 8, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_groupwise_tflite_compat(self):
net = torch.nn.Conv2d(8, 8, 7, groups=4)
x = np.random.rand(1, 8, 224, 224).astype(np.float32)
convert_and_compare_output(net, x, missing_optimizations=True, make_tflite_compatible=True)
def test_groupwise_no_bias(self):
net = torch.nn.Conv2d(6, 12, 4, groups=3, bias=False)
x = np.random.rand(1, 6, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_depthwise_no_bias(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 3, 7, groups=3, bias=False), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_add(self):
class AddTst(Module):
def __init__(self):
Module.__init__(self)
self.conv1 = torch.nn.Conv2d(3, 3, 7)
self.conv2 = torch.nn.Conv2d(3, 3, 7)
def forward(self, x):
return self.conv1(x).relu_() + self.conv2(x).relu_()
net = torch.nn.Sequential(AddTst(), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_global_avrage_pooling(self):
net = torch.nn.Sequential(GlobalAvgPool(), torch.nn.ReLU())
x = np.random.rand(1, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_dropout(self):
net = torch.nn.Sequential(GlobalAvgPool(), torch.nn.Dropout(), torch.nn.ReLU())
net.eval()
x = np.random.rand(1, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_linear(self):
net = torch.nn.Sequential(GlobalAvgPool(), torch.nn.Linear(3, 8), torch.nn.ReLU())
net.eval()
x = np.random.rand(5, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_linear_no_bias(self):
net = torch.nn.Sequential(GlobalAvgPool(), torch.nn.Linear(3, 8, bias=False), torch.nn.ReLU())
net.eval()
x = np.random.rand(5, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_mobilenet_v2(self):
net = models.mobilenet_v2()
net.eval()
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_avg_pool_pad(self):
class PadTst(Module):
def forward(self, x):
return F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
net = torch.nn.Sequential(PadTst(), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_avg_pool_pad_asym(self):
class PadTst(Module):
def forward(self, x):
return F.avg_pool2d(x, kernel_size=(3, 6), stride=(1, 2), padding=(1, 2))
net = torch.nn.Sequential(PadTst(), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_gloabl_avg_pool(self):
class AvgTst(Module):
def forward(self, x):
return F.adaptive_avg_pool2d(x, (1, 1))
net = torch.nn.Sequential(AvgTst(), torch.nn.ReLU())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_flatten(self):
class Tst(Module):
def forward(self, x):
return torch.flatten(x, 1)
net = torch.nn.Sequential(Tst(), torch.nn.ReLU())
x = np.random.rand(1, 3, 1, 1).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_vector_pad(self):
class VectorPad2D(Module):
def forward(self, x):
tt = [torch.nn.functional.pad(x[:, i:i + 1], [1,1,1,1], 'constant', [1,2,3][i])
for i in range(x.shape[1])]
return torch.cat(tt, 1)
net = torch.nn.Sequential(VectorPad2D(), torch.nn.ReLU())
x = np.random.rand(2, 3, 5, 5).astype(np.float32)
convert_and_compare_output(net, x)
def test_vector_pad_addhack(self):
class VectorPad2D(Module):
def forward(self, x):
c = torch.tensor([1,2,3]).reshape(1, 3, 1, 1)
return torch.nn.functional.pad(x - c, [1,1,1,1]) + c
net = torch.nn.Sequential(VectorPad2D(), torch.nn.ReLU())
x = np.random.rand(1, 3, 5, 5).astype(np.float32)
convert_and_compare_output(net, x)
def test_vector_pad_addhack_asym(self):
class VectorPad2D(Module):
def forward(self, x):
c = torch.tensor([1,2,3]).reshape(1, 3, 1, 1)
return torch.nn.functional.pad(x - c, [1,0,1,0]) + c
net = torch.nn.Sequential(VectorPad2D(), torch.nn.ReLU())
x = np.random.rand(1, 3, 5, 5).astype(np.float32)
convert_and_compare_output(net, x)
def test_sigmoid(self):
net = torch.nn.Sequential(torch.nn.Conv2d(3, 16, 7), torch.nn.Sigmoid())
x = np.random.rand(1, 3, 224, 224).astype(np.float32)
convert_and_compare_output(net, x)
def test_upsample_nearest(self):
net = torch.nn.Sequential(torch.nn.UpsamplingNearest2d(scale_factor=2), torch.nn.ReLU())
x = np.random.rand(1, 3, 32, 32).astype(np.float32)
convert_and_compare_output(net, x)
def test_upsample_nearest_v11(self):
net = torch.nn.Sequential(torch.nn.UpsamplingNearest2d(scale_factor=2), torch.nn.ReLU())
x = np.random.rand(1, 3, 32, 32).astype(np.float32)
convert_and_compare_output(net, x, opset_version=11)
def test_upsample_bilinear(self):
net = torch.nn.Sequential(torch.nn.UpsamplingBilinear2d(scale_factor=2), torch.nn.ReLU())
x = np.random.rand(1, 3, 32, 32).astype(np.float32)
convert_and_compare_output(net, x, opset_version=11)
def test_interpolate_nearest(self):
class Net(Module):
def forward(self, x):
return F.interpolate(x, scale_factor=2, mode="nearest")
net = torch.nn.Sequential(Net(), torch.nn.ReLU())
x = np.random.rand(1, 3, 32, 32).astype(np.float32)
convert_and_compare_output(net, x)
def test_interpolate_bilinear(self):
class Net(Module):
def forward(self, x):
return F.interpolate(x, scale_factor=2, mode="bilinear", align_corners=True)
net = torch.nn.Sequential(Net(), torch.nn.ReLU())
x = np.random.rand(1, 3, 32, 32).astype(np.float32)
convert_and_compare_output(net, x, opset_version=11)
def test_eq_mul(self):
class EqProd(Module):
def forward(self, x):
maxmap = F.max_pool2d(x, 3, 1, 1, 1, False, False)
return x * (maxmap == x)
net = torch.nn.Sequential(EqProd(), torch.nn.ReLU())
x = np.random.rand(1, 3, 5, 5).astype(np.float32)
convert_and_compare_output(net, x)
def test_adaptive_avgpool_reshape(self):
class Net(Module):
def forward(self, x):
return F.adaptive_avg_pool2d(x, 1).reshape(x.shape[0], -1)
net = torch.nn.Sequential(Net(), torch.nn.ReLU())
x = np.random.rand(1, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
x = np.random.rand(4, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_bmm(self):
class Net(Module):
def forward(self, x):
x = x.reshape(1, 16, 16)
return torch.bmm(x, x)
net = torch.nn.Sequential(Net(), torch.nn.ReLU())
x = np.random.rand(1, 1, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_matmul(self):
class Net(Module):
def forward(self, x):
x = x.reshape(1, 1, 16, 16)
return torch.matmul(x, x)
net = torch.nn.Sequential(Net(), torch.nn.ReLU())
x = np.random.rand(1, 1, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_unsupported_optimasation(self):
class Reshape(Module):
def forward(self, x):
return x.reshape(4, 4, 16, 16)
net = torch.nn.Sequential(GlobalAvgPool(), torch.nn.Linear(3, 4 * 16 * 16), Reshape(),
torch.nn.Conv2d(4, 3, 3), torch.nn.ReLU())
net.eval()
x = np.random.rand(4, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, missing_optimizations=True)
def test_sqrt(self):
class Sq(Module):
def forward(self, x):
return torch.sqrt(x)
net = torch.nn.Sequential(Sq(), torch.nn.ReLU())
x = np.random.rand(4, 3, 16, 16).astype(np.float32)
is_tf1 = tuple(map(int, tf.__version__.split('.'))) < (2, 0, 0)
convert_and_compare_output(net, x, savable=(not is_tf1))
def test_abs(self):
class Abs(Module):
def forward(self, x):
return torch.abs(x)
net = torch.nn.Sequential(Abs(), torch.nn.ReLU())
x = np.random.rand(4, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x)
def test_neg(self):
class Neg(Module):
def forward(self, x):
return -x
net = torch.nn.Sequential(Neg(), torch.nn.ReLU())
x = np.random.rand(4, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x)
def test_center_crop(self):
class CenterCrop8x8(Module):
def forward(self, x):
n, c, h, w = x.shape
dx = (w - 8) // 2
dy = (h - 8) // 2
crop = x[:, :, dy:dy+8, dx:dx+8]
return crop
net = torch.nn.Sequential(CenterCrop8x8(), torch.nn.ReLU())
x = np.random.rand(4, 3, 16, 32).astype(np.float32)
convert_and_compare_output(net, x, opset_version=11)
def test_mul(self):
class Mul(Module):
def forward(self, x):
return x * x
net = torch.nn.Sequential(Mul(), torch.nn.ReLU())
x = np.random.rand(4, 3, 16, 32).astype(np.float32)
convert_and_compare_output(net, x, opset_version=11)
def test_mul_const(self):
class Mul(Module):
def forward(self, x):
return (2 * x) * (x * 2)
net = torch.nn.Sequential(Mul(), torch.nn.ReLU())
x = np.random.rand(4, 3, 16, 32).astype(np.float32)
convert_and_compare_output(net, x, opset_version=11)
def test_concat_for_OnnxTensor(self):
batch = 4
class Net(Module):
def __init__(self):
super().__init__()
self.conv1 = torch.nn.Conv2d(3, 4, 5)
self.conv2 = torch.nn.Conv2d(3, 4, 5)
self.ClassHead = torch.nn.ModuleList([torch.nn.Conv2d(4, 16, 3), torch.nn.Conv2d(4, 16, 3)])
def forward(self, x):
features = [self.conv1(x), self.conv2(x)]
classifications = torch.cat([self.ClassHead[i](feature).reshape(batch, -1, 2) for i, feature in enumerate(features)], dim=1)
return classifications
x = np.random.rand(batch, 3, 16, 32).astype(np.float32)
convert_and_compare_output(Net(), x, missing_optimizations=True, image_out=False)
def test_transpose_to_onnx_testor(self):
class Net(Module):
def forward(self, x):
return x.permute(0,2,3,1)
x = np.random.rand(2, 3, 16, 32).astype(np.float32)
convert_and_compare_output(Net(), x, image_out=False)
def test_concat_for_OnnxTensor_optimized(self):
batch = 4
class Net(Module):
def __init__(self):
super().__init__()
self.conv1 = torch.nn.Conv2d(3, 4, 5)
self.conv2 = torch.nn.Conv2d(3, 4, 5)
self.ClassHead = torch.nn.ModuleList([torch.nn.Conv2d(4, 16, 3), torch.nn.Conv2d(4, 16, 3)])
def forward(self, x):
features = [self.conv1(x), self.conv2(x)]
classifications = torch.cat([self.ClassHead[i](feature).permute(0,2,3,1).reshape(batch, -1, 2) for i, feature in enumerate(features)], dim=1)
return classifications
x = np.random.rand(batch, 3, 16, 32).astype(np.float32)
convert_and_compare_output(Net(), x, image_out=False)
def test_gather(self):
class Net(Module):
def forward(self, x):
return x.permute(0,2,3,1).select(2,1)
net = torch.nn.Sequential(Net(), torch.nn.ReLU())
x = np.random.rand(2, 3, 16, 32).astype(np.float32)
convert_and_compare_output(net, x, image_out=False)
def test_yolox_focus_module(self):
class Focus(Module):
def forward(self, x):
patch_top_left = x[..., ::2, ::2]
patch_top_right = x[..., ::2, 1::2]
patch_bot_left = x[..., 1::2, ::2]
patch_bot_right = x[..., 1::2, 1::2]
x = torch.cat(
(
patch_top_left,
patch_bot_left,
patch_top_right,
patch_bot_right,
),
dim=1,
)
return x
net = torch.nn.Sequential(Focus(), torch.nn.ReLU())
x = np.random.rand(4, 3, 16, 16).astype(np.float32)
convert_and_compare_output(net, x, opset_version=11)
# def test_inception_v3(self):
# net = models.Inception3(aux_logits=False)
# net.eval()
# x = np.random.rand(1, 3, 299, 299).astype(np.float32)
# convert_and_compare_output(net, x, image_out=False)