Code Monkey home page Code Monkey logo

Comments (7)

MPolaris avatar MPolaris commented on June 18, 2024

That's a good question.
Because tensorflow convolution padding is inconsistent with pytorch, tensorflow 'SAME' padding may pad more pixels on the right than left.
You can see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch.
If you get a good to fix this problem, welcome PR.

from onnx2tflite.

yutaka329 avatar yutaka329 commented on June 18, 2024

thanks, may be solved with some trick.

from onnx2tflite.

yutaka329 avatar yutaka329 commented on June 18, 2024

Hi
I do some tests.
because network has many 3x3 conv with same padding.
I change the code with below code on mobileone. it can reduce many pad and get the same result, reduce the peak of mobile running time. so we can use this trick^^.

  if pads is not None and max(pads) == 1 and max(strides) == 1:
        self.conv = keras.layers.DepthwiseConv2D(
            kernel_size, strides, "SAME", use_bias=False if bias is None else True,
            weights=[weights] if bias is None else [weights, bias],
            dilation_rate=dilations,
            activation=None,
            kernel_initializer='zeros',
            bias_initializer='zeros'
        )
        self.pad =None

from onnx2tflite.

MPolaris avatar MPolaris commented on June 18, 2024

Hi I do some tests. because network has many 3x3 conv with same padding. I change the code with below code on mobileone. it can reduce many pad and get the same result, reduce the peak of mobile running time. so we can use this trick^^.

  if pads is not None and max(pads) == 1 and max(strides) == 1:
        self.conv = keras.layers.DepthwiseConv2D(
            kernel_size, strides, "SAME", use_bias=False if bias is None else True,
            weights=[weights] if bias is None else [weights, bias],
            dilation_rate=dilations,
            activation=None,
            kernel_initializer='zeros',
            bias_initializer='zeros'
        )
        self.pad =None

Nice idea, thanks for your test.
Can you evaluate it on other types of convolution, such as keras.layers.Conv2D and keras.layers.Conv2DTranspose.
It's pleasure for your PR.

from onnx2tflite.

yutaka329 avatar yutaka329 commented on June 18, 2024

DepthwiseConv2D should be Consistent with Conv2D. As to Conv2DTranspose, it need to be tested.

from onnx2tflite.

MPolaris avatar MPolaris commented on June 18, 2024

Thanks for your nice work again, I will test it as soon~

from onnx2tflite.

MPolaris avatar MPolaris commented on June 18, 2024

I was tested it on this model.
Your trick is work well for conv\depthwise conv\group conv, but not with ConvTranspose, thanks for your contribution again.

import torch
import torch.nn as nn

class testmodel(nn.Module):
    def __init__(self) -> None:
        super(testmodel, self).__init__()
        self.conv1 = nn.Conv2d(3, 6, 3, 2, 1)
        self.conv2 = nn.Conv2d(3, 6, 3, 1, 1)
        self.depthwiseConv1 = nn.Conv2d(3, 6, 3, 2, 1, groups=3)
        self.depthwiseConv2 = nn.Conv2d(3, 6, 3, 1, 1, groups=3)
        self.convTranspose1 = nn.ConvTranspose2d(6, 3, 2, 2, 0)
        self.convTranspose2 = nn.ConvTranspose2d(6, 30, 2, 2, 0)
        self.group_conv1 = nn.Conv2d(30, 30, 3, 1, 1, groups=30)

    def forward(self, X):
        x1 = self.conv1(X)
        x2 = self.conv2(X)

        x3 = self.depthwiseConv1(X)
        x4 = self.depthwiseConv2(X)
        
        x5 = self.convTranspose1(x1)
        x6 = self.convTranspose1(x3)

        out = x2+x4+torch.cat((x5, x6), dim=1)
        out = self.convTranspose2(out)
        out = self.group_conv1(out)
        return out

model = testmodel()
X = torch.randn(1, 3, 224, 224)
torch.onnx.export(model, X, './models/opt_test.onnx', opset_version=12)

from onnx2tflite.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.