Answers.Body: <p>You could also have a look at the paper <a href="https://arxiv.org/pdf/1406.4729.pdf" rel="nofollow noreferrer">Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition</a> (2015), where the SPP-net is proposed. SSP-net is based on the use of a &quot;spatial pyramid pooling&quot;, which eliminates the requirement of having fixed-size inputs.</p>
  <p>In the abstract, the authors write</p>
  <blockquote>
  <p>Existing deep convolutional neural networks (CNNs) require a fixed-size (e.g., 224×224) input image. This requirement is &quot;artificial&quot; and may reduce the recognition accuracy for the images or sub-images of an arbitrary size/scale. In this work, we equip the networks with another pooling strategy, &quot;<strong>spatial pyramid pooling</strong>&quot;, to eliminate the above requirement. The new network structure, called <strong>SPP-net</strong>, can generate a fixed-length representation regardless of image size/scale.</p>
  <p>Pyramid pooling is also robust to object deformations. With these advantages, SPP-net should in general improve all CNN-based <strong>image classification</strong> methods. On the ImageNet 2012 dataset, we demonstrate that SPP-net boosts the accuracy of a variety of CNN architectures despite their different designs. On the Pascal VOC 2007 and Caltech101 datasets, SPP-net achieves state-of-theart classification results using a single full-image representation and no fine-tuning. The power of SPP-net is also significant in <strong>object detection</strong>. Using SPP-net, we compute the feature maps from the entire image only once, and then pool features in arbitrary regions (sub-images) to generate fixed-length representations for training
  the detectors. This method avoids repeatedly computing the convolutional features. In processing test images, our method is 24-102× faster than the R-CNN method, while achieving better or comparable accuracy on Pascal VOC 2007. In ImageNet Large Scale Visual Recognition Challenge (ILSVRC) 2014, our methods rank #2 in object detection and #3 in image classification among all 38 teams. This manuscript also introduces the improvement made for this competition.</p>
  </blockquote>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-06T05:07:36.443Z
Answers.LastActivityDate: 2020-06-13T20:28:29.077Z
Answers.LastEditDate: 2020-06-17T09:57:20.710Z
Answers.UserDisplayName: Felix Goldberg
Answers.Body: <p>Assuming you have a large dataset, and it's labeled pixel-wise, one hacky way to solve the issue is to preprocess the images to have same dimensions by inserting horizontal and vertical margins according to your desired dimensions, as for labels you add dummy extra output for the margin pixels so when calculating the loss you could mask the margins.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-05T10:30:25.563Z
Answers.Text: How does one deal with normalization then in these cases? Do you only normalize the pixels in an image that are not included in its margin I guess?
Answers.UserDisplayName: MattSt
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-05T14:57:07.493Z
Answers.Text: Yes, because your data generating process is having different sizes so if you include the margins, you will change the data distribution. margins are inserted to group the training samples into batches because your problem needs to output a fixed output vector.
Answers.UserDisplayName: Fadi Bakoura
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-05T04:00:47.263Z
Answers.LastActivityDate: 2018-05-05T04:00:47.263Z
Answers.UserDisplayName: Fadi Bakoura
Answers.Body: <p>As you want to perform image segmentation, you can use <a href="https://arxiv.org/pdf/1505.04597.pdf" rel="nofollow noreferrer">U-Net</a>, which does not have fully connected layers, but it is a <a href="https://arxiv.org/pdf/1411.4038.pdf" rel="nofollow noreferrer">fully convolutional network</a>, which makes it able to handle inputs of any dimension. You should read the linked papers for more info.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-28T05:29:30.023Z
Answers.LastActivityDate: 2020-06-13T20:21:01.373Z
Answers.LastEditDate: 2020-06-13T20:21:01.373Z
Answers.UserDisplayName: ganLover
Answers.Body: <p>Try resizing the image to the input dimensions of your neural network architecture(keeping it fixed to something like 128*128 in a standard 2D U-net architecture) using <strong>nearest neighbor interpolation</strong> technique. This is because if you resize your image using any other interpolation, it may result in tampering with the ground truth labels. This is particularly a problem in segmentation. You won't face such a problem when it comes to classification.</p>
  
  <p>Try the following:</p>
  
  
  
  <pre><code>import cv2 
  resized_image = cv2.resize(original_image, (new_width, new_height), 
                             interpolation=cv2.INTER_NEAREST)
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-04-20T05:49:49.330Z
Answers.LastActivityDate: 2020-04-21T18:32:01.950Z
Answers.LastEditDate: 2020-04-21T18:32:01.950Z
Answers.UserDisplayName: Shalabh Gupta
Answers.Body: <p>There are 2 problems you might face.</p>
  
  <ol>
  <li><p>Your neural net (in this case convolutional neural net) cannot physically accept images of different resolutions. This is usually the case if one has fully-connected layers, however, if the network is <strong>fully-convolutional</strong>, then it should be able to accept images of any dimension. Fully-convolutional implies that it doesn't contain fully-connected layers, but only convolutional, max-pooling, and batch normalization layers all of which are invariant to the size of the image. </p>
  
  <p>Exactly this approach was proposed in this ground-breaking paper <a href="https://arxiv.org/pdf/1411.4038.pdf" rel="noreferrer">Fully Convolutional Networks for Semantic Segmentation</a>. Keep in mind that their architecture and training methods might be slightly outdated by now. A similar approach was used in widely used <a href="https://arxiv.org/pdf/1505.04597.pdf" rel="noreferrer">U-Net: Convolutional Networks for Biomedical Image Segmentation</a>, and many other architectures for object detection, pose estimation, and segmentation.</p></li>
  <li><p>Convolutional neural nets are not scale-invariant. For example, if one trains on the cats of the same size in pixels on images of a fixed resolution, the net would fail on images of smaller or larger sizes of cats. In order to overcome this problem, I know of two methods (might be more in the literature): </p>
  
  <ol>
  <li><p>multi-scale training of images of different sizes in fully-convolutional nets in order to make the model more robust to changes in scale; and </p></li>
  <li><p>having multi-scale architecture. </p></li>
  </ol>
  
  <p>A place to start is to look at these two notable papers: <a href="https://arxiv.org/pdf/1612.03144.pdf" rel="noreferrer">Feature Pyramid Networks for Object Detection</a> and <a href="https://arxiv.org/pdf/1904.04514.pdf" rel="noreferrer">High-Resolution Representations for Labeling Pixels and Regions</a>.</p></li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-06-05T01:07:47.320Z
Answers.Text: If images in the dataset are of different sizes, we simply need to resize them into a fixed size (say, 224x224), but to the cost of losing the aspect ratio, right? Then what are the side effects of not keeping the aspect ratio? Because the higher the difference in sizes (250x320, 443x538, 654x 811, 443x 1019), the worse your images are distorted.
Answers.UserDisplayName: bit_scientist
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-15T16:12:38.240Z
Answers.Text: Yes, in principle current conv-nets are not truly suited to be aspect-ratio-invariant. In my experience I haven't seen a big problem with resizing images of different aspect ratios to a fixed size but I didn't deal with large differences in aspect ratios within the same dataset (e.g. 1 to 1.75 aspect ratios). If you do have large differences in aspect ratios in the same dataset you might want to try to train with fully-convolutional net and aspect ratio intact, but would probably need to make sure that each batch has images of the same aspect ratio.
Answers.UserDisplayName: Anuar Y
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-10T23:49:32.240Z
Answers.LastActivityDate: 2020-06-13T20:32:07.500Z
Answers.LastEditDate: 2020-06-13T20:32:07.500Z
Answers.UserDisplayName: Anuar Y
Body: <p>I'm facing the problem of having images of different dimensions as inputs in a segmentation task. Note that the images do not even have the same aspect ratio.</p>
  
  <p>One common approach that I found in general in deep learning is to crop the images, as it is also suggested <a href="https://datascience.stackexchange.com/q/16601/10640"><strong>here</strong></a>. However, in my case, I cannot crop the image and keep its center or something similar, since, in segmentation, I want the output to be of the same dimensions as the input.</p>
  
  <p><a href="https://pdfs.semanticscholar.org/d65b/cb276bc6fc9445a78fe26de76a43e09ccd27.pdf" rel="noreferrer"><strong>This</strong></a> paper suggests that in a segmentation task one can feed the same image multiple times to the network but with a different scale and then aggregate the results. If I understand this approach correctly, it would only work if all the input images have the same aspect ratio. Please correct me if I am wrong.</p>
  
  <p>Another alternative would be to just resize each image to fixed dimensions. I think this was also proposed by the answer to <a href="https://ai.stackexchange.com/q/2403/2444"><strong>this</strong></a> question. However, it is not specified in what way images are resized.</p>
  
  <p>I considered taking the maximum width and height in the dataset and resizing all the images to that fixed size in an attempt to avoid information loss. However, I believe that our network might have difficulties with distorted images as the edges in an image might not be clear. </p>
  
  <ol>
  <li><p>What is possibly the best way to resize your images before feeding them to the network?</p></li>
  <li><p>Is there any other option that I am not aware of for solving the problem of having images of different dimensions?</p></li>
  <li><p>Also, which of these approaches you think is the best taking into account the computational complexity but also the possible loss of performance by the network? </p></li>
  </ol>
  
  <p>I would appreciate if the answers to my questions include some link to a source if there is one.</p>
  
Tags: <neural-networks><convolutional-neural-networks><datasets><image-segmentation><fully-convolutional-networks>
Title: How can I deal with images of variable dimensions when doing image segmentation?
UserDisplayName: MattSt
ThreadText: # How can I deal with images of variable dimensions when doing image segmentation?
  
  (Asked by: username_0 on 2018-05-04)
  
  I'm facing the problem of having images of different dimensions as inputs in a segmentation task. Note that the images do not even have the same aspect ratio.
  
  One common approach that I found in general in deep learning is to crop the images, as it is also suggested [**here**](https://datascience.stackexchange.com/q/16601/10640). However, in my case, I cannot crop the image and keep its center or something similar, since, in segmentation, I want the output to be of the same dimensions as the input.
  
  [**This**](https://pdfs.semanticscholar.org/d65b/cb276bc6fc9445a78fe26de76a43e09ccd27.pdf) paper suggests that in a segmentation task one can feed the same image multiple times to the network but with a different scale and then aggregate the results. If I understand this approach correctly, it would only work if all the input images have the same aspect ratio. Please correct me if I am wrong.
  
  Another alternative would be to just resize each image to fixed dimensions. I think this was also proposed by the answer to [**this**](https://ai.stackexchange.com/q/2403/2444) question. However, it is not specified in what way images are resized.
  
  I considered taking the maximum width and height in the dataset and resizing all the images to that fixed size in an attempt to avoid information loss. However, I believe that our network might have difficulties with distorted images as the edges in an image might not be clear.
  
  1. What is possibly the best way to resize your images before feeding them to the network?
  2. Is there any other option that I am not aware of for solving the problem of having images of different dimensions?
  3. Also, which of these approaches you think is the best taking into account the computational complexity but also the possible loss of performance by the network?
  
  I would appreciate if the answers to my questions include some link to a source if there is one.
  
  **Tags:** neural-networks, convolutional-neural-networks, datasets, image-segmentation, fully-convolutional-networks
  
  ---
  
  ## 5 answers
  
  ### Answer by username_5 on 2019-09-10. Score: 16 | Accepted answer
  
  There are 2 problems you might face.
  
  1. Your neural net (in this case convolutional neural net) cannot physically accept images of different resolutions. This is usually the case if one has fully-connected layers, however, if the network is **fully-convolutional**, then it should be able to accept images of any dimension. Fully-convolutional implies that it doesn't contain fully-connected layers, but only convolutional, max-pooling, and batch normalization layers all of which are invariant to the size of the image.
  
     Exactly this approach was proposed in this ground-breaking paper [Fully Convolutional Networks for Semantic Segmentation](https://arxiv.org/pdf/1411.4038.pdf). Keep in mind that their architecture and training methods might be slightly outdated by now. A similar approach was used in widely used [U-Net: Convolutional Networks for Biomedical Image Segmentation](https://arxiv.org/pdf/1505.04597.pdf), and many other architectures for object detection, pose estimation, and segmentation.
  2. Convolutional neural nets are not scale-invariant. For example, if one trains on the cats of the same size in pixels on images of a fixed resolution, the net would fail on images of smaller or larger sizes of cats. In order to overcome this problem, I know of two methods (might be more in the literature):
  
     1. multi-scale training of images of different sizes in fully-convolutional nets in order to make the model more robust to changes in scale; and
     2. having multi-scale architecture.
  
     A place to start is to look at these two notable papers: [Feature Pyramid Networks for Object Detection](https://arxiv.org/pdf/1612.03144.pdf) and [High-Resolution Representations for Labeling Pixels and Regions](https://arxiv.org/pdf/1904.04514.pdf).
  
  > **Comments:**
  > **username_6**: If images in the dataset are of different sizes, we simply need to resize them into a fixed size (say, 224x224), but to the cost of losing the aspect ratio, right? Then what are the side effects of not keeping the aspect ratio? Because the higher the difference in sizes (250x320, 443x538, 654x 811, 443x 1019), the worse your images are distorted.  
  > **username_5**: Yes, in principle current conv-nets are not truly suited to be aspect-ratio-invariant. In my experience I haven't seen a big problem with resizing images of different aspect ratios to a fixed size but I didn't deal with large differences in aspect ratios within the same dataset (e.g. 1 to 1.75 aspect ratios). If you do have large differences in aspect ratios in the same dataset you might want to try to train with fully-convolutional net and aspect ratio intact, but would probably need to make sure that each batch has images of the same aspect ratio.  
  
  ---
  
  ### Answer by username_1 on 2018-05-06. Score: 2
  
  You could also have a look at the paper [Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition](https://arxiv.org/pdf/1406.4729.pdf) (2015), where the SPP-net is proposed. SSP-net is based on the use of a "spatial pyramid pooling", which eliminates the requirement of having fixed-size inputs.
  
  In the abstract, the authors write
  
  > Existing deep convolutional neural networks (CNNs) require a fixed-size (e.g., 224×224) input image. This requirement is "artificial" and may reduce the recognition accuracy for the images or sub-images of an arbitrary size/scale. In this work, we equip the networks with another pooling strategy, "**spatial pyramid pooling**", to eliminate the above requirement. The new network structure, called **SPP-net**, can generate a fixed-length representation regardless of image size/scale.
  >
  > Pyramid pooling is also robust to object deformations. With these advantages, SPP-net should in general improve all CNN-based **image classification** methods. On the ImageNet 2012 dataset, we demonstrate that SPP-net boosts the accuracy of a variety of CNN architectures despite their different designs. On the Pascal VOC 2007 and Caltech101 datasets, SPP-net achieves state-of-theart classification results using a single full-image representation and no fine-tuning. The power of SPP-net is also significant in **object detection**. Using SPP-net, we compute the feature maps from the entire image only once, and then pool features in arbitrary regions (sub-images) to generate fixed-length representations for training
  > the detectors. This method avoids repeatedly computing the convolutional features. In processing test images, our method is 24-102× faster than the R-CNN method, while achieving better or comparable accuracy on Pascal VOC 2007. In ImageNet Large Scale Visual Recognition Challenge (ILSVRC) 2014, our methods rank #2 in object detection and #3 in image classification among all 38 teams. This manuscript also introduces the improvement made for this competition.
  
  
  ---
  
  ### Answer by username_2 on 2018-05-05. Score: 1
  
  Assuming you have a large dataset, and it's labeled pixel-wise, one hacky way to solve the issue is to preprocess the images to have same dimensions by inserting horizontal and vertical margins according to your desired dimensions, as for labels you add dummy extra output for the margin pixels so when calculating the loss you could mask the margins.
  
  > **Comments:**
  > **username_0**: How does one deal with normalization then in these cases? Do you only normalize the pixels in an image that are not included in its margin I guess?  
  > **username_2**: Yes, because your data generating process is having different sizes so if you include the margins, you will change the data distribution. margins are inserted to group the training samples into batches because your problem needs to output a fixed output vector.  
  
  ---
  
  ### Answer by username_3 on 2019-01-28. Score: 1
  
  As you want to perform image segmentation, you can use [U-Net](https://arxiv.org/pdf/1505.04597.pdf), which does not have fully connected layers, but it is a [fully convolutional network](https://arxiv.org/pdf/1411.4038.pdf), which makes it able to handle inputs of any dimension. You should read the linked papers for more info.
  
  
  ---
  
  ### Answer by username_4 on 2020-04-20. Score: 1
  
  Try resizing the image to the input dimensions of your neural network architecture(keeping it fixed to something like 128*128 in a standard 2D U-net architecture) using nearest neighbor interpolation technique. This is because if you resize your image using any other interpolation, it may result in tampering with the ground truth labels. This is particularly a problem in segmentation. You won't face such a problem when it comes to classification.
  Try the following:
  import cv2 
  resized_image = cv2.resize(original_image, (new_width, new_height), 
                             interpolation=cv2.INTER_NEAREST)
  
  
  ---
  
  