{
  "id": 208637,
  "title": "Memory running out when using GPU",
  "url": "/competitions/hubmap-kidney-segmentation/discussion/208637",
  "author_name": "",
  "post_date": "2021-01-04T10:31:22.239085Z",
  "votes": 2,
  "comment_count": 1,
  "views": 0,
  "content": "<p>I have this function:</p>\n<pre><code>def prepareImage(image):\n    image = np.squeeze(image)\n    if image.shape[0] == 3:\n        image = image.swapaxes(0,1)\n        image = image.swapaxes(1,2)\n\n    # Resize:\n    reduce = 2\n    image = cv2.resize(image,\n        (image.shape[1]//reduce, image.shape[0]//reduce),\n        interpolation = cv2.INTER_AREA)\n\n    # Pad the ends:\n    shape = image.shape \n    pad0 = (TILE_SIZE - shape[0]%TILE_SIZE)%TILE_SIZE\n    pad1 = (TILE_SIZE - shape[1]%TILE_SIZE)%TILE_SIZE\n    image = np.pad(image, [[0,pad0], [0,pad1],[0,0]], constant_values=255)\n    image = image / 255.0\n    return image\n</code></pre>\n<p>that prepares images for inference. The problem is that when using GPU, thus having slightly less RAM, I run out of memory. Anyone else facing this issue?</p>",
  "messages": [
    {
      "id": "1137988",
      "postDate": "01/04/2021 10:31:22",
      "content": "<p>I have this function:</p>\n<pre><code>def prepareImage(image):\n    image = np.squeeze(image)\n    if image.shape[0] == 3:\n        image = image.swapaxes(0,1)\n        image = image.swapaxes(1,2)\n\n    # Resize:\n    reduce = 2\n    image = cv2.resize(image,\n        (image.shape[1]//reduce, image.shape[0]//reduce),\n        interpolation = cv2.INTER_AREA)\n\n    # Pad the ends:\n    shape = image.shape \n    pad0 = (TILE_SIZE - shape[0]%TILE_SIZE)%TILE_SIZE\n    pad1 = (TILE_SIZE - shape[1]%TILE_SIZE)%TILE_SIZE\n    image = np.pad(image, [[0,pad0], [0,pad1],[0,0]], constant_values=255)\n    image = image / 255.0\n    return image\n</code></pre>\n<p>that prepares images for inference. The problem is that when using GPU, thus having slightly less RAM, I run out of memory. Anyone else facing this issue?</p>",
      "rawMarkdown": "I have this function:\n```\ndef prepareImage(image):\n    image = np.squeeze(image)\n    if image.shape[0] == 3:\n        image = image.swapaxes(0,1)\n        image = image.swapaxes(1,2)\n\n    # Resize:\n    reduce = 2\n    image = cv2.resize(image,\n        (image.shape[1]//reduce, image.shape[0]//reduce),\n        interpolation = cv2.INTER_AREA)\n\n    # Pad the ends:\n    shape = image.shape \n    pad0 = (TILE_SIZE - shape[0]%TILE_SIZE)%TILE_SIZE\n    pad1 = (TILE_SIZE - shape[1]%TILE_SIZE)%TILE_SIZE\n    image = np.pad(image, [[0,pad0], [0,pad1],[0,0]], constant_values=255)\n    image = image / 255.0\n    return image\n```\n\nthat prepares images for inference. The problem is that when using GPU, thus having slightly less RAM, I run out of memory. Anyone else facing this issue?",
      "votes": null
    },
    {
      "id": "1138319",
      "postDate": "01/04/2021 15:28:23",
      "content": "<p>Solution suggested by <a href=\"https://www.kaggle.com/leighplt\" target=\"_blank\">@leighplt</a> using <em>rasterio</em> . See his <a href=\"https://www.kaggle.com/leighplt/pytorch-fcn-resnet50\" target=\"_blank\">his kernel</a>…  And it is used in many public notebooks.</p>\n<p>import rasterio<br>\nfrom rasterio.windows import Window</p>",
      "rawMarkdown": "Solution suggested by @leighplt using *rasterio* . See his [his kernel](https://www.kaggle.com/leighplt/pytorch-fcn-resnet50)...  And it is used in many public notebooks.\n\nimport rasterio\nfrom rasterio.windows import Window",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 1138319,
      "author_name": "isakev",
      "author_url": "",
      "post_date": "01/04/2021 15:28:23",
      "content": "<p>Solution suggested by <a href=\"https://www.kaggle.com/leighplt\" target=\"_blank\">@leighplt</a> using <em>rasterio</em> . See his <a href=\"https://www.kaggle.com/leighplt/pytorch-fcn-resnet50\" target=\"_blank\">his kernel</a>…  And it is used in many public notebooks.</p>\n<p>import rasterio<br>\nfrom rasterio.windows import Window</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1137988": "I have this function:\n```\ndef prepareImage(image):\n    image = np.squeeze(image)\n    if image.shape[0] == 3:\n        image = image.swapaxes(0,1)\n        image = image.swapaxes(1,2)\n\n    # Resize:\n    reduce = 2\n    image = cv2.resize(image,\n        (image.shape[1]//reduce, image.shape[0]//reduce),\n        interpolation = cv2.INTER_AREA)\n\n    # Pad the ends:\n    shape = image.shape \n    pad0 = (TILE_SIZE - shape[0]%TILE_SIZE)%TILE_SIZE\n    pad1 = (TILE_SIZE - shape[1]%TILE_SIZE)%TILE_SIZE\n    image = np.pad(image, [[0,pad0], [0,pad1],[0,0]], constant_values=255)\n    image = image / 255.0\n    return image\n```\n\nthat prepares images for inference. The problem is that when using GPU, thus having slightly less RAM, I run out of memory. Anyone else facing this issue?",
    "1138319": "Solution suggested by @leighplt using *rasterio* . See his [his kernel](https://www.kaggle.com/leighplt/pytorch-fcn-resnet50)...  And it is used in many public notebooks.\n\nimport rasterio\nfrom rasterio.windows import Window"
  },
  "source": "meta"
}