首页 > 解决方案 > return_counts=True 不返回正确的计数

问题描述

我正在尝试使用以下代码保存包含标签 [0,1,2] 的掩码:

for img in range(len(t2_list)):   #Using t1_list as all lists are of same size
    print("Now preparing image and masks number: ", img)
      
    temp_image_t2=nib.load(t2_list[img]).get_fdata()
    temp_image_t2=scaler.fit_transform(temp_image_t2.reshape(-1, temp_image_t2.shape[-1])).reshape(temp_image_t2.shape)
   
    
        
    temp_mask=nib.load(mask_list[img]).get_fdata()
    temp_mask=temp_mask.astype(np.uint8)

    print(np.unique(temp_mask))
    val, counts = np.unique(temp_mask, return_counts=True)
    
    if (1 - (counts[0]/counts.sum())) > 0.01:  #At least 1% useful volume with labels that are not 0
      print("Save Me")
      temp_mask= to_categorical(temp_mask, num_classes=2)
      np.save('/content/drive/MyDrive/input_data_3channels/images/image_'+str(img)+'.npy', temp_combined_images)
      np.save('/content/drive/MyDrive/input_data_3channels/masks/mask_'+str(img)+'.npy', temp_mask)
    else:
      print("nope”)

即使数组包含 [0 1 2] 这也不会导致我如何解决这个问题:

正在准备图像和蒙版编号:20 [0 1] 否 现在正在准备图像和蒙版编号:21 [0 1] 否 现在正在准备图像和蒙版编号:22 [0 1] 否 现在正在准备图像和蒙版编号:23 [0 1 ] nope 现在准备图像和蒙版编号:24 [0 1] nope 现在准备图像和蒙版编号:25 [0 1] nope 现在准备图像和蒙版编号:26 [0 1 2] nope 现在准备图像和蒙版编号:27 [0 1] nope 现在准备图像和蒙版编号:28

标签: numpytensorflow3d

解决方案


该问题已通过将 val, counts = np.unique(temp_mask, return_counts=True) 更改为:

val, counts = np.unique((np.unique(temp_mask)), return_counts=True)


推荐阅读