{
  "id": 583294,
  "title": "44th Place Solution: [BYU - Locating Bacterial Flagellar Motors 2025]",
  "url": "/competitions/byu-locating-bacterial-flagellar-motors-2025/discussion/583294",
  "author_name": "IBUKI",
  "post_date": "2025-06-06T00:49:19.549000",
  "votes": 6,
  "comment_count": 5,
  "views": 0,
  "content": "<p>First of all, I would like to thank <a href=\"https://www.kaggle.com/playwithme\" target=\"_blank\">@playwithme</a> for sharing an excellent notebook. This is the notebook I referred to during the competition.<br>\nLB0.81-MHAFYOLO -tta-wbf-Submission Notebook(<a href=\"https://www.kaggle.com/code/playwithme/lb0-81-mhafyolo-tta-wbf-submission-notebook\" target=\"_blank\">https://www.kaggle.com/code/playwithme/lb0-81-mhafyolo-tta-wbf-submission-notebook</a>)<br>\nThe model used in the notebook (<a href=\"https://www.kaggle.com/models/yyyy0201/mhafyolo/PyTorch/default\" target=\"_blank\">https://www.kaggle.com/models/yyyy0201/mhafyolo/PyTorch/default</a>) was pre-trained and publicly available.</p>\n<p>My main improvement was quite simple — I only modified one of the functions</p>\n<pre><code> ():\n    \n\n      detections:\n         []\n\n    \n    xy_distance_threshold =  * iou_threshold  \n    z_distance_threshold =                    \n    min_z_span =                               \n\n    \n    detections = (detections, key= d: d[], reverse=)\n    final_detections = []\n\n     detections:\n        base = detections.pop()\n        group = [base]\n        z_set = {base[]}\n        rest = []\n\n         d  detections:\n            dz = (d[] - base[])\n            dy = (d[] - base[])\n            dx = (d[] - base[])\n             dz &lt;= z_distance_threshold  dy &lt;= xy_distance_threshold  dx &lt;= xy_distance_threshold:\n                group.append(d)\n                z_set.add(d[])\n            :\n                rest.append(d)\n\n        \n         (z_set) &gt;= min_z_span:\n            best = (group, key= g: g[])\n            final_detections.append(best)\n\n        detections = rest\n\n     final_detections\n</code></pre>\n<p>Function Overview<br>\nThe perform_3d_nms function performs 3D clustering of detection points, mimicking a Non-Maximum Suppression (NMS) mechanism across 3D space. It filters out redundant detections by identifying spatially close points across Z-slices and keeps only the most confident detection within each valid cluster.</p>\n<hr>\n<p>Parameters<br>\n•    detections: A list of detection dictionaries, each with keys 'z', 'y', 'x', and 'confidence'.<br>\n•    iou_threshold: A scaling factor for the XY distance threshold (e.g., 0.2).</p>\n<hr>\n<p>How It Works</p>\n<ol>\n<li>Threshold Definition:<br>\no    XY distance threshold = 24 * iou_threshold<br>\no    Z-slice range threshold = 10<br>\no    Minimum Z-slice span to be a valid cluster = 3</li>\n<li>Sort detections by confidence (descending)</li>\n<li>Clustering Process:<br>\no    Use the most confident detection as the base.<br>\no    Group together detections that are close in Z, Y, and X directions.<br>\no    If the group spans at least 3 Z-slices, keep the one with the highest confidence.</li>\n<li>Remove processed detections and repeat</li>\n</ol>\n<hr>\n<p>Returns<br>\n•    A list of representative detections (with the highest confidence) from valid 3D clusters.</p>\n<p>The following hyperparameters were used.<br>\nCONFIDENCE_THRESHOLD = 0.7  # Lower threshold to catch more potential motors<br>\nMAX_DETECTIONS_PER_TOMO = 1  # Keep track of top N detections per tomogram<br>\nNMS_IOU_THRESHOLD = 0.7  # Non-maximum suppression threshold for 3D clustering<br>\nCONCENTRATION = 1 # ONLY PROCESS 1/20 slices for fast submission<br>\nSIZE = 1024</p>",
  "messages": [
    {
      "id": 3218186,
      "postDate": "2025-06-06T00:49:19.550Z",
      "content": "<p>First of all, I would like to thank <a href=\"https://www.kaggle.com/playwithme\" target=\"_blank\">@playwithme</a> for sharing an excellent notebook. This is the notebook I referred to during the competition.<br>\nLB0.81-MHAFYOLO -tta-wbf-Submission Notebook(<a href=\"https://www.kaggle.com/code/playwithme/lb0-81-mhafyolo-tta-wbf-submission-notebook\" target=\"_blank\">https://www.kaggle.com/code/playwithme/lb0-81-mhafyolo-tta-wbf-submission-notebook</a>)<br>\nThe model used in the notebook (<a href=\"https://www.kaggle.com/models/yyyy0201/mhafyolo/PyTorch/default\" target=\"_blank\">https://www.kaggle.com/models/yyyy0201/mhafyolo/PyTorch/default</a>) was pre-trained and publicly available.</p>\n<p>My main improvement was quite simple — I only modified one of the functions</p>\n<pre><code> ():\n    \n\n      detections:\n         []\n\n    \n    xy_distance_threshold =  * iou_threshold  \n    z_distance_threshold =                    \n    min_z_span =                               \n\n    \n    detections = (detections, key= d: d[], reverse=)\n    final_detections = []\n\n     detections:\n        base = detections.pop()\n        group = [base]\n        z_set = {base[]}\n        rest = []\n\n         d  detections:\n            dz = (d[] - base[])\n            dy = (d[] - base[])\n            dx = (d[] - base[])\n             dz &lt;= z_distance_threshold  dy &lt;= xy_distance_threshold  dx &lt;= xy_distance_threshold:\n                group.append(d)\n                z_set.add(d[])\n            :\n                rest.append(d)\n\n        \n         (z_set) &gt;= min_z_span:\n            best = (group, key= g: g[])\n            final_detections.append(best)\n\n        detections = rest\n\n     final_detections\n</code></pre>\n<p>Function Overview<br>\nThe perform_3d_nms function performs 3D clustering of detection points, mimicking a Non-Maximum Suppression (NMS) mechanism across 3D space. It filters out redundant detections by identifying spatially close points across Z-slices and keeps only the most confident detection within each valid cluster.</p>\n<hr>\n<p>Parameters<br>\n•    detections: A list of detection dictionaries, each with keys 'z', 'y', 'x', and 'confidence'.<br>\n•    iou_threshold: A scaling factor for the XY distance threshold (e.g., 0.2).</p>\n<hr>\n<p>How It Works</p>\n<ol>\n<li>Threshold Definition:<br>\no    XY distance threshold = 24 * iou_threshold<br>\no    Z-slice range threshold = 10<br>\no    Minimum Z-slice span to be a valid cluster = 3</li>\n<li>Sort detections by confidence (descending)</li>\n<li>Clustering Process:<br>\no    Use the most confident detection as the base.<br>\no    Group together detections that are close in Z, Y, and X directions.<br>\no    If the group spans at least 3 Z-slices, keep the one with the highest confidence.</li>\n<li>Remove processed detections and repeat</li>\n</ol>\n<hr>\n<p>Returns<br>\n•    A list of representative detections (with the highest confidence) from valid 3D clusters.</p>\n<p>The following hyperparameters were used.<br>\nCONFIDENCE_THRESHOLD = 0.7  # Lower threshold to catch more potential motors<br>\nMAX_DETECTIONS_PER_TOMO = 1  # Keep track of top N detections per tomogram<br>\nNMS_IOU_THRESHOLD = 0.7  # Non-maximum suppression threshold for 3D clustering<br>\nCONCENTRATION = 1 # ONLY PROCESS 1/20 slices for fast submission<br>\nSIZE = 1024</p>",
      "rawMarkdown": "First of all, I would like to thank @playwithme for sharing an excellent notebook. This is the notebook I referred to during the competition.\nLB0.81-MHAFYOLO -tta-wbf-Submission Notebook(https://www.kaggle.com/code/playwithme/lb0-81-mhafyolo-tta-wbf-submission-notebook)\nThe model used in the notebook (https://www.kaggle.com/models/yyyy0201/mhafyolo/PyTorch/default) was pre-trained and publicly available.\n\nMy main improvement was quite simple — I only modified one of the functions\n\n```python\ndef perform_3d_nms(detections, iou_threshold):\n    \"\"\"\n    3D NMS-like clustering that considers spatial distance and Z-slice thickness.\n\n    Parameters:\n        detections (list of dict): Detection info list [{'z': int, 'y': int, 'x': int, 'confidence': float}, ...]\n        iou_threshold (float): Threshold to scale the XY spatial distance limit (e.g., 0.2)\n\n    Returns:\n        list of dict: Representative detections after NMS (can include multiple clusters)\n    \"\"\"\n\n    if not detections:\n        return []\n\n    # Clustering conditions\n    xy_distance_threshold = 24 * iou_threshold  # 24 is the assumed side length of a box\n    z_distance_threshold = 10                   # Acceptable range in Z-slice direction\n    min_z_span = 3                              # Minimum number of Z-slices to consider as a valid cluster\n\n    # Sort detections by confidence in descending order\n    detections = sorted(detections, key=lambda d: d['confidence'], reverse=True)\n    final_detections = []\n\n    while detections:\n        base = detections.pop(0)\n        group = [base]\n        z_set = {base['z']}\n        rest = []\n\n        for d in detections:\n            dz = abs(d['z'] - base['z'])\n            dy = abs(d['y'] - base['y'])\n            dx = abs(d['x'] - base['x'])\n            if dz <= z_distance_threshold and dy <= xy_distance_threshold and dx <= xy_distance_threshold:\n                group.append(d)\n                z_set.add(d['z'])\n            else:\n                rest.append(d)\n\n        # If the group spans enough Z-slices, take the highest-confidence detection\n        if len(z_set) >= min_z_span:\n            best = max(group, key=lambda g: g['confidence'])\n            final_detections.append(best)\n\n        detections = rest\n\n    return final_detections\n\n```\n\nFunction Overview\nThe perform_3d_nms function performs 3D clustering of detection points, mimicking a Non-Maximum Suppression (NMS) mechanism across 3D space. It filters out redundant detections by identifying spatially close points across Z-slices and keeps only the most confident detection within each valid cluster.\n________________________________________\nParameters\n•\tdetections: A list of detection dictionaries, each with keys 'z', 'y', 'x', and 'confidence'.\n•\tiou_threshold: A scaling factor for the XY distance threshold (e.g., 0.2).\n________________________________________\nHow It Works\n1.\tThreshold Definition:\no\tXY distance threshold = 24 * iou_threshold\no\tZ-slice range threshold = 10\no\tMinimum Z-slice span to be a valid cluster = 3\n2.\tSort detections by confidence (descending)\n3.\tClustering Process:\no\tUse the most confident detection as the base.\no\tGroup together detections that are close in Z, Y, and X directions.\no\tIf the group spans at least 3 Z-slices, keep the one with the highest confidence.\n4.\tRemove processed detections and repeat\n________________________________________\nReturns\n•\tA list of representative detections (with the highest confidence) from valid 3D clusters.\n\nThe following hyperparameters were used.\nCONFIDENCE_THRESHOLD = 0.7  # Lower threshold to catch more potential motors\nMAX_DETECTIONS_PER_TOMO = 1  # Keep track of top N detections per tomogram\nNMS_IOU_THRESHOLD = 0.7  # Non-maximum suppression threshold for 3D clustering\nCONCENTRATION = 1 # ONLY PROCESS 1/20 slices for fast submission\nSIZE = 1024\n",
      "votes": 6
    },
    {
      "id": 3218413,
      "postDate": "2025-06-06T06:39:26.137Z",
      "content": "<p>Nice solution, we got halfway through RTDETRv2 not as good as my own YOLO model😭</p>",
      "rawMarkdown": "Nice solution, we got halfway through RTDETRv2 not as good as my own YOLO model😭",
      "votes": 1,
      "replies": [
        {
          "id": 3218426,
          "postDate": "2025-06-06T07:16:05.827Z",
          "content": "<p>show your Solution! 😆</p>",
          "rawMarkdown": "show your Solution! 😆"
        }
      ]
    },
    {
      "id": 3218270,
      "postDate": "2025-06-06T03:36:21.203Z",
      "content": "<p>Good job on te solution 🤗</p>",
      "rawMarkdown": "Good job on te solution 🤗",
      "votes": 1
    },
    {
      "id": 3218202,
      "postDate": "2025-06-06T01:14:43.283Z",
      "content": "<p>Well Done, but I ended up not submitting with this weight in the end, hahaha.😭😭😭</p>",
      "rawMarkdown": "Well Done, but I ended up not submitting with this weight in the end, hahaha.😭😭😭",
      "votes": 1
    },
    {
      "id": 3218193,
      "postDate": "2025-06-06T00:54:13.793Z",
      "rawMarkdown": "",
      "isDeleted": true
    }
  ],
  "comments": [
    {
      "id": 3218413,
      "author_name": "yyyy0201",
      "author_url": "",
      "post_date": "2025-06-06T06:39:26.137000",
      "content": "<p>Nice solution, we got halfway through RTDETRv2 not as good as my own YOLO model😭</p>",
      "votes": 1,
      "replies": [
        {
          "id": 3218426,
          "author_name": "Hide on bush",
          "author_url": "",
          "post_date": "2025-06-06T07:16:05.827000",
          "content": "<p>show your Solution! 😆</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 3218270,
      "author_name": "Joseph Hadad",
      "author_url": "",
      "post_date": "2025-06-06T03:36:21.203000",
      "content": "<p>Good job on te solution 🤗</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 3218202,
      "author_name": "Hide on bush",
      "author_url": "",
      "post_date": "2025-06-06T01:14:43.283000",
      "content": "<p>Well Done, but I ended up not submitting with this weight in the end, hahaha.😭😭😭</p>",
      "votes": 1,
      "replies": []
    },
    {
      "id": 3218193,
      "author_name": "",
      "author_url": "",
      "post_date": "2025-06-06T00:54:13.793000",
      "content": "",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "3218186": "First of all, I would like to thank @playwithme for sharing an excellent notebook. This is the notebook I referred to during the competition.\nLB0.81-MHAFYOLO -tta-wbf-Submission Notebook(https://www.kaggle.com/code/playwithme/lb0-81-mhafyolo-tta-wbf-submission-notebook)\nThe model used in the notebook (https://www.kaggle.com/models/yyyy0201/mhafyolo/PyTorch/default) was pre-trained and publicly available.\n\nMy main improvement was quite simple — I only modified one of the functions\n\n```python\ndef perform_3d_nms(detections, iou_threshold):\n    \"\"\"\n    3D NMS-like clustering that considers spatial distance and Z-slice thickness.\n\n    Parameters:\n        detections (list of dict): Detection info list [{'z': int, 'y': int, 'x': int, 'confidence': float}, ...]\n        iou_threshold (float): Threshold to scale the XY spatial distance limit (e.g., 0.2)\n\n    Returns:\n        list of dict: Representative detections after NMS (can include multiple clusters)\n    \"\"\"\n\n    if not detections:\n        return []\n\n    # Clustering conditions\n    xy_distance_threshold = 24 * iou_threshold  # 24 is the assumed side length of a box\n    z_distance_threshold = 10                   # Acceptable range in Z-slice direction\n    min_z_span = 3                              # Minimum number of Z-slices to consider as a valid cluster\n\n    # Sort detections by confidence in descending order\n    detections = sorted(detections, key=lambda d: d['confidence'], reverse=True)\n    final_detections = []\n\n    while detections:\n        base = detections.pop(0)\n        group = [base]\n        z_set = {base['z']}\n        rest = []\n\n        for d in detections:\n            dz = abs(d['z'] - base['z'])\n            dy = abs(d['y'] - base['y'])\n            dx = abs(d['x'] - base['x'])\n            if dz <= z_distance_threshold and dy <= xy_distance_threshold and dx <= xy_distance_threshold:\n                group.append(d)\n                z_set.add(d['z'])\n            else:\n                rest.append(d)\n\n        # If the group spans enough Z-slices, take the highest-confidence detection\n        if len(z_set) >= min_z_span:\n            best = max(group, key=lambda g: g['confidence'])\n            final_detections.append(best)\n\n        detections = rest\n\n    return final_detections\n\n```\n\nFunction Overview\nThe perform_3d_nms function performs 3D clustering of detection points, mimicking a Non-Maximum Suppression (NMS) mechanism across 3D space. It filters out redundant detections by identifying spatially close points across Z-slices and keeps only the most confident detection within each valid cluster.\n________________________________________\nParameters\n•\tdetections: A list of detection dictionaries, each with keys 'z', 'y', 'x', and 'confidence'.\n•\tiou_threshold: A scaling factor for the XY distance threshold (e.g., 0.2).\n________________________________________\nHow It Works\n1.\tThreshold Definition:\no\tXY distance threshold = 24 * iou_threshold\no\tZ-slice range threshold = 10\no\tMinimum Z-slice span to be a valid cluster = 3\n2.\tSort detections by confidence (descending)\n3.\tClustering Process:\no\tUse the most confident detection as the base.\no\tGroup together detections that are close in Z, Y, and X directions.\no\tIf the group spans at least 3 Z-slices, keep the one with the highest confidence.\n4.\tRemove processed detections and repeat\n________________________________________\nReturns\n•\tA list of representative detections (with the highest confidence) from valid 3D clusters.\n\nThe following hyperparameters were used.\nCONFIDENCE_THRESHOLD = 0.7  # Lower threshold to catch more potential motors\nMAX_DETECTIONS_PER_TOMO = 1  # Keep track of top N detections per tomogram\nNMS_IOU_THRESHOLD = 0.7  # Non-maximum suppression threshold for 3D clustering\nCONCENTRATION = 1 # ONLY PROCESS 1/20 slices for fast submission\nSIZE = 1024\n",
    "3218413": "Nice solution, we got halfway through RTDETRv2 not as good as my own YOLO model😭",
    "3218270": "Good job on te solution 🤗",
    "3218202": "Well Done, but I ended up not submitting with this weight in the end, hahaha.😭😭😭",
    "3218193": ""
  }
}