当前位置:网站首页>[paddleseg source code reading] paddleseg calculates Miou
[paddleseg source code reading] paddleseg calculates Miou
2022-07-04 03:37:00 【Master Fuwen】
Take a look Paddleseg/utils/metrics.py Calculation mIoU The process of
calculate_area Function is used to calculate the intersecting region ,
Incoming pred and label The shape is as follows (1, H, W):
( Category 0 To 4,0 For the background class )
[[[0 2 3 4 1 0 0]
[3 2 3 4 1 4 0]
[3 2 4 4 1 0 4]
[3 2 3 4 1 4 1]
[3 2 4 4 1 0 0]]]
def calculate_area(pred, label, num_classes, ignore_index=255):
""" Calculate intersect, prediction and label area This function is used to calculate the intersection 、prediction and label Number of pixels Args: pred (Tensor): The prediction by model. label (Tensor): The ground truth of image. num_classes (int): The unique number of target classes. ignore_index (int): Specifies a target value that is ignored. Default: 255. Returns: Tensor: The intersection area of prediction and the ground on all class. Tensor: The prediction area on all class. Tensor: The ground truth area on all class """
# If pred.shape == (1, 1, 224, 224) Then remove the first dimension
if len(pred.shape) == 4:
pred = paddle.squeeze(pred, axis=1)
if len(label.shape) == 4:
label = paddle.squeeze(label, axis=1)
if not pred.shape == label.shape:
raise ValueError('Shape of `pred` and `label should be equal, '
'but there are {} and {}.'.format(pred.shape,
label.shape))
# This step up to get pred.shape == label.shape == (1, 224, 224)
pred_area = []
label_area = []
intersect_area = []
mask = label != ignore_index # The mask Is used to ignore_index Ignore
for i in range(num_classes): # Start iterating over each class
# take pred Middle is the first i Take out the position of the class
pred_i = paddle.logical_and(pred == i, mask)
# take label pass the civil examinations i Take out the position of the class
label_i = label == i
# Doing and operation of both , Take them out in the same position
intersect_i = paddle.logical_and(pred_i, label_i)
# bool -> int32 After the sum , Send the corresponding list
pred_area.append(paddle.sum(paddle.cast(pred_i, "int32")))
label_area.append(paddle.sum(paddle.cast(label_i, "int32")))
intersect_area.append(paddle.sum(paddle.cast(intersect_i, "int32")))
# Will be more than list Turn into Paddle Tensor
pred_area = paddle.concat(pred_area)
label_area = paddle.concat(label_area)
intersect_area = paddle.concat(intersect_area)
return intersect_area, pred_area, label_area
A print intersect_area, pred_area, label_area The values of the three variables :
>>> intersect_area
[ 97721 147687]
>>> pred_area
[ 99482 151398]
>>> label_area
[101432 149448]
And then call :
metrics_input = (intersect_area, pred_area, label_area)
class_iou, miou = metrics.mean_iou(*metrics_input)
def mean_iou(intersect_area, pred_area, label_area):
""" Calculate iou. Calculation IoU Args: intersect_area (Tensor): The intersection area of prediction and ground truth on all classes. pred_area (Tensor): The prediction area on all classes. label_area (Tensor): The ground truth area on all classes. Returns: np.ndarray: iou on all classes. float: mean iou of all classes. """
# use .numpy() Turn into np.ndarray
intersect_area = intersect_area.numpy()
pred_area = pred_area.numpy()
label_area = label_area.numpy()
# Computational Union
union = pred_area + label_area - intersect_area
class_iou = []
for i in range(len(intersect_area)):
if union[i] == 0:
# If there is no such pixel , Then Union
iou = 0
else:
# Calculate the ratio of intersection and union
iou = intersect_area[i] / union[i]
class_iou.append(iou)
# Calculate the mean
miou = np.mean(class_iou)
return np.array(class_iou), miou
边栏推荐
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- CSP drawing
- Add IDM to Google browser
- [development team follows] API specification
- A review of reverse reinforcement learning at Virginia Tech (VT)
- Es network layer
- This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
- what does ctrl + d do?
- Add token validation in swagger
- Katalon框架测试web(二十一)获取元素属性断言
猜你喜欢

How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!

This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t

SQL語句加强練習(MySQL8.0為例)

Want to do something in production? Then try these redis commands

MySQL one master multiple slaves + linear replication

SQL injection (1) -- determine whether there are SQL injection vulnerabilities

1day vulnerability pushback skills practice (3)

2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。

渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密

Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
随机推荐
Zigzag scan
PHP database connection succeeded, but data cannot be inserted
Leecode 122. Zuijia timing of buying and selling stocks ②
Future源碼一觀-JUC系列
The 37 year old programmer was laid off, and he didn't find a job for 120 days. He had no choice but to go to a small company. As a result, he was confused
Dare to climb here, you're not far from prison, reptile reverse actual combat case
PID of sunflower classic
Consul of distributed service registration discovery and unified configuration management
Have you entered the workplace since the first 00???
EV6 helps the product matrix, and Kia is making efforts in the high-end market. The global sales target in 2022 is 3.15 million?
Aperçu du code source futur - série juc
Recent learning fragmentation (14)
Katalon框架测试web(二十六)自动发邮件
Which product is better if you want to go abroad to insure Xinguan?
New year's first race, submit bug reward more!
深入浅出对话系统——使用Transformer进行文本分类
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
2006 translation
POSTECH | option compatible reward reverse reinforcement learning