RuntimeError: invalid argument 2: size 'xx' is invalid

Hello,
I encounter an issue with RuntimeError but could not solve it. My code structure looks like

1 for data in data_loader:
2     # data has shape (batch, num, channel, height, width)
3     b, n, c, h, w = data.size()
4     data = data.view(b*n, c, h, w)
5     out = model(data) # out has shape (b*n, feat_dim)

I set batch size to 3, and drop_last=False for data_loader. num is the number of frames, which is 10 for example, so (num, channel, height, width) is a sequence of frames in the same video. Each time the initial data has shape (3, 10, 3, 224, 224) and I reshape it into (30, 3, 224, 224) by line 4 so that it can be processed by a 2d CNN. However, the last batch has the number 2, so data becomes (2, 10, 3, 224, 224). When reshaping the last batch by line 4, the systems returns an error

RuntimeError: invalid argument 2: size '[20 x 3 x 224 x 224]' is invalid for input with 2949120 elements at /pytorch/torch/lib/TH/THStorage.c:41

Any idea about what is wrong and how to fix this issue?

Thanks!

I tested this with sample input of size (4, 5, 3, 224, 224) and it does not error out. The error you’re getting is a little bizarre; it implies that the number of elements in the tensor is less than the new view shape.

What exactly is data.size()?

The exact data.size() is (3, 15, 3, 256, 128), and the last batch is (2, 15, 3, 256, 128). The specific code is

arr = []
for data in data_loader:
    # data has shape (batch, num, channel, height, width)
    data = Variable(data.cuda())
    b, n, c, h, w = data.size()
    data = data.view(b*n, c, h, w)
    out = model(data) # out has shape (b*n, feat_dim)
    out = out.data.cpu()
    out = out.view(b, n, -1)
    out = out.mean(1) # so shape is (b, feat_dim)
    arr.append(out)
arr = torch.cat(arr, 0)

The origin is here.

This issue has been solved. It was a typo mistake.:grinning:

1 Like

Where was the typo? I got a similar error and I can’t find nothing

The typo was in my code, not here. It was like

b, n, c, h, w = data1.size()
data1 = data1.view(b*n, c, h, w)
.
.
.
data2 = data2.view(b*n, c, h, w) # forgot to get size of data2 but used size of data1

lol

1 Like

your post helped me cause I had a typo mistake and got the same result too, lol