{
  "id": 128710,
  "title": "[keras] focal implementation",
  "url": "/competitions/bengaliai-cv19/discussion/128710",
  "author_name": "Larry Schuster",
  "post_date": "2020-02-02T18:13:32.309000",
  "votes": 2,
  "comment_count": 0,
  "views": 0,
  "content": "<p>This works exceptionally well on my kernel:</p>\n\n<h2>Focal Loss</h2>\n\n<pre><code>This is the keras implementation of focal loss proposed by Lin et. al. in their Focal Loss for Dense Object Detection paper.\n</code></pre>\n\n<h2>Usage</h2>\n\n<pre><code> model.compile(optimizer=optimizer, loss=[focal_loss(alpha=.25, gamma=2)])\n</code></pre>\n\n<p>```</p>\n\n<p>def focal_loss(gamma=2., alpha=.25):\n    def focal_loss_fixed(y_true, y_pred):\n        pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))\n        pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))\n        return -K.mean(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) - K.mean((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))\n    return focal_loss_fixed\n```\nReference: <a href=\"https://github.com/mkocabas/focal-loss-keras\">focal-loss-keras</a></p>",
  "messages": [
    {
      "id": 735200,
      "postDate": "2020-02-02T18:13:32.310Z",
      "content": "<p>This works exceptionally well on my kernel:</p>\n\n<h2>Focal Loss</h2>\n\n<pre><code>This is the keras implementation of focal loss proposed by Lin et. al. in their Focal Loss for Dense Object Detection paper.\n</code></pre>\n\n<h2>Usage</h2>\n\n<pre><code> model.compile(optimizer=optimizer, loss=[focal_loss(alpha=.25, gamma=2)])\n</code></pre>\n\n<p>```</p>\n\n<p>def focal_loss(gamma=2., alpha=.25):\n    def focal_loss_fixed(y_true, y_pred):\n        pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))\n        pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))\n        return -K.mean(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) - K.mean((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))\n    return focal_loss_fixed\n```\nReference: <a href=\"https://github.com/mkocabas/focal-loss-keras\">focal-loss-keras</a></p>",
      "rawMarkdown": "This works exceptionally well on my kernel:\n\n##Focal Loss\n    This is the keras implementation of focal loss proposed by Lin et. al. in their Focal Loss for Dense Object Detection paper.\n\n##Usage\n     model.compile(optimizer=optimizer, loss=[focal_loss(alpha=.25, gamma=2)])\n```\n\ndef focal_loss(gamma=2., alpha=.25):\n\tdef focal_loss_fixed(y_true, y_pred):\n\t\tpt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))\n\t\tpt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))\n\t\treturn -K.mean(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) - K.mean((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))\n\treturn focal_loss_fixed\n```\nReference: [focal-loss-keras](https://github.com/mkocabas/focal-loss-keras)",
      "votes": 2
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "735200": "This works exceptionally well on my kernel:\n\n##Focal Loss\n    This is the keras implementation of focal loss proposed by Lin et. al. in their Focal Loss for Dense Object Detection paper.\n\n##Usage\n     model.compile(optimizer=optimizer, loss=[focal_loss(alpha=.25, gamma=2)])\n```\n\ndef focal_loss(gamma=2., alpha=.25):\n\tdef focal_loss_fixed(y_true, y_pred):\n\t\tpt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred))\n\t\tpt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred))\n\t\treturn -K.mean(alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) - K.mean((1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0))\n\treturn focal_loss_fixed\n```\nReference: [focal-loss-keras](https://github.com/mkocabas/focal-loss-keras)"
  }
}