AlexanderLutsenko / nobuco

Pytorch to Keras/Tensorflow/TFLite conversion made intuitive
MIT License
272 stars 17 forks source link

Depthwise convolution conversion fails? #2

Closed dazzle-me closed 1 year ago

dazzle-me commented 1 year ago

Hi! Looks like a nice framework for pytorch->tflite conversion, however I face conversion issue when I try to convert depthwise convolution to keras, code below.

Env:

slime@slime:~$ pip freeze | grep -E "torch=|nobuco"
nobuco==0.1.1
torch==2.1.0.dev20230317+cu118

Error:

ValueError: Layer depthwise_conv1d weight shape (3, 32, 1) is not compatible with provided weight shape (3, 1, 32).

Reproducible example:

import torch.nn as nn
import torch

from nobuco.convert.converter import pytorch_to_keras
from nobuco.commons import ChannelOrder

class SimpleDepthwiseConvExample(nn.Module):
    def __init__(self, in_channels=32, out_channels=32, kernel_size=3, stride=1, n_groups=32):
        super().__init__()
        self.conv = nn.Conv1d(in_channels, out_channels, kernel_size, stride, kernel_size//2, groups=n_groups)

    def forward(self, x):
        return self.conv(x)

if __name__ == '__main__':
    bs = 1
    n_channels = 32
    n_groups = n_channels
    # n_groups = 1
    features = 144
    inputs = [
        torch.rand(bs, n_channels, features)
    ]
    model = SimpleDepthwiseConvExample(
        in_channels=n_channels,
        out_channels=n_channels,
        n_groups=n_groups
    )
    keras_model = pytorch_to_keras(
        model, inputs, inputs_channel_order=ChannelOrder.PYTORCH
    )

I think I was able to fix it in nobuco/converters/impl.py, - change weights = weights.transpose((2, 1, 0)) to weights = weights.transpose((2, 0, 1)), after the change checker seems to pass.

Can you confirm that the error is on the side of the library?

AlexanderLutsenko commented 1 year ago

Hey, thanks for reporting! Yes, it is a bug. Fixed in v0.1.4.