{
  "id": 579617,
  "title": "🍿 Tips: Changing YOLO's default behavior",
  "url": "/competitions/byu-locating-bacterial-flagellar-motors-2025/discussion/579617",
  "author_name": "",
  "post_date": "2025-05-19T04:21:40.150260600Z",
  "votes": 24,
  "comment_count": 2,
  "views": 0,
  "content": "<p>Since YOLO is major tools for this competition, I share a simple trick to change default behavior of YOLO.</p>\n<p>Using the below snippet, we can dynamically changes YOLO's behavior.</p>\n<pre><code> typing  \n\n\n ():\n    \n      (obj, method_name):\n         AttributeError(\n            \n        )\n    bound = func.__get__(obj, obj.__class__)\n    (obj, method_name, bound)\n</code></pre>\n<p>For example, the below notebook showcases how we can output featuremap and DFL prediction of YOLO.</p>\n<p><a href=\"https://www.kaggle.com/code/tatamikenn/reverse-engineering-yolo\" target=\"_blank\">https://www.kaggle.com/code/tatamikenn/reverse-engineering-yolo</a></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fcd0b0aa59734b8f19ec0c58103239d33%2FScreenshot%202025-05-19%20at%2013.19.04.png?generation=1747628443010165&amp;alt=media\" alt=\"\"><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F9c18cf4191e67e5e9d18b9558a810ff1%2FScreenshot%202025-05-19%20at%2012.57.07.png?generation=1747628465787381&amp;alt=media\" alt=\"\"></p>",
  "messages": [
    {
      "id": "3204928",
      "postDate": "05/19/2025 04:21:40",
      "content": "<p>Since YOLO is major tools for this competition, I share a simple trick to change default behavior of YOLO.</p>\n<p>Using the below snippet, we can dynamically changes YOLO's behavior.</p>\n<pre><code> typing  \n\n\n ():\n    \n      (obj, method_name):\n         AttributeError(\n            \n        )\n    bound = func.__get__(obj, obj.__class__)\n    (obj, method_name, bound)\n</code></pre>\n<p>For example, the below notebook showcases how we can output featuremap and DFL prediction of YOLO.</p>\n<p><a href=\"https://www.kaggle.com/code/tatamikenn/reverse-engineering-yolo\" target=\"_blank\">https://www.kaggle.com/code/tatamikenn/reverse-engineering-yolo</a></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fcd0b0aa59734b8f19ec0c58103239d33%2FScreenshot%202025-05-19%20at%2013.19.04.png?generation=1747628443010165&amp;alt=media\" alt=\"\"><br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F9c18cf4191e67e5e9d18b9558a810ff1%2FScreenshot%202025-05-19%20at%2012.57.07.png?generation=1747628465787381&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "Since YOLO is major tools for this competition, I share a simple trick to change default behavior of YOLO.\n\nUsing the below snippet, we can dynamically changes YOLO's behavior.\n\n```python\nfrom typing import Callable\n\n\ndef replace_method(obj, method_name: str, func: Callable):\n    \"\"\"\n    Replace a method of an object with a new function.\n\n    Args:\n        obj: The object whose method is to be replaced.\n        method_name (str): The name of the method to replace.\n        func (FunctionType): The new function to set as the method.\n    \"\"\"\n    if not hasattr(obj, method_name):\n        raise AttributeError(\n            f\"{obj.__class__.__name__} has no method '{method_name}' to replace.\"\n        )\n    bound = func.__get__(obj, obj.__class__)\n    setattr(obj, method_name, bound)\n```\n\nFor example, the below notebook showcases how we can output featuremap and DFL prediction of YOLO.\n\nhttps://www.kaggle.com/code/tatamikenn/reverse-engineering-yolo\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fcd0b0aa59734b8f19ec0c58103239d33%2FScreenshot%202025-05-19%20at%2013.19.04.png?generation=1747628443010165&alt=media)\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F9c18cf4191e67e5e9d18b9558a810ff1%2FScreenshot%202025-05-19%20at%2012.57.07.png?generation=1747628465787381&alt=media)",
      "votes": null
    },
    {
      "id": "3204932",
      "postDate": "05/19/2025 04:26:18",
      "content": "<p>You can also use this to extract feature.</p>\n<pre><code> ():\n    features = {}\n     ():\n         ():\n            features[name] = output\n         hook\n    model.model.model[idx].register_forward_hook(make_hook(feature_name))\n     features\n</code></pre>",
      "rawMarkdown": "You can also use this to extract feature.\n\n```{python}\ndef get_feature_map(model, idx, feautre_name):\n    features = {}\n    def make_hook(name):\n        def hook(module, input, output):\n            features[name] = output\n        return hook\n    model.model.model[idx].register_forward_hook(make_hook(feature_name))\n    return features\n```",
      "votes": null
    },
    {
      "id": "3205943",
      "postDate": "05/20/2025 15:37:38",
      "content": "<p>Nice work 👍</p>",
      "rawMarkdown": "Nice work 👍",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 3204932,
      "author_name": "tom99763",
      "author_url": "",
      "post_date": "05/19/2025 04:26:18",
      "content": "<p>You can also use this to extract feature.</p>\n<pre><code> ():\n    features = {}\n     ():\n         ():\n            features[name] = output\n         hook\n    model.model.model[idx].register_forward_hook(make_hook(feature_name))\n     features\n</code></pre>",
      "votes": null,
      "replies": []
    },
    {
      "id": 3205943,
      "author_name": "aliraza1709",
      "author_url": "",
      "post_date": "05/20/2025 15:37:38",
      "content": "<p>Nice work 👍</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "3204928": "Since YOLO is major tools for this competition, I share a simple trick to change default behavior of YOLO.\n\nUsing the below snippet, we can dynamically changes YOLO's behavior.\n\n```python\nfrom typing import Callable\n\n\ndef replace_method(obj, method_name: str, func: Callable):\n    \"\"\"\n    Replace a method of an object with a new function.\n\n    Args:\n        obj: The object whose method is to be replaced.\n        method_name (str): The name of the method to replace.\n        func (FunctionType): The new function to set as the method.\n    \"\"\"\n    if not hasattr(obj, method_name):\n        raise AttributeError(\n            f\"{obj.__class__.__name__} has no method '{method_name}' to replace.\"\n        )\n    bound = func.__get__(obj, obj.__class__)\n    setattr(obj, method_name, bound)\n```\n\nFor example, the below notebook showcases how we can output featuremap and DFL prediction of YOLO.\n\nhttps://www.kaggle.com/code/tatamikenn/reverse-engineering-yolo\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2Fcd0b0aa59734b8f19ec0c58103239d33%2FScreenshot%202025-05-19%20at%2013.19.04.png?generation=1747628443010165&alt=media)\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4910466%2F9c18cf4191e67e5e9d18b9558a810ff1%2FScreenshot%202025-05-19%20at%2012.57.07.png?generation=1747628465787381&alt=media)",
    "3204932": "You can also use this to extract feature.\n\n```{python}\ndef get_feature_map(model, idx, feautre_name):\n    features = {}\n    def make_hook(name):\n        def hook(module, input, output):\n            features[name] = output\n        return hook\n    model.model.model[idx].register_forward_hook(make_hook(feature_name))\n    return features\n```",
    "3205943": "Nice work 👍"
  },
  "source": "meta"
}