Hi everyone, I’m using Pytorch for a project that involves OpenGL/Cuda interop and OpenCV (for visualization).
I’m quite new to Pytorch so I might be missing something but I’m having a problem with the stack function.
Basically, I’m rendering an image (640X640X4) with OpenGL and I’m loading it into a tensor on GPU.
I managed to visualize it properly by converting it to a BGRA matrice with OpenCV using data_ptr.
Then I split the image into 100 non-overlapping patches of (64X64X4) with index /slice operations :
torch::Tensor tensors_stacked;
std::vector<torch::Tensor> patches;
for (int i = 0; i < numPatchesY; ++i) {
for (int j = 0; j < numPatchesX; ++j) {
torch::Tensor patch = image_tensor.index({0,
torch::indexing::Slice(),
torch::indexing::Slice(i * patchSize, (i + 1) * patchSize),
torch::indexing::Slice(j * patchSize, (j + 1) * patchSize)}).clone();
//Visualizing the patch tensor here give expected results.
patches.push_back(patch);
}
}
//Then I stack each patch into a higher dimension tensor.
tensors_stacked = torch::stack(patches);
//Visualizing the patch tensor tensors_stacked[0] here gives weird patterns.
My problem is that the patch resulting from the stacking operation doesn’t seem to be the same from the slicing as you can see now :
Visualizing the patch after slicing => (new user restrictions - can only put one image) Everything is fine !
Visualizing the corresponding patch after stacking =>
I’ve checked min/max value and the Dtype of the Input image_tensor / patch and tensor_stacked[0] which are the same.
I’ve tried to change the memory layout (contiguous / channel_first etc but nothing did the trick).
Regarding the strides and sizes :
- My input image/tensor image_tensor has sizes of [1,4,640,640] and strides [1638400,1,2560,4]
- The patch created after the slice operation has sizes of [4,64,64] and strides [4096,64,1]
- Tensor_stacked has sizes of [100,4,64,64] and strides [64,1,4096]
- Finally, Tensor_stacked[0] has sizes [4,64,64] and strides [4096,64,1]
Like I said, after the stacking operation I’m not able to display the patch properly and I don’t understand why this is not working.
I gladly thank you if you have any explanation for what going on here!