3D Images too large, UNet

goofydoge: <p>I have several 3D image datasets. These images are too large to fit on the GPU we currently have (RTX 4090, 24GB).</p>
<p>I have designed and implemented Lightning modules using MONAI’s UNet. The original implementation involved downsampling the images using MONAI’s spacing transform in order to fit on the GPU. I would prefer to train on full resolution images.</p>
<p>I started to look into how to process these images and for the training step, I found that I could use crop transforms to reduce the image size and collect chunks/patches that way. The validation step, from my understanding, shouldn’t have these types of transforms. (Example: RandCropByPosNegLabel is the most common one).</p>
<p>I started to use MONAI’s sliding window inference, which handles the prediction side. However this will return the entire image’s prediction as one tensor. This tensor is too large and has to be pushed to the CPU. Then the loss function and other metrics must be performed on the CPU. This leads to exceptionally slow training times.</p>
<p>I’ve also tried using MONAI’s GridPatchDataset, but this seems like it’s creating the patches in between each training step, and leads to exceptionally slow training times as well. Even slower than the above mention.</p>
<p>I’m unsure if I’m just googling the wrong things, but I can’t seem to find any solutions that help. I’m already down to batch_size=1, precision=16, etc.</p>
<p>My question is: How are people handling such large images for UNet image segmentation?</p>

KFrank: <p>Hi Goofy!</p>
<aside class="quote no-group" data-username="goofydoge" data-post="1" data-topic="214903" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/8c91f0/48.png" class="avatar"> goofydoge:</div>
<blockquote>
<p>How are people handling such large images for UNet image segmentation?</p>
</blockquote>
</aside>
<p>My recommendation would be to follow the “tiling strategy” mentioned in the<br>
<a href="https://arxiv.org/abs/1505.04597" rel="noopener nofollow ugc">original U-Net paper.</a>  Note that for tiling to work cleanly, the U-Net needs to be<br>
designed and used properly.  (It is well worth reading that original paper with care.)</p>
<p>(Tiling means cutting the input image into overlapping tiles, passing them individually<br>
through the U-Net, and then sewing the output tiles back together to construct the<br>
entire output image.  If your U-Net is properly designed, when you do this you get<br>
the same result – up to numerics – as if you had passed the entire input image<br>
through the U-Net in one go.)</p>
<p>Working with 3D naturally makes greater memory demands.  U-Net has a “field of<br>
view” – that is, the size of the region of the input image that affects a given output<br>
pixel.  You need to be able to fit a field of view in memory for tiling to work (without<br>
making complicated and performance-degrading modifications).  I would expect<br>
that you could fit a field-of-view tile for a “typical” 3D U-Net into 24GB.</p>
<p>I’m not aware of any pre-packaged tiling code (although something could be out<br>
there).  Tiling is in principle straightforward, but requires some detail-oriented<br>
coding to get right.</p>
<p>Best.</p>
<p>K. Frank</p>

goofydoge: <p>I certainly wish I had a mentor to ask these types of questions but I don’t. I greatly appreciate your reply.</p>
<p>I shall go through the UNet paper again and look up the tiling mechanics. My work I think calls it chunking or patching. But I understand the jist of what you mean. I’m more worried about the difficulty of being able to rebuild the image for test metrics/predictions and edge artifacts. But I guess it’s time for me to dig into that and learn.</p>
<p>I was certainly hoping for something more in the prepackaged realm, but I’ve already been feeling like this would require me to start modifying packages or create my own.</p>

KFrank: <p>Hi Goofy!</p>
<aside class="quote no-group" data-username="goofydoge" data-post="3" data-topic="214903" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/g/8c91f0/48.png" class="avatar"> goofydoge:</div>
<blockquote>
<p>edge artifacts</p>
</blockquote>
</aside>
<p>If you’re worried about artifacts due to the edges of the tiles (patches), there won’t<br>
be any artifacts if your U-Net is properly designed and you implement tiling correctly.<br>
(Running the entire image through the U-Net all at once will give the same result – up<br>
to possible round-off error – as running the image through tile by tile, assuming your<br>
tiling strategy has been implemented properly, so, by definition, there won’t be any<br>
artifacts.)</p>
<p>If you’re worried about artifacts at the edges of the entire image, yes, that could<br>
happen.  But such artifacts wouldn’t be specific to tiling or the U-Net architecture.</p>
<aside class="quote no-group">
<blockquote>
<p>I was certainly hoping for something more in the prepackaged realm</p>
</blockquote>
</aside>
<p>U-Net has been quite successful and widely used, so I wouldn’t rule out there being<br>
some pre-packaged tiling implementation.  It would probably be worth some effort to<br>
see if you could find one; I just don’t know of any off-hand.</p>
<p>Good luck!</p>
<p>K. Frank</p>