当前位置:网站首页>[PaddleSeg 源码阅读] PaddleSeg计算 mIoU
[PaddleSeg 源码阅读] PaddleSeg计算 mIoU
2022-07-04 03:32:00 【氵文大师】
来看下 Paddleseg/utils/metrics.py
计算 mIoU 的过程
calculate_area
函数用来计算相交的区域,
送入的 pred 和 label 形如下(1, H, W):
(类别0到4,0为背景类)
[[[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 该函数用于计算交集、prediction 和 label 的像素点数 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 """
# 如果 pred.shape == (1, 1, 224, 224) 则将第一维去掉
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))
# 这一步往上得到的 pred.shape == label.shape == (1, 224, 224)
pred_area = []
label_area = []
intersect_area = []
mask = label != ignore_index # 该 mask 用于将 ignore_index 忽略
for i in range(num_classes): # 开始迭代每一个类
# 将 pred 中是第 i 类的位置拿出来
pred_i = paddle.logical_and(pred == i, mask)
# 将 label 中第 i 类的位置拿出来
label_i = label == i
# 二者做与操作,将二者相同的位置拿出来
intersect_i = paddle.logical_and(pred_i, label_i)
# bool -> int32 后求和,送入对应的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")))
# 将以上list转变为 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
打印一下 intersect_area, pred_area, label_area
三个变量的值:
>>> intersect_area
[ 97721 147687]
>>> pred_area
[ 99482 151398]
>>> label_area
[101432 149448]
然后调用:
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. 计算 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. """
# 用 .numpy() 转化为 np.ndarray
intersect_area = intersect_area.numpy()
pred_area = pred_area.numpy()
label_area = label_area.numpy()
# 计算并集
union = pred_area + label_area - intersect_area
class_iou = []
for i in range(len(intersect_area)):
if union[i] == 0:
# 如果一个该类的像素都没有,则并集
iou = 0
else:
# 计算交并比
iou = intersect_area[i] / union[i]
class_iou.append(iou)
# 计算均值
miou = np.mean(class_iou)
return np.array(class_iou), miou
边栏推荐
- 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?
- PHP database connection succeeded, but data cannot be inserted
- system information
- In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
- Imperial cms7.5 imitation "D9 download station" software application download website source code
- What are the conditions for the opening of Tiktok live broadcast preview?
- Safety tips - seat belt suddenly fails to pull? High speed police remind you how to use safety belts in a standardized way
- 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
- WordPress collection WordPress hang up collection plug-in
猜你喜欢
Li Chuang EDA learning notes 13: electrical network for drawing schematic diagram
JVM family -- monitoring tools
Redis transaction
How to use websocket to realize simple chat function in C #
Development of digital collection trading platform development of digital collection platform
Kiss number + close contact problem
Record a problem that soft deletion fails due to warehouse level error
Imperial cms7.5 imitation "D9 download station" software application download website source code
Lichuang EDA learning notes 14: PCB board canvas settings
Dare to climb here, you're not far from prison, reptile reverse actual combat case
随机推荐
Imperial cms7.5 imitation "D9 download station" software application download website source code
Webhook triggers Jenkins for sonar detection
Unity writes a character controller. The mouse controls the screen to shake and the mouse controls the shooting
Cache general management class + cache httpcontext Current. Cache and httpruntime Differences between caches
150 ppt! The most complete "fair perception machine learning and data mining" tutorial, Dr. AIST Toshihiro kamishima, Japan
CSP drawing
Webhook triggers Jenkins for sonar detection
Command Execution Vulnerability - command execution - vulnerability sites - code injection - vulnerability exploitation - joint execution - bypass (spaces, keyword filtering, variable bypass) - two ex
There is no need to authorize the automatic dream weaving collection plug-in for dream weaving collection
(column 23) typical C language problem: find the minimum common multiple and maximum common divisor of two numbers. (two solutions)
Learning video website
Johnson–Lindenstrauss Lemma
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
Jenkins configures IP address access
[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
Talking about custom conditions and handling errors in MySQL Foundation
Add IDM to Google browser
Latex tips slash \backslash
【.NET+MQTT】.NET6 環境下實現MQTT通信,以及服務端、客戶端的雙邊消息訂閱與發布的代碼演示
Amélioration de l'efficacité de la requête 10 fois! 3 solutions d'optimisation pour résoudre le problème de pagination profonde MySQL