Hi, I’m trying to crop a section of a 4 component tensor [batch, channel, height, weight] that was originally a numpy image. I am using numpy-like indexing, here’s the code:
# img_mod is a pytorch tensor that was a numpy array
b = img_mod.shape[0]
c = img_mod.shape[1]
h = img_mod.shape[2]
w = img_mod.shape[3]
# Trying to crop the first quadrant
img_mod_frag = img_mod[:, :, 0:h//2, 0:w//2]
# Reshape the components to make the tensors displayable
# Display img_mod
plt.imshow(img_mod.reshape(h, w, c))
plt.title("img_mod")
plt.show()
# Display img_mod_frag
plt.imshow(img_mod_frag.reshape(h//2, w//2, c))
plt.title("img_mod_frag")
plt.show()
My intention is to crop the first quadrant of the image but these are the results:
The resulting image is ‘mixed’, I don’t know if this is the correct behaviour of tensors. Then, I would like to process the fragment in a simple CNN, but even if I crop the image properly transforming it to numpy and indexing It, when I process It with my model() function It ‘mixes’ the fragment too. Like the img_mod_frag case.
My question is if this is the correct way to crop propperly a tensor quadrant. If it’s not, What’s the correct way? Am I doing something wrong? or should I train my model with these ‘mixed’ fragments?
Thanks.