{
  "id": 166408,
  "title": "collate_fn in pytorch",
  "url": "/competitions/global-wheat-detection/discussion/166408",
  "author_name": "",
  "post_date": "2020-07-12T19:49:03.415268500Z",
  "votes": null,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Can anybody explain me what is collate_fn in pytorch dataloader?</p>",
  "messages": [
    {
      "id": "926593",
      "postDate": "07/12/2020 19:49:03",
      "content": "<p>Can anybody explain me what is collate_fn in pytorch dataloader?</p>",
      "rawMarkdown": "Can anybody explain me what is collate_fn in pytorch dataloader?",
      "votes": null
    },
    {
      "id": "926613",
      "postDate": "07/12/2020 20:03:24",
      "content": "<p>It is pretty simple, it's just used to \"group together\" the samples within your batch. For example, with no collate function your batch data would look like:\n<code>[(img_0, targets_0), (img_1, targets_1), ...]</code>\nbut with the collate function it would be more like\n<code>[(img_0, img_1), (targets_0, targets_1), ...]</code>\nSo it's just an easy way to organize the data within a batch to be passed to the network.</p>",
      "rawMarkdown": "It is pretty simple, it's just used to \"group together\" the samples within your batch. For example, with no collate function your batch data would look like:\n`[(img_0, targets_0), (img_1, targets_1), ...]`\nbut with the collate function it would be more like\n`[(img_0, img_1), (targets_0, targets_1), ...]`\nSo it's just an easy way to organize the data within a batch to be passed to the network.",
      "votes": null
    },
    {
      "id": "926646",
      "postDate": "07/12/2020 20:34:18",
      "content": "<p>I think you mostly have to write this function yourself when the tensors across different samples are of different lengths. Since the default collate_fn function tries to stack multiple tensors together, they have to be of the same length for it to work. In the case of this competition, the bounding boxes (and corresponding labels) are of different lengths in different images, hence you have to use a custom collate_fn to create a batch.</p>",
      "rawMarkdown": "I think you mostly have to write this function yourself when the tensors across different samples are of different lengths. Since the default collate\\_fn function tries to stack multiple tensors together, they have to be of the same length for it to work. In the case of this competition, the bounding boxes (and corresponding labels) are of different lengths in different images, hence you have to use a custom collate\\_fn to create a batch.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 926613,
      "author_name": "matthewmasters",
      "author_url": "",
      "post_date": "07/12/2020 20:03:24",
      "content": "<p>It is pretty simple, it's just used to \"group together\" the samples within your batch. For example, with no collate function your batch data would look like:\n<code>[(img_0, targets_0), (img_1, targets_1), ...]</code>\nbut with the collate function it would be more like\n<code>[(img_0, img_1), (targets_0, targets_1), ...]</code>\nSo it's just an easy way to organize the data within a batch to be passed to the network.</p>",
      "votes": null,
      "replies": []
    },
    {
      "id": 926646,
      "author_name": "jonykarki",
      "author_url": "",
      "post_date": "07/12/2020 20:34:18",
      "content": "<p>I think you mostly have to write this function yourself when the tensors across different samples are of different lengths. Since the default collate_fn function tries to stack multiple tensors together, they have to be of the same length for it to work. In the case of this competition, the bounding boxes (and corresponding labels) are of different lengths in different images, hence you have to use a custom collate_fn to create a batch.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "926593": "Can anybody explain me what is collate_fn in pytorch dataloader?",
    "926613": "It is pretty simple, it's just used to \"group together\" the samples within your batch. For example, with no collate function your batch data would look like:\n`[(img_0, targets_0), (img_1, targets_1), ...]`\nbut with the collate function it would be more like\n`[(img_0, img_1), (targets_0, targets_1), ...]`\nSo it's just an easy way to organize the data within a batch to be passed to the network.",
    "926646": "I think you mostly have to write this function yourself when the tensors across different samples are of different lengths. Since the default collate\\_fn function tries to stack multiple tensors together, they have to be of the same length for it to work. In the case of this competition, the bounding boxes (and corresponding labels) are of different lengths in different images, hence you have to use a custom collate\\_fn to create a batch."
  },
  "source": "meta"
}