【发布时间】:2018-04-25 12:35:51
【问题描述】:
假设我有一个数组
[[0 2 1]
[1 0 1]
[2 1 1]]
我想把它转换成张量的形式
[[[1 0 0]
[0 1 0]
[0 0 0]]
[[0 0 1]
[1 0 1]
[0 1 1]]
[[0 1 0]
[0 0 0]
[1 0 0]]]
每个深度层(索引i)是一个二进制掩码,显示i 在输入中出现的位置。
我为此编写了代码,它可以正常工作,但速度太慢,无法使用。我可以用另一个向量化操作替换这个函数中的循环吗?
def im2segmap(im, depth):
tensor = np.zeros((im.shape[0], im.shape[1], num_classes))
for c in range(depth):
rows, cols = np.argwhere(im==c).T
tensor[c, rows, cols] = 1
return tensor
【问题讨论】:
标签: python numpy vectorization