当前位置:网站首页>[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
边栏推荐
- Katalon框架测试web(二十六)自动发邮件
- 機器學習基礎:用 Lasso 做特征選擇
- JSON string conversion in unity
- Learning video website
- Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
- 选择排序与冒泡排序模板
- Is online futures account opening safe and reliable? Which domestic futures company is better?
- Baijia forum the founding of the Eastern Han Dynasty
- Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
- A review of reverse reinforcement learning at Virginia Tech (VT)
猜你喜欢
What is cloud primordial?
MySQL query
Dare to climb here, you're not far from prison, reptile reverse actual combat case
Development of digital collection trading platform development of digital collection platform
Session learning diary 1
Defensive programming skills
functools下的reduce函数
How to use websocket to realize simple chat function in C #
2022-07-03:数组里有0和1,一定要翻转一个区间,翻转:0变1,1变0。 请问翻转后可以使得1的个数最多是多少? 来自小红书。3.13笔试。
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
随机推荐
Redis notes (I) Linux installation process of redis
What kind of experience is it when the Institute earns 20000 yuan a month!
Why is it recommended that technologists write blogs?
CSP drawing
Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
How about the ratings of 2022 Spring Festival Gala in all provinces? Map analysis helps you show clearly!
Teach you how to optimize SQL
Dare to climb here, you're not far from prison, reptile reverse actual combat case
Detailed explanation of PPTC self recovery fuse
1289_FreeRTOS中vTaskSuspend()接口实现分析
Typical applications of minimum spanning tree
SQL語句加强練習(MySQL8.0為例)
SQL injection (1) -- determine whether there are SQL injection vulnerabilities
Contest3145 - the 37th game of 2021 freshman individual training match_ F: Smallest ball
[database I] database overview, common commands, view the table structure of 'demo data', simple query, condition query, sorting data, data processing function (single row processing function), groupi
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?
[Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
SQL语句加强练习(MySQL8.0为例)
Osnabrueck University | overview of specific architectures in the field of reinforcement learning
Which product is better if you want to go abroad to insure Xinguan?