Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split PyTorch tensor into overlapping chunks

Given a batch of images of shape (batch, c, h, w), I want to reshape it into (-1, depth, c, h, w) such that the i-th "chunk" of size d contains frames i -> i+d. Basically, using .view(-1, d, c, h, w) would reshape the tensor into d-size chunks where the index of the first image would be a multiple of d, which isnt what I want.

Scalar example:

if the original tensor is something like:

[1,2,3,4,5,6,7,8,9,10,11,12] and d is 2; 

view() would return : [[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]];

however, I want to get:

[[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10],[10,11],[11,12]]

I wrote this function to do so:

def chunk_slicing(data, depth):
    output = []
    for i in range(data.shape[0] - depth+1):
        temp = data[i:i+depth]
        output.append(temp)
    return torch.Tensor(np.array([t.numpy() for t in output]))

However I need a function that is useable as part of a PyTorch model as this function causes this error :

RuntimeError: Can't call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
like image 403
Mohamad Moustafa Avatar asked Oct 19 '25 20:10

Mohamad Moustafa


1 Answers

IIUC, You need torch.Tensor.unfold.

import torch
x = torch.arange(1, 13)
x.unfold(dimension = 0,size = 2, step = 1)

tensor([[ 1,  2],
        [ 2,  3],
        [ 3,  4],
        [ 4,  5],
        [ 5,  6],
        [ 6,  7],
        [ 7,  8],
        [ 8,  9],
        [ 9, 10],
        [10, 11],
        [11, 12]])

Another example with size = 3 and step = 2.

>>> torch.arange(1, 10).unfold(dimension = 0,size = 3, step = 2)

tensor([[1, 2, 3],  # window with size = 3
# step : ---1--2---
        [3, 4, 5],  # 'step = 2' so start from 3
        [5, 6, 7],
        [7, 8, 9]])
like image 142
I'mahdi Avatar answered Oct 22 '25 08:10

I'mahdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!