Git Product home page Git Product logo

pytorch2keras's Introduction

pytorch2keras

Pytorch to Keras model convertor. Very beta for now.

Important notice

To use converter properly, please, make changes in your ~/.keras/keras.json:

...
"image_data_format": "channels_first",
...

Note 1: some layers parameters (like ceiling and etc) isn't supported. Note 2: recurrent layers isn't supported too.

How to

It's a convertor of pytorch graph to a keras (tensorflow backend) graph.

Firstly, we need to load (or create) pytorch model.

For example:

class TestConv2d(nn.Module):
    """Module for Conv2d conversion testing
    """

    def __init__(self, inp=10, out=16, kernel_size=3, bias=True):
        super(TestConv2d, self).__init__()
        self.conv2d = nn.Conv2d(inp, out, stride=(inp % 3 + 1), kernel_size=kernel_size, bias=bias)

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

model = TestConv2d()
# load weights here
# model.load_state_dict(torch.load(path_to_weights.pth))

The next step - iterate model with some data (for gradients computing):

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

We've got some useless output. Now we use this output variable to traverse the graph.

from converter import pytorch_to_keras
k_model = pytorch_to_keras((10, 32, 32,), output)  #we should specify shape of the input tensor

That's all! If all is ok, the Keras model was stored to the k_model variable.

Supported layers

Layers:

  • Linear
  • Conv2d
  • ConvTranspose2d
  • MaxPool2d
  • AvgPool2d

Reshape:

  • View

Activations:

  • ReLU
  • LeakyReLU
  • PReLU
  • SELU
  • Tanh
  • Softmax
  • Softplus
  • Softsign
  • Sigmoid

Element-wise:

  • Addition
  • Multiplication
  • Subtraction

Models converted with pytorch2keras

  • ResNet18
  • ResNet50
  • SqueezeNet
  • DenseNet
  • AlexNet

Usage

Look at the tests directory.

License

This software is covered by MIT License.

pytorch2keras's People

Contributors

gmalivenko avatar

Watchers

 avatar  avatar  avatar

Forkers

rahulsingh731

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.