fix: 旋转预处理对齐上游 BILINEAR、点选顺序换成真匈牙利算法
Build and Push / build (push) Successful in 4m24s

对照上游 Python + onnxruntime 做了 A/B(模型直接用仓库里那几份 onnx),结论:

1) 旋转:同一张官方例图,上游预处理预测 229,我们预测 319,差 90°。
   逐项排除后锁定在 resize 滤波器:上游 solve_single_rotate 用 PIL 的 BILINEAR,
   我们用了 imaging.Lanczos;把 Go 的裁剪 + BILINEAR 复现出来正好是 229。
   → preprocessRotation 改 imaging.Linear。这是 360 类分类器,缩放核足以改变 argmax。

2) 点选顺序:我们的 "hungarian" 其实是逐行贪心,不是匈牙利算法。
   同一组检测框实测:
     贪心    0.7289 + 0.6894 + 0.7238 = 2.1421(顺序错:提示第1个对到目标第2个)
     全局最优 0.6721 + 0.8752 + 0.7238 = 2.2711(与上游 linear_sum_assignment 一致)
   → 新增 hungarianAssign(增广路 O(n^3),行数>列数时补方阵),
     未匹配位置按上游填 [0,0,0,0] 而不是 null。

3) 顺带清掉 Math 里每次请求都打印的 DEBUG 输出(output 前 10 个值 / 位置0 argmax)。

A/B 还确认了两件「不是 bug」的事:
· OCR 官方例图我们出 'jepv',上游 Python 也是 'jepv' → 大小写是模型行为,不是移植问题
· Math 官方例图上游读出 '8×1=?' 计算 8,我们返回 8 → 一致
This commit is contained in:
Hermes
2026-12-03 17:36:41 +08:00
parent 5e9cabeb2f
commit 5a5850e9be
+107 -41
View File
@@ -312,34 +312,11 @@ func (h *Handler) Math(imageBase64 string) (result string, err error) {
return "", err
}
// DEBUG: 打印输出前10个值
debugStr := "ONNX output first 10: "
for i := 0; i < 10 && i < len(output); i++ {
debugStr += fmt.Sprintf("%.4f ", output[i])
}
fmt.Println(debugStr)
// DEBUG: 对第一个位置 argmax
maxIdx := 0
maxProb := float32(-math.MaxFloat32)
for c := 0; c < 19; c++ {
if output[c] > maxProb {
maxProb = output[c]
maxIdx = c
}
}
// 【注意】这里用 string 索引访问多字节字符可能有问题,仅用于调试
debugChars := []string{"", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "-", "*", "/", "÷", "×", "=", "?"}
if maxIdx >= 0 && maxIdx < len(debugChars) {
fmt.Printf("DEBUG: Position 0 argmax = %d (char: %s)\n", maxIdx, debugChars[maxIdx])
}
if len(output) == 0 {
return "", fmt.Errorf("模型输出为空")
}
expr := decodeMathFromOutput(output)
fmt.Printf("DEBUG: Decoded expression = '%s'\n", expr)
if expr == "" {
return "", fmt.Errorf("无法识别表达式")
}
@@ -773,7 +750,10 @@ func preprocessRotation(img image.Image) ([]float32, error) {
centerCropped := imaging.Crop(cropped, image.Rect(offset, offset, offset+newSize, offset+newSize))
// resize 到 224x224
resized := imaging.Resize(centerCropped, 224, 224, imaging.Lanczos)
// 必须用双线性:上游 solve_single_rotate 用的是 PIL 的 Image.Resampling.BILINEAR。
// 换成 Lanczos 会让同一个样本的预测类别从 229 跳到 319(实测同一张图差 90°),
// 因为这是个 360 类的分类器,缩放核的差异足以改变 argmax。
resized := imaging.Resize(centerCropped, 224, 224, imaging.Linear)
rgb := imaging.Clone(resized)
// ImageNet 标准化
@@ -1261,11 +1241,15 @@ func (h *Handler) hungarianMatch(orderImgBase64, targetImgBase64 string, orderBo
}
}
assignments := hungarian(costMatrix)
assignments := hungarianAssign(costMatrix)
// 上游 handle_matching 的做法:结果长度 = 提示框个数,未匹配的位置填 [0,0,0,0]
result := make([]Detection, numOrders)
for i := range result {
result[i] = Detection{Class: "", Box: []int{0, 0, 0, 0}}
}
for i, j := range assignments {
if j >= 0 && j < len(targetBoxes) {
if i < len(result) && j >= 0 && j < len(targetBoxes) {
result[i] = targetBoxes[j]
}
}
@@ -1340,35 +1324,117 @@ func computeHistogram(img image.Image) []int {
return hist
}
func hungarian(costMatrix [][]float64) []int {
// hungarianAssign 求最小代价分配(匈牙利 / 增广路算法,O(n^3))。
// 行数可以大于列数:内部先补成方阵(虚列代价取一个足够大的值),最后只保留真实列的分配。
// 返回 assign[i] = 列号;-1 表示该行没有可行匹配。
//
// 为什么不能用贪心:点选顺序是「提示图从左到右 → 目标图点击坐标」的全局最优匹配,
// 逐行取最优会拿到局部解。实测同一组检测框:
//
// 贪心 相似度 0.7289 + 0.6894 + 0.7238 = 2.1421(顺序错)
// 全局最优 相似度 0.6721 + 0.8752 + 0.7238 = 2.2711(顺序对)
func hungarianAssign(costMatrix [][]float64) []int {
n := len(costMatrix)
if n == 0 {
return nil
}
m := len(costMatrix[0])
used := make([]bool, m)
result := make([]int, n)
for i := range result {
result[i] = -1
if m == 0 {
return nil
}
size := n
if m > size {
size = m
}
// 补成方阵:虚行列代价取比真实代价大得多的值
big := 0.0
for i := 0; i < n; i++ {
bestJ := -1
bestCost := 1.0
for j := 0; j < m; j++ {
if !used[j] && costMatrix[i][j] < bestCost {
bestCost = costMatrix[i][j]
bestJ = j
if costMatrix[i][j] > big {
big = costMatrix[i][j]
}
}
if bestJ >= 0 {
result[i] = bestJ
used[bestJ] = true
}
big = big*float64(size+1) + 1
cost := make([][]float64, size)
for i := range cost {
cost[i] = make([]float64, size)
for j := range cost[i] {
if i < n && j < m {
cost[i][j] = costMatrix[i][j]
} else {
cost[i][j] = big
}
}
}
return result
const inf = 1e18
u := make([]float64, size+1)
v := make([]float64, size+1)
p := make([]int, size+1)
way := make([]int, size+1)
for i := 1; i <= size; i++ {
p[0] = i
j0 := 0
minv := make([]float64, size+1)
used := make([]bool, size+1)
for j := 1; j <= size; j++ {
minv[j] = inf
}
for {
used[j0] = true
i0 := p[j0]
delta := inf
j1 := 0
for j := 1; j <= size; j++ {
if used[j] {
continue
}
cur := cost[i0-1][j-1] - u[i0] - v[j]
if cur < minv[j] {
minv[j] = cur
way[j] = j0
}
if minv[j] < delta {
delta = minv[j]
j1 = j
}
}
for j := 0; j <= size; j++ {
if used[j] {
u[p[j]] += delta
v[j] -= delta
} else {
minv[j] -= delta
}
}
j0 = j1
if p[j0] == 0 {
break
}
}
for j0 != 0 {
j1 := way[j0]
p[j0] = p[j1]
j0 = j1
}
}
assign := make([]int, n)
for i := range assign {
assign[i] = -1
}
for j := 1; j <= size; j++ {
row := p[j] - 1
if row >= 0 && row < n && j-1 < m {
assign[row] = j - 1
}
}
return assign
}
// ===================== 工具函数 =====================