{
  "id": 170449,
  "title": "Get shape of TensorSpec",
  "url": "/competitions/landmark-retrieval-2020/discussion/170449",
  "author_name": "",
  "post_date": "2020-07-27T17:36:39.504045200Z",
  "votes": null,
  "comment_count": 1,
  "views": 0,
  "content": "<p>Hi</p>\n\n<p>I'm trying to get shape from TensorSpec with dynamic shape.</p>\n\n<p>I used \n@mayukh18  code to create a submision (<a href=\"https://www.kaggle.com/mayukh18/creating-submission-from-your-own-model/data\">notebook</a>)</p>\n\n<p>`</p>\n\n<pre><code>@tf.function(input_signature=[\n  tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image')\n])\ndef call(self, input_image):\n    output_tensors = {}\n\n    # resizing\n    im = tf.image.resize(input_image, (224,224))\n\n    # preprocessing\n    im = preprocess_input(im)\n\n    extracted_features = self.model(tf.convert_to_tensor([im], dtype=tf.uint8))[0]\n    output_tensors['global_descriptor'] = tf.identity(extracted_features, name='global_descriptor')\n    return output_tensors\n</code></pre>\n\n<p>`</p>\n\n<p>But i can't figure it out how to get it.</p>\n\n<p>Anyone knows how to get input _ image shape? Because every time i call input _ image.shape or tf.shape(input _ image) i get always (None, None, 3) but i need the shape of the image input</p>",
  "messages": [
    {
      "id": "948153",
      "postDate": "07/27/2020 17:36:39",
      "content": "<p>Hi</p>\n\n<p>I'm trying to get shape from TensorSpec with dynamic shape.</p>\n\n<p>I used \n@mayukh18  code to create a submision (<a href=\"https://www.kaggle.com/mayukh18/creating-submission-from-your-own-model/data\">notebook</a>)</p>\n\n<p>`</p>\n\n<pre><code>@tf.function(input_signature=[\n  tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image')\n])\ndef call(self, input_image):\n    output_tensors = {}\n\n    # resizing\n    im = tf.image.resize(input_image, (224,224))\n\n    # preprocessing\n    im = preprocess_input(im)\n\n    extracted_features = self.model(tf.convert_to_tensor([im], dtype=tf.uint8))[0]\n    output_tensors['global_descriptor'] = tf.identity(extracted_features, name='global_descriptor')\n    return output_tensors\n</code></pre>\n\n<p>`</p>\n\n<p>But i can't figure it out how to get it.</p>\n\n<p>Anyone knows how to get input _ image shape? Because every time i call input _ image.shape or tf.shape(input _ image) i get always (None, None, 3) but i need the shape of the image input</p>",
      "rawMarkdown": "Hi\n\nI'm trying to get shape from TensorSpec with dynamic shape.\n\nI used \n@mayukh18  code to create a submision ([notebook](https://www.kaggle.com/mayukh18/creating-submission-from-your-own-model/data))\n\n\n`\n\n    @tf.function(input_signature=[\n      tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image')\n    ])\n    def call(self, input_image):\n        output_tensors = {}\n        \n        # resizing\n        im = tf.image.resize(input_image, (224,224))\n        \n        # preprocessing\n        im = preprocess_input(im)\n        \n        extracted_features = self.model(tf.convert_to_tensor([im], dtype=tf.uint8))[0]\n        output_tensors['global_descriptor'] = tf.identity(extracted_features, name='global_descriptor')\n        return output_tensors\n\n`\n\n\nBut i can't figure it out how to get it.\n\nAnyone knows how to get input _ image shape? Because every time i call input _ image.shape or tf.shape(input _ image) i get always (None, None, 3) but i need the shape of the image input",
      "votes": null
    },
    {
      "id": "950196",
      "postDate": "07/29/2020 08:45:48",
      "content": "<p>I finally manage to get dynamic shape of an image with variable Height and width.</p>\n\n<p>You need to us tf.shape because we are in graph mode and cast it (I looked at tf.image.central_crope code) as:</p>\n\n<p>`\n    shape = tf.shape(image_tensor)</p>\n\n<pre><code>image_height_ = shape[0]\nimage_width_ = shape[1]\n\nimage_height = math_ops.cast(image_height_, dtypes.float64)\nimage_width = math_ops.cast(image_width_, dtypes.float64)\n</code></pre>\n\n<p>`</p>",
      "rawMarkdown": "I finally manage to get dynamic shape of an image with variable Height and width.\n\nYou need to us tf.shape because we are in graph mode and cast it (I looked at tf.image.central_crope code) as:\n\n`\n    shape = tf.shape(image_tensor)\n    \n    image_height_ = shape[0]\n    image_width_ = shape[1]\n    \n    image_height = math_ops.cast(image_height_, dtypes.float64)\n    image_width = math_ops.cast(image_width_, dtypes.float64)\n`",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 950196,
      "author_name": "stenford23",
      "author_url": "",
      "post_date": "07/29/2020 08:45:48",
      "content": "<p>I finally manage to get dynamic shape of an image with variable Height and width.</p>\n\n<p>You need to us tf.shape because we are in graph mode and cast it (I looked at tf.image.central_crope code) as:</p>\n\n<p>`\n    shape = tf.shape(image_tensor)</p>\n\n<pre><code>image_height_ = shape[0]\nimage_width_ = shape[1]\n\nimage_height = math_ops.cast(image_height_, dtypes.float64)\nimage_width = math_ops.cast(image_width_, dtypes.float64)\n</code></pre>\n\n<p>`</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "948153": "Hi\n\nI'm trying to get shape from TensorSpec with dynamic shape.\n\nI used \n@mayukh18  code to create a submision ([notebook](https://www.kaggle.com/mayukh18/creating-submission-from-your-own-model/data))\n\n\n`\n\n    @tf.function(input_signature=[\n      tf.TensorSpec(shape=[None, None, 3], dtype=tf.uint8, name='input_image')\n    ])\n    def call(self, input_image):\n        output_tensors = {}\n        \n        # resizing\n        im = tf.image.resize(input_image, (224,224))\n        \n        # preprocessing\n        im = preprocess_input(im)\n        \n        extracted_features = self.model(tf.convert_to_tensor([im], dtype=tf.uint8))[0]\n        output_tensors['global_descriptor'] = tf.identity(extracted_features, name='global_descriptor')\n        return output_tensors\n\n`\n\n\nBut i can't figure it out how to get it.\n\nAnyone knows how to get input _ image shape? Because every time i call input _ image.shape or tf.shape(input _ image) i get always (None, None, 3) but i need the shape of the image input",
    "950196": "I finally manage to get dynamic shape of an image with variable Height and width.\n\nYou need to us tf.shape because we are in graph mode and cast it (I looked at tf.image.central_crope code) as:\n\n`\n    shape = tf.shape(image_tensor)\n    \n    image_height_ = shape[0]\n    image_width_ = shape[1]\n    \n    image_height = math_ops.cast(image_height_, dtypes.float64)\n    image_width = math_ops.cast(image_width_, dtypes.float64)\n`"
  },
  "source": "meta"
}