{
  "id": 463578,
  "title": "The dreaded scoring error is back!",
  "url": "/competitions/blood-vessel-segmentation/discussion/463578",
  "author_name": "zUni8789798",
  "post_date": "2023-12-26T01:23:05.709000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Hey guys, I'm trying to shake off the dreaded scoring error, but to no avail.</p>\n<p>Some things I've tried:</p>\n<ul>\n<li>switching from 0 1 to 1 1</li>\n<li>resizing mask to original size</li>\n<li>removing small objects</li>\n</ul>\n<p>Maybe I'm doing these submissions wrong? I'll post my code below. I've been experimenting with different variations of the above solutions for quite some time now:</p>\n<p>`import numpy as np</p>\n<p>def rle_encode(img):<br>\n    '''<br>\n    img: numpy array, 1 - mask, 0 - background<br>\n    Returns run length as string formated<br>\n    '''<br>\n    pixels = img.flatten()<br>\n    pixels = np.concatenate([[0], pixels, [0]])<br>\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1<br>\n    runs[1::2] -= runs[::2]<br>\n    rle = ' '.join(str(x) for x in runs)<br>\n    if rle == '':<br>\n        rle = '1 1'<br>\n    return rle<br>\n`</p>\n<p>`import cv2<br>\nimport numpy as np<br>\nimport matplotlib.pyplot as plt<br>\nimport nibabel as nib<br>\nimport glob<br>\nimport os<br>\nfrom PIL import Image<br>\ndef resize_mask(mask, target_shape):<br>\n    thresholded_mask = ((mask &gt; 0.4) * 255).astype(np.uint8)<br>\n    resized_mask = cv2.resize(thresholded_mask, (target_shape[1], target_shape[0]), interpolation=cv2.INTER_NEAREST)<br>\n    return resized_mask</p>\n<p>def remove_small_objects(mask, min_size=30):<br>\n    structure = np.ones((3, 3), dtype=int)  <br>\n    labeled, ncomponents = ndimage.label(mask, structure)<br>\n    unique, counts = np.unique(labeled, return_counts=True)</p>\n<pre><code> unique_label,   (unique, counts):\n      &lt; min_size:\n        mask[labeled == unique_label] = 0\n mask`\n</code></pre>\n<p>`import numpy as np<br>\nimport matplotlib.pyplot as plt<br>\nimport nibabel as nib<br>\nimport glob<br>\nimport os<br>\nfrom PIL import Image<br>\nfrom scipy import ndimage</p>\n<p>output_folder = '/kaggle/working/'<br>\nsubmission = []</p>\n<p>for p_img in(ls_images):<br>\n    path_ = p_img.split(os.path.sep)<br>\n    dataset = path_[-3]<br>\n    slice_id, _ = os.path.splitext(path_[-1])<br>\n    img = plt.imread(p_img)<br>\n    img_shape = img.shape[:2]</p>\n<pre><code>mask_path = sorted(glob.glob(os.path.join(output_folder, slice_id, '*.nii.gz')))[0]\nmask_nii = nib.load(mask_path)\nmask = mask_nii.get_fdata()\ntarget_shape =img_shape\n\nresized_mask = resize_mask(mask, target_shape)\nbinary_mask = resized_mask &gt; 0.5  \ncleaned_mask = remove_small_objects(binary_mask)\n\n\nsubmission.append({\n    : f,\n    : rle_encode(cleaned_mask)\n})`\n</code></pre>",
  "messages": [
    {
      "id": 2574460,
      "postDate": "2023-12-26T01:23:05.710Z",
      "content": "<p>Hey guys, I'm trying to shake off the dreaded scoring error, but to no avail.</p>\n<p>Some things I've tried:</p>\n<ul>\n<li>switching from 0 1 to 1 1</li>\n<li>resizing mask to original size</li>\n<li>removing small objects</li>\n</ul>\n<p>Maybe I'm doing these submissions wrong? I'll post my code below. I've been experimenting with different variations of the above solutions for quite some time now:</p>\n<p>`import numpy as np</p>\n<p>def rle_encode(img):<br>\n    '''<br>\n    img: numpy array, 1 - mask, 0 - background<br>\n    Returns run length as string formated<br>\n    '''<br>\n    pixels = img.flatten()<br>\n    pixels = np.concatenate([[0], pixels, [0]])<br>\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1<br>\n    runs[1::2] -= runs[::2]<br>\n    rle = ' '.join(str(x) for x in runs)<br>\n    if rle == '':<br>\n        rle = '1 1'<br>\n    return rle<br>\n`</p>\n<p>`import cv2<br>\nimport numpy as np<br>\nimport matplotlib.pyplot as plt<br>\nimport nibabel as nib<br>\nimport glob<br>\nimport os<br>\nfrom PIL import Image<br>\ndef resize_mask(mask, target_shape):<br>\n    thresholded_mask = ((mask &gt; 0.4) * 255).astype(np.uint8)<br>\n    resized_mask = cv2.resize(thresholded_mask, (target_shape[1], target_shape[0]), interpolation=cv2.INTER_NEAREST)<br>\n    return resized_mask</p>\n<p>def remove_small_objects(mask, min_size=30):<br>\n    structure = np.ones((3, 3), dtype=int)  <br>\n    labeled, ncomponents = ndimage.label(mask, structure)<br>\n    unique, counts = np.unique(labeled, return_counts=True)</p>\n<pre><code> unique_label,   (unique, counts):\n      &lt; min_size:\n        mask[labeled == unique_label] = 0\n mask`\n</code></pre>\n<p>`import numpy as np<br>\nimport matplotlib.pyplot as plt<br>\nimport nibabel as nib<br>\nimport glob<br>\nimport os<br>\nfrom PIL import Image<br>\nfrom scipy import ndimage</p>\n<p>output_folder = '/kaggle/working/'<br>\nsubmission = []</p>\n<p>for p_img in(ls_images):<br>\n    path_ = p_img.split(os.path.sep)<br>\n    dataset = path_[-3]<br>\n    slice_id, _ = os.path.splitext(path_[-1])<br>\n    img = plt.imread(p_img)<br>\n    img_shape = img.shape[:2]</p>\n<pre><code>mask_path = sorted(glob.glob(os.path.join(output_folder, slice_id, '*.nii.gz')))[0]\nmask_nii = nib.load(mask_path)\nmask = mask_nii.get_fdata()\ntarget_shape =img_shape\n\nresized_mask = resize_mask(mask, target_shape)\nbinary_mask = resized_mask &gt; 0.5  \ncleaned_mask = remove_small_objects(binary_mask)\n\n\nsubmission.append({\n    : f,\n    : rle_encode(cleaned_mask)\n})`\n</code></pre>",
      "rawMarkdown": "Hey guys, I'm trying to shake off the dreaded scoring error, but to no avail.\n\nSome things I've tried:\n\n* switching from 0 1 to 1 1\n* resizing mask to original size\n* removing small objects\n\n\nMaybe I'm doing these submissions wrong? I'll post my code below. I've been experimenting with different variations of the above solutions for quite some time now:\n\n\n`import numpy as np\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    rle = ' '.join(str(x) for x in runs)\n    if rle == '':\n        rle = '1 1'\n    return rle\n`\n\n\n`import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport nibabel as nib\nimport glob\nimport os\nfrom PIL import Image\ndef resize_mask(mask, target_shape):\n    thresholded_mask = ((mask > 0.4) * 255).astype(np.uint8)\n    resized_mask = cv2.resize(thresholded_mask, (target_shape[1], target_shape[0]), interpolation=cv2.INTER_NEAREST)\n    return resized_mask\n\ndef remove_small_objects(mask, min_size=30):\n    structure = np.ones((3, 3), dtype=int)  \n    labeled, ncomponents = ndimage.label(mask, structure)\n    unique, counts = np.unique(labeled, return_counts=True)\n    \n    for unique_label, count in zip(unique, counts):\n        if count < min_size:\n            mask[labeled == unique_label] = 0\n    return mask`\n\n`import numpy as np\nimport matplotlib.pyplot as plt\nimport nibabel as nib\nimport glob\nimport os\nfrom PIL import Image\nfrom scipy import ndimage\n\n\noutput_folder = '/kaggle/working/'\nsubmission = []\n\nfor p_img in(ls_images):\n    path_ = p_img.split(os.path.sep)\n    dataset = path_[-3]\n    slice_id, _ = os.path.splitext(path_[-1])\n    img = plt.imread(p_img)\n    img_shape = img.shape[:2]\n\n    mask_path = sorted(glob.glob(os.path.join(output_folder, slice_id, '*.nii.gz')))[0]\n    mask_nii = nib.load(mask_path)\n    mask = mask_nii.get_fdata()\n    target_shape =img_shape\n\n    resized_mask = resize_mask(mask, target_shape)\n    binary_mask = resized_mask > 0.5  # Convert to binary if necessary\n    cleaned_mask = remove_small_objects(binary_mask)\n\n    # Submission entry\n    submission.append({\n        \"id\": f\"{dataset}_{slice_id}\",\n        \"rle\": rle_encode(cleaned_mask)\n    })`\n",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "2574460": "Hey guys, I'm trying to shake off the dreaded scoring error, but to no avail.\n\nSome things I've tried:\n\n* switching from 0 1 to 1 1\n* resizing mask to original size\n* removing small objects\n\n\nMaybe I'm doing these submissions wrong? I'll post my code below. I've been experimenting with different variations of the above solutions for quite some time now:\n\n\n`import numpy as np\n\ndef rle_encode(img):\n    '''\n    img: numpy array, 1 - mask, 0 - background\n    Returns run length as string formated\n    '''\n    pixels = img.flatten()\n    pixels = np.concatenate([[0], pixels, [0]])\n    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1\n    runs[1::2] -= runs[::2]\n    rle = ' '.join(str(x) for x in runs)\n    if rle == '':\n        rle = '1 1'\n    return rle\n`\n\n\n`import cv2\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport nibabel as nib\nimport glob\nimport os\nfrom PIL import Image\ndef resize_mask(mask, target_shape):\n    thresholded_mask = ((mask > 0.4) * 255).astype(np.uint8)\n    resized_mask = cv2.resize(thresholded_mask, (target_shape[1], target_shape[0]), interpolation=cv2.INTER_NEAREST)\n    return resized_mask\n\ndef remove_small_objects(mask, min_size=30):\n    structure = np.ones((3, 3), dtype=int)  \n    labeled, ncomponents = ndimage.label(mask, structure)\n    unique, counts = np.unique(labeled, return_counts=True)\n    \n    for unique_label, count in zip(unique, counts):\n        if count < min_size:\n            mask[labeled == unique_label] = 0\n    return mask`\n\n`import numpy as np\nimport matplotlib.pyplot as plt\nimport nibabel as nib\nimport glob\nimport os\nfrom PIL import Image\nfrom scipy import ndimage\n\n\noutput_folder = '/kaggle/working/'\nsubmission = []\n\nfor p_img in(ls_images):\n    path_ = p_img.split(os.path.sep)\n    dataset = path_[-3]\n    slice_id, _ = os.path.splitext(path_[-1])\n    img = plt.imread(p_img)\n    img_shape = img.shape[:2]\n\n    mask_path = sorted(glob.glob(os.path.join(output_folder, slice_id, '*.nii.gz')))[0]\n    mask_nii = nib.load(mask_path)\n    mask = mask_nii.get_fdata()\n    target_shape =img_shape\n\n    resized_mask = resize_mask(mask, target_shape)\n    binary_mask = resized_mask > 0.5  # Convert to binary if necessary\n    cleaned_mask = remove_small_objects(binary_mask)\n\n    # Submission entry\n    submission.append({\n        \"id\": f\"{dataset}_{slice_id}\",\n        \"rle\": rle_encode(cleaned_mask)\n    })`\n"
  }
}