{
  "id": 547890,
  "title": "How to retrieve the targets correctly?",
  "url": "/competitions/czii-cryo-et-object-identification/discussion/547890",
  "author_name": "Sinan Calisir",
  "post_date": "2024-11-24T00:51:48.444000",
  "votes": 8,
  "comment_count": 16,
  "views": 0,
  "content": "<p>Hi,</p>\n<p>I am a newbie to this topic and struggling to create the image-target pairs from the files correctly. I investigated the <a href=\"https://github.com/czimaginginstitute/2024_czii_mlchallenge_notebooks/tree/main\" target=\"_blank\">notebooks</a> from the host as well but they confused me even more because of the tools involved. I also checked the popular notebooks <a href=\"https://www.kaggle.com/code/fnands/baseline-unet-train-submit\" target=\"_blank\">here</a> and <a href=\"https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder\" target=\"_blank\">here</a> but couldn't figure them from there as well. </p>\n<p>I read the image data like this:</p>\n<pre><code>data_dirs = (glob())\n data_dir  data_dirs:\n    f = zarr.(data_dir)\n    (data_dir)\n    (f.tree())\n    image = np.array(f.get())\n    (image.shape)\n</code></pre>\n<p>and read the target like this following the code from <a href=\"https://www.kaggle.com/hengck23\" target=\"_blank\">@hengck23</a> </p>\n<pre><code>TARGETS = [\n    ,\n    ,\n    ,\n    ,\n    ,\n    \n]\n\n ():\n    location = {}\n    json_dir = \n     target  TARGETS:\n        json_file = \n         (json_file, )  f:\n            annotations = json.load(f)\n        points = annotations[]\n        num_points = (points)\n        loc = np.array([(points[i][].values())  i  (num_points)])\n        location[target] = loc\n     location\n</code></pre>\n<p>The targets are here:</p>\n<pre><code>'apo-ferritin' array(      \n          \n            \n            \n              \n             \n             \n            \n...\n</code></pre>\n<p>I am mostly a Pytorch person and I was planning to do something like this:</p>\n<pre><code> ():\n     ():\n        \n     ():\n        \n     ():\n        data_dir = .data_dirs[idx]\n        f = zarr.(data_dir)\n        image = np.array(f.get())\n        targets = .target_data[experiment_id]\n        target_coordinates = targets[] \n         .transform:\n            image = .transform(image)\n         image, target_coordinates       \n</code></pre>\n<p>But I couldn't connect the bridge between the two. I am not looking for a spoon feeding but only a direction or notebook that would be helpful to understand and proceed further.</p>\n<p>Thanks</p>",
  "messages": [
    {
      "id": 3053823,
      "postDate": "2024-11-24T00:51:48.443Z",
      "content": "<p>Hi,</p>\n<p>I am a newbie to this topic and struggling to create the image-target pairs from the files correctly. I investigated the <a href=\"https://github.com/czimaginginstitute/2024_czii_mlchallenge_notebooks/tree/main\" target=\"_blank\">notebooks</a> from the host as well but they confused me even more because of the tools involved. I also checked the popular notebooks <a href=\"https://www.kaggle.com/code/fnands/baseline-unet-train-submit\" target=\"_blank\">here</a> and <a href=\"https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder\" target=\"_blank\">here</a> but couldn't figure them from there as well. </p>\n<p>I read the image data like this:</p>\n<pre><code>data_dirs = (glob())\n data_dir  data_dirs:\n    f = zarr.(data_dir)\n    (data_dir)\n    (f.tree())\n    image = np.array(f.get())\n    (image.shape)\n</code></pre>\n<p>and read the target like this following the code from <a href=\"https://www.kaggle.com/hengck23\" target=\"_blank\">@hengck23</a> </p>\n<pre><code>TARGETS = [\n    ,\n    ,\n    ,\n    ,\n    ,\n    \n]\n\n ():\n    location = {}\n    json_dir = \n     target  TARGETS:\n        json_file = \n         (json_file, )  f:\n            annotations = json.load(f)\n        points = annotations[]\n        num_points = (points)\n        loc = np.array([(points[i][].values())  i  (num_points)])\n        location[target] = loc\n     location\n</code></pre>\n<p>The targets are here:</p>\n<pre><code>'apo-ferritin' array(      \n          \n            \n            \n              \n             \n             \n            \n...\n</code></pre>\n<p>I am mostly a Pytorch person and I was planning to do something like this:</p>\n<pre><code> ():\n     ():\n        \n     ():\n        \n     ():\n        data_dir = .data_dirs[idx]\n        f = zarr.(data_dir)\n        image = np.array(f.get())\n        targets = .target_data[experiment_id]\n        target_coordinates = targets[] \n         .transform:\n            image = .transform(image)\n         image, target_coordinates       \n</code></pre>\n<p>But I couldn't connect the bridge between the two. I am not looking for a spoon feeding but only a direction or notebook that would be helpful to understand and proceed further.</p>\n<p>Thanks</p>",
      "rawMarkdown": "Hi,\n\nI am a newbie to this topic and struggling to create the image-target pairs from the files correctly. I investigated the [notebooks](https://github.com/czimaginginstitute/2024_czii_mlchallenge_notebooks/tree/main) from the host as well but they confused me even more because of the tools involved. I also checked the popular notebooks [here](https://www.kaggle.com/code/fnands/baseline-unet-train-submit) and [here](https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder) but couldn't figure them from there as well. \n\nI read the image data like this:\n```python\ndata_dirs = sorted(glob(\"/kaggle/input/czii-cryo-et-object-identification/train/static/ExperimentRuns/*/*/denoised*\"))\nfor data_dir in data_dirs:\n    f = zarr.open(data_dir)\n    print(data_dir)\n    print(f.tree())\n    image = np.array(f.get(0))\n    print(image.shape)\n```\n\nand read the target like this following the code from @hengck23 \n\n```python\nTARGETS = [\n    \"apo-ferritin\",\n    \"beta-amylase\",\n    \"beta-galactosidase\",\n    \"ribosome\",\n    \"thyroglobulin\",\n    \"virus-like-particle\"\n]\n\ndef read_single_target(_id, overlay_dir):\n    location = {}\n    json_dir = f\"{overlay_dir}/{_id}/Picks\"\n    for target in TARGETS:\n        json_file = f\"{json_dir}/{target}.json\"\n        with open(json_file, \"r\") as f:\n            annotations = json.load(f)\n        points = annotations[\"points\"]\n        num_points = len(points)\n        loc = np.array([list(points[i][\"location\"].values()) for i in range(num_points)])\n        location[target] = loc\n    return location\n```\n\nThe targets are here:\n\n```json\n{'apo-ferritin': array([[6072.464, 4038.   ,  715.679],\n        [5967.452, 4228.213, 1124.601],\n        [5847.622, 5066.71 ,  678.893],\n        [ 472.853, 5632.618,  580.676],\n        [ 564.507, 5604.88 ,  678.16 ],\n        [ 890.414, 5615.759,  467.69 ],\n        [5613.561,  672.73 ,  265.579],\n        [ 458.087, 1972.013,  765.906],\n...\n```\n\nI am mostly a Pytorch person and I was planning to do something like this:\n\n```python\nclass CryoETDataset(Dataset):\n    def __init__(self, data_dirs, target_data, transform=None):\n        pass\n    def __len__(self):\n        pass\n    def __getitem__(self, idx):\n        data_dir = self.data_dirs[idx]\n        f = zarr.open(data_dir)\n        image = np.array(f.get(0))\n        targets = self.target_data[experiment_id]\n        target_coordinates = targets[\"apo-ferritin\"] # single target for now\n        if self.transform:\n            image = self.transform(image)\n        return image, target_coordinates       \n```\n\nBut I couldn't connect the bridge between the two. I am not looking for a spoon feeding but only a direction or notebook that would be helpful to understand and proceed further.\n\nThanks",
      "votes": 8
    },
    {
      "id": 3053843,
      "postDate": "2024-11-24T02:09:56.947Z",
      "content": "<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fd828b08e3ab1dc323458767902964344%2FSelection_732.png?generation=1732414133881392&amp;alt=media\" alt=\"\"></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fe89b286cb21ea08f3bf93e45e320a742%2FSelection_733.png?generation=1732414185676234&amp;alt=media\" alt=\"\"></p>\n<p>try to explore yourself first, if you still have doubts, post it here later.<br>\nyou should google around for\" how to use deep network for prediction of point coordinate\". i think you can easily find paper and code or blog. then ask chatgpt to read it and ask chatgpt questions.<br>\nyou can then feed in chatgpt my code or your question above to link between the papers/codes you found with my code or your questions.</p>",
      "rawMarkdown": "![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fd828b08e3ab1dc323458767902964344%2FSelection_732.png?generation=1732414133881392&alt=media)\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fe89b286cb21ea08f3bf93e45e320a742%2FSelection_733.png?generation=1732414185676234&alt=media)\n\ntry to explore yourself first, if you still have doubts, post it here later.\nyou should google around for\" how to use deep network for prediction of point coordinate\". i think you can easily find paper and code or blog. then ask chatgpt to read it and ask chatgpt questions.\nyou can then feed in chatgpt my code or your question above to link between the papers/codes you found with my code or your questions.",
      "votes": 4,
      "replies": [
        {
          "id": 3054292,
          "postDate": "2024-11-24T14:33:50.890Z",
          "content": "<p>Thank you so much <a href=\"https://www.kaggle.com/hengck23\" target=\"_blank\">@hengck23</a>. This is the next item in my to-do list. </p>",
          "rawMarkdown": "Thank you so much @hengck23. This is the next item in my to-do list. "
        },
        {
          "id": 3058903,
          "postDate": "2024-11-30T06:16:47.463Z",
          "content": "<p>very helpful</p>",
          "rawMarkdown": "very helpful\n",
          "isDeleted": true
        }
      ]
    },
    {
      "id": 3054095,
      "postDate": "2024-11-24T10:20:01.553Z",
      "content": "<p>Hi. The coordinates need to be scaled about 10 times (zyx/=10) for the VoxelSpacing10.000 zarr (the only ones pressent at test). The nearest integer to them are the indices of depth (z), height (y) and width (x). That's the centroid coordinates of the corresponding particle in the corresponding array (zarr file once readed). So the slice z at row y and column x will certainly have the particle. The nearest slices to z close to the same y and x will have too, depends on the volume of the particle. Virus, apo-ferritin and ribosomes are bigger, around 32 pixels of diameter while thy and beta are much smaller.</p>",
      "rawMarkdown": "Hi. The coordinates need to be scaled about 10 times (zyx/=10) for the VoxelSpacing10.000 zarr (the only ones pressent at test). The nearest integer to them are the indices of depth (z), height (y) and width (x). That's the centroid coordinates of the corresponding particle in the corresponding array (zarr file once readed). So the slice z at row y and column x will certainly have the particle. The nearest slices to z close to the same y and x will have too, depends on the volume of the particle. Virus, apo-ferritin and ribosomes are bigger, around 32 pixels of diameter while thy and beta are much smaller.",
      "votes": 1,
      "replies": [
        {
          "id": 3054290,
          "postDate": "2024-11-24T14:32:43.473Z",
          "content": "<p>Ooohww, thank you so much!! Now it makes sense, I think this was the missing piece in my understanding. To confirm it, for this example <code>TS_99_9</code> I read the annotations, and one of the coordinates in the annotations are like this: <code>[3319.52, 5476.3, 1014.51]</code>. I assume this is x, y, z because the max value for the annotations can be at max 1840, 6300, 6300 but the z values are much lower. But the volume/image data is z, y, x. So if I draw a circle around this with radius 15 (took from <a href=\"https://www.kaggle.com/hengck23\" target=\"_blank\">@hengck23</a> 's code - ribosome was radius 150 and divided by 10 again), I get the following image with annotations.</p>\n<pre><code>circle = plt.Circle((, ), radius=, fill=)\nca = plt.gca()\nca.add_patch(circle)\nplt.imshow(volume[])\nplt.show()\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1098475%2F2e5dad72765d2cf8a0d98b60d0dc70d6%2FScreenshot%202024-11-24%20at%2015.25.28.png?generation=1732458331695298&amp;alt=media\" alt=\"\"></p>\n<p>Do you think is this the correct interpretation?</p>",
          "rawMarkdown": "Ooohww, thank you so much!! Now it makes sense, I think this was the missing piece in my understanding. To confirm it, for this example `TS_99_9` I read the annotations, and one of the coordinates in the annotations are like this: `[3319.52, 5476.3, 1014.51]`. I assume this is x, y, z because the max value for the annotations can be at max 1840, 6300, 6300 but the z values are much lower. But the volume/image data is z, y, x. So if I draw a circle around this with radius 15 (took from @hengck23 's code - ribosome was radius 150 and divided by 10 again), I get the following image with annotations.\n\n```python\ncircle = plt.Circle((332, 548), radius=15, fill=False)\nca = plt.gca()\nca.add_patch(circle)\nplt.imshow(volume[101])\nplt.show()\n```\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1098475%2F2e5dad72765d2cf8a0d98b60d0dc70d6%2FScreenshot%202024-11-24%20at%2015.25.28.png?generation=1732458331695298&alt=media)\n\nDo you think is this the correct interpretation?",
          "votes": 1,
          "replies": [
            {
              "id": 3054639,
              "postDate": "2024-11-24T23:34:48.813Z",
              "content": "<p>To be more precise, you should actually be using a width of 10.012 to scale the pixels, although it probably only matters in the bottom right of the images for the smallest particles (i.e. apo-ferritin) where the max error is just over a pixel.  You might look at <a href=\"https://www.kaggle.com/code/davidlist/experiment-ts-6-4-visualization\" target=\"_blank\">https://www.kaggle.com/code/davidlist/experiment-ts-6-4-visualization</a> which covers a lot of info on this subject.  Actually, not sure I cover the 10.012 scaling, but you can find the discussion here: <a href=\"https://www.kaggle.com/competitions/czii-cryo-et-object-identification/discussion/545381\" target=\"_blank\">https://www.kaggle.com/competitions/czii-cryo-et-object-identification/discussion/545381</a></p>",
              "rawMarkdown": "To be more precise, you should actually be using a width of 10.012 to scale the pixels, although it probably only matters in the bottom right of the images for the smallest particles (i.e. apo-ferritin) where the max error is just over a pixel.  You might look at [https://www.kaggle.com/code/davidlist/experiment-ts-6-4-visualization](https://www.kaggle.com/code/davidlist/experiment-ts-6-4-visualization) which covers a lot of info on this subject.  Actually, not sure I cover the 10.012 scaling, but you can find the discussion here: [https://www.kaggle.com/competitions/czii-cryo-et-object-identification/discussion/545381](https://www.kaggle.com/competitions/czii-cryo-et-object-identification/discussion/545381)",
              "votes": 2
            },
            {
              "id": 3055383,
              "postDate": "2024-11-25T18:29:09.027Z",
              "content": "<p>Oh, that's perfect; thank you for sharing! I was digging into properly scaling and preparing the data loader. This will come in handy.</p>",
              "rawMarkdown": "Oh, that's perfect; thank you for sharing! I was digging into properly scaling and preparing the data loader. This will come in handy."
            }
          ]
        },
        {
          "id": 3054295,
          "postDate": "2024-11-24T14:38:27.683Z",
          "content": "<blockquote>\n  <p>The nearest slices to z close to the same y and x will have too, depending on the volume of the particle</p>\n</blockquote>\n<p>And this is because it's 3D, right? Depending on the size of a particle, a couple of images to the left or to the right will have the same targets? Let's say for a volume 184, 630, 630, if there is a target at (64, 630, 630) more likely there will be around (60/61/62/63, 630, 630) and (65/66/67, 630, 630)?</p>",
          "rawMarkdown": "> The nearest slices to z close to the same y and x will have too, depending on the volume of the particle\n\nAnd this is because it's 3D, right? Depending on the size of a particle, a couple of images to the left or to the right will have the same targets? Let's say for a volume 184, 630, 630, if there is a target at (64, 630, 630) more likely there will be around (60/61/62/63, 630, 630) and (65/66/67, 630, 630)?",
          "replies": [
            {
              "id": 3054325,
              "postDate": "2024-11-24T15:12:22.890Z",
              "content": "<p>That's the hard part, the model has to figure out. The labels are points. We can simplify as spheres, so as pixels we see in x and y we should espect in z. But depth in this case is a bit more complex.</p>",
              "rawMarkdown": "That's the hard part, the model has to figure out. The labels are points. We can simplify as spheres, so as pixels we see in x and y we should espect in z. But depth in this case is a bit more complex.",
              "votes": 2
            },
            {
              "id": 3054340,
              "postDate": "2024-11-24T15:32:07.053Z",
              "content": "<p>Oh okay, I see. So it's also quite important how you pass the volumes to the network as the targets might be in the borders, right? I need to figure that out as well 😅 Thank you!</p>",
              "rawMarkdown": "Oh okay, I see. So it's also quite important how you pass the volumes to the network as the targets might be in the borders, right? I need to figure that out as well 😅 Thank you!"
            },
            {
              "id": 3054366,
              "postDate": "2024-11-24T16:06:29.147Z",
              "content": "<p>One more thing <a href=\"https://www.kaggle.com/snnclsr\" target=\"_blank\">@snnclsr</a> </p>\n<p>Actual scale is in zarr_file.attrs['multiscales'][0]['datasets'][0]['coordinateTransformations'][0]['scale']</p>\n<p>Is almost 10 but is different to each volume.</p>",
              "rawMarkdown": "One more thing @snnclsr \n\nActual scale is in zarr_file.attrs['multiscales'][0]['datasets'][0]['coordinateTransformations'][0]['scale']\n\nIs almost 10 but is different to each volume.",
              "votes": 1
            },
            {
              "id": 3055384,
              "postDate": "2024-11-25T18:29:32.180Z",
              "content": "<p>Thank you for the reminder! I am checking those now.</p>",
              "rawMarkdown": "Thank you for the reminder! I am checking those now."
            },
            {
              "id": 3055486,
              "postDate": "2024-11-25T20:00:23.860Z",
              "content": "<p><a href=\"https://www.kaggle.com/sacuscreed\" target=\"_blank\">@sacuscreed</a> one more question if you don't mind:</p>\n<p>So I came up with something like this to create the spherical masks with the help of ChatGPT:</p>\n<pre><code>    mask = np.zeros(volume_shape, dtype=np.uint8)\n\n    z, y, x = np.ogrid[:volume_shape[], :volume_shape[], :volume_shape[]]\n     center  centers:\n        \n        cx, cy, cz = center\n        distance = (z - cz)** + (y - cy)** + (x - cx)**\n        mask |= (distance &lt;= radius**)  \n</code></pre>\n<p>And this is what it looks like for ribosomes on a single slice: <br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1098475%2F4350b548bb4c7b8137cd3f602870d196%2FScreenshot%202024-11-25%20at%2020.58.17.png?generation=1732564687768691&amp;alt=media\" alt=\"\"></p>\n<p>A bit vague but do you think this looks correct? And creating the target as a sphere from coordinates + radius is a feasible approach for modeling, wdyt?</p>",
              "rawMarkdown": "@sacuscreed one more question if you don't mind:\n\nSo I came up with something like this to create the spherical masks with the help of ChatGPT:\n```python\n    mask = np.zeros(volume_shape, dtype=np.uint8)\n\n    z, y, x = np.ogrid[:volume_shape[0], :volume_shape[1], :volume_shape[2]]\n    for center in centers:\n        # cz, cy, cx = center\n        cx, cy, cz = center\n        distance = (z - cz)**2 + (y - cy)**2 + (x - cx)**2\n        mask |= (distance <= radius**2)  # Combine masks using logical OR\n```\nAnd this is what it looks like for ribosomes on a single slice: \n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1098475%2F4350b548bb4c7b8137cd3f602870d196%2FScreenshot%202024-11-25%20at%2020.58.17.png?generation=1732564687768691&alt=media)\n\nA bit vague but do you think this looks correct? And creating the target as a sphere from coordinates + radius is a feasible approach for modeling, wdyt?"
            },
            {
              "id": 3055502,
              "postDate": "2024-11-25T20:21:39.693Z",
              "content": "<p>I'm using a different approach that doesn't use masking (which to be fair could be why I'm in 169th place) but given that the scoring is based on spheres (i.e. distance) I don't see why modeling the particles that way would cause issues.</p>",
              "rawMarkdown": "I'm using a different approach that doesn't use masking (which to be fair could be why I'm in 169th place) but given that the scoring is based on spheres (i.e. distance) I don't see why modeling the particles that way would cause issues.",
              "votes": 2
            },
            {
              "id": 3055566,
              "postDate": "2024-11-25T21:41:49.210Z",
              "content": "<p>I,ve been working on heatmaps till now. But David's point sounds correct.</p>",
              "rawMarkdown": "I,ve been working on heatmaps till now. But David's point sounds correct.",
              "votes": 2
            },
            {
              "id": 3055571,
              "postDate": "2024-11-25T21:50:10.903Z",
              "content": "<p>Okay, thank you so much both! Let's see how far I can go with this approach.</p>",
              "rawMarkdown": "Okay, thank you so much both! Let's see how far I can go with this approach.",
              "votes": 1
            }
          ]
        }
      ]
    }
  ],
  "comments": [
    {
      "id": 3053843,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "2024-11-24T02:09:56.947000",
      "content": "<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fd828b08e3ab1dc323458767902964344%2FSelection_732.png?generation=1732414133881392&amp;alt=media\" alt=\"\"></p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fe89b286cb21ea08f3bf93e45e320a742%2FSelection_733.png?generation=1732414185676234&amp;alt=media\" alt=\"\"></p>\n<p>try to explore yourself first, if you still have doubts, post it here later.<br>\nyou should google around for\" how to use deep network for prediction of point coordinate\". i think you can easily find paper and code or blog. then ask chatgpt to read it and ask chatgpt questions.<br>\nyou can then feed in chatgpt my code or your question above to link between the papers/codes you found with my code or your questions.</p>",
      "votes": 4,
      "replies": [
        {
          "id": 3054292,
          "author_name": "Sinan Calisir",
          "author_url": "",
          "post_date": "2024-11-24T14:33:50.890000",
          "content": "<p>Thank you so much <a href=\"https://www.kaggle.com/hengck23\" target=\"_blank\">@hengck23</a>. This is the next item in my to-do list. </p>",
          "votes": 0,
          "replies": []
        },
        {
          "id": 3058903,
          "author_name": "",
          "author_url": "",
          "post_date": "2024-11-30T06:16:47.463000",
          "content": "<p>very helpful</p>",
          "votes": 0,
          "replies": []
        }
      ]
    },
    {
      "id": 3054095,
      "author_name": "Ángel Jacinto Sánchez Ruiz",
      "author_url": "",
      "post_date": "2024-11-24T10:20:01.553000",
      "content": "<p>Hi. The coordinates need to be scaled about 10 times (zyx/=10) for the VoxelSpacing10.000 zarr (the only ones pressent at test). The nearest integer to them are the indices of depth (z), height (y) and width (x). That's the centroid coordinates of the corresponding particle in the corresponding array (zarr file once readed). So the slice z at row y and column x will certainly have the particle. The nearest slices to z close to the same y and x will have too, depends on the volume of the particle. Virus, apo-ferritin and ribosomes are bigger, around 32 pixels of diameter while thy and beta are much smaller.</p>",
      "votes": 1,
      "replies": [
        {
          "id": 3054290,
          "author_name": "Sinan Calisir",
          "author_url": "",
          "post_date": "2024-11-24T14:32:43.473000",
          "content": "<p>Ooohww, thank you so much!! Now it makes sense, I think this was the missing piece in my understanding. To confirm it, for this example <code>TS_99_9</code> I read the annotations, and one of the coordinates in the annotations are like this: <code>[3319.52, 5476.3, 1014.51]</code>. I assume this is x, y, z because the max value for the annotations can be at max 1840, 6300, 6300 but the z values are much lower. But the volume/image data is z, y, x. So if I draw a circle around this with radius 15 (took from <a href=\"https://www.kaggle.com/hengck23\" target=\"_blank\">@hengck23</a> 's code - ribosome was radius 150 and divided by 10 again), I get the following image with annotations.</p>\n<pre><code>circle = plt.Circle((, ), radius=, fill=)\nca = plt.gca()\nca.add_patch(circle)\nplt.imshow(volume[])\nplt.show()\n</code></pre>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1098475%2F2e5dad72765d2cf8a0d98b60d0dc70d6%2FScreenshot%202024-11-24%20at%2015.25.28.png?generation=1732458331695298&amp;alt=media\" alt=\"\"></p>\n<p>Do you think is this the correct interpretation?</p>",
          "votes": 1,
          "replies": [
            {
              "id": 3054639,
              "author_name": "David List",
              "author_url": "",
              "post_date": "2024-11-24T23:34:48.813000",
              "content": "<p>To be more precise, you should actually be using a width of 10.012 to scale the pixels, although it probably only matters in the bottom right of the images for the smallest particles (i.e. apo-ferritin) where the max error is just over a pixel.  You might look at <a href=\"https://www.kaggle.com/code/davidlist/experiment-ts-6-4-visualization\" target=\"_blank\">https://www.kaggle.com/code/davidlist/experiment-ts-6-4-visualization</a> which covers a lot of info on this subject.  Actually, not sure I cover the 10.012 scaling, but you can find the discussion here: <a href=\"https://www.kaggle.com/competitions/czii-cryo-et-object-identification/discussion/545381\" target=\"_blank\">https://www.kaggle.com/competitions/czii-cryo-et-object-identification/discussion/545381</a></p>",
              "votes": 2,
              "replies": []
            },
            {
              "id": 3055383,
              "author_name": "Sinan Calisir",
              "author_url": "",
              "post_date": "2024-11-25T18:29:09.027000",
              "content": "<p>Oh, that's perfect; thank you for sharing! I was digging into properly scaling and preparing the data loader. This will come in handy.</p>",
              "votes": 0,
              "replies": []
            }
          ]
        },
        {
          "id": 3054295,
          "author_name": "Sinan Calisir",
          "author_url": "",
          "post_date": "2024-11-24T14:38:27.683000",
          "content": "<blockquote>\n  <p>The nearest slices to z close to the same y and x will have too, depending on the volume of the particle</p>\n</blockquote>\n<p>And this is because it's 3D, right? Depending on the size of a particle, a couple of images to the left or to the right will have the same targets? Let's say for a volume 184, 630, 630, if there is a target at (64, 630, 630) more likely there will be around (60/61/62/63, 630, 630) and (65/66/67, 630, 630)?</p>",
          "votes": 0,
          "replies": [
            {
              "id": 3054325,
              "author_name": "Ángel Jacinto Sánchez Ruiz",
              "author_url": "",
              "post_date": "2024-11-24T15:12:22.890000",
              "content": "<p>That's the hard part, the model has to figure out. The labels are points. We can simplify as spheres, so as pixels we see in x and y we should espect in z. But depth in this case is a bit more complex.</p>",
              "votes": 2,
              "replies": []
            },
            {
              "id": 3054340,
              "author_name": "Sinan Calisir",
              "author_url": "",
              "post_date": "2024-11-24T15:32:07.053000",
              "content": "<p>Oh okay, I see. So it's also quite important how you pass the volumes to the network as the targets might be in the borders, right? I need to figure that out as well 😅 Thank you!</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 3054366,
              "author_name": "Ángel Jacinto Sánchez Ruiz",
              "author_url": "",
              "post_date": "2024-11-24T16:06:29.147000",
              "content": "<p>One more thing <a href=\"https://www.kaggle.com/snnclsr\" target=\"_blank\">@snnclsr</a> </p>\n<p>Actual scale is in zarr_file.attrs['multiscales'][0]['datasets'][0]['coordinateTransformations'][0]['scale']</p>\n<p>Is almost 10 but is different to each volume.</p>",
              "votes": 1,
              "replies": []
            },
            {
              "id": 3055384,
              "author_name": "Sinan Calisir",
              "author_url": "",
              "post_date": "2024-11-25T18:29:32.180000",
              "content": "<p>Thank you for the reminder! I am checking those now.</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 3055486,
              "author_name": "Sinan Calisir",
              "author_url": "",
              "post_date": "2024-11-25T20:00:23.860000",
              "content": "<p><a href=\"https://www.kaggle.com/sacuscreed\" target=\"_blank\">@sacuscreed</a> one more question if you don't mind:</p>\n<p>So I came up with something like this to create the spherical masks with the help of ChatGPT:</p>\n<pre><code>    mask = np.zeros(volume_shape, dtype=np.uint8)\n\n    z, y, x = np.ogrid[:volume_shape[], :volume_shape[], :volume_shape[]]\n     center  centers:\n        \n        cx, cy, cz = center\n        distance = (z - cz)** + (y - cy)** + (x - cx)**\n        mask |= (distance &lt;= radius**)  \n</code></pre>\n<p>And this is what it looks like for ribosomes on a single slice: <br>\n<img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F1098475%2F4350b548bb4c7b8137cd3f602870d196%2FScreenshot%202024-11-25%20at%2020.58.17.png?generation=1732564687768691&amp;alt=media\" alt=\"\"></p>\n<p>A bit vague but do you think this looks correct? And creating the target as a sphere from coordinates + radius is a feasible approach for modeling, wdyt?</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 3055502,
              "author_name": "David List",
              "author_url": "",
              "post_date": "2024-11-25T20:21:39.693000",
              "content": "<p>I'm using a different approach that doesn't use masking (which to be fair could be why I'm in 169th place) but given that the scoring is based on spheres (i.e. distance) I don't see why modeling the particles that way would cause issues.</p>",
              "votes": 2,
              "replies": []
            },
            {
              "id": 3055566,
              "author_name": "Ángel Jacinto Sánchez Ruiz",
              "author_url": "",
              "post_date": "2024-11-25T21:41:49.210000",
              "content": "<p>I,ve been working on heatmaps till now. But David's point sounds correct.</p>",
              "votes": 2,
              "replies": []
            },
            {
              "id": 3055571,
              "author_name": "Sinan Calisir",
              "author_url": "",
              "post_date": "2024-11-25T21:50:10.903000",
              "content": "<p>Okay, thank you so much both! Let's see how far I can go with this approach.</p>",
              "votes": 1,
              "replies": []
            }
          ]
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "3053823": "Hi,\n\nI am a newbie to this topic and struggling to create the image-target pairs from the files correctly. I investigated the [notebooks](https://github.com/czimaginginstitute/2024_czii_mlchallenge_notebooks/tree/main) from the host as well but they confused me even more because of the tools involved. I also checked the popular notebooks [here](https://www.kaggle.com/code/fnands/baseline-unet-train-submit) and [here](https://www.kaggle.com/code/hengck23/3d-unet-using-2d-image-encoder) but couldn't figure them from there as well. \n\nI read the image data like this:\n```python\ndata_dirs = sorted(glob(\"/kaggle/input/czii-cryo-et-object-identification/train/static/ExperimentRuns/*/*/denoised*\"))\nfor data_dir in data_dirs:\n    f = zarr.open(data_dir)\n    print(data_dir)\n    print(f.tree())\n    image = np.array(f.get(0))\n    print(image.shape)\n```\n\nand read the target like this following the code from @hengck23 \n\n```python\nTARGETS = [\n    \"apo-ferritin\",\n    \"beta-amylase\",\n    \"beta-galactosidase\",\n    \"ribosome\",\n    \"thyroglobulin\",\n    \"virus-like-particle\"\n]\n\ndef read_single_target(_id, overlay_dir):\n    location = {}\n    json_dir = f\"{overlay_dir}/{_id}/Picks\"\n    for target in TARGETS:\n        json_file = f\"{json_dir}/{target}.json\"\n        with open(json_file, \"r\") as f:\n            annotations = json.load(f)\n        points = annotations[\"points\"]\n        num_points = len(points)\n        loc = np.array([list(points[i][\"location\"].values()) for i in range(num_points)])\n        location[target] = loc\n    return location\n```\n\nThe targets are here:\n\n```json\n{'apo-ferritin': array([[6072.464, 4038.   ,  715.679],\n        [5967.452, 4228.213, 1124.601],\n        [5847.622, 5066.71 ,  678.893],\n        [ 472.853, 5632.618,  580.676],\n        [ 564.507, 5604.88 ,  678.16 ],\n        [ 890.414, 5615.759,  467.69 ],\n        [5613.561,  672.73 ,  265.579],\n        [ 458.087, 1972.013,  765.906],\n...\n```\n\nI am mostly a Pytorch person and I was planning to do something like this:\n\n```python\nclass CryoETDataset(Dataset):\n    def __init__(self, data_dirs, target_data, transform=None):\n        pass\n    def __len__(self):\n        pass\n    def __getitem__(self, idx):\n        data_dir = self.data_dirs[idx]\n        f = zarr.open(data_dir)\n        image = np.array(f.get(0))\n        targets = self.target_data[experiment_id]\n        target_coordinates = targets[\"apo-ferritin\"] # single target for now\n        if self.transform:\n            image = self.transform(image)\n        return image, target_coordinates       \n```\n\nBut I couldn't connect the bridge between the two. I am not looking for a spoon feeding but only a direction or notebook that would be helpful to understand and proceed further.\n\nThanks",
    "3053843": "![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fd828b08e3ab1dc323458767902964344%2FSelection_732.png?generation=1732414133881392&alt=media)\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fe89b286cb21ea08f3bf93e45e320a742%2FSelection_733.png?generation=1732414185676234&alt=media)\n\ntry to explore yourself first, if you still have doubts, post it here later.\nyou should google around for\" how to use deep network for prediction of point coordinate\". i think you can easily find paper and code or blog. then ask chatgpt to read it and ask chatgpt questions.\nyou can then feed in chatgpt my code or your question above to link between the papers/codes you found with my code or your questions.",
    "3054095": "Hi. The coordinates need to be scaled about 10 times (zyx/=10) for the VoxelSpacing10.000 zarr (the only ones pressent at test). The nearest integer to them are the indices of depth (z), height (y) and width (x). That's the centroid coordinates of the corresponding particle in the corresponding array (zarr file once readed). So the slice z at row y and column x will certainly have the particle. The nearest slices to z close to the same y and x will have too, depends on the volume of the particle. Virus, apo-ferritin and ribosomes are bigger, around 32 pixels of diameter while thy and beta are much smaller."
  }
}