当前位置:网站首页>[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
边栏推荐
- Es network layer
- System integration meets the three business needs of enterprises
- Mindmanager2022 efficient and easy to use office mind map MindManager
- Contest3145 - the 37th game of 2021 freshman individual training match_ F: Smallest ball
- [latex] production of complex tables: excel2latex and detail adjustment
- [PaddleSeg 源码阅读] PaddleSeg计算 mIoU
- If you have just joined a new company, don't be fired because of your mistakes
- Slurm view node configuration information
- Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
- Third party login initial version
猜你喜欢
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Mindmanager2022 efficient and easy to use office mind map MindManager
[untitled]
If you have just joined a new company, don't be fired because of your mistakes
Easy to win insert sort
Have you entered the workplace since the first 00???
Setting methods, usage methods and common usage scenarios of environment variables in postman
PID of sunflower classic
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
Zhihu million hot discussion: why can we only rely on job hopping for salary increase? Bosses would rather hire outsiders with a high salary than get a raise?
随机推荐
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
Detailed explanation of PPTC self recovery fuse
Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
Consul of distributed service registration discovery and unified configuration management
数据库SQL语句汇总,持续更新......
Webhook triggers Jenkins for sonar detection
[PaddleSeg 源码阅读] PaddleSeg 自定义数据类
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
How to pipe several commands in Go?
Exercices de renforcement des déclarations SQL (MySQL 8.0 par exemple)
选择排序与冒泡排序模板
Leecode 122. Zuijia timing of buying and selling stocks ②
[PaddleSeg 源码阅读] PaddleSeg计算Dice
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Wechat official account web page authorization
If you have just joined a new company, don't be fired because of your mistakes
Audio and video technology development weekly | 232
What kind of experience is it when the Institute earns 20000 yuan a month!
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
[UE4] parse JSON string