{
  "id": 335087,
  "title": "evaluation_metric",
  "url": "/competitions/hubmap-organ-segmentation/discussion/335087",
  "author_name": "Arvind Devarkonda",
  "post_date": "2022-07-04T16:29:37.072000",
  "votes": 2,
  "comment_count": 5,
  "views": 0,
  "content": "<p>how to define metric function</p>",
  "messages": [
    {
      "id": 1843539,
      "postDate": "2022-07-05T02:21:04.753Z",
      "content": "<p><a href=\"https://www.kaggle.com/arvinddevarkonda\" target=\"_blank\">@arvinddevarkonda</a> </p>\n<h1><strong>Dice Coefficient Evaluation Metric</strong></h1>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4906684%2Feceea9c3e1c59e78c97a88b17e8d9e64%2Fdownload.png?generation=1656987569815331&amp;alt=media\" alt=\"\"></p>\n<pre><code>from keras import backend as K\nfrom keras.losses import binary_crossentropy\nimport tensorflow as tf\n\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\ndef iou_coef(y_true, y_pred, smooth=1):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n    iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n    return iou\n\ndef dice_loss(y_true, y_pred):\n    smooth = 1.\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = y_true_f * y_pred_f\n    score = (2. * K.sum(intersection) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    return 1. - score\n\ndef bce_dice_loss(y_true, y_pred):\n    return binary_crossentropy(tf.cast(y_true, tf.float32), y_pred) + 0.5 * dice_loss(tf.cast(y_true, tf.float32), y_pred)\n</code></pre>\n<p><strong>Implemented the loss function in below notebook</strong>, Refer it<br>\n<a href=\"https://www.kaggle.com/code/muki2003/hubmap-hpa-deep-learning-u-net-cnn\" target=\"_blank\">https://www.kaggle.com/code/muki2003/hubmap-hpa-deep-learning-u-net-cnn</a></p>",
      "rawMarkdown": "@arvinddevarkonda \n\n# **Dice Coefficient Evaluation Metric**\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4906684%2Feceea9c3e1c59e78c97a88b17e8d9e64%2Fdownload.png?generation=1656987569815331&alt=media)\n\n```\nfrom keras import backend as K\nfrom keras.losses import binary_crossentropy\nimport tensorflow as tf\n\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\ndef iou_coef(y_true, y_pred, smooth=1):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n    iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n    return iou\n\ndef dice_loss(y_true, y_pred):\n    smooth = 1.\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = y_true_f * y_pred_f\n    score = (2. * K.sum(intersection) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    return 1. - score\n\ndef bce_dice_loss(y_true, y_pred):\n    return binary_crossentropy(tf.cast(y_true, tf.float32), y_pred) + 0.5 * dice_loss(tf.cast(y_true, tf.float32), y_pred)\n```\n\n**Implemented the loss function in below notebook**, Refer it\nhttps://www.kaggle.com/code/muki2003/hubmap-hpa-deep-learning-u-net-cnn",
      "votes": 1,
      "replies": [
        {
          "id": 1843634,
          "postDate": "2022-07-05T04:00:52.010Z",
          "content": "<p>can i use this same function in pytorch too</p>",
          "rawMarkdown": "can i use this same function in pytorch too"
        },
        {
          "id": 1844175,
          "postDate": "2022-07-05T11:50:47.833Z",
          "rawMarkdown": "",
          "isDeleted": true
        },
        {
          "id": 1844177,
          "postDate": "2022-07-05T11:53:38.630Z",
          "content": "<p><a href=\"https://www.kaggle.com/arvinddevarkonda\" target=\"_blank\">@arvinddevarkonda</a> </p>\n<p>Below Notebook will help for evaluation metric in keras &amp; pytorch,<br>\n<a href=\"https://www.kaggle.com/code/bigironsphere/loss-function-library-keras-pytorch/notebook\" target=\"_blank\">https://www.kaggle.com/code/bigironsphere/loss-function-library-keras-pytorch/notebook</a></p>\n<p>This kernel provides a reference library for some popular custom loss functions that you can easily import into your code.</p>",
          "rawMarkdown": "@arvinddevarkonda \n\nBelow Notebook will help for evaluation metric in keras & pytorch,\nhttps://www.kaggle.com/code/bigironsphere/loss-function-library-keras-pytorch/notebook\n\nThis kernel provides a reference library for some popular custom loss functions that you can easily import into your code.",
          "votes": 1
        }
      ]
    },
    {
      "id": 1843236,
      "postDate": "2022-07-04T16:29:37.073Z",
      "content": "<p>how to define metric function</p>",
      "rawMarkdown": "how to define metric function",
      "votes": 2
    },
    {
      "id": 1843273,
      "postDate": "2022-07-04T16:58:46.867Z",
      "content": "<p>This is how you define a metric function:</p>\n<pre><code>def metric_function():\n       pass\n</code></pre>",
      "rawMarkdown": "This is how you define a metric function:\n\n```\ndef metric_function():\n       pass\n```"
    }
  ],
  "comments": [
    {
      "id": 1843539,
      "author_name": "MUKILAN S",
      "author_url": "",
      "post_date": "2022-07-05T02:21:04.753000",
      "content": "<p><a href=\"https://www.kaggle.com/arvinddevarkonda\" target=\"_blank\">@arvinddevarkonda</a> </p>\n<h1><strong>Dice Coefficient Evaluation Metric</strong></h1>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4906684%2Feceea9c3e1c59e78c97a88b17e8d9e64%2Fdownload.png?generation=1656987569815331&amp;alt=media\" alt=\"\"></p>\n<pre><code>from keras import backend as K\nfrom keras.losses import binary_crossentropy\nimport tensorflow as tf\n\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\ndef iou_coef(y_true, y_pred, smooth=1):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n    iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n    return iou\n\ndef dice_loss(y_true, y_pred):\n    smooth = 1.\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = y_true_f * y_pred_f\n    score = (2. * K.sum(intersection) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    return 1. - score\n\ndef bce_dice_loss(y_true, y_pred):\n    return binary_crossentropy(tf.cast(y_true, tf.float32), y_pred) + 0.5 * dice_loss(tf.cast(y_true, tf.float32), y_pred)\n</code></pre>\n<p><strong>Implemented the loss function in below notebook</strong>, Refer it<br>\n<a href=\"https://www.kaggle.com/code/muki2003/hubmap-hpa-deep-learning-u-net-cnn\" target=\"_blank\">https://www.kaggle.com/code/muki2003/hubmap-hpa-deep-learning-u-net-cnn</a></p>",
      "votes": 1,
      "replies": [
        {
          "id": 1843634,
          "author_name": "Arvind Devarkonda",
          "author_url": "",
          "post_date": "2022-07-05T04:00:52.010000",
          "content": "<p>can i use this same function in pytorch too</p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1844175,
          "author_name": "",
          "author_url": "",
          "post_date": "2022-07-05T11:50:47.833000",
          "content": "",
          "votes": 0,
          "replies": []
        },
        {
          "id": 1844177,
          "author_name": "MUKILAN S",
          "author_url": "",
          "post_date": "2022-07-05T11:53:38.630000",
          "content": "<p><a href=\"https://www.kaggle.com/arvinddevarkonda\" target=\"_blank\">@arvinddevarkonda</a> </p>\n<p>Below Notebook will help for evaluation metric in keras &amp; pytorch,<br>\n<a href=\"https://www.kaggle.com/code/bigironsphere/loss-function-library-keras-pytorch/notebook\" target=\"_blank\">https://www.kaggle.com/code/bigironsphere/loss-function-library-keras-pytorch/notebook</a></p>\n<p>This kernel provides a reference library for some popular custom loss functions that you can easily import into your code.</p>",
          "votes": 1,
          "replies": []
        }
      ]
    },
    {
      "id": 1843273,
      "author_name": "Uros Jarc",
      "author_url": "",
      "post_date": "2022-07-04T16:58:46.867000",
      "content": "<p>This is how you define a metric function:</p>\n<pre><code>def metric_function():\n       pass\n</code></pre>",
      "votes": 0,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "1843539": "@arvinddevarkonda \n\n# **Dice Coefficient Evaluation Metric**\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F4906684%2Feceea9c3e1c59e78c97a88b17e8d9e64%2Fdownload.png?generation=1656987569815331&alt=media)\n\n```\nfrom keras import backend as K\nfrom keras.losses import binary_crossentropy\nimport tensorflow as tf\n\ndef dice_coef(y_true, y_pred, smooth=1):\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = K.sum(y_true_f * y_pred_f)\n    return (2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n\ndef iou_coef(y_true, y_pred, smooth=1):\n    intersection = K.sum(K.abs(y_true * y_pred), axis=[1,2,3])\n    union = K.sum(y_true,[1,2,3])+K.sum(y_pred,[1,2,3])-intersection\n    iou = K.mean((intersection + smooth) / (union + smooth), axis=0)\n    return iou\n\ndef dice_loss(y_true, y_pred):\n    smooth = 1.\n    y_true_f = K.flatten(y_true)\n    y_pred_f = K.flatten(y_pred)\n    intersection = y_true_f * y_pred_f\n    score = (2. * K.sum(intersection) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth)\n    return 1. - score\n\ndef bce_dice_loss(y_true, y_pred):\n    return binary_crossentropy(tf.cast(y_true, tf.float32), y_pred) + 0.5 * dice_loss(tf.cast(y_true, tf.float32), y_pred)\n```\n\n**Implemented the loss function in below notebook**, Refer it\nhttps://www.kaggle.com/code/muki2003/hubmap-hpa-deep-learning-u-net-cnn",
    "1843236": "how to define metric function",
    "1843273": "This is how you define a metric function:\n\n```\ndef metric_function():\n       pass\n```"
  }
}