{
  "id": 465834,
  "title": "Converting output to originial size",
  "url": "/competitions/blood-vessel-segmentation/discussion/465834",
  "author_name": "nima iji",
  "post_date": "2024-01-05T20:14:19.675000",
  "votes": 0,
  "comment_count": 10,
  "views": 0,
  "content": "<p>Hi there,</p>\n<p>I am trying to get better scores. But my huge problem is the results shape. I have to convert my outputs shape to the original shape. one of the methods I'm using is tf.resize(…). but it won't work vey well. Is there any other solutions for this?</p>\n<p>This is my notebook: <a href=\"https://www.kaggle.com/code/nimaiji/segnet-tensorflow-blood-vessel-segmentation\" target=\"_blank\">https://www.kaggle.com/code/nimaiji/segnet-tensorflow-blood-vessel-segmentation</a></p>",
  "messages": [
    {
      "id": 2588881,
      "postDate": "2024-01-05T20:21:19.640Z",
      "content": "<p>Hi, what process fellow your inputs before feeding your model, resize?</p>",
      "rawMarkdown": "Hi, what process fellow your inputs before feeding your model, resize?",
      "replies": [
        {
          "id": 2588891,
          "postDate": "2024-01-05T20:35:50.023Z",
          "content": "<p>This is it<br>\n`def preprocess_label(source, is_image=False):<br>\n    if is_image:<br>\n        label = source<br>\n    else:<br>\n        label = get_image(source)</p>\n<pre><code> .ndim &gt;   .shape[] &gt; :\n     = [..., ]\n\n =  / SIZE_F  .() &gt;   \n\n = tf.convert_to_tensor(, dtype=tf.float32)\n\n .ndim == :\n     = [..., tf.newaxis]\n\n# Ensure mask tensor  3D  this point\n .ndim != :\n    raise ValueError('Label tensor must be   [, , channels]')\n\n = tf..resize(, [SIZE, SIZE], =tf..ResizeMethod.NEAREST_NEIGHBOR)\n\n = tf.where( &gt; TH, ., .)\n\n `\n</code></pre>",
          "rawMarkdown": "This is it\n`def preprocess_label(source, is_image=False):\n    if is_image:\n        label = source\n    else:\n        label = get_image(source)\n    \n    if label.ndim > 2 and label.shape[2] > 1:\n        label = label[..., 0]\n        \n    label = label / SIZE_F if label.max() > 1 else label\n    \n    label = tf.convert_to_tensor(label, dtype=tf.float32)\n    \n    if label.ndim == 2:\n        label = label[..., tf.newaxis]\n    \n    # Ensure mask tensor is 3D at this point\n    if label.ndim != 3:\n        raise ValueError('Label tensor must be 3 dimensions [height, width, channels]')\n        \n    label = tf.image.resize(label, [SIZE, SIZE], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)\n    \n    label = tf.where(label > TH, 1., 0.)\n    \n    return label`",
          "replies": [
            {
              "id": 2588901,
              "postDate": "2024-01-05T20:42:49.427Z",
              "content": "<p>If I understand correctly your model's output is label and you are resizing it with<br>\nlabel = tf.image.resize(label, [SIZE, SIZE], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)<br>\nbefore submiting.<br>\nIf that's the case each input has his own shape and is not SIZExSIZE, is HxW</p>",
              "rawMarkdown": "If I understand correctly your model's output is label and you are resizing it with\nlabel = tf.image.resize(label, [SIZE, SIZE], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)\nbefore submiting.\nIf that's the case each input has his own shape and is not SIZExSIZE, is HxW"
            },
            {
              "id": 2588923,
              "postDate": "2024-01-05T21:10:18.907Z",
              "content": "<p>No, actually this function is for preprocessing. So I resize all inputs (by tf.resize(..)) to 512x512 or 256x256 and after training the model, I convert them to 1303x912 which is the original size. </p>",
              "rawMarkdown": "No, actually this function is for preprocessing. So I resize all inputs (by tf.resize(..)) to 512x512 or 256x256 and after training the model, I convert them to 1303x912 which is the original size. "
            },
            {
              "id": 2598273,
              "postDate": "2024-01-12T09:02:51.023Z",
              "content": "<p>I think the images and labels have not a unique size. i.e., some of them have different sizes, and it seems like there are about 4 kinds of shapes.</p>\n<p>If I'm correct, how are other people dealing with it?</p>",
              "rawMarkdown": "I think the images and labels have not a unique size. i.e., some of them have different sizes, and it seems like there are about 4 kinds of shapes.\n\nIf I'm correct, how are other people dealing with it?",
              "isDeleted": true
            },
            {
              "id": 2600854,
              "postDate": "2024-01-14T00:38:16.653Z",
              "content": "<p>I've checked the sizes. All are the same. An image of a kidney has the same dimension as its label. However, the images in different folders have different dimensions. You can search EDA in codes to see these.</p>",
              "rawMarkdown": "I've checked the sizes. All are the same. An image of a kidney has the same dimension as its label. However, the images in different folders have different dimensions. You can search EDA in codes to see these."
            },
            {
              "id": 2602200,
              "postDate": "2024-01-15T01:07:45.123Z",
              "content": "<p>Ah I might have missed it. Thanks!</p>\n<p>But still wondering, then, do you guys make and train multiple models for all those different sizes?</p>",
              "rawMarkdown": "Ah I might have missed it. Thanks!\n\nBut still wondering, then, do you guys make and train multiple models for all those different sizes?",
              "isDeleted": true
            },
            {
              "id": 2602203,
              "postDate": "2024-01-15T01:11:42.187Z",
              "content": "<p>No, well in the preprocessing we resize all images to a certain size. And after training, we resize the predicted labels to the original size.</p>",
              "rawMarkdown": "No, well in the preprocessing we resize all images to a certain size. And after training, we resize the predicted labels to the original size."
            },
            {
              "id": 2602206,
              "postDate": "2024-01-15T01:29:16.947Z",
              "content": "<p>Sorry about asking much, but as a beginner still wondering about something.</p>\n<p>The model would output only one size of predicted labels, and we have multiple sizes of labels. Then, do you just upsampling the predicted labels to the size in accordance with its original label size? e.g., output(512, 512) -&gt; (1302, 912) or (1401, 1401) something like this?</p>\n<p>But then, my question is the upsampling parameters not needed to be included in the model to be trained?(Actually, I got the answer myself while writing down the question)</p>\n<p>Then, also as for another question, I think 'upsampling' and 'round' would be needed in the output to compare with labels of the original sizes. Then, are they differentiable with the respect of the final loss value?</p>\n<p>Maybe I'm lost in how to implement segmentation task as a whole…</p>",
              "rawMarkdown": "Sorry about asking much, but as a beginner still wondering about something.\n\nThe model would output only one size of predicted labels, and we have multiple sizes of labels. Then, do you just upsampling the predicted labels to the size in accordance with its original label size? e.g., output(512, 512) -> (1302, 912) or (1401, 1401) something like this?\n\nBut then, my question is the upsampling parameters not needed to be included in the model to be trained?(Actually, I got the answer myself while writing down the question)\n\nThen, also as for another question, I think 'upsampling' and 'round' would be needed in the output to compare with labels of the original sizes. Then, are they differentiable with the respect of the final loss value?\n\nMaybe I'm lost in how to implement segmentation task as a whole...",
              "isDeleted": true
            },
            {
              "id": 2602215,
              "postDate": "2024-01-15T01:42:06.510Z",
              "content": "<p>It's good to talk about these things. Please, first check this link: <a href=\"https://paperswithcode.com/method/segnet#:~:text=SegNet%20is%20a%20semantic%20segmentation,layers%20in%20the%20VGG16%20network\" target=\"_blank\">https://paperswithcode.com/method/segnet#:~:text=SegNet%20is%20a%20semantic%20segmentation,layers%20in%20the%20VGG16%20network</a>.</p>\n<p>During the training process, we want to extract features and find exclusive attributes of images. But the result dimension is too small to classify by them. So what is happening in SegNet, is that it use upsampling the exclusive features for recreating the label but in a defined output shape. After generating predicted labels it needs to be resized again to the original size of the image.</p>",
              "rawMarkdown": "It's good to talk about these things. Please, first check this link: https://paperswithcode.com/method/segnet#:~:text=SegNet%20is%20a%20semantic%20segmentation,layers%20in%20the%20VGG16%20network.\n\nDuring the training process, we want to extract features and find exclusive attributes of images. But the result dimension is too small to classify by them. So what is happening in SegNet, is that it use upsampling the exclusive features for recreating the label but in a defined output shape. After generating predicted labels it needs to be resized again to the original size of the image."
            }
          ]
        }
      ]
    },
    {
      "id": 2588878,
      "postDate": "2024-01-05T20:14:19.677Z",
      "content": "<p>Hi there,</p>\n<p>I am trying to get better scores. But my huge problem is the results shape. I have to convert my outputs shape to the original shape. one of the methods I'm using is tf.resize(…). but it won't work vey well. Is there any other solutions for this?</p>\n<p>This is my notebook: <a href=\"https://www.kaggle.com/code/nimaiji/segnet-tensorflow-blood-vessel-segmentation\" target=\"_blank\">https://www.kaggle.com/code/nimaiji/segnet-tensorflow-blood-vessel-segmentation</a></p>",
      "rawMarkdown": "Hi there,\n\nI am trying to get better scores. But my huge problem is the results shape. I have to convert my outputs shape to the original shape. one of the methods I'm using is tf.resize(...). but it won't work vey well. Is there any other solutions for this?\n\nThis is my notebook: https://www.kaggle.com/code/nimaiji/segnet-tensorflow-blood-vessel-segmentation"
    }
  ],
  "comments": [
    {
      "id": 2588881,
      "author_name": "Ángel Jacinto Sánchez Ruiz",
      "author_url": "",
      "post_date": "2024-01-05T20:21:19.640000",
      "content": "<p>Hi, what process fellow your inputs before feeding your model, resize?</p>",
      "votes": 0,
      "replies": [
        {
          "id": 2588891,
          "author_name": "nima iji",
          "author_url": "",
          "post_date": "2024-01-05T20:35:50.023000",
          "content": "<p>This is it<br>\n`def preprocess_label(source, is_image=False):<br>\n    if is_image:<br>\n        label = source<br>\n    else:<br>\n        label = get_image(source)</p>\n<pre><code> .ndim &gt;   .shape[] &gt; :\n     = [..., ]\n\n =  / SIZE_F  .() &gt;   \n\n = tf.convert_to_tensor(, dtype=tf.float32)\n\n .ndim == :\n     = [..., tf.newaxis]\n\n# Ensure mask tensor  3D  this point\n .ndim != :\n    raise ValueError('Label tensor must be   [, , channels]')\n\n = tf..resize(, [SIZE, SIZE], =tf..ResizeMethod.NEAREST_NEIGHBOR)\n\n = tf.where( &gt; TH, ., .)\n\n `\n</code></pre>",
          "votes": 0,
          "replies": [
            {
              "id": 2588901,
              "author_name": "Ángel Jacinto Sánchez Ruiz",
              "author_url": "",
              "post_date": "2024-01-05T20:42:49.427000",
              "content": "<p>If I understand correctly your model's output is label and you are resizing it with<br>\nlabel = tf.image.resize(label, [SIZE, SIZE], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)<br>\nbefore submiting.<br>\nIf that's the case each input has his own shape and is not SIZExSIZE, is HxW</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2588923,
              "author_name": "nima iji",
              "author_url": "",
              "post_date": "2024-01-05T21:10:18.907000",
              "content": "<p>No, actually this function is for preprocessing. So I resize all inputs (by tf.resize(..)) to 512x512 or 256x256 and after training the model, I convert them to 1303x912 which is the original size. </p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2598273,
              "author_name": "",
              "author_url": "",
              "post_date": "2024-01-12T09:02:51.023000",
              "content": "<p>I think the images and labels have not a unique size. i.e., some of them have different sizes, and it seems like there are about 4 kinds of shapes.</p>\n<p>If I'm correct, how are other people dealing with it?</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2600854,
              "author_name": "nima iji",
              "author_url": "",
              "post_date": "2024-01-14T00:38:16.653000",
              "content": "<p>I've checked the sizes. All are the same. An image of a kidney has the same dimension as its label. However, the images in different folders have different dimensions. You can search EDA in codes to see these.</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2602200,
              "author_name": "",
              "author_url": "",
              "post_date": "2024-01-15T01:07:45.123000",
              "content": "<p>Ah I might have missed it. Thanks!</p>\n<p>But still wondering, then, do you guys make and train multiple models for all those different sizes?</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2602203,
              "author_name": "nima iji",
              "author_url": "",
              "post_date": "2024-01-15T01:11:42.187000",
              "content": "<p>No, well in the preprocessing we resize all images to a certain size. And after training, we resize the predicted labels to the original size.</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2602206,
              "author_name": "",
              "author_url": "",
              "post_date": "2024-01-15T01:29:16.947000",
              "content": "<p>Sorry about asking much, but as a beginner still wondering about something.</p>\n<p>The model would output only one size of predicted labels, and we have multiple sizes of labels. Then, do you just upsampling the predicted labels to the size in accordance with its original label size? e.g., output(512, 512) -&gt; (1302, 912) or (1401, 1401) something like this?</p>\n<p>But then, my question is the upsampling parameters not needed to be included in the model to be trained?(Actually, I got the answer myself while writing down the question)</p>\n<p>Then, also as for another question, I think 'upsampling' and 'round' would be needed in the output to compare with labels of the original sizes. Then, are they differentiable with the respect of the final loss value?</p>\n<p>Maybe I'm lost in how to implement segmentation task as a whole…</p>",
              "votes": 0,
              "replies": []
            },
            {
              "id": 2602215,
              "author_name": "nima iji",
              "author_url": "",
              "post_date": "2024-01-15T01:42:06.510000",
              "content": "<p>It's good to talk about these things. Please, first check this link: <a href=\"https://paperswithcode.com/method/segnet#:~:text=SegNet%20is%20a%20semantic%20segmentation,layers%20in%20the%20VGG16%20network\" target=\"_blank\">https://paperswithcode.com/method/segnet#:~:text=SegNet%20is%20a%20semantic%20segmentation,layers%20in%20the%20VGG16%20network</a>.</p>\n<p>During the training process, we want to extract features and find exclusive attributes of images. But the result dimension is too small to classify by them. So what is happening in SegNet, is that it use upsampling the exclusive features for recreating the label but in a defined output shape. After generating predicted labels it needs to be resized again to the original size of the image.</p>",
              "votes": 0,
              "replies": []
            }
          ]
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2588881": "Hi, what process fellow your inputs before feeding your model, resize?",
    "2588878": "Hi there,\n\nI am trying to get better scores. But my huge problem is the results shape. I have to convert my outputs shape to the original shape. one of the methods I'm using is tf.resize(...). but it won't work vey well. Is there any other solutions for this?\n\nThis is my notebook: https://www.kaggle.com/code/nimaiji/segnet-tensorflow-blood-vessel-segmentation"
  }
}