{
  "id": 69440,
  "title": "Faster scoring?",
  "url": "/competitions/airbus-ship-detection/discussion/69440",
  "author_name": "Zineng Tang",
  "post_date": "2018-10-23T19:47:54.692000",
  "votes": 0,
  "comment_count": 0,
  "views": 0,
  "content": "<p>Below is the code for validation.</p>\n\n<p>But does anyone has faster one since I'd like to choose a different threshold other than 0.5 for prediction?</p>\n\n<p>def get_score(pred, true):\n    n_th = 10\n    b = 4\n    thresholds = [0.5 + 0.05*i for i in range(n_th)]\n    n_masks = len(true)\n    n_pred = len(pred)\n    ious = []\n    score = 0\n    for mask in true:\n        buf = []\n        for p in pred: buf.append(IoU(p,mask))\n        ious.append(buf)\n    for t in thresholds: <br>\n        tp, fp, fn = 0, 0, 0\n        for i in range(n_masks):\n            match = False\n            for j in range(n_pred):\n                if ious[i][j] &gt; t: match = True\n            if not match: fn += 1 <br>\n        for j in range(n_pred):\n            match = False\n            for i in range(n_masks):\n                if ious[i][j] &gt; t: match = True\n            if match: tp += 1\n            else: fp += 1\n        score += ((b+1)*tp)/((b+1)*tp + b*fn + fp) <br>\n    return score/n_th</p>\n\n<p>def IoU(pred, targs):\n    pred = (pred &gt; 0.5).astype(float)\n    intersection = (pred*targs).sum()\n    return intersection / ((pred+targs).sum() - intersection + 1.0)</p>",
  "messages": [
    {
      "id": 409070,
      "postDate": "2018-10-23T19:47:54.693Z",
      "content": "<p>Below is the code for validation.</p>\n\n<p>But does anyone has faster one since I'd like to choose a different threshold other than 0.5 for prediction?</p>\n\n<p>def get_score(pred, true):\n    n_th = 10\n    b = 4\n    thresholds = [0.5 + 0.05*i for i in range(n_th)]\n    n_masks = len(true)\n    n_pred = len(pred)\n    ious = []\n    score = 0\n    for mask in true:\n        buf = []\n        for p in pred: buf.append(IoU(p,mask))\n        ious.append(buf)\n    for t in thresholds: <br>\n        tp, fp, fn = 0, 0, 0\n        for i in range(n_masks):\n            match = False\n            for j in range(n_pred):\n                if ious[i][j] &gt; t: match = True\n            if not match: fn += 1 <br>\n        for j in range(n_pred):\n            match = False\n            for i in range(n_masks):\n                if ious[i][j] &gt; t: match = True\n            if match: tp += 1\n            else: fp += 1\n        score += ((b+1)*tp)/((b+1)*tp + b*fn + fp) <br>\n    return score/n_th</p>\n\n<p>def IoU(pred, targs):\n    pred = (pred &gt; 0.5).astype(float)\n    intersection = (pred*targs).sum()\n    return intersection / ((pred+targs).sum() - intersection + 1.0)</p>",
      "rawMarkdown": "Below is the code for validation.\n\nBut does anyone has faster one since I'd like to choose a different threshold other than 0.5 for prediction?\n \ndef get_score(pred, true):\n    n_th = 10\n    b = 4\n    thresholds = [0.5 + 0.05*i for i in range(n_th)]\n    n_masks = len(true)\n    n_pred = len(pred)\n    ious = []\n    score = 0\n    for mask in true:\n        buf = []\n        for p in pred: buf.append(IoU(p,mask))\n        ious.append(buf)\n    for t in thresholds:   \n        tp, fp, fn = 0, 0, 0\n        for i in range(n_masks):\n            match = False\n            for j in range(n_pred):\n                if ious[i][j] &gt; t: match = True\n            if not match: fn += 1  \n        for j in range(n_pred):\n            match = False\n            for i in range(n_masks):\n                if ious[i][j] &gt; t: match = True\n            if match: tp += 1\n            else: fp += 1\n        score += ((b+1)*tp)/((b+1)*tp + b*fn + fp)       \n    return score/n_th\n\n\ndef IoU(pred, targs):\n    pred = (pred &gt; 0.5).astype(float)\n    intersection = (pred*targs).sum()\n    return intersection / ((pred+targs).sum() - intersection + 1.0)"
    }
  ],
  "comments": [],
  "raw_markdown_by_id": {
    "409070": "Below is the code for validation.\n\nBut does anyone has faster one since I'd like to choose a different threshold other than 0.5 for prediction?\n \ndef get_score(pred, true):\n    n_th = 10\n    b = 4\n    thresholds = [0.5 + 0.05*i for i in range(n_th)]\n    n_masks = len(true)\n    n_pred = len(pred)\n    ious = []\n    score = 0\n    for mask in true:\n        buf = []\n        for p in pred: buf.append(IoU(p,mask))\n        ious.append(buf)\n    for t in thresholds:   \n        tp, fp, fn = 0, 0, 0\n        for i in range(n_masks):\n            match = False\n            for j in range(n_pred):\n                if ious[i][j] &gt; t: match = True\n            if not match: fn += 1  \n        for j in range(n_pred):\n            match = False\n            for i in range(n_masks):\n                if ious[i][j] &gt; t: match = True\n            if match: tp += 1\n            else: fp += 1\n        score += ((b+1)*tp)/((b+1)*tp + b*fn + fp)       \n    return score/n_th\n\n\ndef IoU(pred, targs):\n    pred = (pred &gt; 0.5).astype(float)\n    intersection = (pred*targs).sum()\n    return intersection / ((pred+targs).sum() - intersection + 1.0)"
  }
}