Fix inconsistent padding scheme between onnx and pytorch #41
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The scheme between torch.nn.functional.Pad and onnx's pad is different if the pads receive 8-dimensional padding.
Given input with size: (1,3,10,10) and
pads=(1,1,2,2,3,3,4,4)
,F.Pad
will result in size (9,9,14,12) (as declared in your test scripts). However, onnx will output result with size: (5,7,16,16) following their documentation.Therefore, the
pads
parameter loaded fromonnx_model.graph
should be transformed to the PyTorch version so the padding size is correct.Unfortunately, I find that the
pads
parameter will be placed inonnx_model.graph.initializer
instead of the node's parameter, so a simple preprocess of Pad nodes' parameter is not feasible :(.So I have to add an additional branch (which is ugly...) when loading the initializer parameter: if the targeting node is
Pad
we will check if the pads parameter needs to be preprocessed.I write a program to exhibit this bug:
Through this code snippet, we can see that the correct output shape should be:
(batch_size, 226, 226, 3)
but ONNX2PyTorch will output(batch_size+1, 225, 225, 3)
Current fix can pass all existing tests.