图像分割指标python代码
时间: 2023-11-12 18:59:02 浏览: 206
好的,以下是常用的图像分割指标的 Python 代码实现:
1. Mean Intersection over Union (mIoU)
```python
import numpy as np
def mean_iou(y_true, y_pred):
num_classes = y_pred.shape[-1]
iou = []
for class_id in range(num_classes):
y_true_class = y_true[..., class_id]
y_pred_class = y_pred[..., class_id]
intersection = np.logical_and(y_true_class, y_pred_class).sum()
union = np.logical_or(y_true_class, y_pred_class).sum()
if union == 0:
iou.append(0)
else:
iou.append(intersection / union)
return np.mean(iou)
```
2. Dice coefficient
```python
def dice_coef(y_true, y_pred, smooth=1):
intersection = np.sum(y_true * y_pred)
return (2. * intersection + smooth) / (np.sum(y_true) + np.sum(y_pred) + smooth)
```
3. F1 score
```python
def f1_score(y_true, y_pred):
tp = np.sum(y_true * y_pred)
fp = np.sum((1 - y_true) * y_pred)
fn = np.sum(y_true * (1 - y_pred))
precision = tp / (tp + fp)
recall = tp / (tp + fn)
return 2 * ((precision * recall) / (precision + recall))
```
阅读全文