Image tensor spliting

HARIHK: <p>I have a dataset of 256 medical images. I am trying to classify the health lung vs diseased one. The resolution of images is too big around 5000 * 5000 of max. And not all the images are in the same size. So I planned to split the images into 256*256 pixels and create a bag with the split images and give them one label for the whole bag. then try Multiple instance learning. Can you please help me with this?</p>
<p>thanks</p>

ptrblck: <p>I think <a href="https://discuss.pytorch.org/t/creating-nonoverlapping-patches-from-3d-data-and-reshape-them-back-to-the-image/51210/6">this post</a> might be useful, which explains how to use <code>unfold</code> to create patches of the input and how to reshape them back.<br>
Let me know, if this approach would be applicable for your use case and please let me know, if you get stuck implementing it. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Also, since you are working with medical image data, have a look at <a href="https://github.com/Project-MONAI/MONAI">MONAI</a> and <a href="https://github.com/fepegar/torchio">TorchIO</a>.<br>
I know, that MONAI supports a <a href="https://docs.monai.io/en/latest/highlights.html?highlight=sliding#sliding-window-inference">sliding window inference workflow</a>, but I’m unsure if it’s usable for training or if TorchIO supports it.</p>
<p>CC <a class="mention" href="/u/monai">@MONAI</a>, <a class="mention" href="/u/fepegar">@fepegar</a> for more information <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

HARIHK: <p>thanks for the reply.<br>
kc, kh, kw = 256, 256, 256# kernel size<br>
dc, dh, dw = 256, 256, 256  # stride</p>
<pre><code>        x = F.pad(x, (imgs.size(2) % kw // 2, imgs.size(2) % kw // 2,
                      imgs.size(1) % kh // 2, imgs.size(1) % kh // 2,
                      imgs.size(0) % kc // 2, imgs.size(0) % kc // 2))

        patches = x.unfold(1, kw, dw).unfold(2, kh, dh).unfold(3, kc, dc)

        patches = patches.contiguous().view(-1, kc, kh, kw)
        print(patches.shape)
</code></pre>
<p>img size = torch.Size([1, 3, 3055, 2935])</p>
<p>if I run the above code I am getting this error</p>
<p>RuntimeError: shape ‘[-1, 1, 256, 265]’ is invalid for input of size 45416448</p>

ptrblck: <p>You might need to adapt the padding for your input based on <a href="https://discuss.pytorch.org/t/how-to-extract-smaller-image-patches-3d/16837/36">this post</a> and make sure the output shape is as expected.</p>

fepegar: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="95091">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>Also, since you are working with medical image data, have a look at <a href="https://github.com/Project-MONAI/MONAI" rel="noopener nofollow ugc">MONAI</a> and <a href="https://github.com/fepegar/torchio" rel="noopener nofollow ugc">TorchIO</a>.<br>
I know, that MONAI supports a <a href="https://docs.monai.io/en/latest/highlights.html?highlight=sliding#sliding-window-inference" rel="noopener nofollow ugc">sliding window inference workflow</a>, but I’m unsure if it’s usable for training or if TorchIO supports it.</p>
<p>CC <a class="mention" href="/u/monai">@MONAI</a>, <a class="mention" href="/u/fepegar">@fepegar</a> for more information <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
</blockquote>
</aside>
<p>Thanks for the heads up!</p>
<p>TorchIO does support sliding window inference: see <a href="https://torchio.readthedocs.io/data/patch_based.html" rel="noopener nofollow ugc">Patch-based pipelines</a>.</p>