从喀拉拉邦的pytorch导入正确的模型

时间:2019-01-24 18:35:57

标签: keras pytorch

如何将此模型从pytorch导入到keras?我从底部开始编写模型,但是Keras和pytorch模型给出的结果不同。

class net_pytorch(torch.nn.Module):
    def __init__(self,Nin=6,Nout=1,Nlinear=112*60):
        super(vel_regressor, self).__init__()

        self.model1 = torch.nn.Sequential(
        torch.nn.Conv1d(Nin,60,kernel_size=3,stride=1,groups=Nin),
        torch.nn.ReLU(),
        torch.nn.Conv1d(60,120,kernel_size=3,stride=1,groups=Nin),
        torch.nn.ReLU(),
        torch.nn.Conv1d(120,240,kernel_size=3,stride=1),
        torch.nn.ReLU(),
        torch.nn.MaxPool1d(10, stride=6),
        )

        self.model2=model2=torch.nn.Sequential(
        torch.nn.Linear(Nlinear, 10*40),
        torch.nn.ReLU(),
        torch.nn.Linear(10*40, 100),
        torch.nn.ReLU(),
        torch.nn.Linear(100, Nout)
        )

    def forward(self, x):
        x = self.model1(x)
        x = x.view(x.size(0), -1)
        x = self.model2(x)
        return x

我如何在喀拉拉邦写它:

def net_keras():

  model2 = Sequential()

  model2.add(layers.SeparableConv1D(60, 3, strides=1, activation='relu', depth_multiplier = 6 , name = 'model1.0', input_shape=(200, 6)))
  model2.add(layers.SeparableConv1D(120, 3, strides=1, activation='relu', depth_multiplier = 6, name = 'model1.2'))
  model2.add(layers.SeparableConv1D(240, 3, strides=1, activation='relu', name = 'model1.4'))
  model2.add(layers.GlobalAveragePooling1D())
  model2.add(layers.Dense(6720, activation='relu', name = 'model2.0'))
  model2.add(layers.Dense(400, activation='relu', name = 'model2.2'))
  model2.add(layers.Dense(100, activation='relu', name = 'model2.4'))
  model2.add(layers.Dense(3))

  model2.compile(optimizer=Adam(), loss='mae')

  return model2

我尝试使用nn-transfer进行转换,但出现错误:

Layer names in PyTorch state_dict ['model1.0', 'model1.2', 'model1.4', 'model2.0', 'model2.2', 'model2.4']
Layer names in Keras HDF5 ['dense_1', 'global_average_pooling1d_1', 'model1.0', 'model1.2', 'model1.4', 'model2.0', 'model2.2', 'model2.4']
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-49-a3986379ed2b> in <module>()
----> 1 transfer.pytorch_to_keras(pytorch_network, model2)

/content/nn-transfer/nn_transfer/transfer.py in pytorch_to_keras(pytorch_model, keras_model, flip_filters, flip_channels, verbose)
    122         for layer in pytorch_layer_names:
    123 
--> 124             params = util.dig_to_params(model_weights[layer])
    125 
    126             weight_key = layer + '.weight'

/content/nn-transfer/nn_transfer/util.py in dig_to_params(keras_h5_layer)
     23     # ['dense_2']['dense_3']['conv2d_7']['dense_4']['conv1']
     24     while not _contains_weights(keras_h5_layer):
---> 25         keras_h5_layer = keras_h5_layer[list(keras_h5_layer.keys())[0]]
     26 
     27     return keras_h5_layer

AttributeError: 'Dataset' object has no attribute 'keys'

我也尝试使用pytorch2keras,但它不适用于组!=1。

MMdnn也不适用于此模型(图像错误)。

MMdnn error

1 个答案:

答案 0 :(得分:0)

这是错误的地方:

model.add(layers.Conv1D(60, 3, strides=1, activation='relu', input_shape=(None, 200), name='model1.0'))

除非输入始终为input_shape=(None,6),否则应使用BatchSizex200x6

此外,还有一些工具可以在不同架构之间转换模型,例如https://github.com/Microsoft/MMdnn