Creating nonoverlapping patches from 3D data and reshape them back to the image

banikr: <p>Hello,<br>
I was working with a NN that takes patches of size 32x32x32. The test image sizes are 172x220x156.<br>
How do I get non-overlapping patches and add the output patches to get the whole image together.<br>
I have tried hardcoding with loops and indexing but is there any other way to do so?<br>
Also looked in torch.unfold option too… but not sure how to implement that in my case.</p>

ptrblck: <p>In your last topic, I’ve posted a small example, how to get non-overlapping patches: <a href="https://discuss.pytorch.org/t/creating-3d-dataset-dataloader-with-patches/50861/2">link</a>.</p>
<p>Have a look at <a href="https://discuss.pytorch.org/t/patch-making-does-pytorch-have-anything-to-offer/33850/11">this post</a> to see, how these patches can be transformed into the original input tensor.</p>
<p>Note, that your current input shape it not divisible without a remainder by 32, so you might want to add padding to the input before unfolding.</p>

banikr: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a></p>
<p>I have followed the code for reconstructing the estimated image.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/4/4e3d5830f0b3976b2d616b9dd930b7cd73e04719.png" data-download-href="https://discuss.pytorch.org/uploads/default/4e3d5830f0b3976b2d616b9dd930b7cd73e04719" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/4/4e3d5830f0b3976b2d616b9dd930b7cd73e04719_2_375x375.png" alt="image" data-base62-sha1="ba8FHKRi3NKyeJXziNq5z4iiK0h" width="375" height="375" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/4/4e3d5830f0b3976b2d616b9dd930b7cd73e04719_2_375x375.png, https://discuss.pytorch.org/uploads/default/optimized/2X/4/4e3d5830f0b3976b2d616b9dd930b7cd73e04719_2_562x562.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/4/4e3d5830f0b3976b2d616b9dd930b7cd73e04719_2_750x750.png 2x" data-dominant-color="292929"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1372×1368 98.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>As you can see, some parts are quite fine whereas some are pretty bad and mostly the patches can be visible like mosaics.<br>
Is there a way might make the patches smoother to form the image.</p>
<p>TIA</p>

ptrblck: <p>Did you run the last equality check in the linked code and if so, was it successful?</p>

banikr: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
How do I run the equality test in my case?<br>
The output from network is of size <code>output = np.array(output) #(12,10,32,32,32)</code><br>
and the final image size after the code is <code>(1,160,192,128)</code></p>
<p>The example you gave takes an image, breaks it down and then reconstructs it back.<br>
In my case the image shape changes in the <code>test_loader</code> from <code>(1,172,220,156)</code> to <code>(10,1,32,32,32)</code></p>
<p>Please see the codes below for detail(I mentioned the shapes as comments)</p>
<pre><code class="lang-auto">    for i in filenames_test: #filenames_test consists a single 172,220,156 shaped image
        path_mr = os.path.join(dir_test, 'MR', i)
        MR = nib.load(path_mr).get_data()
        path_ct = os.path.join(dir_test, 'CT', i)
        CT = nib.load(path_ct).get_data() 
        X = get_nonoverlapping_patch(MR,patch_size) # torch.Size([120,32,32,32])
        y = get_nonoverlapping_patch(CT,patch_size)
        #get_nonoverlapping_patch creates the patches, before the reshape section
        patches_MR.append(X)
        patches_CT.append(y)

    print(i,X.shape,y.shape) #(120, 32, 32, 32)

    test_dataset = Dataset(patches_MR, patches_CT)
    test_loader = data.DataLoader(test_dataset, batch_size=10, shuffle=False)

    for i,sample in enumerate(test_loader):
        with torch.no_grad():
            mr = sample[0].float().to(device)
            ct = sample[1].float().to(device)
            pred = model(mr)
            CT_synth = pred.detach().cpu().numpy().squeeze(1)
            CT_sq = ct.detach().cpu().numpy().squeeze(1)
            output.append(CT_synth)

    """Now recontruct the output"""
    output = np.array(output) #(12,10,32,32,32)
    output = output.reshape(-1,32,32,32) #(120,32,32,32)

    output = torch.tensor(output)
    output = output.unsqueeze(0) #(1,120,32,32,32)
    unfold_shape = [1, 172 // 32, 220 // 32, 156 // 32, 32, 32, 32]

    output_c = unfold_shape[1] * unfold_shape[4] #corresponding number of blocks x patch dimension
    output_h = unfold_shape[2] * unfold_shape[5]
    output_w = unfold_shape[3] * unfold_shape[6]

    Orig = output.view(unfold_shape)  # torch.Size([1, 5, 6, 4, 32, 32, 32])
    Orig1 = Orig.permute(0, 1, 4, 2, 5, 3, 6).contiguous() #torch.Size([1, 5, 32, 6, 32, 4, 32])
    Orig2 = Orig1.view(1, output_c, output_h, output_w) #torch.Size([1, 160, 192, 128])

    Orig3 = Orig2.squeeze(0) #torch.Size([160, 192, 128])
    Orig4 = np.array(Orig3) #numpy.ndarray
</code></pre>

ptrblck: <p>The error might come from the fact, that you cannot divide the input into <code>32</code>-sized patches.<br>
Try to pad the input before unfolding it:</p>
<pre><code class="lang-python">x = torch.randn(1, 172, 220, 156)
kc, kh, kw = 32, 32, 32  # kernel size
dc, dh, dw = 32, 32, 32  # stride
# Pad to multiples of 32
x = F.pad(x, (x.size(2)%kw // 2, x.size(2)%kw // 2,
              x.size(1)%kh // 2, x.size(1)%kh // 2,
              x.size(0)%kc // 2, x.size(0)%kc // 2))

patches = x.unfold(1, kc, dc).unfold(2, kh, dh).unfold(3, kw, dw)
unfold_shape = patches.size()
patches = patches.contiguous().view(-1, kc, kh, kw)
print(patches.shape)

# Reshape back
patches_orig = patches.view(unfold_shape)
output_c = unfold_shape[1] * unfold_shape[4]
output_h = unfold_shape[2] * unfold_shape[5]
output_w = unfold_shape[3] * unfold_shape[6]
patches_orig = patches_orig.permute(0, 1, 4, 2, 5, 3, 6).contiguous()
patches_orig = patches_orig.view(1, output_c, output_h, output_w)

# Check for equality
print((patches_orig == x[:, :output_c, :output_h, :output_w]).all())
&gt; tensor(1, dtype=torch.uint8)
</code></pre>

banikr: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a><br>
I performed the equality test on a CT image by padding, creating non-overlapping patches and reconstructing it back. The patches were correctly located.</p>
<p>I did the following:</p>
<ul>
<li>Padded the MR and CT images to the size of multiples of 32.</li>
<li>Took the non-overlapping patches and created the test dataset.</li>
<li>From the output reconstructed the image by connecting the patches.</li>
</ul>
<p>But the same mismatches are found:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/b/bec09c2cc85583ecd24c0c7303b90d9d26181744.png" data-download-href="https://discuss.pytorch.org/uploads/default/bec09c2cc85583ecd24c0c7303b90d9d26181744" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/b/bec09c2cc85583ecd24c0c7303b90d9d26181744_2_381x375.png" alt="image" data-base62-sha1="rdtnQ8LrJXsIgKqg8cpD8mUW3nS" width="381" height="375" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/b/bec09c2cc85583ecd24c0c7303b90d9d26181744_2_381x375.png, https://discuss.pytorch.org/uploads/default/optimized/2X/b/bec09c2cc85583ecd24c0c7303b90d9d26181744_2_571x562.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/b/bec09c2cc85583ecd24c0c7303b90d9d26181744.png 2x" data-dominant-color="444445"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">661×649 75 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>What might be the reason, am I not training the network enough? or shall I try overlapping patches and do intensity averaging in overlapping regions?</p>

ptrblck: <p>Oh, wait. The posted image is the result of your model, not the reconstructed pictures using the patches?<br>
If so, I misunderstood this particular issue.</p>
<aside class="quote no-group" data-username="banikr" data-post="7" data-topic="51210">
<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/banikr/48/54353_2.png" class="avatar"> banikr:</div>
<blockquote>
<p>or shall I try overlapping patches and do intensity averaging in overlapping regions?</p>
</blockquote>
</aside>
<p>That would be a reasonable approach. The original UNet paper also used overlapping windows with good segmentation results. This is of course different to your approach, but I would assume it should smooth the image better.</p>

banikr: <p>Yes, these are the output of the network.<br>
Sorry if I was not clear to you before. If I have to use overlapping patches and intensity averaging, do you have any source of such code?</p>

ptrblck: <p>Unfortunately not. While the original UNet paper used a sliding window approach, they have created segmentation masks, which is quite different to your MR output.</p>

goenka04: <p><a class="mention" href="/u/ptrblck">@ptrblck</a> : Hi Sir,</p>
<p>Can you please share the code snipped for extracting random patches from 3D MRI Images using unfold option. Also, how can we view this patches and construct an image back from these patches.<br>
It would be very helpful. I have seen multiple examples but i am not getting exact solution like while viewing everything becomes noisy thats why could you please help.</p>
<p>Thanks<br>
Nitika</p>

banikr: <p>Hi I think the code you are looking for is shared above here: <a href="https://discuss.pytorch.org/t/creating-nonoverlapping-patches-from-3d-data-and-reshape-them-back-to-the-image/51210/6" class="inline-onebox">Creating nonoverlapping patches from 3D data and reshape them back to the image - #6 by ptrblck</a> . It has both unfold and reshape options.<br>
To view the patches in a software use ITKsnap or FSL. Or if you want to visualize in notebook, use itkwidgets and there should be a bunch of other visualization open-sourced software. If the image is same then you shouldn’t get any noisy/tiled reshape as I had. But I was using output of the network with non-overlap training anyway.</p>

goenka04: <aside class="quote no-group" data-username="banikr" data-post="12" data-topic="51210">
<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/banikr/48/54353_2.png" class="avatar"> banikr:</div>
<blockquote>
<p>itkwidgets</p>
</blockquote>
</aside>
<p>Dear Sir,</p>
<p>This is my code<br>
import torch<br>
import nibabel as nib<br>
import matplotlib.pyplot as plt</p>
<p>tmap_img = nib.load(‘/home/nitika/PHD/rough/Image1.nii.gz’)<br>
print(tmap_img.shape)<br>
plt.imshow(tmap_img.get_fdata()[10])<br>
S = tmap_img.shape[0] # channel dim<br>
W = tmap_img.shape[1] # width<br>
H = tmap_img.shape[2] # height<br>
batch_size = 1</p>
<p>x = torch.randn(batch_size, S, W, H)<br>
kc, kh, kw = 32, 32, 32  # kernel size<br>
dc, dh, dw = 32, 32, 32  # stride</p>
<h1><a name="pad-to-multiples-of-32-1" class="anchor" href="#pad-to-multiples-of-32-1"></a>Pad to multiples of 32</h1>
<p>x = F.pad(x, (x.size(2)%kw // 2, x.size(2)%kw // 2,<br>
x.size(1)%kh // 2, x.size(1)%kh // 2,<br>
x.size(0)%kc // 2, x.size(0)%kc // 2))</p>
<p>patches = x.unfold(1, kc, dc).unfold(2, kh, dh).unfold(3, kw, dw)<br>
unfold_shape = patches.size()<br>
patches = patches.contiguous().view(-1, kc, kh, kw)<br>
print(patches.shape)</p>
<h1><a name="reshape-back-2" class="anchor" href="#reshape-back-2"></a>Reshape back</h1>
<p>patches_orig = patches.view(unfold_shape)<br>
output_c = unfold_shape[1] * unfold_shape[4]<br>
output_h = unfold_shape[2] * unfold_shape[5]<br>
output_w = unfold_shape[3] * unfold_shape[6]<br>
patches_orig = patches_orig.permute(0, 1, 4, 2, 5, 3, 6).contiguous()<br>
patches_orig = patches_orig.view(1, output_c, output_h, output_w)<br>
import tensorflow as tf</p>
<h1><a name="check-for-equality-3" class="anchor" href="#check-for-equality-3"></a>Check for equality</h1>
<p>print((patches_orig == x[:, :output_c, :output_h, :output_w]).all())</p>
<p>print(‘HI’, patches_orig.shape)</p>
<p>#============PLease help here how to view this original image reconstructed in python notebook====<br>
<span class="hashtag-raw">#plt</span>.imshow(patches_orig) ==&gt; THrows an error ypeError: Invalid shape (1, 64, 128, 96) for image data</p>
<p><span class="hashtag-raw">#tensor</span>(1, dtype=torch.uint8)</p>
<p>and this is output<br>
(91, 109, 91)<br>
torch.Size([24, 32, 32, 32])<br>
tensor(True)<br>
HI torch.Size([1, 64, 128, 96])<br>
also one image is displayed</p>
<p>Can you please help in last section of verifying original image<br>
#============PLease help here how to view this original image reconstructed in python notebook====<br>
<span class="hashtag-raw">#plt</span>.imshow(patches_orig) ==&gt; THrows an error ypeError: Invalid shape (1, 64, 128, 96) for image data</p>
<p>Also how can we send these patches directly to 3D -CNN.</p>
<p>It would be very helpful if you can guide please.</p>

banikr: <aside class="quote no-group" data-username="goenka04" data-post="13" data-topic="51210">
<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/goenka04/48/33984_2.png" class="avatar"> goenka04:</div>
<blockquote>
<p><span class="hashtag-raw">#plt</span>.imshow(patches_orig) ==&gt; THrows an error ypeError: Invalid shape (1, 64, 128, 96)</p>
</blockquote>
</aside>
<p>You can’t visualize more than 3 channels by <code>matplotlib imshow</code> here channel is 64. But you can visualize 2D slices to check the image.</p>
<pre><code class="lang-auto">plt.imshow(patches_orig[0, 32,...], cmap='gray') 
plt.show()
</code></pre>
<p>Or if you are using notebook IDE, follow <a href="https://github.com/jpambrun/itk-jupyter-widgets" rel="noopener nofollow ugc">this tutorial</a> to visualize 3D images.<br>
After that, you have to create <code>Dataset</code> and <code>DataLoader</code> to make the images iterable, convert to tensor, and other augmentations/transformations to make it as following structure:<br>
<code>Batch_size, modality, H, W, D</code><br>
If you are using only MR or T1 the modality should be 1.</p>
<p>Check out PyTorch and other tutorials for these basic things.</p>
<p>Sending the Dataloader sample patches to 3D CNN depends on what kind of CNN or network architecture you will be using and also what your application is.</p>
<p>For example, in the case of 3D UNet, the image sizes have to be divisible by 2 and have to be 4/5 magnitude multiples of 2 considering maxpool operation.<br>
For example, in 2D UNet <code>224, 224</code> is a preferred dimension since both dimensions are divisible by 2: 224&gt;112&gt;64&gt;32&gt;16 …<br>
Once you have the network chosen, you can easily feed the images as following:</p>
<pre><code class="lang-auto">input, target = next(iter(Dataloader)) # taking one sample
model = Chosen_CNN(**args)
output = model(input)
</code></pre>
<p>Also, I didn’t mention loss criteria, optimizers, and metric stuff that comes into the broad context.</p>

goenka04: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/a/3/a3e5b24c9b81e90eac95e032a1fc1dda76b2ca89.png" data-download-href="https://discuss.pytorch.org/uploads/default/a3e5b24c9b81e90eac95e032a1fc1dda76b2ca89" title="Image 1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/a/3/a3e5b24c9b81e90eac95e032a1fc1dda76b2ca89_2_580x499.png" alt="Image 1" data-base62-sha1="nnTWUhoGoXmfOnAEmIwXxOZpZgJ" width="580" height="499" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/a/3/a3e5b24c9b81e90eac95e032a1fc1dda76b2ca89_2_580x499.png, https://discuss.pytorch.org/uploads/default/optimized/3X/a/3/a3e5b24c9b81e90eac95e032a1fc1dda76b2ca89_2_870x748.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/a/3/a3e5b24c9b81e90eac95e032a1fc1dda76b2ca89.png 2x" data-dominant-color="B2C8C5"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Image 1</span><span class="informations">962×829 413 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Sir… after using itkwidgets view method i am getting this image but it is not MRI Image.<br>
Both view(patches_orig[0]) and view(x[0]) returns the same image.</p>
<p>Can you please guide on this.</p>
<p>Also i have doubt that for every MRI Image we are just passing its dimensions in unfold function, how will it make difference for multiple MRI Image and multiple classes.</p>

banikr: <p>Can you share the sample data you are visualizing in ITKwidgets. I am not an expert in ITKwidgets but I can visualize in other open source software and provide you feedback.</p>
<p>Have you tried plotting a single plane/slice as I had mentioned?</p>
<aside class="quote no-group" data-username="banikr" data-post="14" data-topic="51210">
<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/banikr/48/54353_2.png" class="avatar"> banikr:</div>
<blockquote>
<pre><code class="lang-auto">plt.imshow(patches_orig[0, 32,...], cmap='gray') 
plt.show()
</code></pre>
</blockquote>
</aside>

goenka04: <p>Yes with that also same type of image is coming.<br>
I am attaching the output as well as Python code file.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/2/9256e984ada4c7fb83c3a03e8f4399b09d78ab88.png" data-download-href="https://discuss.pytorch.org/uploads/default/9256e984ada4c7fb83c3a03e8f4399b09d78ab88" title="SLice"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/2/9256e984ada4c7fb83c3a03e8f4399b09d78ab88_2_574x500.png" alt="SLice" data-base62-sha1="kSzUZiUGYVoQuJ9ieN3PPHgWl0A" width="574" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/2/9256e984ada4c7fb83c3a03e8f4399b09d78ab88_2_574x500.png, https://discuss.pytorch.org/uploads/default/original/3X/9/2/9256e984ada4c7fb83c3a03e8f4399b09d78ab88.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/2/9256e984ada4c7fb83c3a03e8f4399b09d78ab88.png 2x" data-dominant-color="EAEAEA"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">SLice</span><span class="informations">708×616 114 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
This text will be hidden</p>
<p>import torch<br>
import nibabel as nib<br>
import matplotlib.pyplot as plt<br>
from itkwidgets import view<br>
from IPython.display import display<br>
import ipyvolume as ipv<br>
import torch.nn.functional as F<br>
import itk</p>
<p>tmap_img = nib.load(‘/home/nitika/PHD/rough/Image1.nii.gz’)<br>
print(tmap_img.shape)<br>
<span class="hashtag-raw">#plt</span>.imshow(tmap_img.get_fdata()[10])</p>
<p>S = tmap_img.shape[0] # channel dim<br>
W = tmap_img.shape[1] # width<br>
H = tmap_img.shape[2] # height<br>
batch_size = 3</p>
<p>x = torch.randn(batch_size, S, W, H)<br>
kc, kh, kw = 32, 32, 32  # kernel size<br>
dc, dh, dw = 32, 32, 32  # stride</p>
<h1><a name="pad-to-multiples-of-32-1" class="anchor" href="#pad-to-multiples-of-32-1"></a>Pad to multiples of 32</h1>
<p>x = F.pad(x, (x.size(2)%kw // 2, x.size(2)%kw // 2,<br>
x.size(1)%kh // 2, x.size(1)%kh // 2,<br>
x.size(0)%kc // 2, x.size(0)%kc // 2))</p>
<p>patches = x.unfold(1, kc, dc).unfold(2, kh, dh).unfold(3, kw, dw)<br>
unfold_shape = patches.size()<br>
view(unfold_shape)<br>
patches = patches.contiguous().view(-1, kc, kh, kw)<br>
print(patches.shape)</p>
<h1><a name="reshape-back-2" class="anchor" href="#reshape-back-2"></a>Reshape back</h1>
<p>patches_orig = patches.view(unfold_shape)<br>
output_c = unfold_shape[1] * unfold_shape[4]<br>
output_h = unfold_shape[2] * unfold_shape[5]<br>
output_w = unfold_shape[3] * unfold_shape[6]<br>
patches_orig = patches_orig.permute(0, 1, 4, 2, 5, 3, 6).contiguous()<br>
patches_orig = patches_orig.view(batch_size, output_c, output_h, output_w)<br>
import tensorflow as tf</p>
<h1><a name="check-for-equality-3" class="anchor" href="#check-for-equality-3"></a>Check for equality</h1>
<p>print((patches_orig == x[:, :output_c, :output_h, :output_w]).all())</p>
<p>print(‘HI’, patches_orig.shape)<br>
<span class="hashtag-raw">#view</span>(patches_orig[0])<br>
<span class="hashtag-raw">#view</span>(x[0])<br>
plt.imshow(patches_orig[0, 32,…], cmap=‘gray’)<br>
plt.show()<br>
<span class="hashtag-raw">#tensor</span>(1, dtype=torch.uint8)</p>

banikr: <p>Hi, check the notebook here: <a href="https://github.com/ranabanik/DebugTorch/blob/master/Codes/Create_3D_patches_unfold.ipynb" class="inline-onebox" rel="noopener nofollow ugc">DebugTorch/Create_3D_patches_unfold.ipynb at master · ranabanik/DebugTorch · GitHub</a></p>
<p>It is nothing new from what ptrblck suggested, I just tried to organize codes and visualize.</p>

goenka04: <p>Hi Sir… Most of the code ran properly except the last part.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/6/f61a5a467e392259d781eb9b744a30449d41bfac.png" data-download-href="https://discuss.pytorch.org/uploads/default/f61a5a467e392259d781eb9b744a30449d41bfac" title="patch1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f61a5a467e392259d781eb9b744a30449d41bfac_2_688x500.png" alt="patch1" data-base62-sha1="z77UUMsmxPpOUIV5hs6lL4ilCIQ" width="688" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f61a5a467e392259d781eb9b744a30449d41bfac_2_688x500.png, https://discuss.pytorch.org/uploads/default/original/3X/f/6/f61a5a467e392259d781eb9b744a30449d41bfac.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/f/6/f61a5a467e392259d781eb9b744a30449d41bfac.png 2x" data-dominant-color="EFF0F0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">patch1</span><span class="informations">927×673 121 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Can you please tell whats the importance of this part : 0,0+2:96-3,0+9:128-10,0+2:96-3]<br>
originalMR = np.array(patches_orig[0,0+2:96-3,0+9:128-10,0+2:96-3])<br>
originalMR = np.transpose(originalMR, ())<br>
print(originalMR.shape, type(originalMR), originalMR.dtype)</p>
<p>since its returning error and not matching with original shape.</p>

banikr: <aside class="quote no-group" data-username="goenka04" data-post="19" data-topic="51210">
<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/goenka04/48/33984_2.png" class="avatar"> goenka04:</div>
<blockquote>
<p>Can you please tell whats the importance of this part : 0,0+2:96-3,0+9:128-10,0+2:96-3]</p>
</blockquote>
</aside>
<p>To crop out the original size MR from padded dimensions.</p>
<aside class="quote no-group" data-username="goenka04" data-post="19" data-topic="51210">
<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/goenka04/48/33984_2.png" class="avatar"> goenka04:</div>
<blockquote>
<p>originalMR = np.transpose(originalMR, ())</p>
</blockquote>
</aside>
<p>Comment out this line any transpose operation in the code. Should be fine.<br>
Or check the git link, I have updated.</p>