The size of images working with Unet

887574002: <p>Hi</p>
<p>I have a question about Unet. I have a 3D dataset, in which each volume is of size 112x40x40. For my convenient I resized the volumes (using interpolation) to the size 128x128x128 and trained a Unet for segmentation.</p>
<p>Now, for some reasons I want to preserve the original size of volumes, is there any way that I can train Unet with the original dimensions 112x40x40?</p>
<p>My thought is that the spatial dimension of images should be of power 2 when we are working with Unet. Is that correct?</p>
<p>If it is not possible that train Unet with the original size of volumes, is there any other way than interpolation and zero padding that I can resize volumes to size 128x128x128?</p>
<p>Thank you in advance.</p>
<p>Best</p>

ptrblck: <aside class="quote no-group" data-username="887574002" data-post="1" data-topic="132405">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/887574002/48/35835_2.png" class="avatar"> 887574002:</div>
<blockquote>
<p>My thought is that the spatial dimension of images should be of power 2 when we are working with Unet. Is that correct?</p>
</blockquote>
</aside>
<p>It depends on the implementation and how flexible the model architecture is to arbitrary input shapes.<br>
While powers of two are often easier to handle, you could certainly write a more “robust” model accepting more input shapes e.g. by padding/slicing the activations if needed.</p>

887574002: <p>Thank you. Do you know any GitHub repository that has such flexible implementations of Unet ?</p>

ptrblck: <p>No, I don’t know a specific UNet implementation with a focus on variable input shapes.<br>
However, once you’ve found a “good” implementation you could check which shapes would work, which would fail, and adapt the code if needed.<br>
E.g. transposed conv layers give you the option to specify the <code>output_size</code> in case multiple output sizes would be possible and you could of course add padding etc. based on shape conditions.<br>
Generally I see that models are written for specific use cases and not “universal” workloads. E.g. if the author of a model expects to work with input images using a static shape of <code>224x224</code>, I don’t think much work will be spent on making the model flexible for other inputs.</p>

arya47: <p>I have implemented a 2d UNET which retains image dim:</p><aside class="onebox githubfolder" data-onebox-src="https://github.com/aryaman4152/model-implementations-PyTorch/tree/main/UNET">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/aryaman4152/model-implementations-PyTorch/tree/main/UNET" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h3><a href="https://github.com/aryaman4152/model-implementations-PyTorch/tree/main/UNET" target="_blank" rel="noopener nofollow ugc">model-implementations-PyTorch/UNET at main ·...</a></h3>

  <p><a href="https://github.com/aryaman4152/model-implementations-PyTorch/tree/main/UNET" target="_blank" rel="noopener nofollow ugc">main/UNET</a></p>

  <p><span class="label1">A collection of pytorch implementations of different models - model-implementations-PyTorch/UNET at main · aryaman4152/model-implementations-PyTorch</span></p>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>have a look the differences between a standard one and the preserve_dim model. Can be easily converted to 3d by changing layers to conv3d…</p>

887574002: <p>Thank you for your answer. Just one more question, if we use zero padding instead of interpolation to resize the images (from 112 to 128) dose it affect the performance of Unet in terms of Dice score for example ?</p>
<p>In my experiments I just resized the images using zero padding instead of interpolation and I trained the Unet. The dice with zero padding was 0.6807 while with interpolation it was 0.8304. I am wondering if this drop in performance is natural or I am doing something wrong. Couldn’t find anything in net.</p>
<p>By resizing with zero padding, I mean resizing the volumes of size (112x40x40) to (128x128x128). Just one point is that. the dataset that I am working with it is very small and imbalanced. I am trying to detect some signals from noise where in each volume we have less than 0.1 percent signal. Could it be a reason that model dose not work with zero padding?</p>
<p>Thank you.</p>

887574002: <p>Thank you so much.</p>
<p>I would take a look.</p>

ptrblck: <p>The drop in performance sounds reasonable, since your inputs would only contain:</p>
<pre><code class="lang-python">(112*40*40) / (128*128*128) * 100
~ 8.54%
</code></pre>
<p>valid data while the rest of the input values doesn’t contain any information.</p>

887574002: <p>Ok, thank you. So if I use a Unet with the original size of images I mean the size (112x40x40) then I should expect the same performance as zero padding in terms of Dice score, am I right?</p>
<p>Thank you for all your inputs.</p>

887574002: <p>I looked at your Github, I quickly tried to check it on the example that you gave. Pytorch gave me an error about <code>padding =same</code>, it expects an integer or tuple not str. It shouldn’t be like that but I have this error.</p>
<p>By the way, dose your model also support the dimensions that are not equal?</p>
<p>I mean : hight is different from width.</p>

arya47: <p>Should work for dimensions that are not equal, dont really see why it might fail. However i made the model according to dimensions in the paper.</p>
<p>With the error on <code>padding = 'same'</code> not sure why that’s there since conv3d also supports padding=same. Can you share the error message</p>

887574002: <p>I tried the 2d version of your code that I get the error of the <code>padding='same'</code>. I posted the error here.<br>
Can be the version of pytorch be a problem?</p>
<p>`TypeError                                 Traceback (most recent call last)<br>
 in <br>
1 # testing<br>
2 x = torch.randn((1,1,572, 572))<br>
----&gt; 3 pred = model(x)<br>
4 pred.shape</p>
<p>~/anaconda3/envs/vir-env/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, x)<br>
25         # CONTRACTION<br>
26         # down 1<br>
—&gt; 27         x = self.down1(x)<br>
28         skip_connections.append(x)<br>
29         x = self.pool(x)</p>
<p>~/anaconda3/envs/vir-env/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p> in forward(self, x)<br>
10<br>
11     def forward(self, x):<br>
—&gt; 12         return self.process(x)</p>
<p>~/anaconda3/envs/vir-env/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p>~/anaconda3/envs/vir-env/lib/python3.8/site-packages/torch/nn/modules/container.py in forward(self, input)<br>
115     def forward(self, input):<br>
116         for module in self:<br>
 → 117             input = module(input)<br>
118         return input<br>
119</p>
<p>~/anaconda3/envs/vir-env/lib/python3.8/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
725             result = self._slow_forward(*input, **kwargs)<br>
726         else:<br>
 → 727             result = self.forward(*input, **kwargs)<br>
728         for hook in itertools.chain(<br>
729                 _global_forward_hooks.values(),</p>
<p>~/anaconda3/envs/vir-env/lib/python3.8/site-packages/torch/nn/modules/conv.py in forward(self, input)<br>
421<br>
422     def forward(self, input: Tensor) → Tensor:<br>
 → 423         return self._conv_forward(input, self.weight)<br>
424<br>
425 class Conv3d(_ConvNd):</p>
<p>~/anaconda3/envs/vir-env/lib/python3.8/site-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)<br>
417                             weight, self.bias, self.stride,<br>
418                             _pair(0), self.dilation, self.groups)<br>
 → 419         return F.conv2d(input, weight, self.bias, self.stride,<br>
420                         self.padding, self.dilation, self.groups)<br>
421</p>
<p>TypeError: conv2d(): argument ‘padding’ (position 5) must be tuple of ints, not str</p>
<p>`</p>

ptrblck: <p>You might need to update your PyTorch version, as this padding option is relatively new.</p>

887574002: <p>Ok, thank you very much.</p>