numpy - 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
解决方案
该问题已通过将 val, counts = np.unique(temp_mask, return_counts=True) 更改为:
val, counts = np.unique((np.unique(temp_mask)), return_counts=True)
推荐阅读
- xcode - 如何在 Xcode 12.3 中运行之前更新的 React Native 项目?
- python - 快速移动窗口时小部件消失
- java - Creating and using annotation in java
- swift - 如何链接 Swift Futures
- java - 如何在 Android Material TextInputLayout 中将 SpannableString 设置为 EditText?
- python - 如何在 Tkinter 中构建包含文本、表情符号图标和链接的标签
- java - JDA message.getReactions.size 总是返回 0
- database - 如何从 TFX BulkInferrer 获取数据帧或数据库写入?
- android - 使用 HILT 时将整数传递给 ViewModel
- nginx - Nginx inode 信息泄露