{
  "id": 175404,
  "title": "Did anyone train on pytorch and convert it to tensorflow model?",
  "url": "/competitions/landmark-retrieval-2020/discussion/175404",
  "author_name": "",
  "post_date": "2020-08-18T05:16:39.412933300Z",
  "votes": 1,
  "comment_count": 1,
  "views": 0,
  "content": "<p>I'm personally not familiar with this approach. However, I noticed that pytorch provides better options with multi-gpu setup , than tensorflow. Something, I really struggled with. I welcome all to share any personal experience on this, even if it is not related to this competition. </p>",
  "messages": [
    {
      "id": "974967",
      "postDate": "08/18/2020 05:16:39",
      "content": "<p>I'm personally not familiar with this approach. However, I noticed that pytorch provides better options with multi-gpu setup , than tensorflow. Something, I really struggled with. I welcome all to share any personal experience on this, even if it is not related to this competition. </p>",
      "rawMarkdown": "I'm personally not familiar with this approach. However, I noticed that pytorch provides better options with multi-gpu setup , than tensorflow. Something, I really struggled with. I welcome all to share any personal experience on this, even if it is not related to this competition.",
      "votes": null
    },
    {
      "id": "975819",
      "postDate": "08/18/2020 13:36:50",
      "content": "<p>Check out the package: <a href=\"https://github.com/nerox8664/pytorch2keras\" target=\"_blank\">https://github.com/nerox8664/pytorch2keras</a></p>\n<p>Example that shows how it could be used in this competition:</p>\n<pre><code>import numpy as np\nimport tensorflow as tf\nimport torch\nimport torchvision\nfrom pytorch2keras import pytorch_to_keras\n\n\ndef create_torch_model(shape=(3, 300, 300)):\n    model = torchvision.models.resnet18(pretrained=True)\n    x = torch.randn(1, *shape)\n    return pytorch_to_keras(model, x, [(3, None, None)], verbose=False, change_ordering=False)\n\n\nclass MyModel(tf.keras.Model):\n    def __init__(self):\n        super(MyModel, self).__init__()\n        self.model = create_torch_model()\n\n    @tf.function(input_signature=[tf.TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name='input_image')])\n    def call(self, x):\n        x = tf.expand_dims(x, 0)\n        x = tf.keras.backend.permute_dimensions(x, (0, 3, 1, 2))\n        x = self.model(x)\n        named_output_tensors = {'global_descriptor': tf.identity(x, name='global_descriptor')}\n        return named_output_tensors\n\n\nif __name__ == '__main__':\n    model = MyModel()\n\n    image = np.zeros((300, 300, 3)).astype(np.uint8)\n    tf_image = tf.convert_to_tensor(image)\n\n    out = model.call(tf_image)['global_descriptor'].numpy()\n\n    assert out.shape == (1, 1000)\n</code></pre>\n<p>Unfortunately some layers/architectures do not work out of box, but it could be fixed by patching the source codes.</p>",
      "rawMarkdown": "Check out the package: https://github.com/nerox8664/pytorch2keras\n\nExample that shows how it could be used in this competition:\n```\nimport numpy as np\nimport tensorflow as tf\nimport torch\nimport torchvision\nfrom pytorch2keras import pytorch_to_keras\n\n\ndef create_torch_model(shape=(3, 300, 300)):\n    model = torchvision.models.resnet18(pretrained=True)\n    x = torch.randn(1, *shape)\n    return pytorch_to_keras(model, x, [(3, None, None)], verbose=False, change_ordering=False)\n\n\nclass MyModel(tf.keras.Model):\n    def __init__(self):\n        super(MyModel, self).__init__()\n        self.model = create_torch_model()\n\n    @tf.function(input_signature=[tf.TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name='input_image')])\n    def call(self, x):\n        x = tf.expand_dims(x, 0)\n        x = tf.keras.backend.permute_dimensions(x, (0, 3, 1, 2))\n        x = self.model(x)\n        named_output_tensors = {'global_descriptor': tf.identity(x, name='global_descriptor')}\n        return named_output_tensors\n\n\nif __name__ == '__main__':\n    model = MyModel()\n\n    image = np.zeros((300, 300, 3)).astype(np.uint8)\n    tf_image = tf.convert_to_tensor(image)\n\n    out = model.call(tf_image)['global_descriptor'].numpy()\n\n    assert out.shape == (1, 1000)\n```\n\nUnfortunately some layers/architectures do not work out of box, but it could be fixed by patching the source codes.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 975819,
      "author_name": "u1234x1234",
      "author_url": "",
      "post_date": "08/18/2020 13:36:50",
      "content": "<p>Check out the package: <a href=\"https://github.com/nerox8664/pytorch2keras\" target=\"_blank\">https://github.com/nerox8664/pytorch2keras</a></p>\n<p>Example that shows how it could be used in this competition:</p>\n<pre><code>import numpy as np\nimport tensorflow as tf\nimport torch\nimport torchvision\nfrom pytorch2keras import pytorch_to_keras\n\n\ndef create_torch_model(shape=(3, 300, 300)):\n    model = torchvision.models.resnet18(pretrained=True)\n    x = torch.randn(1, *shape)\n    return pytorch_to_keras(model, x, [(3, None, None)], verbose=False, change_ordering=False)\n\n\nclass MyModel(tf.keras.Model):\n    def __init__(self):\n        super(MyModel, self).__init__()\n        self.model = create_torch_model()\n\n    @tf.function(input_signature=[tf.TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name='input_image')])\n    def call(self, x):\n        x = tf.expand_dims(x, 0)\n        x = tf.keras.backend.permute_dimensions(x, (0, 3, 1, 2))\n        x = self.model(x)\n        named_output_tensors = {'global_descriptor': tf.identity(x, name='global_descriptor')}\n        return named_output_tensors\n\n\nif __name__ == '__main__':\n    model = MyModel()\n\n    image = np.zeros((300, 300, 3)).astype(np.uint8)\n    tf_image = tf.convert_to_tensor(image)\n\n    out = model.call(tf_image)['global_descriptor'].numpy()\n\n    assert out.shape == (1, 1000)\n</code></pre>\n<p>Unfortunately some layers/architectures do not work out of box, but it could be fixed by patching the source codes.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "974967": "I'm personally not familiar with this approach. However, I noticed that pytorch provides better options with multi-gpu setup , than tensorflow. Something, I really struggled with. I welcome all to share any personal experience on this, even if it is not related to this competition.",
    "975819": "Check out the package: https://github.com/nerox8664/pytorch2keras\n\nExample that shows how it could be used in this competition:\n```\nimport numpy as np\nimport tensorflow as tf\nimport torch\nimport torchvision\nfrom pytorch2keras import pytorch_to_keras\n\n\ndef create_torch_model(shape=(3, 300, 300)):\n    model = torchvision.models.resnet18(pretrained=True)\n    x = torch.randn(1, *shape)\n    return pytorch_to_keras(model, x, [(3, None, None)], verbose=False, change_ordering=False)\n\n\nclass MyModel(tf.keras.Model):\n    def __init__(self):\n        super(MyModel, self).__init__()\n        self.model = create_torch_model()\n\n    @tf.function(input_signature=[tf.TensorSpec(shape=(None, None, 3), dtype=tf.uint8, name='input_image')])\n    def call(self, x):\n        x = tf.expand_dims(x, 0)\n        x = tf.keras.backend.permute_dimensions(x, (0, 3, 1, 2))\n        x = self.model(x)\n        named_output_tensors = {'global_descriptor': tf.identity(x, name='global_descriptor')}\n        return named_output_tensors\n\n\nif __name__ == '__main__':\n    model = MyModel()\n\n    image = np.zeros((300, 300, 3)).astype(np.uint8)\n    tf_image = tf.convert_to_tensor(image)\n\n    out = model.call(tf_image)['global_descriptor'].numpy()\n\n    assert out.shape == (1, 1000)\n```\n\nUnfortunately some layers/architectures do not work out of box, but it could be fixed by patching the source codes."
  },
  "source": "meta"
}