重塑 4D/5D 阵列的技巧或模式,(视频到帧) [英] Tips or patterns for reshaping 4D/5D arrays, (videos to frames)

查看:66
本文介绍了重塑 4D/5D 阵列的技巧或模式,(视频到帧)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难在 numpy/pytorch 中可视化重塑 4D 5D 数组.(我假设两者都以类似的方式重塑,我目前正在使用 pytorch!).

I find it really hard to visualize reshaping 4D 5D arrays in numpy/pytorch. (I assume both reshape in similar patter, I am using pytorch currently!).

假设我有尺寸为 [N x C x D x H x W] 的视频

Like suppose I have videos with dimension [N x C x D x H x W]

(数量视频 x 频道视频 x 帧视频 x 高度视频 x 宽度视频)

(num videos x channels video x frames video x height video x width video)

假设我想将视频重塑为 [N x C x H x W] 的帧,我应该如何进行重塑.

Suppose I want to reshape video into frames as [N x C x H x W], how should I proceed in reshape.

简单地应用 x = x.reshape(N*D, C, H, W) 实际上并没有做到,它给出了错误的元素顺序.

Simple applying x = x.reshape(N*D, C, H, W) doesn't actually do it, it gives wrong order of elements.

你能帮助我了解如何做到这一点,以及你对模式的任何直觉吗?

Can you help me with how to do this, and any slight of intuition of pattern you used?

顺便说一句,如果我有一个视频(即假设我使用 1x3x100x256x256:

On a sidenote, if i have one video (i.e suppose 1x3x100x256x256 I use :

以下代码方法:

x = x.squeeze(0).T.reshape((100,3,256,256))[:,:,None,:,:] 并且它有效

太好了.无法找出超过 1 个视频.

great. Couldnt figure out for more than 1 video.

谢谢!

根据要求:

input = np.random.randn(N,C,D,H,W)
output = np.zeros((N*D,C,H,W))

根据请求,一个基于 for 循环的代码来显示我想要的<代码>对于范围内的 h(N):对于范围(D)中的我:对于范围内的 j(C):对于范围内的 k (H):对于范围内的 l(W):输出[h*D + i,j,k,l] = 输入[h,j,i,k,l]

As per the request, a for loop based code to show what I want for h in range(N): for i in range(D): for j in range(C): for k in range(H): for l in range(W): output[h*D + i,j,k,l] = input[h,j,i,k,l]

推荐答案

简单地交换第二和第三个轴,然后将新的第二个轴(旧的第三个)与第一个进行整形合并 -

Simply swap the second and third axes, and then merge the new second axis (old third one) with the first one with reshaping -

output = input_array.swapaxes(1,2).reshape(N*D,C,H,W)

我们也可以使用 transpose : input_array.transpose(0,2,1,3,4) 来获得相同的交换轴效果.

We can also use transpose : input_array.transpose(0,2,1,3,4) to get the same swapping axes effect.

一般的直观方法请参考在 NumPy 中将 4D 数组重塑为 2D 数组背后的直觉和想法.

For a general intuitive method, please refer to Intuition and idea behind reshaping 4D array to 2D array in NumPy.

这篇关于重塑 4D/5D 阵列的技巧或模式,(视频到帧)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆