我有一个数组preds
,其值是0,1 or 2
。
preds
的形状和值如下所示:
torch.Size([1, 256, 256])
tensor([[[1, 1, 0, ..., 1, 1, 2],
[1, 0, 1, ..., 1, 1, 1],
[0, 0, 0, ..., 1, 1, 1],
...,
[0, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1],
[1, 1, 1, ..., 1, 1, 1]]])
我只关心第二维和第三维(256 x 256
)。我想使用matplotlib
创建一个遮罩,分别绘制0
,1
和2
。基本上,我想可视化图像中拥有0
的所有位置,拥有1
的位置以及拥有2
的位置的位置。我要塞巴蒂的阴谋。
我在想类似的东西。例如1
类:
plt.show
示例:
zero = (preds==0).nonzero() #index of all values which have 0
nonzero = (preds!=0).nonzero() # index of all value which are not 0
# assign 1 to all zero
# assign 0 to all nonzero
# join the above two array
# plot the array
我只是不知道如何在我的伪代码中添加这两个数组(nonzero
和zero
)。
还有更多的Python方式吗?我不想遍历所有索引值,然后为它们分配值。
答案 0 :(得分:0)
我可以使用np.where
来做评论中建议的人。看起来像这样:
#preds in the matrix where I want the 0 and 1s
preds_clear = np.where(preds==0, 1, 0)
plt.imshow(preds_clear[0,:,:])