How to do batch filling?


I need to fill images with other image patches during training. Since I’m training with mini-batch, is there any efficient way to do this?

For example, I have a mini-batch of images of size [B, 3, 128, 128]. I also have patches of size [B, 4, 3, 32, 32], where 4 is the number of patches. Besides, I have the bounding box indicating the location of patches of size [B, 4, 4]. How can I fill in the patches in a batch-wise way? You can ignore the overlap between patches.


Could you explain a bit more about how you’ve defined the bounding boxes?
Does each patch have 4 values, corresponding to the upper left x, y and lower right x, y coordinates?

Hi @ptrblck, you’re right. I answer this question on stackoverflow, How to do batch filling in pytorch. But looks like for-loop isn’t very efficient. Any ideas of improvement?

I think the problematic part trying to get rid of the loops would be the batched way of creating the range indexing for batches.
Probably creating the linear indices beforehand and index the result tensor directly would work.
However, how performance critical is this code?
E.g. if you just run it once in your script, I wouldn’t invest a lot of time improving its performance, but focus on other parts of the code.

Thanks guys! Actually I just use for loop suggested by @Zhihao_Wu . Because I find out that my GPU can only afford batch size of 8. So for loop do not decrease the performance.