gmalivenko / pytorch2keras

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

Slicing with dynamic input shapes #61

Closed Kreiswolke closed 5 years ago

Kreiswolke commented 5 years ago

I am converting a pretrained model from pytorch to keras. In the model there is a slicing operator. When converting


input_np = np.random.uniform(0, 1, (1, 3, 224, 224))
input_var = Variable(torch.FloatTensor(input_np))
keras_model = pytorch_to_keras(pytorch_model, input_var, [(3, None, None)], verbose=True) 

I get errors for inputs which are not 224x224 due to the slicing operator which has a fixed [axis, start, end] attribute in onnx. Any idea how this could be made dynamically for varying input?

gmalivenko commented 5 years ago

hello @Kreiswolke. Can you show me the model definition so I can reproduce the error?

Kreiswolke commented 5 years ago

Sure, bagnet can be installed from https://github.com/wielandbrendel/bag-of-local-features-models

In the code below I am replacing the output fc layer with an Identity:

import bagnets.pytorch

pytorch_model = bagnets.pytorch.bagnet8()

class Identity(torch.nn.Module):
    def __init__(self):
        super(Identity, self).__init__()

    def forward(self, x):
        return x

pytorch_model.fc = Identity()

input_np = np.random.uniform(0, 1, (1, 3, 224, 224))
input_var = Variable(torch.FloatTensor(input_np))
k_model = pytorch_to_keras(pytorch_model, input_var, [(3, None, None)], verbose=True) 

img = np.random.random(size=(1,3,225,225))
output = k_model.predict(img)