{
  "id": 547528,
  "title": "How to convert labels with particle centroids to tensor of probabilities?",
  "url": "/competitions/czii-cryo-et-object-identification/discussion/547528",
  "author_name": "",
  "post_date": "2024-11-22T06:39:15.893531900Z",
  "votes": 1,
  "comment_count": 3,
  "views": 0,
  "content": "<p>The code below (<a href=\"https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder\" target=\"_blank\">taken from this notebook</a>) converts tensor with probabilities of particle existence to centroid coordinates:</p>\n<pre><code> cc3d\n\n ():\n    _,D,H,W = probability.shape\n\n    location={}\n     p  PARTICLE:\n        p = dotdict(p)\n        l = p.label\n\n        cc, P = cc3d.connected_components(probability[l]&gt;cfg.threshold[p.name], return_N=)\n        stats = cc3d.statistics(cc)\n        zyx=stats[][:]*\n        xyz = np.ascontiguousarray(zyx[:,::-]) \n        location[p.name]=xyz\n        \n     location\n</code></pre>\n<p>How can I convert train label with particle centroids and radius to tensor of probabilities?</p>\n<p>UPDATE:</p>\n<p>I want to try this code:</p>\n<pre><code> copick_utils.segmentation  segmentation_from_picks\ndata_dicts = []\n run  tqdm(root.runs):\n    tomogram = run.get_voxel_spacing(voxel_size).get_tomogram(tomo_type).numpy()\n    segmentation = run.get_segmentations(name=copick_segmentation_name, user_id=copick_user_name, voxel_size=voxel_size, is_multilabel=)[].numpy()\n    data_dicts.append({: tomogram, : segmentation})\n</code></pre>",
  "messages": [
    {
      "id": "3052187",
      "postDate": "11/22/2024 06:39:15",
      "content": "<p>The code below (<a href=\"https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder\" target=\"_blank\">taken from this notebook</a>) converts tensor with probabilities of particle existence to centroid coordinates:</p>\n<pre><code> cc3d\n\n ():\n    _,D,H,W = probability.shape\n\n    location={}\n     p  PARTICLE:\n        p = dotdict(p)\n        l = p.label\n\n        cc, P = cc3d.connected_components(probability[l]&gt;cfg.threshold[p.name], return_N=)\n        stats = cc3d.statistics(cc)\n        zyx=stats[][:]*\n        xyz = np.ascontiguousarray(zyx[:,::-]) \n        location[p.name]=xyz\n        \n     location\n</code></pre>\n<p>How can I convert train label with particle centroids and radius to tensor of probabilities?</p>\n<p>UPDATE:</p>\n<p>I want to try this code:</p>\n<pre><code> copick_utils.segmentation  segmentation_from_picks\ndata_dicts = []\n run  tqdm(root.runs):\n    tomogram = run.get_voxel_spacing(voxel_size).get_tomogram(tomo_type).numpy()\n    segmentation = run.get_segmentations(name=copick_segmentation_name, user_id=copick_user_name, voxel_size=voxel_size, is_multilabel=)[].numpy()\n    data_dicts.append({: tomogram, : segmentation})\n</code></pre>",
      "rawMarkdown": "The code below ([taken from this notebook](https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder)) converts tensor with probabilities of particle existence to centroid coordinates:\n\n```python\nimport cc3d\n\ndef probability_to_location(probability,cfg):\n    _,D,H,W = probability.shape\n\n    location={}\n    for p in PARTICLE:\n        p = dotdict(p)\n        l = p.label\n\n        cc, P = cc3d.connected_components(probability[l]>cfg.threshold[p.name], return_N=True)\n        stats = cc3d.statistics(cc)\n        zyx=stats['centroids'][1:]*10\n        xyz = np.ascontiguousarray(zyx[:,::-1]) \n        location[p.name]=xyz\n        '''\n            j=1\n            z,y,x = np.where(cc==j)\n            z=z.mean()\n            y=y.mean()\n            x=x.mean()\n            print([x,y,z])\n        '''\n    return location\n```\n\nHow can I convert train label with particle centroids and radius to tensor of probabilities?\n\nUPDATE:\n\nI want to try this code:\n\n```python\nfrom copick_utils.segmentation import segmentation_from_picks\ndata_dicts = []\nfor run in tqdm(root.runs):\n    tomogram = run.get_voxel_spacing(voxel_size).get_tomogram(tomo_type).numpy()\n    segmentation = run.get_segmentations(name=copick_segmentation_name, user_id=copick_user_name, voxel_size=voxel_size, is_multilabel=True)[0].numpy()\n    data_dicts.append({\"image\": tomogram, \"label\": segmentation})\n```",
      "votes": null
    },
    {
      "id": "3052304",
      "postDate": "11/22/2024 09:07:37",
      "content": "<p>you want to give a confidence score to your predicted coordinates?</p>\n<p>eg. from (x,y,z,label) to (x,y,z,label,score)?</p>",
      "rawMarkdown": "you want to give a confidence score to your predicted coordinates?\n\neg. from (x,y,z,label) to (x,y,z,label,score)?",
      "votes": null
    },
    {
      "id": "3052313",
      "postDate": "11/22/2024 09:17:02",
      "content": "<p>Greetings, Hengck23!<br>\nI am trying to make train in your public notebook (it is great!).<br>\nYour model predicts (outputs) a tensor of probabilities with dimension [7,184,630,630] (for hi res images).<br>\nOur train labels are [label,x,y,z]. I want to convert them to [7,184,630,630] tensor to be able to use some loss function.</p>",
      "rawMarkdown": "Greetings, Hengck23!\nI am trying to make train in your public notebook (it is great!).\nYour model predicts (outputs) a tensor of probabilities with dimension [7,184,630,630] (for hi res images).\nOur train labels are [label,x,y,z]. I want to convert them to [7,184,630,630] tensor to be able to use some loss function.",
      "votes": null
    },
    {
      "id": "3052343",
      "postDate": "11/22/2024 10:01:35",
      "content": "<p>So you basically want the one-hot-encoded version of the labels? </p>\n<p>If your current labels are in of shape <code>184x630x630</code>, then they should have labels as integers, i.e. <code>0, 1, 2, 3, 4, 5, 6</code>. </p>\n<p>To get them to be of the shape <code>7x184x630x630</code>, you need to one-hot-encode them. </p>\n<p>I.e. label <code>1</code> becomes <code>[0, 1, 0, 0, 0, 0, 0, 0]</code> and label <code>7</code> becomes <code>[0, 0, 0, 0, 0, 0, 0, 1]</code></p>\n<p>If you look at the losses from Monai, e.g. <code>monai.losses.DiceLoss(include_background=True, to_onehot_y=False)</code>, you can see that it will do it for you in this case. </p>\n<p>If you want to do it manually (assuming you are using pytorch) <code>torch.nn.functional.one_hot</code> is your best bet. </p>",
      "rawMarkdown": "So you basically want the one-hot-encoded version of the labels? \n\nIf your current labels are in of shape `184x630x630`, then they should have labels as integers, i.e. `0, 1, 2, 3, 4, 5, 6`. \n\nTo get them to be of the shape `7x184x630x630`, you need to one-hot-encode them. \n\nI.e. label `1` becomes `[0, 1, 0, 0, 0, 0, 0, 0]` and label `7` becomes `[0, 0, 0, 0, 0, 0, 0, 1]`\n\nIf you look at the losses from Monai, e.g. `monai.losses.DiceLoss(include_background=True, to_onehot_y=False)`, you can see that it will do it for you in this case. \n\nIf you want to do it manually (assuming you are using pytorch) `torch.nn.functional.one_hot` is your best bet.",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 3052304,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "11/22/2024 09:07:37",
      "content": "<p>you want to give a confidence score to your predicted coordinates?</p>\n<p>eg. from (x,y,z,label) to (x,y,z,label,score)?</p>",
      "votes": null,
      "replies": [
        {
          "id": 3052313,
          "author_name": "kaggledummie007",
          "author_url": "",
          "post_date": "11/22/2024 09:17:02",
          "content": "<p>Greetings, Hengck23!<br>\nI am trying to make train in your public notebook (it is great!).<br>\nYour model predicts (outputs) a tensor of probabilities with dimension [7,184,630,630] (for hi res images).<br>\nOur train labels are [label,x,y,z]. I want to convert them to [7,184,630,630] tensor to be able to use some loss function.</p>",
          "votes": null,
          "replies": [
            {
              "id": 3052343,
              "author_name": "fnands",
              "author_url": "",
              "post_date": "11/22/2024 10:01:35",
              "content": "<p>So you basically want the one-hot-encoded version of the labels? </p>\n<p>If your current labels are in of shape <code>184x630x630</code>, then they should have labels as integers, i.e. <code>0, 1, 2, 3, 4, 5, 6</code>. </p>\n<p>To get them to be of the shape <code>7x184x630x630</code>, you need to one-hot-encode them. </p>\n<p>I.e. label <code>1</code> becomes <code>[0, 1, 0, 0, 0, 0, 0, 0]</code> and label <code>7</code> becomes <code>[0, 0, 0, 0, 0, 0, 0, 1]</code></p>\n<p>If you look at the losses from Monai, e.g. <code>monai.losses.DiceLoss(include_background=True, to_onehot_y=False)</code>, you can see that it will do it for you in this case. </p>\n<p>If you want to do it manually (assuming you are using pytorch) <code>torch.nn.functional.one_hot</code> is your best bet. </p>",
              "votes": null,
              "replies": []
            }
          ]
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "3052187": "The code below ([taken from this notebook](https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder)) converts tensor with probabilities of particle existence to centroid coordinates:\n\n```python\nimport cc3d\n\ndef probability_to_location(probability,cfg):\n    _,D,H,W = probability.shape\n\n    location={}\n    for p in PARTICLE:\n        p = dotdict(p)\n        l = p.label\n\n        cc, P = cc3d.connected_components(probability[l]>cfg.threshold[p.name], return_N=True)\n        stats = cc3d.statistics(cc)\n        zyx=stats['centroids'][1:]*10\n        xyz = np.ascontiguousarray(zyx[:,::-1]) \n        location[p.name]=xyz\n        '''\n            j=1\n            z,y,x = np.where(cc==j)\n            z=z.mean()\n            y=y.mean()\n            x=x.mean()\n            print([x,y,z])\n        '''\n    return location\n```\n\nHow can I convert train label with particle centroids and radius to tensor of probabilities?\n\nUPDATE:\n\nI want to try this code:\n\n```python\nfrom copick_utils.segmentation import segmentation_from_picks\ndata_dicts = []\nfor run in tqdm(root.runs):\n    tomogram = run.get_voxel_spacing(voxel_size).get_tomogram(tomo_type).numpy()\n    segmentation = run.get_segmentations(name=copick_segmentation_name, user_id=copick_user_name, voxel_size=voxel_size, is_multilabel=True)[0].numpy()\n    data_dicts.append({\"image\": tomogram, \"label\": segmentation})\n```",
    "3052304": "you want to give a confidence score to your predicted coordinates?\n\neg. from (x,y,z,label) to (x,y,z,label,score)?",
    "3052313": "Greetings, Hengck23!\nI am trying to make train in your public notebook (it is great!).\nYour model predicts (outputs) a tensor of probabilities with dimension [7,184,630,630] (for hi res images).\nOur train labels are [label,x,y,z]. I want to convert them to [7,184,630,630] tensor to be able to use some loss function.",
    "3052343": "So you basically want the one-hot-encoded version of the labels? \n\nIf your current labels are in of shape `184x630x630`, then they should have labels as integers, i.e. `0, 1, 2, 3, 4, 5, 6`. \n\nTo get them to be of the shape `7x184x630x630`, you need to one-hot-encode them. \n\nI.e. label `1` becomes `[0, 1, 0, 0, 0, 0, 0, 0]` and label `7` becomes `[0, 0, 0, 0, 0, 0, 0, 1]`\n\nIf you look at the losses from Monai, e.g. `monai.losses.DiceLoss(include_background=True, to_onehot_y=False)`, you can see that it will do it for you in this case. \n\nIf you want to do it manually (assuming you are using pytorch) `torch.nn.functional.one_hot` is your best bet."
  },
  "source": "meta"
}