简体   繁体   English

如何将3D数组转换为argmax的2D数组?

[英]How can I convert a 3D array into a 2D array of argmax?

I have an Numpy array of size (W, H, C), where 'C' is a number of classes for a semantic segmentation task. 我有一个大小为(W,H,C)的Numpy数组,其中“ C”是语义分割任务的许多类。 What I need is a Numpy array of size (H, W), where each element is the index of the class that is appropriate for that pixel. 我需要的是一个大小为(H,W)的Numpy数组,其中每个元素都是适合该像素的类的索引。

I've found a way to do it that runs VERY slowly. 我找到了一种运行速度非常慢的方法。

masks = {list of 2d binary masks}
output_mask = np.zeros(width * height)
output_mask = output_mask.reshape(width, height)

for i in range(width):
    for j in range(height):
        class_id = 0
        for mask in masks:
            class_id += 1
            if mask[i, j] == 1:
                output_mask[i, j] = class_id

I was hoping there might be a better way. 我希望有更好的方法。 Can anyone help me? 谁能帮我?

import numpy as np

arr = np.random.rand(10, 10, 3)
max_val = np.argmax(arr, axis=-1)

print(max_val.shape)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM