当前位置:网站首页>[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
边栏推荐
- JVM family -- heap analysis
- Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
- Is it really so difficult to learn redis? Today, a fan will share his personal learning materials!
- 2022 attached lifting scaffold worker (special type of construction work) free test questions and attached lifting scaffold worker (special type of construction work) examination papers 2022 attached
- How to use STR function of C language
- Sword finger offer:55 - I. depth of binary tree
- 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?
- [Wu Enda deep learning] beginner learning record 3 (regularization / error reduction)
- super_ Subclass object memory structure_ Inheritance tree traceability
猜你喜欢
Monitoring - Prometheus introduction
Jenkins continuous integration environment construction V (Jenkins common construction triggers)
PID of sunflower classic
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
How to use websocket to realize simple chat function in C #
What is cloud primordial?
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Setting methods, usage methods and common usage scenarios of environment variables in postman
SQL語句加强練習(MySQL8.0為例)
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
随机推荐
How to use STR function of C language
Contest3145 - the 37th game of 2021 freshman individual training match_ 1: Origami
Contest3145 - the 37th game of 2021 freshman individual training match_ J: Eat radish
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
Contest3145 - the 37th game of 2021 freshman individual training match_ F: Smallest ball
Zlmediakit compilation and webrtc push-pull flow testing
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Is online futures account opening safe and reliable? Which domestic futures company is better?
Tsinghua University product: penalty gradient norm improves generalization of deep learning model
Slurm view node configuration information
Leecode 122. Zuijia timing of buying and selling stocks ②
Enhanced for loop
JS object definition
Rhcsa day 3
Add IDM to Google browser
PHP database connection succeeded, but data cannot be inserted
Explain AI accelerator in detail: why is this the golden age of AI accelerator?
What is cloud primordial?
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush