Yes with that also same type of image is coming.
I am attaching the output as well as Python code file.

SLice708×616 114 KB

This text will be hidden
import torch
import nibabel as nib
import matplotlib.pyplot as plt
from itkwidgets import view
from IPython.display import display
import ipyvolume as ipv
import torch.nn.functional as F
import itk
tmap_img = nib.load(’/home/nitika/PHD/rough/Image1.nii.gz’)
print(tmap_img.shape)
#plt.imshow(tmap_img.get_fdata()[10])
S = tmap_img.shape[0] # channel dim
W = tmap_img.shape[1] # width
H = tmap_img.shape[2] # height
batch_size = 3
x = torch.randn(batch_size, S, W, H)
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()
view(unfold_shape)
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(batch_size, output_c, output_h, output_w)
import tensorflow as tf

Check for equality
print((patches_orig == x[:, :output_c, :output_h, :output_w]).all())
print(‘HI’, patches_orig.shape)
#view(patches_orig[0])
#view(x[0])
plt.imshow(patches_orig[0, 32,…], cmap=‘gray’)
plt.show()
#tensor(1, dtype=torch.uint8)