目标检测-Iou (交并比)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import numpy as np

def ComputeIOU(boxA, boxB):
## 计算相交框的坐标
x1 = np.max([boxA[0], boxB[0]])
x2 = np.min([boxA[2], boxB[2]])
y1 = np.max([boxA[1], boxB[1]])
y2 = np.min([boxA[3], boxB[3]])

width = np.max([0, x2 - x1 + 1])
height = np.max([0, y2 - y1 + 1])
inter_area = width * height

# 计算两个框的面积
area_A = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
area_B = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)

# 计算并区域面积
union_area = area_A + area_B - inter_area

# 计算IOU
iou = inter_area / union_area
return iou

if __name__ == "__main__":
boxA = [1,1,3,3]
boxB = [2,2,4,4]
IOU = ComputeIOU(boxA, boxB)
print(f"IOU: {IOU:.4f}")

手撕RMSNorm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import torch
import torch.nn as nn
class RMSNorm(nn.Module):
def __init__(self, dim, eps=1e-8):
super(RMSNorm, self).__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))

def forward(self, x):
rms = torch.sqrt(torch.mean(x ** 2, dim=-1, keepdim=True) + self.eps)
return x / rms * self.weight

batch_size = 2
sequence_length = 3
hidden_size = 4
x = torch.randn(batch_size, sequence_length, hidden_size)
rms_norm = RMSNorm(hidden_size)
output = rms_norm(x)
print(output.shape)

手撕softmax

pytorch版本

cpp版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void navieSoftmax(float* dst, float* src, int data_len) {
// 1. get max
float max_value = -FLT_MIN; // set it to MIN_FLOAT
for (int i = 0; i < data_len; i++) {
if (src[i] > max_value) {
max_value = src[i];
}
}

// 2. get sum
float sum = 0.f;
for (int i = 0; i < data_len; i++) {
sum += std::expf(src[i] - max_value);
}

// 3. caculate output
for (int i = 0; i < data_len; i++) {
dst[i] = std::expf(src[i] - max_value) / sum;
}

return;
}