18
$\begingroup$

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. 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 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 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.

$\endgroup$
9
$\begingroup$

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. 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, 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 and High-Resolution Representations for Labeling Pixels and Regions.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – bit_scientist Jun 5 at 1:07
1
$\begingroup$

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.

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – MattSt May 5 '18 at 10:30
  • $\begingroup$ 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. $\endgroup$ – Fadi Bakoura May 5 '18 at 14:57
1
$\begingroup$

As you want to perform image segmentation, you can use U-Net, which does not have fully connected layers, but it is a fully convolutional network, which makes it able to handle inputs of any dimension. You should read the linked papers for more info.

| improve this answer | |
$\endgroup$
1
$\begingroup$

You could also have a look at the paper Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition (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.

| improve this answer | |
$\endgroup$
0
$\begingroup$

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)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.