假设我有一个形状(32, 32, 96)的特征图(即三维数组)
In [573]: feature_map = np.random.randint(low=0, high=255, size=(32, 32, 96))现在,我想将每个功能地图分别可视化。因此,我想提取每个额叶切片(即二维数组的形状(32, 32)),这样就能给出96个这样的特征图。
如何获得这些数组(可能是而不是复制)以提高内存效率?因为这只是为了可视化,所以一个视图就足够了!
发布于 2018-01-26 16:12:28
我还意识到可以利用numpy.dsplit()来实现这样的3D数组,因为我们正试图将其按深度划分。但是,我必须另外使用np.squeeze()来消除第三维空间。此外,根据我的情况需要,它还返回数组的视图。
# splitting it into 96 slices in one-go!
In [659]: np.dsplit(feature_map, feature_map.shape[-1])
In [660]: np.dsplit(feature_map, feature_map.shape[-1])[10].flags
Out[660]:
C_CONTIGUOUS : False
F_CONTIGUOUS : False
OWNDATA : False #<============== NO copy is made
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
In [661]: np.dsplit(feature_map, feature_map.shape[-1])[10].shape
Out[661]: (32, 32, 1)
# getting rid of unitary dimension with `np.squeeze`
In [662]: np.squeeze(np.dsplit(feature_map, feature_map.shape[-1])[10]).shape
Out[662]: (32, 32)
In [663]: np.squeeze(np.dsplit(feature_map, feature_map.shape[-1])[10]).flags
Out[663]:
C_CONTIGUOUS : False
F_CONTIGUOUS : False
OWNDATA : False #<============== NO copy is made
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False发布于 2018-01-26 15:25:30
可以使用np.transpose和切片操作(而不是创建数组的副本):
feature_map = np.random.randint(low=0, high=255, size=(32, 32, 96))
feature_map = np.transpose(feature_map, axes=[2, 0, 1])
for i in range(feature_map.shape[0]):
print(feature_map[i].shape) # a view of original array. shape=(32, 32)..。或者只是切片:
for i in range(feature_map.shape[2]):
print(feature_map[:, :, i].shape) # a view of original array. shape=(32, 32)发布于 2018-01-26 15:28:49
import numpy as np
def do_something(array_slice):
print array_slice
feature_map = np.random.randint(low=0, high=255, size=(3, 3, 9))
# loop over the indices of the last dimension of the array (i.e. 0 to 8)
for level in range(feature_map.shape[2]):
# now take only the 2d-slice of the first two dimensions at the height of 'level'
do_something(feature_map[:,:,level])
# you could also take a slice from another dimension
for level in range(feature_map.shape[1]):
do_something(feature_map[:,level,:])https://stackoverflow.com/questions/48464281
复制相似问题