onnx / onnx

Open standard for machine learning interoperability
https://onnx.ai/
Apache License 2.0
17.79k stars 3.67k forks source link

Error in Pad #4199

Open softwarePupil opened 2 years ago

softwarePupil commented 2 years ago

Ask a Question

Question

I have made a very simple network with only pad and relu operation.After generating the onnx model and visualizing him, the tensor size after pad becomes (????).

class Model(nn.Module):
    def __init__(self):
        super(Model,self).__init__()
        self.reflection_pad = nn.ReflectionPad2d(1)
        self.relu1 = nn.ReLU(inplace=True)
    def forward(self, x):
        x = self.reflection_pad(x)
        x = self.relu1(x)
        return x

image Did I make a mistake somewhere?

jcwchen commented 2 years ago

Hi @softwarePupil, Did you still see (?, ?, ?, ?) after using onnx.shape_inference? Could you please share this model for me to repro if possible? Thanks.

youjin-c commented 2 years ago

I have a similar issue with ReflectionPad2d

I trained a CycleGAN, and after the Pad node, every shape is unknown. Initially, I had an error msg of Resource import for /Users/youjin/Downloads/latest_net_G (1).onnx failed: The ONNX network's output '625' dimensions should be non-negative.

To trace back, I used onnx.save(onnx.shape_inference.infer_shapes(onnx.load(save_path)), save_path) to see the shapes image and found that after the first initial pad node, all following shapes are unknown.

This is the link of the onnx file just in case you want to see

jcwchen commented 2 years ago

Thanks for providing the model -- I can repro this issue. Pad cannot infer a concrete shape inference, because it needs to know the exact value of second input "pads" (it only knows its shape instead): https://github.com/onnx/onnx/blob/f0d824c0ead2825f7c24790c05f985ad1fb909f2/onnx/defs/tensor/old.cc#L2325 Therefore it can only perform rank inference. Basic ONNX shape inference can only propagate "shape" and here such a pads value comes from "ConstantOfShape" through a few operators. Although ONNX does have data propagation to handle this kind of situation, currently Transpose does not have data propagation support.

youjin-c commented 2 years ago

Hello @jcwchen , Thanks for the reply. I wonder if the answer implies that I cannot use my model due to having transpose and pad? Or should I be able to use it even if onnx cannot infer shapes? Sorry for such a noob question. I am confused if a normal onnx model should show all shapes, at least shapes of input and output, or if a model is still normal without shape inference.

jcwchen commented 2 years ago

No worries. I am happy to resolve your confusion. Please note that ONNX shape inference is not guaranteed to be complete. See limitation here. Therefore, it's possible to see some nodes cannot get the inferred shapes from ONNX shape inference and they are still valid ONNX models.