Skip to content

DequantizeLinear.py can't compile layer when x_scale_rank == 1 #733

@ReschakRyan

Description

@ReschakRyan

Issue Type

Others

OS

Linux

onnx2tf version number

1.22.3

onnx version number

1.17.0

onnxruntime version number

1.20.0

onnxsim (onnx_simplifier) version number

0.4.36

tensorflow version number

2.18.0

Download URL for ONNX

https://drive.google.com/file/d/1V67C1yzkjCejLkR5vykay3MTsN7BXFPH/view?usp=drivesdk

Parameter Replacement JSON

None

Description

Hello to those involved (let me know if the link to the file is broken)

  1. I am trying to convert a QAT YOLO model from onnx to tensorflow for research purposes.
  2. The error I am getting on a layer is on line https://github.com/PINTO0309/onnx2tf/blob/main/onnx2tf/ops/DequantizeLinear.py#L106 where the tf.reshape is passed (tensor=<tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.14131849], dtype=float32)> and shape=([1,1,1,3])). In other words it's impossible to do.
  3. Currently I am trying to mess with the DequantizeLinear.py file manual to see if I can fix the problem

The problem is happening with this simple part of the model:
`class Conv(nn.Module):
"""Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)."""

default_act = nn.SiLU()  # default activation

def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
    """Initialize Conv layer with given arguments including activation."""
    super().__init__()
    self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
    self.bn = nn.BatchNorm2d(c2)
    self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()

def forward(self, x):
    """Apply convolution, batch normalization and activation to input tensor."""
    return self.act(self.bn(self.conv(x)))

def forward_fuse(self, x):
    """Perform transposed convolution of 2D data."""
    return self.act(self.conv(x))`

It is fused together and wrapped around the model is the Dequant and Quat layers to do QAT.

These are all the translated layers for this model:

%/0/conv/Cast_output_0 = Cast[to = 2](%/quant/QuantizeLinear_output_0)
  %/0/conv/Constant_output_0 = Constant[value = <Scalar Tensor []>]()
  %/0/conv/Constant_1_output_0 = Constant[value = <Scalar Tensor []>]()
  %/0/conv/DequantizeLinear_output_0 = DequantizeLinear(%/0/conv/Cast_output_0, %/0/conv/Constant_output_0, %/0/conv/Constant_1_output_0)
  %/0/conv/Constant_2_output_0 = Constant[value = <Tensor>]()
  %/0/conv/Constant_3_output_0 = Constant[value = <Tensor>]()
  %/0/conv/Constant_4_output_0 = Constant[value = <Tensor>]()
  %/0/conv/DequantizeLinear_1_output_0 = DequantizeLinear(%/0/conv/Constant_2_output_0, %/0/conv/Constant_3_output_0, %/0/conv/Constant_4_output_0)
  %/0/conv/Constant_5_output_0 = Constant[value = <Tensor>]()
  %/0/conv/ConstantOfShape_output_0 = ConstantOfShape[value = <Tensor>](%/0/conv/Constant_5_output_0)
  %/0/conv/Constant_6_output_0 = Constant[value = <Tensor>]()
  %/0/conv/Constant_7_output_0 = Constant[value = <Tensor>]()
  %/0/conv/Cast_1_output_0 = Cast[to = 6](%/0/conv/ConstantOfShape_output_0)
  %/0/conv/DequantizeLinear_2_output_0 = DequantizeLinear(%/0/conv/Constant_6_output_0, %/0/conv/Constant_7_output_0, %/0/conv/Cast_1_output_0)
  %/0/conv/Conv_output_0 = Conv[dilations = [1, 1], group = 1, kernel_shape = [3, 3], pads = [1, 1, 1, 1], strides = [2, 2]](%/0/conv/DequantizeLinear_output_0, %/0/conv/DequantizeLinear_1_output_0, %/0/conv/DequantizeLinear_2_output_0)
  %/0/conv/Relu_output_0 = Relu(%/0/conv/Conv_output_0)
  %/0/conv/Constant_8_output_0 = Constant[value = <Scalar Tensor []>]()
  %/0/conv/Constant_9_output_0 = Constant[value = <Scalar Tensor []>]()
  %/0/conv/QuantizeLinear_output_0 = QuantizeLinear(%/0/conv/Relu_output_0, %/0/conv/Constant_8_output_0, %/0/conv/Constant_9_output_0)

The issue arrises with DequantizeLinear_1_output_0
These are it's input layers:

(Pdb) graph_node_input_1
Variable (wa/0/conv/Constant_2_output_0): (shape=[16, 3, 3, 3], dtype=int8)
(Pdb) graph_node_input_2
Variable (wa/0/conv/Constant_3_output_0): (shape=[1], dtype=float32)
(Pdb) graph_node_input_3
Variable (wa/0/conv/Constant_4_output_0): (shape=[1], dtype=int8)

Because Constant_2 has the shape it has, the program tries to reshape Constant_3_output_0 (a single value) to be [1, 1, 1, 3] which is impossible
I know this should be possible as I am able to use onnx2tf to compile and translate the model without doing quantization (no QAT) and the onnx model I can run inference on.
This is the command I run to execute onnx2tf (I have tried it in python script too and it's the same result)

onnx2tf -i "runs/detect/train6/weights/best.onnx" -o "runs/detect/train6/weights/best_saved_model" -nuo --verbosity info -oiqt -qt per-tensor -cind images "runs/detect/train6/weights/best_saved_model/tmp_tflite_int8_calibration_images.npy" "[[[[0, 0, 0]]]]" "[[[[255, 255, 255]]]]"

Any help would be greatly appreciated :) Thank you in advance.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions