【问题标题】:return_counts=True not returning right countreturn_counts=True 不返回正确的计数
【发布时间】:2021-12-25 13:03:01
【问题描述】:

我正在尝试使用代码保存包含标签 [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] 没有 现在准备图像和蒙版数量:24 [0 1] 没有 现在准备图像和蒙版数量:25 [0 1] 没有 现在准备图像和蒙版数量:26 [0 1 2] 没有 现在准备图像和蒙版数量:27 [0 1] 没有 现在准备图像和蒙版编号:28

【问题讨论】:

    标签: numpy tensorflow 3d


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-20
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      相关资源
      最近更新 更多