{
  "id": 421363,
  "title": "fast computation of 2 set of instance mask IOU",
  "url": "/competitions/hubmap-hacking-the-human-vasculature/discussion/421363",
  "author_name": "",
  "post_date": "2023-07-05T05:11:34.654625700Z",
  "votes": 5,
  "comment_count": 2,
  "views": 0,
  "content": "<p>i need to compute IOU for 2 set of instance mask.<br>\nfor example, in computation of mAP metric or dynmic matching (bipartite matching) in DETR like instance mask segmentation, or in ensemble</p>\n<pre><code>assume theare non overlapping, then you can assign albel=1,2,3 ... to mask object 1,2,3 ...\n\n= hxwof labels 0,12,3...\n\nm1 = instance1.flatten() =[1,1,1, 0,0,0, ...1,1, ....2,2 ...] \nm2 = instance2.flatten() =[1,1,0,0,0,0, ...3,3, ....2,2 ...]  \n\nmap:\n(m1,m2)--&gt; unique values ...  \ne.g.\n(0,0)=0\n(1,0)=1 \n(2,0)=2\n..\n(a,b)=ab\n\nuse\ntorch.bincoun( unique values) to count co-occurences\n\nthen insection of object-a in image1object-b in mage2 is count of ab\n\nthis is the cheap way to do np.histogram2d\n(note: torch has histogramdd but that does't support cuda)\n</code></pre>",
  "messages": [
    {
      "id": "2330588",
      "postDate": "07/05/2023 05:11:34",
      "content": "<p>i need to compute IOU for 2 set of instance mask.<br>\nfor example, in computation of mAP metric or dynmic matching (bipartite matching) in DETR like instance mask segmentation, or in ensemble</p>\n<pre><code>assume theare non overlapping, then you can assign albel=1,2,3 ... to mask object 1,2,3 ...\n\n= hxwof labels 0,12,3...\n\nm1 = instance1.flatten() =[1,1,1, 0,0,0, ...1,1, ....2,2 ...] \nm2 = instance2.flatten() =[1,1,0,0,0,0, ...3,3, ....2,2 ...]  \n\nmap:\n(m1,m2)--&gt; unique values ...  \ne.g.\n(0,0)=0\n(1,0)=1 \n(2,0)=2\n..\n(a,b)=ab\n\nuse\ntorch.bincoun( unique values) to count co-occurences\n\nthen insection of object-a in image1object-b in mage2 is count of ab\n\nthis is the cheap way to do np.histogram2d\n(note: torch has histogramdd but that does't support cuda)\n</code></pre>",
      "rawMarkdown": "i need to compute IOU for 2 set of instance mask.\nfor example, in computation of mAP metric or dynmic matching (bipartite matching) in DETR like instance mask segmentation, or in ensemble\n\n```\nassume the instance are non overlapping, then you can assign albel=1,2,3 ... to mask object 1,2,3 ...\n\ninstance \n= hxw array of labels 0,12,3...\n\nm1 = instance1.flatten() =[1,1,1, 0,0,0, ...1,1, ....2,2 ...] #0 is bakground\nm2 = instance2.flatten() =[1,1,0,0,0,0, ...3,3, ....2,2 ...]  \n\nmap:\n(m1,m2)--> unique values ...  \ne.g.\n(0,0)=0\n(1,0)=1 \n(2,0)=2\n...\n(a,b)=ab\n\nuse\ntorch.bincoun( unique values) to count co-occurences\n\nthen insection of object-a in image1 and object-b in mage2 is count of ab\n\nthis is the cheap way to do np.histogram2d\n(note: torch has histogramdd but that does't support cuda)\n\n```",
      "votes": null
    },
    {
      "id": "2330911",
      "postDate": "07/05/2023 08:32:52",
      "content": "<pre><code>def (instance1, instance2):\nm1 = instance1.(-).()\nm2 = instance2.(-).()\nnum1 = instance1.()+\nnum2 = instance2.()+\nintersect = m1*num2 + m2\ncount = torch.(intersect,minlength=num1*num2)\ncount12 = count.(num1,num2)\ncount1 = torch.(m1)\ncount2 = torch.(m2)\nreturn count12, count1, count2\n\n\nhist, p_h, t_h = (p_instance, t_instance)\niou = hist.()/t_h.().()\n</code></pre>\n<p>eaxmple</p>\n<pre><code> :\ninstance1 = torch.LongTensor([\n    ,,,,,,,,,,,,,,,,,,,,,,,\n]).cuda()\ninstance2 = torch.LongTensor([\n   \n    ,,,,,,,,,,,,,,,,,,,,,,,\n]).cuda()\nhistogram2d(instance1, instance2)\n\n()\n</code></pre>\n<pre><code>related : useful gpu connected components labeling\n##  https:\n##  cc_torch  connected_components_labeling\n\ndist = sementic_sgementation_of_object_center =unet(image)\nd = (dist&gt;).byte() #torch.uint8\ncc = connected_components_labeling(d) #label is assigned random number\np_instance_count, p_instance = torch.unique(cc,return_inverse = ) #relabel to ,,,, ....\n</code></pre>",
      "rawMarkdown": "```\n\ndef histogram2d(instance1, instance2):\n\tm1 = instance1.reshape(-1).long()\n\tm2 = instance2.reshape(-1).long()\n\tnum1 = instance1.max()+1\n\tnum2 = instance2.max()+1\n\tintersect = m1*num2 + m2\n\tcount = torch.bincount(intersect,minlength=num1*num2)\n\tcount12 = count.reshape(num1,num2)\n\tcount1 = torch.bincount(m1)\n\tcount2 = torch.bincount(m2)\n\treturn count12, count1, count2\n\t\n\t\nhist, p_h, t_h = histogram2d(p_instance, t_instance)\niou = hist.float()/t_h.float().unsqueeze(0)\n\n\n```\n\neaxmple\n```\nif 0:\n\tinstance1 = torch.LongTensor([\n\t\t0,0,1,1,1,1,0,0,2,2,2,2,0,0,0,0,3,3,3,0,0,0,0,0\n\t]).cuda()\n\tinstance2 = torch.LongTensor([\n\t   #0,0,1,1,1,1,0,0,2,2,2,2,0,0,0,0,3,3,3,0,0,0,0,0\n\t\t0,0,1,1,1,0,0,0,2,2,2,0,0,0,0,0,3,3,4,4,4,5,0,0\n\t]).cuda()\n\thistogram2d(instance1, instance2)\n\t'''\n\ttensor([[10,  0,  0,  0,  2,  1],\n\t        [ 1,  3,  0,  0,  0,  0],\n\t        [ 1,  0,  3,  0,  0,  0],\n\t        [ 0,  0,  0,  2,  1,  0]], device='cuda:0')\n\t'''\n\texit(0)\n```\n\n```\nrelated : useful gpu connected components labeling\n##  https://github.com/zsef123/Connected_components_PyTorch/blob/main/example.ipynb\n## from cc_torch import connected_components_labeling\n\t\ndist = sementic_sgementation_of_object_center =unet(image)\nd = (dist>3).byte() #torch.uint8\ncc = connected_components_labeling(d) #label is assigned random number\np_instance_count, p_instance = torch.unique(cc,return_inverse = True) #relabel to 1,2,3,4, ....\n\t\n```",
      "votes": null
    },
    {
      "id": "2331137",
      "postDate": "07/05/2023 11:41:51",
      "content": "<p>Do all the masks in your case intersect with each other?<br>\nIf not, it might be better to calculate iou based on bounding boxes and then refine mask_iou only for intersecting masks.</p>",
      "rawMarkdown": "Do all the masks in your case intersect with each other?\nIf not, it might be better to calculate iou based on bounding boxes and then refine mask_iou only for intersecting masks.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2330911,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "07/05/2023 08:32:52",
      "content": "<pre><code>def (instance1, instance2):\nm1 = instance1.(-).()\nm2 = instance2.(-).()\nnum1 = instance1.()+\nnum2 = instance2.()+\nintersect = m1*num2 + m2\ncount = torch.(intersect,minlength=num1*num2)\ncount12 = count.(num1,num2)\ncount1 = torch.(m1)\ncount2 = torch.(m2)\nreturn count12, count1, count2\n\n\nhist, p_h, t_h = (p_instance, t_instance)\niou = hist.()/t_h.().()\n</code></pre>\n<p>eaxmple</p>\n<pre><code> :\ninstance1 = torch.LongTensor([\n    ,,,,,,,,,,,,,,,,,,,,,,,\n]).cuda()\ninstance2 = torch.LongTensor([\n   \n    ,,,,,,,,,,,,,,,,,,,,,,,\n]).cuda()\nhistogram2d(instance1, instance2)\n\n()\n</code></pre>\n<pre><code>related : useful gpu connected components labeling\n##  https:\n##  cc_torch  connected_components_labeling\n\ndist = sementic_sgementation_of_object_center =unet(image)\nd = (dist&gt;).byte() #torch.uint8\ncc = connected_components_labeling(d) #label is assigned random number\np_instance_count, p_instance = torch.unique(cc,return_inverse = ) #relabel to ,,,, ....\n</code></pre>",
      "votes": null,
      "replies": []
    },
    {
      "id": 2331137,
      "author_name": "tsobolev",
      "author_url": "",
      "post_date": "07/05/2023 11:41:51",
      "content": "<p>Do all the masks in your case intersect with each other?<br>\nIf not, it might be better to calculate iou based on bounding boxes and then refine mask_iou only for intersecting masks.</p>",
      "votes": null,
      "replies": []
    }
  ],
  "raw_markdown_by_id": {
    "2330588": "i need to compute IOU for 2 set of instance mask.\nfor example, in computation of mAP metric or dynmic matching (bipartite matching) in DETR like instance mask segmentation, or in ensemble\n\n```\nassume the instance are non overlapping, then you can assign albel=1,2,3 ... to mask object 1,2,3 ...\n\ninstance \n= hxw array of labels 0,12,3...\n\nm1 = instance1.flatten() =[1,1,1, 0,0,0, ...1,1, ....2,2 ...] #0 is bakground\nm2 = instance2.flatten() =[1,1,0,0,0,0, ...3,3, ....2,2 ...]  \n\nmap:\n(m1,m2)--> unique values ...  \ne.g.\n(0,0)=0\n(1,0)=1 \n(2,0)=2\n...\n(a,b)=ab\n\nuse\ntorch.bincoun( unique values) to count co-occurences\n\nthen insection of object-a in image1 and object-b in mage2 is count of ab\n\nthis is the cheap way to do np.histogram2d\n(note: torch has histogramdd but that does't support cuda)\n\n```",
    "2330911": "```\n\ndef histogram2d(instance1, instance2):\n\tm1 = instance1.reshape(-1).long()\n\tm2 = instance2.reshape(-1).long()\n\tnum1 = instance1.max()+1\n\tnum2 = instance2.max()+1\n\tintersect = m1*num2 + m2\n\tcount = torch.bincount(intersect,minlength=num1*num2)\n\tcount12 = count.reshape(num1,num2)\n\tcount1 = torch.bincount(m1)\n\tcount2 = torch.bincount(m2)\n\treturn count12, count1, count2\n\t\n\t\nhist, p_h, t_h = histogram2d(p_instance, t_instance)\niou = hist.float()/t_h.float().unsqueeze(0)\n\n\n```\n\neaxmple\n```\nif 0:\n\tinstance1 = torch.LongTensor([\n\t\t0,0,1,1,1,1,0,0,2,2,2,2,0,0,0,0,3,3,3,0,0,0,0,0\n\t]).cuda()\n\tinstance2 = torch.LongTensor([\n\t   #0,0,1,1,1,1,0,0,2,2,2,2,0,0,0,0,3,3,3,0,0,0,0,0\n\t\t0,0,1,1,1,0,0,0,2,2,2,0,0,0,0,0,3,3,4,4,4,5,0,0\n\t]).cuda()\n\thistogram2d(instance1, instance2)\n\t'''\n\ttensor([[10,  0,  0,  0,  2,  1],\n\t        [ 1,  3,  0,  0,  0,  0],\n\t        [ 1,  0,  3,  0,  0,  0],\n\t        [ 0,  0,  0,  2,  1,  0]], device='cuda:0')\n\t'''\n\texit(0)\n```\n\n```\nrelated : useful gpu connected components labeling\n##  https://github.com/zsef123/Connected_components_PyTorch/blob/main/example.ipynb\n## from cc_torch import connected_components_labeling\n\t\ndist = sementic_sgementation_of_object_center =unet(image)\nd = (dist>3).byte() #torch.uint8\ncc = connected_components_labeling(d) #label is assigned random number\np_instance_count, p_instance = torch.unique(cc,return_inverse = True) #relabel to 1,2,3,4, ....\n\t\n```",
    "2331137": "Do all the masks in your case intersect with each other?\nIf not, it might be better to calculate iou based on bounding boxes and then refine mask_iou only for intersecting masks."
  },
  "source": "meta"
}