imageI我正在使用CNN进行体素分类以进行图像分割。我目前正在使用嵌套的for循环来生成用于神经网络的补丁,但是因为我拥有的图像是3维的,所以证明它需要永远。
无论如何,这是可以优化的,我无法看到?
def data_gen(image, labels, patch_size):
"""
:param image: 3 dimensional numpy matrix of image
:param labels: 3 dimensional numpy matrix of mask
:param patch_size: desried dimension of patch size
:return: array of patches and array of labels
"""
Patch_array = np.array([])
Label_array = np.array([])
PS = (patch_size - 1)/2
npad = ((PS, PS), (PS, PS), (PS,PS))
# Pad whole image with zeros to allow for patches at edges
image_pad = np.pad(image, pad_width=npad, mode='constant', constant_values = 0)
# Cycle over all voxels
for x in range(PS, image.shape[0]):
for y in range(PS, image.shape[1]):
for z in range(PS, image.shape[2]):
# Add patch to array
Patch_array = np.append(Patch_array, image_pad[x-PS:x+PS+1,y-PS:y+PS+1,z-PS:z+PS+1])
Label_array = np.append(Label_array, labels[x,y,z])
return Label_array, Patch_array
谢谢!
答案 0 :(得分:1)
有一个Scikit-Image实用功能:
http://scikit-image.org/docs/dev/api/skimage.util.html#view-as-windows
e.g。
from skimage.util.shape import view_as_windows
window_shape = (patch_size, patch_size, patch_size)
patch_array = view_as_windows(A, window_shape)
在幕后,这会使用as_strided
中的numpy.lib.stride_tricks
。