{
  "id": 391059,
  "title": "[Lessons] Implement DALI in the style of Pytorch ",
  "url": "/competitions/rsna-breast-cancer-detection/discussion/391059",
  "author_name": "ForcewithMe",
  "post_date": "2023-02-28T09:11:19.335000",
  "votes": 18,
  "comment_count": 7,
  "views": 0,
  "content": "<p>Thanks <a href=\"https://www.kaggle.com/theoviel\" target=\"_blank\">@theoviel</a> , <a href=\"https://www.kaggle.com/tivfrvqhs5\" target=\"_blank\">@tivfrvqhs5</a> , <a href=\"https://www.kaggle.com/christofhenkel\" target=\"_blank\">@christofhenkel</a> , <a href=\"https://www.kaggle.com/outwrest\" target=\"_blank\">@outwrest</a> for your effort and introducion to DALI. DALI is one of the best things I have learned in this competition. Based on the work they published before, I wrapped all the data reading, image decoding, preprocessing process(dali and customized), data generation, into a custom data iterator <strong>in a fully aligned Pytorch style</strong>. </p>\n<p>Talk is cheap. Show you <a href=\"https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402\" target=\"_blank\">the code</a>. It's modified based on the public notebook of <a href=\"https://www.kaggle.com/outwrest\" target=\"_blank\">@outwrest</a> .</p>\n<p>Some brief introduction: Dali pipeline should always contain 2 or 3 steps. </p>\n<ol>\n<li>Design an iterator to retrieve and read raw data in batches. You can use <code>fn.external_source</code> or a custom iterator.</li>\n<li>Implement a DALI pipeline, and perform all DALI operations in the pipeline, such as decoding, Pad, Resize, etc. of images.</li>\n<li>Implement a DALIGenericIterator, or customize your data iterator (optional).</li>\n</ol>\n<p>In fact, in the third step <strong>we can implement any operations</strong> that need to be done outside of <code>DALI pipe</code>, such as <code>numpy</code>, <code>opencv</code>, <code>yolo</code>, and aligning to pytrorch style, by inheriting DALIGenericIterator and plugging the operations into the custom data iterator. It's <code>CustomDALIGenericIterator</code> in <a href=\"https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402\" target=\"_blank\">the code</a>. After that, <code>j2k_iter</code> and <code>jll_iter</code> in this code have <strong>the exact same APIs as Pytorch Dataloader</strong>, which is easier to read and use.</p>\n<pre><code>j2k_iter = (length=(J2Ki), pipelines=, yolo_model=yolo_model)\n  in (j2k_iter):\n     = (x)\n    ...\n\njll_iter = (length=(JLLi), pipelines=, yolo_model=yolo_model)\n  in (jll_iter):\n     = (x)\n    ...\n</code></pre>",
  "messages": [
    {
      "id": 2162527,
      "postDate": "2023-02-28T09:11:19.337Z",
      "content": "<p>Thanks <a href=\"https://www.kaggle.com/theoviel\" target=\"_blank\">@theoviel</a> , <a href=\"https://www.kaggle.com/tivfrvqhs5\" target=\"_blank\">@tivfrvqhs5</a> , <a href=\"https://www.kaggle.com/christofhenkel\" target=\"_blank\">@christofhenkel</a> , <a href=\"https://www.kaggle.com/outwrest\" target=\"_blank\">@outwrest</a> for your effort and introducion to DALI. DALI is one of the best things I have learned in this competition. Based on the work they published before, I wrapped all the data reading, image decoding, preprocessing process(dali and customized), data generation, into a custom data iterator <strong>in a fully aligned Pytorch style</strong>. </p>\n<p>Talk is cheap. Show you <a href=\"https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402\" target=\"_blank\">the code</a>. It's modified based on the public notebook of <a href=\"https://www.kaggle.com/outwrest\" target=\"_blank\">@outwrest</a> .</p>\n<p>Some brief introduction: Dali pipeline should always contain 2 or 3 steps. </p>\n<ol>\n<li>Design an iterator to retrieve and read raw data in batches. You can use <code>fn.external_source</code> or a custom iterator.</li>\n<li>Implement a DALI pipeline, and perform all DALI operations in the pipeline, such as decoding, Pad, Resize, etc. of images.</li>\n<li>Implement a DALIGenericIterator, or customize your data iterator (optional).</li>\n</ol>\n<p>In fact, in the third step <strong>we can implement any operations</strong> that need to be done outside of <code>DALI pipe</code>, such as <code>numpy</code>, <code>opencv</code>, <code>yolo</code>, and aligning to pytrorch style, by inheriting DALIGenericIterator and plugging the operations into the custom data iterator. It's <code>CustomDALIGenericIterator</code> in <a href=\"https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402\" target=\"_blank\">the code</a>. After that, <code>j2k_iter</code> and <code>jll_iter</code> in this code have <strong>the exact same APIs as Pytorch Dataloader</strong>, which is easier to read and use.</p>\n<pre><code>j2k_iter = (length=(J2Ki), pipelines=, yolo_model=yolo_model)\n  in (j2k_iter):\n     = (x)\n    ...\n\njll_iter = (length=(JLLi), pipelines=, yolo_model=yolo_model)\n  in (jll_iter):\n     = (x)\n    ...\n</code></pre>",
      "rawMarkdown": "Thanks @theoviel , @tivfrvqhs5 , @christofhenkel , @outwrest for your effort and introducion to DALI. DALI is one of the best things I have learned in this competition. Based on the work they published before, I wrapped all the data reading, image decoding, preprocessing process(dali and customized), data generation, into a custom data iterator **in a fully aligned Pytorch style**. \n\nTalk is cheap. Show you [the code](https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402). It's modified based on the public notebook of @outwrest .\n\nSome brief introduction: Dali pipeline should always contain 2 or 3 steps. \n\n1.\tDesign an iterator to retrieve and read raw data in batches. You can use `fn.external_source` or a custom iterator.\n2.\tImplement a DALI pipeline, and perform all DALI operations in the pipeline, such as decoding, Pad, Resize, etc. of images.\n3.\tImplement a DALIGenericIterator, or customize your data iterator (optional).\n\nIn fact, in the third step **we can implement any operations** that need to be done outside of `DALI pipe`, such as `numpy`, `opencv`, `yolo`, and aligning to pytrorch style, by inheriting DALIGenericIterator and plugging the operations into the custom data iterator. It's `CustomDALIGenericIterator` in [the code](https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402). After that, `j2k_iter` and `jll_iter` in this code have **the exact same APIs as Pytorch Dataloader**, which is easier to read and use.\n\n```\n\nj2k_iter = CustomDALIGenericIterator(length=len(J2Ki), pipelines=[j2k_pipe], yolo_model=yolo_model)\nfor x in tqdm(j2k_iter):\n    y = model(x)\n    ...\n\njll_iter = CustomDALIGenericIterator(length=len(JLLi), pipelines=[jll_pipe], yolo_model=yolo_model)\nfor x in tqdm(jll_iter):\n    y = model(x)\n    ...\n\n```",
      "votes": 18
    },
    {
      "id": 2164686,
      "postDate": "2023-03-01T18:39:09.837Z",
      "content": "<p>Hey, that's super cool! DALI is awesome, there is something to learn! This is my first time developing a pipeline and I was learning alongside everyone you mentioned (they're all awesome ❤️❤️). I stopped competing in this competition and I kinda regret it. I just posted <a href=\"https://www.kaggle.com/code/outwrest/2xt4-dali\" target=\"_blank\">one of my WIP notebooks</a> where I was testing 2xGPU with DALI, one big addition left is adding JLL GPU decoding but it should be another full end2end with GPU apply_windowing operation. With your additions, it can be even better (and faster 👀)! </p>\n<p>I hope to use DALI more often in future competitions. 😆</p>\n<p>Anyways, congratulations on the competition and great notebook!  🎉🎉🎉🎉</p>",
      "rawMarkdown": "Hey, that's super cool! DALI is awesome, there is something to learn! This is my first time developing a pipeline and I was learning alongside everyone you mentioned (they're all awesome ❤️❤️). I stopped competing in this competition and I kinda regret it. I just posted [one of my WIP notebooks](https://www.kaggle.com/code/outwrest/2xt4-dali) where I was testing 2xGPU with DALI, one big addition left is adding JLL GPU decoding but it should be another full end2end with GPU apply_windowing operation. With your additions, it can be even better (and faster 👀)! \n\nI hope to use DALI more often in future competitions. 😆\n\nAnyways, congratulations on the competition and great notebook!  🎉🎉🎉🎉",
      "votes": 1,
      "replies": [
        {
          "id": 2164693,
          "postDate": "2023-03-01T18:41:27.853Z",
          "content": "<p>Also, my original public notebook looks so bad, I should've refactored it before making it public. Hope it wasn't bad to read 😂😂</p>",
          "rawMarkdown": "Also, my original public notebook looks so bad, I should've refactored it before making it public. Hope it wasn't bad to read 😂😂",
          "votes": 1,
          "replies": [
            {
              "id": 2165051,
              "postDate": "2023-03-02T00:44:02.570Z",
              "content": "<p>Thank you. Your original notebook is already very good. Look forward to see you in next competition.</p>",
              "rawMarkdown": "Thank you. Your original notebook is already very good. Look forward to see you in next competition.",
              "votes": 1
            }
          ]
        }
      ]
    },
    {
      "id": 2165045,
      "postDate": "2023-03-02T00:36:24.427Z",
      "content": "<p>i use <a href=\"https://github.com/kornia/kornia\" target=\"_blank\">https://github.com/kornia/kornia</a> for gpu agumention.<br>\nit use augmentation as a nn.Module</p>",
      "rawMarkdown": "i use https://github.com/kornia/kornia for gpu agumention.\nit use augmentation as a nn.Module",
      "votes": 2
    },
    {
      "id": 2162908,
      "postDate": "2023-02-28T13:22:31.360Z",
      "content": "<p>Thank you for posting! What are the benefits of using <code>DALI</code>? Is it faster or capable to handle larger batches? Thank you!</p>",
      "rawMarkdown": "Thank you for posting! What are the benefits of using `DALI`? Is it faster or capable to handle larger batches? Thank you!",
      "votes": 2,
      "replies": [
        {
          "id": 2162925,
          "postDate": "2023-02-28T13:31:30.013Z",
          "content": "<p>Hi <a href=\"https://www.kaggle.com/jiweiliu\" target=\"_blank\">@jiweiliu</a> , the main reason that DALI is widely used in this competition is that it can decode jpeg2000 images with a much faster speed.</p>",
          "rawMarkdown": "Hi @jiweiliu , the main reason that DALI is widely used in this competition is that it can decode jpeg2000 images with a much faster speed.",
          "votes": 1
        }
      ]
    },
    {
      "id": 2164261,
      "postDate": "2023-03-01T12:55:55.803Z",
      "content": "<p>Very useful and practical! Thanks!</p>",
      "rawMarkdown": "Very useful and practical! Thanks!"
    }
  ],
  "comments": [
    {
      "id": 2164686,
      "author_name": "outwrest",
      "author_url": "",
      "post_date": "2023-03-01T18:39:09.837000",
      "content": "<p>Hey, that's super cool! DALI is awesome, there is something to learn! This is my first time developing a pipeline and I was learning alongside everyone you mentioned (they're all awesome ❤️❤️). I stopped competing in this competition and I kinda regret it. I just posted <a href=\"https://www.kaggle.com/code/outwrest/2xt4-dali\" target=\"_blank\">one of my WIP notebooks</a> where I was testing 2xGPU with DALI, one big addition left is adding JLL GPU decoding but it should be another full end2end with GPU apply_windowing operation. With your additions, it can be even better (and faster 👀)! </p>\n<p>I hope to use DALI more often in future competitions. 😆</p>\n<p>Anyways, congratulations on the competition and great notebook!  🎉🎉🎉🎉</p>",
      "votes": 1,
      "replies": [
        {
          "id": 2164693,
          "author_name": "outwrest",
          "author_url": "",
          "post_date": "2023-03-01T18:41:27.853000",
          "content": "<p>Also, my original public notebook looks so bad, I should've refactored it before making it public. Hope it wasn't bad to read 😂😂</p>",
          "votes": 1,
          "replies": [
            {
              "id": 2165051,
              "author_name": "ForcewithMe",
              "author_url": "",
              "post_date": "2023-03-02T00:44:02.570000",
              "content": "<p>Thank you. Your original notebook is already very good. Look forward to see you in next competition.</p>",
              "votes": 1,
              "replies": []
            }
          ]
        }
      ]
    },
    {
      "id": 2165045,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "2023-03-02T00:36:24.427000",
      "content": "<p>i use <a href=\"https://github.com/kornia/kornia\" target=\"_blank\">https://github.com/kornia/kornia</a> for gpu agumention.<br>\nit use augmentation as a nn.Module</p>",
      "votes": 2,
      "replies": []
    },
    {
      "id": 2162908,
      "author_name": "Jiwei Liu",
      "author_url": "",
      "post_date": "2023-02-28T13:22:31.360000",
      "content": "<p>Thank you for posting! What are the benefits of using <code>DALI</code>? Is it faster or capable to handle larger batches? Thank you!</p>",
      "votes": 2,
      "replies": [
        {
          "id": 2162925,
          "author_name": "ForcewithMe",
          "author_url": "",
          "post_date": "2023-02-28T13:31:30.013000",
          "content": "<p>Hi <a href=\"https://www.kaggle.com/jiweiliu\" target=\"_blank\">@jiweiliu</a> , the main reason that DALI is widely used in this competition is that it can decode jpeg2000 images with a much faster speed.</p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 2164261,
      "author_name": "Fatemeh.V.Younesi",
      "author_url": "",
      "post_date": "2023-03-01T12:55:55.803000",
      "content": "<p>Very useful and practical! Thanks!</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2162527": "Thanks @theoviel , @tivfrvqhs5 , @christofhenkel , @outwrest for your effort and introducion to DALI. DALI is one of the best things I have learned in this competition. Based on the work they published before, I wrapped all the data reading, image decoding, preprocessing process(dali and customized), data generation, into a custom data iterator **in a fully aligned Pytorch style**. \n\nTalk is cheap. Show you [the code](https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402). It's modified based on the public notebook of @outwrest .\n\nSome brief introduction: Dali pipeline should always contain 2 or 3 steps. \n\n1.\tDesign an iterator to retrieve and read raw data in batches. You can use `fn.external_source` or a custom iterator.\n2.\tImplement a DALI pipeline, and perform all DALI operations in the pipeline, such as decoding, Pad, Resize, etc. of images.\n3.\tImplement a DALIGenericIterator, or customize your data iterator (optional).\n\nIn fact, in the third step **we can implement any operations** that need to be done outside of `DALI pipe`, such as `numpy`, `opencv`, `yolo`, and aligning to pytrorch style, by inheriting DALIGenericIterator and plugging the operations into the custom data iterator. It's `CustomDALIGenericIterator` in [the code](https://www.kaggle.com/code/forcewithme/rsna-elegant-end2end-dali-infer-in-pytorch-style?scriptVersionId=120573402). After that, `j2k_iter` and `jll_iter` in this code have **the exact same APIs as Pytorch Dataloader**, which is easier to read and use.\n\n```\n\nj2k_iter = CustomDALIGenericIterator(length=len(J2Ki), pipelines=[j2k_pipe], yolo_model=yolo_model)\nfor x in tqdm(j2k_iter):\n    y = model(x)\n    ...\n\njll_iter = CustomDALIGenericIterator(length=len(JLLi), pipelines=[jll_pipe], yolo_model=yolo_model)\nfor x in tqdm(jll_iter):\n    y = model(x)\n    ...\n\n```",
    "2164686": "Hey, that's super cool! DALI is awesome, there is something to learn! This is my first time developing a pipeline and I was learning alongside everyone you mentioned (they're all awesome ❤️❤️). I stopped competing in this competition and I kinda regret it. I just posted [one of my WIP notebooks](https://www.kaggle.com/code/outwrest/2xt4-dali) where I was testing 2xGPU with DALI, one big addition left is adding JLL GPU decoding but it should be another full end2end with GPU apply_windowing operation. With your additions, it can be even better (and faster 👀)! \n\nI hope to use DALI more often in future competitions. 😆\n\nAnyways, congratulations on the competition and great notebook!  🎉🎉🎉🎉",
    "2165045": "i use https://github.com/kornia/kornia for gpu agumention.\nit use augmentation as a nn.Module",
    "2162908": "Thank you for posting! What are the benefits of using `DALI`? Is it faster or capable to handle larger batches? Thank you!",
    "2164261": "Very useful and practical! Thanks!"
  }
}