{ "info": { "author": "Grigory Malivenko", "author_email": "nerox8664@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Intended Audience :: Science/Research", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python", "Topic :: Scientific/Engineering :: Image Recognition" ], "description": "# pytorch2keras\n\n[![Build Status](https://travis-ci.com/nerox8664/pytorch2keras.svg?branch=master)](https://travis-ci.com/nerox8664/pytorch2keras)\n[![GitHub License](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)\n[![Python Version](https://img.shields.io/badge/python-2.7%2C3.6-lightgrey.svg)](https://github.com/nerox8664/pytorch2keras)\n[![Downloads](https://pepy.tech/badge/pytorch2keras)](https://pepy.tech/project/pytorch2keras)\n![PyPI](https://img.shields.io/pypi/v/pytorch2keras.svg)\n[![Readthedocs](https://img.shields.io/readthedocs/pytorch2keras.svg)](https://pytorch2keras.readthedocs.io/en/latest/)\n\n\nPyTorch to Keras model converter.\n\n## Installation\n\n```\npip install pytorch2keras \n```\n\n## Important notice\n\nTo use the converter properly, please, make changes in your `~/.keras/keras.json`:\n\n\n```\n...\n\"backend\": \"tensorflow\",\n\"image_data_format\": \"channels_first\",\n...\n```\n\n## Tensorflow.js\n\nFor the proper conversion to a tensorflow.js format, please use the new flag `names='short'`.\n\nHere is a short instruction how to get a tensorflow.js model:\n\n1. First of all, you have to convert your model to Keras with this converter:\n\n```\nk_model = pytorch_to_keras(model, input_var, [(10, 32, 32,)], verbose=True, names='short') \n```\n\n2. Now you have Keras model. You can save it as h5 file and then convert it with `tensorflowjs_converter` but it doesn't work sometimes. As alternative, you may get Tensorflow Graph and save it as a frozen model:\n\n```\n# Function below copied from here:\n# https://stackoverflow.com/questions/45466020/how-to-export-keras-h5-to-tensorflow-pb \ndef freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):\n \"\"\"\n Freezes the state of a session into a pruned computation graph.\n\n Creates a new computation graph where variable nodes are replaced by\n constants taking their current value in the session. The new graph will be\n pruned so subgraphs that are not necessary to compute the requested\n outputs are removed.\n @param session The TensorFlow session to be frozen.\n @param keep_var_names A list of variable names that should not be frozen,\n or None to freeze all the variables in the graph.\n @param output_names Names of the relevant graph outputs.\n @param clear_devices Remove the device directives from the graph for better portability.\n @return The frozen graph definition.\n \"\"\"\n from tensorflow.python.framework.graph_util import convert_variables_to_constants\n graph = session.graph\n with graph.as_default():\n freeze_var_names = \\\n list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))\n output_names = output_names or []\n output_names += [v.op.name for v in tf.global_variables()]\n input_graph_def = graph.as_graph_def()\n if clear_devices:\n for node in input_graph_def.node:\n node.device = \"\"\n frozen_graph = convert_variables_to_constants(session, input_graph_def,\n output_names, freeze_var_names)\n return frozen_graph\n\n\nfrom keras import backend as K\nimport tensorflow as tf\nfrozen_graph = freeze_session(K.get_session(),\n output_names=[out.op.name for out in k_model.outputs])\n\ntf.train.write_graph(frozen_graph, \".\", \"my_model.pb\", as_text=False)\nprint([i for i in k_model.outputs])\n\n```\n\n3. You will see the output layer name, so, now it's time to convert `my_model.pb` to tfjs model:\n\n```\ntensorflowjs_converter \\\n --input_format=tf_frozen_model \\\n --output_node_names='TANHTObs/Tanh' \\\n my_model.pb \\\n model_tfjs\n```\n\n4. Thats all!\n\n```\nconst MODEL_URL = `model_tfjs/tensorflowjs_model.pb`;\nconst WEIGHTS_URL = `model_tfjs/weights_manifest.json`;\ncont model = await tf.loadFrozenModel(MODEL_URL, WEIGHTS_URL);\n```\n\n## How to use\n\nIt's the converter of PyTorch graph to a Keras (Tensorflow backend) model.\n\nFirstly, we need to load (or create) a valid PyTorch model:\n\n```\nclass TestConv2d(nn.Module):\n \"\"\"\n Module for Conv2d testing\n \"\"\"\n\n def __init__(self, inp=10, out=16, kernel_size=3):\n super(TestConv2d, self).__init__()\n self.conv2d = nn.Conv2d(inp, out, stride=1, kernel_size=kernel_size, bias=True)\n\n def forward(self, x):\n x = self.conv2d(x)\n return x\n\nmodel = TestConv2d()\n\n# load weights here\n# model.load_state_dict(torch.load(path_to_weights.pth))\n```\n\nThe next step - create a dummy variable with correct shape:\n\n```\ninput_np = np.random.uniform(0, 1, (1, 10, 32, 32))\ninput_var = Variable(torch.FloatTensor(input_np))\n```\n\nWe use the dummy-variable to trace the model (with jit.trace):\n\n```\nfrom pytorch2keras import pytorch_to_keras\n# we should specify shape of the input tensor\nk_model = pytorch_to_keras(model, input_var, [(10, 32, 32,)], verbose=True) \n```\n\nYou can also set H and W dimensions to None to make your model shape-agnostic (e.g. fully convolutional netowrk):\n\n```\nfrom pytorch2keras.converter import pytorch_to_keras\n# we should specify shape of the input tensor\nk_model = pytorch_to_keras(model, input_var, [(10, None, None,)], verbose=True) \n```\n\nThat's all! If all the modules have converted properly, the Keras model will be stored in the `k_model` variable.\n\n\n## API\n\nHere is the only method `pytorch_to_keras` from `pytorch2keras` module.\n\n```\ndef pytorch_to_keras(\n model, args, input_shapes=None,\n change_ordering=False, verbose=False, name_policy=None,\n):\n```\n\nOptions:\n\n* `model` - a PyTorch model (nn.Module) to convert;\n* `args` - a list of dummy variables with proper shapes;\n* `input_shapes` - (experimental) list with overrided shapes for inputs;\n* `change_ordering` - (experimental) boolean, if enabled, the converter will try to change `BCHW` to `BHWC`\n* `verbose` - boolean, detailed log of conversion\n* `name_policy` - (experimental) choice from [`keep`, `short`, `random`]. The selector set the target layer naming policy.\n\n## Supported layers\n\n* Activations:\n + ReLU\n + LeakyReLU\n + SELU\n + Sigmoid\n + Softmax\n + Tanh\n\n* Constants\n\n* Convolutions:\n + Conv2d\n + ConvTrsnpose2d\n\n* Element-wise:\n + Add\n + Mul\n + Sub\n + Div\n\n* Linear\n\n* Normalizations:\n + BatchNorm2d\n + InstanceNorm2d\n\n* Poolings:\n + MaxPool2d\n + AvgPool2d\n + Global MaxPool2d (adaptive pooling to shape [1, 1])\n\n\n## Models converted with pytorch2keras\n\n* ResNet*\n* VGG*\n* PreResNet*\n* DenseNet*\n* AlexNet\n* Mobilenet v2\n\n## Usage\nLook at the `tests` directory.\n\n## License\nThis software is covered by MIT License.", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/nerox8664/pytorch2keras", "keywords": "machine-learning deep-learning pytorch keras neuralnetwork vgg resnet densenet drn dpn darknet squeezenet mobilenet", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "pytorch2keras", "package_url": "https://pypi.org/project/pytorch2keras/", "platform": "", "project_url": "https://pypi.org/project/pytorch2keras/", "project_urls": { "Homepage": "https://github.com/nerox8664/pytorch2keras" }, "release_url": "https://pypi.org/project/pytorch2keras/0.2.3/", "requires_dist": null, "requires_python": "", "summary": "The deep learning models convertor", "version": "0.2.3" }, "last_serial": 5458562, "releases": { "0.1.10": [ { "comment_text": "", "digests": { "md5": "dc228c453148654dc8cffdd438453ba3", "sha256": "84e079e9182d4c9d01d7894cd11eb5a15edefadb495e0d04e5f05d8dfddeed25" }, "downloads": -1, "filename": "pytorch2keras-0.1.10.tar.gz", "has_sig": false, "md5_digest": "dc228c453148654dc8cffdd438453ba3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 28238, "upload_time": "2018-11-29T08:50:55", "url": "https://files.pythonhosted.org/packages/e5/b5/219f20e3475fc2abe56a51aa17485fca9b5a1749246dbf0b05e0cde4a922/pytorch2keras-0.1.10.tar.gz" } ], "0.1.11": [ { "comment_text": "", "digests": { "md5": "a7bc5d7a9fbaa15e21d0b4e1b47058cc", "sha256": "b873f4027f6844da47d178b46dd4418f0f9be674de746a427c9d90dbc6c0dfb1" }, "downloads": -1, "filename": "pytorch2keras-0.1.11.tar.gz", "has_sig": false, "md5_digest": "a7bc5d7a9fbaa15e21d0b4e1b47058cc", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 29954, "upload_time": "2018-12-05T14:13:44", "url": "https://files.pythonhosted.org/packages/70/0b/c7ebebf86171c5af6ed90bbec771e4756a100e09a46c17374087b8d6c7c4/pytorch2keras-0.1.11.tar.gz" } ], "0.1.12": [ { "comment_text": "", "digests": { "md5": "46fca074cf048c42ffdf9492ab843c1f", "sha256": "e000999caee615a5339449f3ed0bcc4ae3829c18519c32608f6d19e803538ab7" }, "downloads": -1, "filename": "pytorch2keras-0.1.12.tar.gz", "has_sig": false, "md5_digest": "46fca074cf048c42ffdf9492ab843c1f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 29776, "upload_time": "2018-12-10T13:08:53", "url": "https://files.pythonhosted.org/packages/4b/2f/b897e804622ff05af6f36ae0582b0ad4e098ac7954d9a9fb66018ce5bd0d/pytorch2keras-0.1.12.tar.gz" } ], "0.1.13": [ { "comment_text": "", "digests": { "md5": "818b6fed9300ba220258b34f11977a5e", "sha256": "d587711f33634755d047cbaac3ba8644491e052c2c45aa6f60f1710d2cff5466" }, "downloads": -1, "filename": "pytorch2keras-0.1.13.tar.gz", "has_sig": false, "md5_digest": "818b6fed9300ba220258b34f11977a5e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 29811, "upload_time": "2018-12-21T10:24:42", "url": "https://files.pythonhosted.org/packages/49/85/d989ccd204fdc0f20e62e8cd44fa944e0802343fff2a4655513085233b51/pytorch2keras-0.1.13.tar.gz" } ], "0.1.14": [ { "comment_text": "", "digests": { "md5": "3e13e8ba9945ed6e14fbbad87064e79b", "sha256": "1c141d156534f70982d33aed5c98ddb720cf00ad5f6f377caa4e69d9191d2c57" }, "downloads": -1, "filename": "pytorch2keras-0.1.14.tar.gz", "has_sig": false, "md5_digest": "3e13e8ba9945ed6e14fbbad87064e79b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 30412, "upload_time": "2019-01-28T17:53:35", "url": "https://files.pythonhosted.org/packages/48/21/6fc98aa74f060be61b2c831153cbaa1abe6ed39579cc322f9f6b04ac5f60/pytorch2keras-0.1.14.tar.gz" } ], "0.1.15": [ { "comment_text": "", "digests": { "md5": "8d4d5b956e1e241dc2f3c7f6b9a1ff93", "sha256": "959db3ae494dc3edec74627a4650c20cdcc4d98b176940f1f13d90fd27e0b656" }, "downloads": -1, "filename": "pytorch2keras-0.1.15.tar.gz", "has_sig": false, "md5_digest": "8d4d5b956e1e241dc2f3c7f6b9a1ff93", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 30440, "upload_time": "2019-01-29T11:08:43", "url": "https://files.pythonhosted.org/packages/fe/48/ccd8161568f678f99d46e0212347474588ff4adb4232512a3939234ac869/pytorch2keras-0.1.15.tar.gz" } ], "0.1.16": [ { "comment_text": "", "digests": { "md5": "e4ed7323dffd7fbe7fc6470b1aa3a8ee", "sha256": "4836acaef650ff6a5b2499f2f0ffd4b74195b9dc7e8262697a797bcfec8f3da8" }, "downloads": -1, "filename": "pytorch2keras-0.1.16.tar.gz", "has_sig": false, "md5_digest": "e4ed7323dffd7fbe7fc6470b1aa3a8ee", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 31725, "upload_time": "2019-03-11T15:08:14", "url": "https://files.pythonhosted.org/packages/42/ac/9281601ee9e6bc1d255fd2307acc3a58a25e680362ab837743481fc17e00/pytorch2keras-0.1.16.tar.gz" } ], "0.1.17": [ { "comment_text": "", "digests": { "md5": "0ae81708a1dd37ebc7498cb94964e214", "sha256": "d7305a1a388caf6a5ad12fa760fe7bf5f2b39c732d613601428dbf7dab6b2281" }, "downloads": -1, "filename": "pytorch2keras-0.1.17.tar.gz", "has_sig": false, "md5_digest": "0ae81708a1dd37ebc7498cb94964e214", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 32089, "upload_time": "2019-05-13T09:33:17", "url": "https://files.pythonhosted.org/packages/f7/21/3805bb4e89c11ff5f001f73d4fc03d6ca51a1895a62cfc7c10e2d5840e36/pytorch2keras-0.1.17.tar.gz" } ], "0.1.18": [ { "comment_text": "", "digests": { "md5": "355b8dfee31e5657bf7696bfe46aa8c3", "sha256": "0ce73e02e11ef28953aef9fe85239ddd0ebcf43fa4f0c89564a594ac4d9cbb24" }, "downloads": -1, "filename": "pytorch2keras-0.1.18.tar.gz", "has_sig": false, "md5_digest": "355b8dfee31e5657bf7696bfe46aa8c3", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 32112, "upload_time": "2019-06-13T12:42:39", "url": "https://files.pythonhosted.org/packages/dd/f0/d09d0b06809df3a1bc2c60f0cf36f98d8a09edf4982a56aea25100cb5c45/pytorch2keras-0.1.18.tar.gz" } ], "0.1.19": [ { "comment_text": "", "digests": { "md5": "11dedd0d920083a1c576ddf60e3846aa", "sha256": "087e75e68d6539d2a3cd4e5bd8407b81f3da0801126976b856b7d4e7f0756a0a" }, "downloads": -1, "filename": "pytorch2keras-0.1.19.tar.gz", "has_sig": false, "md5_digest": "11dedd0d920083a1c576ddf60e3846aa", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 32762, "upload_time": "2019-06-13T14:11:14", "url": "https://files.pythonhosted.org/packages/8d/36/493370e7d871d7e93bb1d1a2e2b8beb7ab1264edcbec30f204cd3ad99789/pytorch2keras-0.1.19.tar.gz" } ], "0.1.5": [ { "comment_text": "", "digests": { "md5": "dc56b19bd291a0f05ec31ef81b301b8f", "sha256": "fdb76596d6bcfdcf301522f4530f869e11567b75e270fe7c448c8477f89cb4cc" }, "downloads": -1, "filename": "pytorch2keras-0.1.5-py3-none-any.whl", "has_sig": false, "md5_digest": "dc56b19bd291a0f05ec31ef81b301b8f", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 78006, "upload_time": "2018-10-16T18:34:39", "url": "https://files.pythonhosted.org/packages/25/08/206b9c66b9f54c3155f24e8b019ecee48b2f75da357e5bd75e04c53dfefb/pytorch2keras-0.1.5-py3-none-any.whl" } ], "0.1.6": [ { "comment_text": "", "digests": { "md5": "d519d7d3ce297c95a1d5c26542deb35c", "sha256": "0f2957835e9f6a55d2927feed27b5c5f46a54c3723494952c045f061236868e4" }, "downloads": -1, "filename": "pytorch2keras-0.1.6.tar.gz", "has_sig": false, "md5_digest": "d519d7d3ce297c95a1d5c26542deb35c", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23433, "upload_time": "2018-11-13T16:34:00", "url": "https://files.pythonhosted.org/packages/2f/61/9fd96f62814ddf7d0a3d60db85be6965451c7d6efd8d5c52b1877db68bd3/pytorch2keras-0.1.6.tar.gz" } ], "0.1.7": [ { "comment_text": "", "digests": { "md5": "db6d92a521344a5fe514962a013664a1", "sha256": "0d10d27c7ba9c65dd4defca4313b7e6a7f5de7be2de3a7e5d237eef25749275b" }, "downloads": -1, "filename": "pytorch2keras-0.1.7.tar.gz", "has_sig": false, "md5_digest": "db6d92a521344a5fe514962a013664a1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 27193, "upload_time": "2018-11-24T16:40:17", "url": "https://files.pythonhosted.org/packages/30/bc/c5b88b00b3e31a1b9035a35ddeb36d96d537c970013ad17dac086aba82ba/pytorch2keras-0.1.7.tar.gz" } ], "0.1.8": [ { "comment_text": "", "digests": { "md5": "2fadf63632a8f8f5bae2156f5742b425", "sha256": "1408e72419ba53bc11817c7f47471c85aba2e3ca819275eb42c40ad3c3386602" }, "downloads": -1, "filename": "pytorch2keras-0.1.8.tar.gz", "has_sig": false, "md5_digest": "2fadf63632a8f8f5bae2156f5742b425", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 27833, "upload_time": "2018-11-28T21:02:53", "url": "https://files.pythonhosted.org/packages/38/3f/b04827ab0298525ed9bfc1cc3542b9aab30297c5a02084f4b1ec47d906d2/pytorch2keras-0.1.8.tar.gz" } ], "0.1.9": [ { "comment_text": "", "digests": { "md5": "23bd4356432c5218562d5e68c8e44cdf", "sha256": "3880876a451b95ee0fa3ee6c7d73e6bde87fa5c52716ed0e65b44dcb89cba093" }, "downloads": -1, "filename": "pytorch2keras-0.1.9.tar.gz", "has_sig": false, "md5_digest": "23bd4356432c5218562d5e68c8e44cdf", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 27932, "upload_time": "2018-11-28T21:18:15", "url": "https://files.pythonhosted.org/packages/3a/86/757a3e9336f6920dc2bdad3d4e86acfbb7654f2e05670b819247b1597fd2/pytorch2keras-0.1.9.tar.gz" } ], "0.2.0": [ { "comment_text": "", "digests": { "md5": "7b31c639f6463cb857e1cb9dfda788ca", "sha256": "9098e1ba8d2200e71f44433749e2702e95cabcab27bde5b66bcaa8896c46bd4f" }, "downloads": -1, "filename": "pytorch2keras-0.2.0.tar.gz", "has_sig": false, "md5_digest": "7b31c639f6463cb857e1cb9dfda788ca", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 20560, "upload_time": "2019-06-27T08:41:35", "url": "https://files.pythonhosted.org/packages/fa/82/32aa8983723a6e6c412b14a3471931800bca5301dfbf4aff7ec812244a8e/pytorch2keras-0.2.0.tar.gz" } ], "0.2.1": [ { "comment_text": "", "digests": { "md5": "485e9c00500ae4fd7c4744270f449617", "sha256": "91acae7e288888f6641cd5a5334466efd5197317d0bfbcce49d041ce87c2345a" }, "downloads": -1, "filename": "pytorch2keras-0.2.1.tar.gz", "has_sig": false, "md5_digest": "485e9c00500ae4fd7c4744270f449617", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 20523, "upload_time": "2019-06-27T09:00:56", "url": "https://files.pythonhosted.org/packages/25/49/22c1408189794e058f24447119f1e1153f38437bb4de3a21c175fb0c5d7d/pytorch2keras-0.2.1.tar.gz" } ], "0.2.2": [ { "comment_text": "", "digests": { "md5": "8fe6ba9db3e1486b8650e8a720b5d25b", "sha256": "605f050c7a1866a205784320f50d72f9786be8e671bb9cc9afecb44747a8477e" }, "downloads": -1, "filename": "pytorch2keras-0.2.2.tar.gz", "has_sig": false, "md5_digest": "8fe6ba9db3e1486b8650e8a720b5d25b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 20802, "upload_time": "2019-06-27T12:01:06", "url": "https://files.pythonhosted.org/packages/8c/8a/17df2051ccb8d522c5d6635d52494f266ca9fdc606ceb718aa03cf525d5c/pytorch2keras-0.2.2.tar.gz" } ], "0.2.3": [ { "comment_text": "", "digests": { "md5": "fe2fa8ed9eabecbb8cdbe379d20ee557", "sha256": "2f220e5ec9bd9bf4da7030bbbb527ceedebf2de8367507eed605b3f41fd7b63e" }, "downloads": -1, "filename": "pytorch2keras-0.2.3.tar.gz", "has_sig": false, "md5_digest": "fe2fa8ed9eabecbb8cdbe379d20ee557", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 17718, "upload_time": "2019-06-27T19:37:47", "url": "https://files.pythonhosted.org/packages/2c/99/d2bee9623ad6792f895478cee6c7e6c3394f1f66c43d0c9e0b492b584dc9/pytorch2keras-0.2.3.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "fe2fa8ed9eabecbb8cdbe379d20ee557", "sha256": "2f220e5ec9bd9bf4da7030bbbb527ceedebf2de8367507eed605b3f41fd7b63e" }, "downloads": -1, "filename": "pytorch2keras-0.2.3.tar.gz", "has_sig": false, "md5_digest": "fe2fa8ed9eabecbb8cdbe379d20ee557", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 17718, "upload_time": "2019-06-27T19:37:47", "url": "https://files.pythonhosted.org/packages/2c/99/d2bee9623ad6792f895478cee6c7e6c3394f1f66c43d0c9e0b492b584dc9/pytorch2keras-0.2.3.tar.gz" } ] }