{
  "id": 384059,
  "title": "Softmax or sigmoid for binary classification",
  "url": "/competitions/rsna-breast-cancer-detection/discussion/384059",
  "author_name": "Shujaat Hasan",
  "post_date": "2023-02-06T12:23:59.079000",
  "votes": 0,
  "comment_count": 1,
  "views": 0,
  "content": "<p>HI everyone,</p>\n<p>I'm confused why Im not able to submit my results. As the problem is binary classification, so I tried sigmoid as my last layer and one neuron with BCE loss. It kept giving submission error, but when I tried doing it by using cross entropy loss and 10 neuron in output layer it worked. If anyone help me on this, it would be grateful.<br>\nThanks</p>",
  "messages": [
    {
      "id": 2134960,
      "postDate": "2023-02-08T11:06:19.487Z",
      "content": "<p>It should works using a single neuron with sigmoid activation. Why would you use 10 neurons in your output layer for binary classification?<br>\nIf you get the <em>Submission Scoring Error</em>, it might be because your submission file is not in the good format or there are duplicates. What I suggest is that you look at the way you generate the submission.csv file. <br>\n I personally use this code from a <a href=\"https://www.kaggle.com/code/markwijkhuizen/rsna-convnextv2-inference-tensorflow\" target=\"_blank\">notebook</a> of Mark Wijkhuizen:</p>\n<pre><code>SUBMISSION_ROWS = []\ndf = pd.read_csv(TEST_CSV_PATH)\n\n# Loop for each group (patient_id/laterality)\nfor (patient_id, laterality), group_df in tqdm(df.groupby(['patient_id', 'laterality'])):\n    cancer = 0\n    for _, row in group_df.iterrows():\n        # Load image\n        image_id = row['image_id']\n        img = cv2.imread(f'{image_id}.png', -1)\n\n        img = np.expand_dims(img, [0, 3])\n        cancer += model.predict_on_batch(img).squeeze() / len(group_df)\n        os.remove(f'{image_id}.png')\n\n    # Add submission row\n    SUBMISSION_ROWS.append({\n        'prediction_id': f'{patient_id}_{laterality}',\n        'cancer': np.int8(cancer &gt; THRESHOLD_BEST)\n    })\n\nsubmission_df = pd.DataFrame(SUBMISSION_ROWS)\nsubmission_df.to_csv('submission.csv', index=False)\npd.read_csv('submission.csv')\n</code></pre>",
      "rawMarkdown": "It should works using a single neuron with sigmoid activation. Why would you use 10 neurons in your output layer for binary classification?\nIf you get the *Submission Scoring Error*, it might be because your submission file is not in the good format or there are duplicates. What I suggest is that you look at the way you generate the submission.csv file. \n I personally use this code from a [notebook](https://www.kaggle.com/code/markwijkhuizen/rsna-convnextv2-inference-tensorflow) of Mark Wijkhuizen:\n```\nSUBMISSION_ROWS = []\ndf = pd.read_csv(TEST_CSV_PATH)\n\n# Loop for each group (patient_id/laterality)\nfor (patient_id, laterality), group_df in tqdm(df.groupby(['patient_id', 'laterality'])):\n    cancer = 0\n    for _, row in group_df.iterrows():\n        # Load image\n        image_id = row['image_id']\n        img = cv2.imread(f'{image_id}.png', -1)\n        \n        img = np.expand_dims(img, [0, 3])\n        cancer += model.predict_on_batch(img).squeeze() / len(group_df)\n        os.remove(f'{image_id}.png')\n    \n    # Add submission row\n    SUBMISSION_ROWS.append({\n        'prediction_id': f'{patient_id}_{laterality}',\n        'cancer': np.int8(cancer > THRESHOLD_BEST)\n    })\n\nsubmission_df = pd.DataFrame(SUBMISSION_ROWS)\nsubmission_df.to_csv('submission.csv', index=False)\npd.read_csv('submission.csv')\n```",
      "votes": 1
    },
    {
      "id": 2131824,
      "postDate": "2023-02-06T12:23:59.080Z",
      "content": "<p>HI everyone,</p>\n<p>I'm confused why Im not able to submit my results. As the problem is binary classification, so I tried sigmoid as my last layer and one neuron with BCE loss. It kept giving submission error, but when I tried doing it by using cross entropy loss and 10 neuron in output layer it worked. If anyone help me on this, it would be grateful.<br>\nThanks</p>",
      "rawMarkdown": "HI everyone,\n\nI'm confused why Im not able to submit my results. As the problem is binary classification, so I tried sigmoid as my last layer and one neuron with BCE loss. It kept giving submission error, but when I tried doing it by using cross entropy loss and 10 neuron in output layer it worked. If anyone help me on this, it would be grateful.\nThanks"
    }
  ],
  "comments": [
    {
      "id": 2134960,
      "author_name": "Paul Bacher",
      "author_url": "",
      "post_date": "2023-02-08T11:06:19.487000",
      "content": "<p>It should works using a single neuron with sigmoid activation. Why would you use 10 neurons in your output layer for binary classification?<br>\nIf you get the <em>Submission Scoring Error</em>, it might be because your submission file is not in the good format or there are duplicates. What I suggest is that you look at the way you generate the submission.csv file. <br>\n I personally use this code from a <a href=\"https://www.kaggle.com/code/markwijkhuizen/rsna-convnextv2-inference-tensorflow\" target=\"_blank\">notebook</a> of Mark Wijkhuizen:</p>\n<pre><code>SUBMISSION_ROWS = []\ndf = pd.read_csv(TEST_CSV_PATH)\n\n# Loop for each group (patient_id/laterality)\nfor (patient_id, laterality), group_df in tqdm(df.groupby(['patient_id', 'laterality'])):\n    cancer = 0\n    for _, row in group_df.iterrows():\n        # Load image\n        image_id = row['image_id']\n        img = cv2.imread(f'{image_id}.png', -1)\n\n        img = np.expand_dims(img, [0, 3])\n        cancer += model.predict_on_batch(img).squeeze() / len(group_df)\n        os.remove(f'{image_id}.png')\n\n    # Add submission row\n    SUBMISSION_ROWS.append({\n        'prediction_id': f'{patient_id}_{laterality}',\n        'cancer': np.int8(cancer &gt; THRESHOLD_BEST)\n    })\n\nsubmission_df = pd.DataFrame(SUBMISSION_ROWS)\nsubmission_df.to_csv('submission.csv', index=False)\npd.read_csv('submission.csv')\n</code></pre>",
      "votes": 1,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2134960": "It should works using a single neuron with sigmoid activation. Why would you use 10 neurons in your output layer for binary classification?\nIf you get the *Submission Scoring Error*, it might be because your submission file is not in the good format or there are duplicates. What I suggest is that you look at the way you generate the submission.csv file. \n I personally use this code from a [notebook](https://www.kaggle.com/code/markwijkhuizen/rsna-convnextv2-inference-tensorflow) of Mark Wijkhuizen:\n```\nSUBMISSION_ROWS = []\ndf = pd.read_csv(TEST_CSV_PATH)\n\n# Loop for each group (patient_id/laterality)\nfor (patient_id, laterality), group_df in tqdm(df.groupby(['patient_id', 'laterality'])):\n    cancer = 0\n    for _, row in group_df.iterrows():\n        # Load image\n        image_id = row['image_id']\n        img = cv2.imread(f'{image_id}.png', -1)\n        \n        img = np.expand_dims(img, [0, 3])\n        cancer += model.predict_on_batch(img).squeeze() / len(group_df)\n        os.remove(f'{image_id}.png')\n    \n    # Add submission row\n    SUBMISSION_ROWS.append({\n        'prediction_id': f'{patient_id}_{laterality}',\n        'cancer': np.int8(cancer > THRESHOLD_BEST)\n    })\n\nsubmission_df = pd.DataFrame(SUBMISSION_ROWS)\nsubmission_df.to_csv('submission.csv', index=False)\npd.read_csv('submission.csv')\n```",
    "2131824": "HI everyone,\n\nI'm confused why Im not able to submit my results. As the problem is binary classification, so I tried sigmoid as my last layer and one neuron with BCE loss. It kept giving submission error, but when I tried doing it by using cross entropy loss and 10 neuron in output layer it worked. If anyone help me on this, it would be grateful.\nThanks"
  }
}