ThanatosShinji / onnx-tool

A parser, editor and profiler tool for ONNX models.
https://pypi.org/project/onnx-tool/
MIT License
399 stars 52 forks source link

MAC calculation #60

Open ohjunee opened 11 months ago

ohjunee commented 11 months ago

I know the MAC calculation method of Conv2d as follows.

Conv2d MACs =K*K*Cin*Cout*img*img_w Kernel_size : K In channel: Cin Out channel: Cout Output Image: img_h*img_w

However, the result I calculated has some error with the value obtained from the formula. I think this error value is an error for the Bias calculation (Add).

image
    def get_model_GMACs(self, modelpath):
        import onnx_tool
        m = onnx_tool.Model(modelpath)
        m.graph.shape_infer(self.make_dummy_input())  
        m.graph.profile()
        return m.graph.macs / 1_000_000_000

Calculate the MAC of the onnx model using the following formula. What formula is used to calculate the value of onnx_tool.Model(modelpath).graph.macs provided by onnx-tool?

ThanatosShinji commented 11 months ago

The same as yours: Conv2d MACs =K*K*Cin*Cout*img*img_w except if bias exists, then Conv2d MACs+=Cout

ohjunee commented 11 months ago
image

In my case, if bias exists, Conv2d MACs+=Ch*Layers*Imgsz*Imgsz K: Kernel Size Ch: Channel (In_channels = Out_channels) Imgsz: Image Size (Width = Height) Layers: Number of layers (modules)

The MAC calculation for the above equation is almost similar to the macs value calculated through onnx-tool, and an error occurs by adding only the output channel as in the equation below.

Conv2d MACs+=Cout Is it correct that the Cout in the above equation means the output channel number of layers number of input pixels?

Thank you for your help.

ThanatosShinji commented 11 months ago

Sorry, I've made a mistake. Conv2D MACs+=Cout is not correct. It should be MACs+=Cout*img_h*img_w*batch, as you can find the formula code here