gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
858 stars 143 forks source link

Add support for Conv1D #107

Open syomantak opened 4 years ago

syomantak commented 4 years ago

Feature request It would be great if you can add support for conv1D as well!

londumas commented 3 years ago

Here is a minimal failing example:

from torch import nn
import numpy as np
from torch.autograd import Variable
import torch

class TestConv1d(nn.Module):
    """
    Module for Conv2d testing
    """

    def __init__(self, inp=10, out=16, kernel_size=3):
        super(TestConv1d, self).__init__()
        self.conv1d = nn.Conv1d(inp, out, stride=1, kernel_size=kernel_size, bias=True)

    def forward(self, x):
        x = self.conv1d(x)
        return x

model = TestConv1d()

input_np = np.random.uniform(0, 1, (1, 10, 32))
input_var = Variable(torch.FloatTensor(input_np))

from pytorch2keras import pytorch_to_keras
# we should specify shape of the input tensor
k_model = pytorch_to_keras(model, input_var, [(10, 32,)], verbose=True)

Results:

INFO:pytorch2keras:Converter is called.
WARNING:pytorch2keras:Custom shapes isn't supported now.
DEBUG:pytorch2keras:Input_names:
DEBUG:pytorch2keras:['input_0']
DEBUG:pytorch2keras:Output_names:
DEBUG:pytorch2keras:['output_0']
INFO:onnx2keras:Converter is called.
DEBUG:onnx2keras:List input shapes:
DEBUG:onnx2keras:[(10, 32)]
DEBUG:onnx2keras:List inputs:
DEBUG:onnx2keras:Input 0 -> input_0.
DEBUG:onnx2keras:List outputs:
DEBUG:onnx2keras:Output 0 -> output_0.
DEBUG:onnx2keras:Gathering weights to dictionary.
DEBUG:onnx2keras:Found weight conv1d.weight with shape (16, 10, 3).
DEBUG:onnx2keras:Found weight conv1d.bias with shape (16,).
DEBUG:onnx2keras:Found input input_0 with shape (10, 32)
DEBUG:onnx2keras:######
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Converting ONNX operation
DEBUG:onnx2keras:type: Conv
DEBUG:onnx2keras:node_name: output_0
DEBUG:onnx2keras:node_params: {'dilations': [1], 'group': 1, 'kernel_shape': [3], 'pads': [0, 0], 'strides': [1], 'change_ordering': False, 'name_policy': None}
DEBUG:onnx2keras:...
DEBUG:onnx2keras:Check if all inputs are available:
DEBUG:onnx2keras:Check input 0 (name input_0).
DEBUG:onnx2keras:Check input 1 (name conv1d.weight).
DEBUG:onnx2keras:The input not found in layers / model inputs.
DEBUG:onnx2keras:Found in weights, add as a numpy constant.
DEBUG:onnx2keras:Check input 2 (name conv1d.bias).
DEBUG:onnx2keras:The input not found in layers / model inputs.
DEBUG:onnx2keras:Found in weights, add as a numpy constant.
DEBUG:onnx2keras:... found all, continue
DEBUG:onnx2keras:conv:Conv with bias
graph(%input_0 : Float(1, 10, 32, strides=[320, 32, 1], requires_grad=0, device=cpu),
      %conv1d.weight : Float(16, 10, 3, strides=[30, 3, 1], requires_grad=1, device=cpu),
      %conv1d.bias : Float(16, strides=[1], requires_grad=1, device=cpu)):
  %output_0 : Float(1, 16, 30, strides=[480, 30, 1], requires_grad=1, device=cpu) = onnx::Conv[dilations=[1], group=1, kernel_shape=[3], pads=[0, 0], strides=[1]](%input_0, %conv1d.weight, %conv1d.bias) # /home/<me>/.local/lib/python3.7/site-packages/torch/nn/modules/conv.py:295:0
  return (%output_0)

3 10 16 True

TypeErrorTraceback (most recent call last)
<ipython-input-24-ad3fc126a0ef> in <module>
      1 from pytorch2keras import pytorch_to_keras
      2 # we should specify shape of the input tensor
----> 3 k_model = pytorch_to_keras(model, input_var, [(10, 32,)], verbose=True)

~/.local/lib/python3.7/site-packages/pytorch2keras/converter.py in pytorch_to_keras(model, args, input_shapes, change_ordering, verbose, name_policy, use_optimizer, do_constant_folding)
     81     k_model = onnx_to_keras(onnx_model=onnx_model, input_names=input_names,
     82                             input_shapes=input_shapes, name_policy=name_policy,
---> 83                             verbose=verbose, change_ordering=change_ordering)
     84 
     85     return k_model

~/.local/lib/python3.7/site-packages/onnx2keras/converter.py in onnx_to_keras(onnx_model, input_names, input_shapes, name_policy, verbose, change_ordering)
    179             lambda_funcs,
    180             node_name,
--> 181             keras_names
    182         )
    183         if isinstance(keras_names, list):

~/.local/lib/python3.7/site-packages/onnx2keras/convolution_layers.py in convert_conv(node, params, layers, lambda_func, node_name, keras_name)
    195 
    196         lambda_layer = keras.layers.Lambda(target_layer, name=keras_name)
--> 197         lambda_layer[keras_name] = target_layer
    198         layers[node_name] = lambda_layer(input_0)
    199 

TypeError: 'Lambda' object does not support item assignment
londumas commented 3 years ago

Linked to https://github.com/gmalivenko/onnx2keras/issues/120