{
  "id": 362641,
  "title": "Open sourcing my solution [2D Model, PyTorch]",
  "url": "/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/362641",
  "author_name": "Samuel Cortinhas",
  "post_date": "2022-10-28T09:27:00.476000",
  "votes": 17,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Even though our team dropped 350 places in private LB I still wanted to share our approach (&amp; code) in the spirit of collaboration and helping others learn. First I want to say a huge thank you to my team mate <a href=\"https://www.kaggle.com/jirkaborovec\" target=\"_blank\">@jirkaborovec</a>, kaggle and all the hosts - this really was a fantastic competition.</p>\n<h2>Model architecture</h2>\n<p><a href=\"https://postimg.cc/mzg7csjV\" target=\"_blank\"><img src=\"https://i.postimg.cc/264GKjZp/2-D-model-architecture.png\" alt=\"2-D-model-architecture.png\"></a></p>\n<p>Our model was inspired by <a href=\"https://www.kaggle.com/code/vslaykovsky/train-pytorch-effnetv2-baseline-cv-0-49\" target=\"_blank\">Vladimir's</a> that most have seen and <a href=\"https://www.kaggle.com/competitions/siim-isic-melanoma-classification/discussion/175412\" target=\"_blank\">1st place from the Melanoma Classification comp</a>. We used image data and meta data simultaneously in the hopes of helping the model better predict which vertebrae is in each image.</p>\n<h2>Feature engineering</h2>\n<p>We extracted metadata (see my <a href=\"https://www.kaggle.com/datasets/samuelcortinhas/rsna-2022-spine-fracture-detection-metadata\" target=\"_blank\">dataset here</a>) from the dicom files including slice number, slice thickness and patient position. We then performed feature engineering on this:</p>\n<ul>\n<li>Calculate maximum slice number</li>\n<li>Calculate ratio of slice number to max slice</li>\n<li>Create indicator if images are reversed in z-axis (i.e. order of vertebrae is reversed).</li>\n</ul>\n<h2>Learning rate scheduler</h2>\n<p><img src=\"https://i.postimg.cc/5tbHbxHq/lrschedule.png\"></p>\n<p>We used CosineAnnealingWarmupRestarts from the <a href=\"https://github.com/katsura-jp/pytorch-cosine-annealing-with-warmup\" target=\"_blank\">github here</a>. Each cycle is one epoch. Optimisers tried included Adam and AdamW. </p>\n<h2>Augmentations</h2>\n<p><img src=\"https://i.postimg.cc/0yFzYBVr/augs.png\"></p>\n<ul>\n<li>A.Resize(*(256,256), interpolation=cv2.INTER_LINEAR)</li>\n<li>A.HorizontalFlip(p=0.35)</li>\n<li>A.ShiftScaleRotate(…, p=0.4)</li>\n<li>A.RandomBrightnessContrast(…, p=0.5)</li>\n<li>A.OneOf([A.GridDistortion(p=1.0),A.ElasticTransform(p=1.0)], p=0.25)</li>\n</ul>\n<p>A=Albumentations library. See links at the bottom for code.</p>\n<h2>Torch, GPU, epochs</h2>\n<p>We used PyTorch. It was actually my first time using it and I really like it. We only used kaggle GPU and kaggle notebooks to train our models. 1 epoch took roughly 10 hours. Best submission used 6 epochs. </p>\n<h2>Code</h2>\n<p>I am open sourcing all my code for anyone that wants to look. I've tried to comment it as best as possible. Happy to answer questions if anything is unclear.</p>\n<ol>\n<li><a href=\"https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-train-pytorch\" target=\"_blank\">RNSA - 2D model [Train] [PyTorch]</a> (training script)</li>\n<li><a href=\"https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-validate-pytorch\" target=\"_blank\">RNSA - 2D model [Validate] [PyTorch]</a> (evaluate on competition metric)</li>\n<li><a href=\"https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-infer-pytorch\" target=\"_blank\">RNSA - 2D model [Infer] [PyTorch]</a> (submission notebook)</li>\n</ol>\n<h2>Lessons learnt</h2>\n<ul>\n<li>If there was more time I would have liked to train a segmentation model</li>\n<li>Crop images to focus on region of interest</li>\n<li>Look into 2.5D</li>\n</ul>",
  "messages": [
    {
      "id": 2007507,
      "postDate": "2022-10-28T09:27:00.477Z",
      "content": "<p>Even though our team dropped 350 places in private LB I still wanted to share our approach (&amp; code) in the spirit of collaboration and helping others learn. First I want to say a huge thank you to my team mate <a href=\"https://www.kaggle.com/jirkaborovec\" target=\"_blank\">@jirkaborovec</a>, kaggle and all the hosts - this really was a fantastic competition.</p>\n<h2>Model architecture</h2>\n<p><a href=\"https://postimg.cc/mzg7csjV\" target=\"_blank\"><img src=\"https://i.postimg.cc/264GKjZp/2-D-model-architecture.png\" alt=\"2-D-model-architecture.png\"></a></p>\n<p>Our model was inspired by <a href=\"https://www.kaggle.com/code/vslaykovsky/train-pytorch-effnetv2-baseline-cv-0-49\" target=\"_blank\">Vladimir's</a> that most have seen and <a href=\"https://www.kaggle.com/competitions/siim-isic-melanoma-classification/discussion/175412\" target=\"_blank\">1st place from the Melanoma Classification comp</a>. We used image data and meta data simultaneously in the hopes of helping the model better predict which vertebrae is in each image.</p>\n<h2>Feature engineering</h2>\n<p>We extracted metadata (see my <a href=\"https://www.kaggle.com/datasets/samuelcortinhas/rsna-2022-spine-fracture-detection-metadata\" target=\"_blank\">dataset here</a>) from the dicom files including slice number, slice thickness and patient position. We then performed feature engineering on this:</p>\n<ul>\n<li>Calculate maximum slice number</li>\n<li>Calculate ratio of slice number to max slice</li>\n<li>Create indicator if images are reversed in z-axis (i.e. order of vertebrae is reversed).</li>\n</ul>\n<h2>Learning rate scheduler</h2>\n<p><img src=\"https://i.postimg.cc/5tbHbxHq/lrschedule.png\"></p>\n<p>We used CosineAnnealingWarmupRestarts from the <a href=\"https://github.com/katsura-jp/pytorch-cosine-annealing-with-warmup\" target=\"_blank\">github here</a>. Each cycle is one epoch. Optimisers tried included Adam and AdamW. </p>\n<h2>Augmentations</h2>\n<p><img src=\"https://i.postimg.cc/0yFzYBVr/augs.png\"></p>\n<ul>\n<li>A.Resize(*(256,256), interpolation=cv2.INTER_LINEAR)</li>\n<li>A.HorizontalFlip(p=0.35)</li>\n<li>A.ShiftScaleRotate(…, p=0.4)</li>\n<li>A.RandomBrightnessContrast(…, p=0.5)</li>\n<li>A.OneOf([A.GridDistortion(p=1.0),A.ElasticTransform(p=1.0)], p=0.25)</li>\n</ul>\n<p>A=Albumentations library. See links at the bottom for code.</p>\n<h2>Torch, GPU, epochs</h2>\n<p>We used PyTorch. It was actually my first time using it and I really like it. We only used kaggle GPU and kaggle notebooks to train our models. 1 epoch took roughly 10 hours. Best submission used 6 epochs. </p>\n<h2>Code</h2>\n<p>I am open sourcing all my code for anyone that wants to look. I've tried to comment it as best as possible. Happy to answer questions if anything is unclear.</p>\n<ol>\n<li><a href=\"https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-train-pytorch\" target=\"_blank\">RNSA - 2D model [Train] [PyTorch]</a> (training script)</li>\n<li><a href=\"https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-validate-pytorch\" target=\"_blank\">RNSA - 2D model [Validate] [PyTorch]</a> (evaluate on competition metric)</li>\n<li><a href=\"https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-infer-pytorch\" target=\"_blank\">RNSA - 2D model [Infer] [PyTorch]</a> (submission notebook)</li>\n</ol>\n<h2>Lessons learnt</h2>\n<ul>\n<li>If there was more time I would have liked to train a segmentation model</li>\n<li>Crop images to focus on region of interest</li>\n<li>Look into 2.5D</li>\n</ul>",
      "rawMarkdown": "Even though our team dropped 350 places in private LB I still wanted to share our approach (& code) in the spirit of collaboration and helping others learn. First I want to say a huge thank you to my team mate @jirkaborovec, kaggle and all the hosts - this really was a fantastic competition.\n\n## Model architecture\n\n[![2-D-model-architecture.png](https://i.postimg.cc/264GKjZp/2-D-model-architecture.png)](https://postimg.cc/mzg7csjV)\n\nOur model was inspired by [Vladimir's](https://www.kaggle.com/code/vslaykovsky/train-pytorch-effnetv2-baseline-cv-0-49) that most have seen and [1st place from the Melanoma Classification comp](https://www.kaggle.com/competitions/siim-isic-melanoma-classification/discussion/175412). We used image data and meta data simultaneously in the hopes of helping the model better predict which vertebrae is in each image.\n\n## Feature engineering\n\nWe extracted metadata (see my [dataset here](https://www.kaggle.com/datasets/samuelcortinhas/rsna-2022-spine-fracture-detection-metadata)) from the dicom files including slice number, slice thickness and patient position. We then performed feature engineering on this:\n\n* Calculate maximum slice number\n* Calculate ratio of slice number to max slice\n* Create indicator if images are reversed in z-axis (i.e. order of vertebrae is reversed).\n\n## Learning rate scheduler\n\n<img src='https://i.postimg.cc/5tbHbxHq/lrschedule.png' width=400>\n\nWe used CosineAnnealingWarmupRestarts from the [github here](https://github.com/katsura-jp/pytorch-cosine-annealing-with-warmup). Each cycle is one epoch. Optimisers tried included Adam and AdamW. \n\n## Augmentations\n\n<img src='https://i.postimg.cc/0yFzYBVr/augs.png' width=400>\n\n* A.Resize(*(256,256), interpolation=cv2.INTER_LINEAR)\n* A.HorizontalFlip(p=0.35)\n* A.ShiftScaleRotate(..., p=0.4)\n* A.RandomBrightnessContrast(..., p=0.5)\n* A.OneOf([A.GridDistortion(p=1.0),A.ElasticTransform(p=1.0)], p=0.25)\n\nA=Albumentations library. See links at the bottom for code.\n\n## Torch, GPU, epochs\n\nWe used PyTorch. It was actually my first time using it and I really like it. We only used kaggle GPU and kaggle notebooks to train our models. 1 epoch took roughly 10 hours. Best submission used 6 epochs. \n\n## Code\n\nI am open sourcing all my code for anyone that wants to look. I've tried to comment it as best as possible. Happy to answer questions if anything is unclear.\n\n1. [RNSA - 2D model [Train] [PyTorch]](https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-train-pytorch) (training script)\n2. [RNSA - 2D model [Validate] [PyTorch]](https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-validate-pytorch) (evaluate on competition metric)\n3. [RNSA - 2D model [Infer] [PyTorch]](https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-infer-pytorch) (submission notebook)\n\n## Lessons learnt\n\n* If there was more time I would have liked to train a segmentation model\n* Crop images to focus on region of interest\n* Look into 2.5D",
      "votes": 17
    },
    {
      "id": 2013398,
      "postDate": "2022-11-01T21:31:04.437Z",
      "content": "<p>Your solution is top notch <a href=\"https://www.kaggle.com/samuelcortinhas\" target=\"_blank\">@samuelcortinhas</a> </p>",
      "rawMarkdown": "Your solution is top notch @samuelcortinhas ",
      "votes": 1
    },
    {
      "id": 2013559,
      "postDate": "2022-11-02T02:45:52.843Z",
      "content": "<p><a href=\"https://www.kaggle.com/samuelcortinhas\" target=\"_blank\">@samuelcortinhas</a> <br>\nI just started my data science journey on kaggle and thinking of participating in competitions. I have understanding of ml, dl, data cleaning and eda</p>\n<p>I have been following your impeccable work since I started my journey here. </p>\n<p>So, I want to ask you advice regarding, <br>\nWhat do you suggest me to start with to Ace in competitions ?</p>",
      "rawMarkdown": "@samuelcortinhas \nI just started my data science journey on kaggle and thinking of participating in competitions. I have understanding of ml, dl, data cleaning and eda\n\nI have been following your impeccable work since I started my journey here. \n\nSo, I want to ask you advice regarding, \nWhat do you suggest me to start with to Ace in competitions ?"
    }
  ],
  "comments": [
    {
      "id": 2013398,
      "author_name": "Allena Venkata Sai Abhishek",
      "author_url": "",
      "post_date": "2022-11-01T21:31:04.437000",
      "content": "<p>Your solution is top notch <a href=\"https://www.kaggle.com/samuelcortinhas\" target=\"_blank\">@samuelcortinhas</a> </p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 2013559,
      "author_name": "Allena Venkata Sai Abhishek",
      "author_url": "",
      "post_date": "2022-11-02T02:45:52.843000",
      "content": "<p><a href=\"https://www.kaggle.com/samuelcortinhas\" target=\"_blank\">@samuelcortinhas</a> <br>\nI just started my data science journey on kaggle and thinking of participating in competitions. I have understanding of ml, dl, data cleaning and eda</p>\n<p>I have been following your impeccable work since I started my journey here. </p>\n<p>So, I want to ask you advice regarding, <br>\nWhat do you suggest me to start with to Ace in competitions ?</p>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2007507": "Even though our team dropped 350 places in private LB I still wanted to share our approach (& code) in the spirit of collaboration and helping others learn. First I want to say a huge thank you to my team mate @jirkaborovec, kaggle and all the hosts - this really was a fantastic competition.\n\n## Model architecture\n\n[![2-D-model-architecture.png](https://i.postimg.cc/264GKjZp/2-D-model-architecture.png)](https://postimg.cc/mzg7csjV)\n\nOur model was inspired by [Vladimir's](https://www.kaggle.com/code/vslaykovsky/train-pytorch-effnetv2-baseline-cv-0-49) that most have seen and [1st place from the Melanoma Classification comp](https://www.kaggle.com/competitions/siim-isic-melanoma-classification/discussion/175412). We used image data and meta data simultaneously in the hopes of helping the model better predict which vertebrae is in each image.\n\n## Feature engineering\n\nWe extracted metadata (see my [dataset here](https://www.kaggle.com/datasets/samuelcortinhas/rsna-2022-spine-fracture-detection-metadata)) from the dicom files including slice number, slice thickness and patient position. We then performed feature engineering on this:\n\n* Calculate maximum slice number\n* Calculate ratio of slice number to max slice\n* Create indicator if images are reversed in z-axis (i.e. order of vertebrae is reversed).\n\n## Learning rate scheduler\n\n<img src='https://i.postimg.cc/5tbHbxHq/lrschedule.png' width=400>\n\nWe used CosineAnnealingWarmupRestarts from the [github here](https://github.com/katsura-jp/pytorch-cosine-annealing-with-warmup). Each cycle is one epoch. Optimisers tried included Adam and AdamW. \n\n## Augmentations\n\n<img src='https://i.postimg.cc/0yFzYBVr/augs.png' width=400>\n\n* A.Resize(*(256,256), interpolation=cv2.INTER_LINEAR)\n* A.HorizontalFlip(p=0.35)\n* A.ShiftScaleRotate(..., p=0.4)\n* A.RandomBrightnessContrast(..., p=0.5)\n* A.OneOf([A.GridDistortion(p=1.0),A.ElasticTransform(p=1.0)], p=0.25)\n\nA=Albumentations library. See links at the bottom for code.\n\n## Torch, GPU, epochs\n\nWe used PyTorch. It was actually my first time using it and I really like it. We only used kaggle GPU and kaggle notebooks to train our models. 1 epoch took roughly 10 hours. Best submission used 6 epochs. \n\n## Code\n\nI am open sourcing all my code for anyone that wants to look. I've tried to comment it as best as possible. Happy to answer questions if anything is unclear.\n\n1. [RNSA - 2D model [Train] [PyTorch]](https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-train-pytorch) (training script)\n2. [RNSA - 2D model [Validate] [PyTorch]](https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-validate-pytorch) (evaluate on competition metric)\n3. [RNSA - 2D model [Infer] [PyTorch]](https://www.kaggle.com/samuelcortinhas/rnsa-2d-model-infer-pytorch) (submission notebook)\n\n## Lessons learnt\n\n* If there was more time I would have liked to train a segmentation model\n* Crop images to focus on region of interest\n* Look into 2.5D",
    "2013398": "Your solution is top notch @samuelcortinhas ",
    "2013559": "@samuelcortinhas \nI just started my data science journey on kaggle and thinking of participating in competitions. I have understanding of ml, dl, data cleaning and eda\n\nI have been following your impeccable work since I started my journey here. \n\nSo, I want to ask you advice regarding, \nWhat do you suggest me to start with to Ace in competitions ?"
  }
}